TextInput::make('phone') ->label('شماره موبایل') ->required() ->live() ->tel() ->afterStateHydrated(function (TextInput $component, $state) { $component->state(preg_replace('/[^0-9]/', '', $state)); }), Hidden::make('user_id') ->default(fn ($get) => \App\Models\User::firstOrCreate( ['phone' => $get('phone')], ['password' => bcrypt(Str::random(8))] )->id), Hidden::make('admin') ->default(fn () => 1), Select::make('address_id') ->label('آدرس کاربر') ->options(fn ($get) => Address::where('user_id', User::where('phone', $get('phone'))->value('id')) ->pluck('address', 'id')) ->searchable() ->preload() ->required() ->hidden(fn ($get) => $get('country') && $get('province') && $get('city') && $get('address')) ->live() ->afterStateUpdated(function ($state, callable $set) { $address = Address::find($state); if ($address) { $set('country', $address->country); $set('province', $address->province); $set('city', $address->city); $set('address', $address->address); } else { $set('country', null); $set('province', null); $set('city', null); $set('address', null); } }) ->hidden(fn ($get) => !User::where('phone', $get('phone'))->exists()), TextInput::make('country') ->label('کشور') ->live() ->required(fn ($get) => !$get('address_id')) ->visible(fn ($get) => !$get('address_id')), TextInput::make('province') ->label('استان') ->live() ->required(fn ($get) => !$get('address_id')) ->visible(fn ($get) => !$get('address_id')), TextInput::make('city') ->label('شهر') ->required(fn ($get) => !$get('address_id')) ->live() ->visible(fn ($get) => !$get('address_id')), Textarea::make('address') ->label('آدرس') ->required(fn ($get) => !$get('address_id')) ->live() ->visible(fn ($get) => !$get('address_id')), Textarea::make('postal_code') ->label('کد پستی') ->visible(fn ($get) => !$get('address_id')), Repeater::make('order_details') ->label('محصولات') ->relationship('order_details') ->saveRelationshipsUsing(null) ->dehydrated() ->schema([ Select::make('product_id') ->label('محصول') ->searchable() ->preload() ->options(Product::all()->pluck('name', 'id')) ->required() ->afterStateUpdated(function ($state, callable $set, callable $get) { $product = Product::find($state); $quantity = $get('quantity') ?? 1; $price = $product?->price_for_quantity($quantity) ?? 0; $set('price', $price); }), TextInput::make('quantity') ->label('تعداد') ->live() ->numeric() ->required() ->afterStateUpdated(function ($state, callable $set, callable $get) { $product = Product::find($get('product_id')); $price = $product?->price_for_quantity($state) ?? 0; $set('price', $price); }), Hidden::make('price') ->dehydrated(true) ]) ->minItems(1) ->columns(2), TextInput::make('fname') ->label('نام تحویل گیرنده') ->required(), TextInput::make('lname') ->label('نام خانوادگی تحویل گیرنده') ->required(), Select::make('send_id') ->label('روش ارسال') ->options(Send::all()->pluck('name', 'id')) ->searchable() ->live() ->nullable(), TextInput::make('custom_send_price') ->label('هزینه ارسال') ->numeric() ->required(fn ($get) => !$get('send_id')) ->visible(fn ($get) => $get('send_id') == null), Select::make('order_type') ->label('نوع سفارش') ->options([ 'تلگرام' => 'تلگرام', 'اینستاگرام' => 'اینستاگرام', 'حضوری' => 'حضوری', ]) ->required(),