161 lines
5.8 KiB
PHP
161 lines
5.8 KiB
PHP
<?php
|
|
|
|
use App\Enums\UserStatus;
|
|
use App\Enums\UserType;
|
|
use App\Models\AdminLog;
|
|
use App\Models\User;
|
|
use App\Notifications\WelcomeAccountNotification;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Livewire\Volt\Component;
|
|
|
|
new class extends Component {
|
|
public string $full_name = '';
|
|
public string $national_id = '';
|
|
public string $email = '';
|
|
public string $phone = '';
|
|
public string $password = '';
|
|
public string $preferred_language = 'ar';
|
|
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'full_name' => ['required', 'string', 'max:255'],
|
|
'national_id' => ['required', 'string', 'max:50', 'unique:users,national_id'],
|
|
'email' => ['required', 'email', 'max:255', 'unique:users,email'],
|
|
'phone' => ['required', 'string', 'max:20'],
|
|
'password' => ['required', 'string', 'min:8'],
|
|
'preferred_language' => ['required', 'in:ar,en'],
|
|
];
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'national_id.unique' => __('clients.national_id_exists'),
|
|
'email.unique' => __('clients.email_exists'),
|
|
];
|
|
}
|
|
|
|
public function create(): void
|
|
{
|
|
$validated = $this->validate();
|
|
|
|
$plainPassword = $validated['password'];
|
|
|
|
$user = User::create([
|
|
'user_type' => UserType::Individual,
|
|
'full_name' => $validated['full_name'],
|
|
'national_id' => $validated['national_id'],
|
|
'email' => $validated['email'],
|
|
'phone' => $validated['phone'],
|
|
'password' => Hash::make($plainPassword),
|
|
'preferred_language' => $validated['preferred_language'],
|
|
'status' => UserStatus::Active,
|
|
]);
|
|
|
|
AdminLog::create([
|
|
'admin_id' => auth()->id(),
|
|
'action' => 'create',
|
|
'target_type' => 'user',
|
|
'target_id' => $user->id,
|
|
'new_values' => $user->only(['full_name', 'email', 'national_id', 'phone', 'preferred_language']),
|
|
'ip_address' => request()->ip(),
|
|
'created_at' => now(),
|
|
]);
|
|
|
|
$user->notify(new WelcomeAccountNotification($plainPassword));
|
|
|
|
session()->flash('success', __('clients.client_created'));
|
|
|
|
$this->redirect(route('admin.clients.individual.index'), navigate: true);
|
|
}
|
|
}; ?>
|
|
|
|
<div>
|
|
<div class="mb-6">
|
|
<flux:button variant="ghost" :href="route('admin.clients.individual.index')" wire:navigate icon="arrow-left">
|
|
{{ __('clients.back_to_clients') }}
|
|
</flux:button>
|
|
</div>
|
|
|
|
<div class="mb-6">
|
|
<flux:heading size="xl">{{ __('clients.create_client') }}</flux:heading>
|
|
<flux:text class="mt-1 text-zinc-500">{{ __('clients.individual_clients') }}</flux:text>
|
|
</div>
|
|
|
|
<div class="rounded-lg border border-zinc-200 bg-white p-6">
|
|
<form wire:submit="create" class="space-y-6">
|
|
<div class="grid gap-6 sm:grid-cols-2">
|
|
<flux:field>
|
|
<flux:label class="required">{{ __('clients.full_name') }}</flux:label>
|
|
<flux:input
|
|
wire:model="full_name"
|
|
type="text"
|
|
required
|
|
autofocus
|
|
/>
|
|
<flux:error name="full_name" />
|
|
</flux:field>
|
|
|
|
<flux:field>
|
|
<flux:label class="required">{{ __('clients.national_id') }}</flux:label>
|
|
<flux:input
|
|
wire:model="national_id"
|
|
type="text"
|
|
required
|
|
/>
|
|
<flux:error name="national_id" />
|
|
</flux:field>
|
|
|
|
<flux:field>
|
|
<flux:label class="required">{{ __('clients.email') }}</flux:label>
|
|
<flux:input
|
|
wire:model="email"
|
|
type="email"
|
|
required
|
|
/>
|
|
<flux:error name="email" />
|
|
</flux:field>
|
|
|
|
<flux:field>
|
|
<flux:label class="required">{{ __('clients.phone') }}</flux:label>
|
|
<flux:input
|
|
wire:model="phone"
|
|
type="tel"
|
|
required
|
|
/>
|
|
<flux:error name="phone" />
|
|
</flux:field>
|
|
|
|
<flux:field>
|
|
<flux:label class="required">{{ __('clients.password') }}</flux:label>
|
|
<flux:input
|
|
wire:model="password"
|
|
type="password"
|
|
required
|
|
/>
|
|
<flux:error name="password" />
|
|
</flux:field>
|
|
|
|
<flux:field>
|
|
<flux:label class="required">{{ __('clients.preferred_language') }}</flux:label>
|
|
<flux:select wire:model="preferred_language" required>
|
|
<flux:select.option value="ar">{{ __('clients.arabic') }}</flux:select.option>
|
|
<flux:select.option value="en">{{ __('clients.english') }}</flux:select.option>
|
|
</flux:select>
|
|
<flux:error name="preferred_language" />
|
|
</flux:field>
|
|
</div>
|
|
|
|
<div class="flex items-center justify-end gap-4 border-t border-zinc-200 pt-6">
|
|
<flux:button variant="ghost" :href="route('admin.clients.individual.index')" wire:navigate>
|
|
{{ __('clients.cancel') }}
|
|
</flux:button>
|
|
<flux:button variant="primary" type="submit">
|
|
{{ __('clients.create') }}
|
|
</flux:button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|