libra/resources/views/livewire/admin/clients/individual/edit.blade.php

195 lines
7.2 KiB
PHP

<?php
use App\Enums\UserStatus;
use App\Models\AdminLog;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rule;
use Livewire\Volt\Component;
new class extends Component {
public User $client;
public string $full_name = '';
public string $national_id = '';
public string $email = '';
public string $phone = '';
public string $password = '';
public string $preferred_language = '';
public string $status = '';
public function mount(User $client): void
{
$this->client = $client;
$this->full_name = $client->full_name;
$this->national_id = $client->national_id ?? '';
$this->email = $client->email;
$this->phone = $client->phone ?? '';
$this->preferred_language = $client->preferred_language ?? 'ar';
$this->status = $client->status->value;
}
public function rules(): array
{
return [
'full_name' => ['required', 'string', 'max:255'],
'national_id' => ['required', 'string', 'max:50', Rule::unique('users', 'national_id')->ignore($this->client->id)],
'email' => ['required', 'email', 'max:255', Rule::unique('users', 'email')->ignore($this->client->id)],
'phone' => ['required', 'string', 'max:20'],
'password' => ['nullable', 'string', 'min:8'],
'preferred_language' => ['required', 'in:ar,en'],
'status' => ['required', 'in:active,deactivated'],
];
}
public function messages(): array
{
return [
'national_id.unique' => __('clients.national_id_exists'),
'email.unique' => __('clients.email_exists'),
];
}
public function update(): void
{
$validated = $this->validate();
$oldValues = $this->client->only(['full_name', 'email', 'national_id', 'phone', 'preferred_language', 'status']);
$this->client->full_name = $validated['full_name'];
$this->client->national_id = $validated['national_id'];
$this->client->email = $validated['email'];
$this->client->phone = $validated['phone'];
$this->client->preferred_language = $validated['preferred_language'];
$this->client->status = $validated['status'];
if (! empty($validated['password'])) {
$this->client->password = Hash::make($validated['password']);
}
$this->client->save();
AdminLog::create([
'admin_id' => auth()->id(),
'action' => 'update',
'target_type' => 'user',
'target_id' => $this->client->id,
'old_values' => $oldValues,
'new_values' => $this->client->only(['full_name', 'email', 'national_id', 'phone', 'preferred_language', 'status']),
'ip_address' => request()->ip(),
'created_at' => now(),
]);
session()->flash('success', __('clients.client_updated'));
$this->redirect(route('admin.clients.individual.index'), navigate: true);
}
public function with(): array
{
return [
'statuses' => UserStatus::cases(),
];
}
}; ?>
<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.edit_client') }}</flux:heading>
<flux:text class="mt-1 text-zinc-500">{{ $client->full_name }}</flux:text>
</div>
<div class="rounded-lg border border-zinc-200 bg-white p-6">
<form wire:submit="update" 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>{{ __('clients.password') }}</flux:label>
<flux:input
wire:model="password"
type="password"
placeholder="{{ __('Leave blank to keep current password') }}"
/>
<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>
<flux:field>
<flux:label class="required">{{ __('clients.status') }}</flux:label>
<flux:select wire:model="status" required>
@foreach ($statuses as $statusOption)
<flux:select.option value="{{ $statusOption->value }}">
{{ __('clients.' . $statusOption->value) }}
</flux:select.option>
@endforeach
</flux:select>
<flux:error name="status" />
</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.update') }}
</flux:button>
</div>
</form>
</div>
</div>