<?php

use App\Models\User;
use Livewire\Volt\Component;
use Illuminate\Support\Facades\Hash;

new class extends Component {

    public ?User $user = null;

    public string $name = '';
    public string $email = '';
    public string $password = '';
    public string $role = 'user';
    public $profession = '';
    public $domicile = '';
    public $age = '';
    public $gender = '';

    public function mount(?User $user = null)
    {
        $this->user = $user;

        if ($user) {
            $this->name = $user->name;
            $this->email = $user->email;
            $this->role = $user->role;
            $this->profession = $user->profession;
            $this->domicile = $user->domicile;
            $this->age = $user->age;
            $this->gender = $user->gender;
        }
    }

    protected function rules()
    {
        return [
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users,email,' . ($this->user ? $this->user->id : 'null'),
            'password' => $this->user ? 'nullable|min:6' : 'required|min:6',
            'role' => 'required|in:admin,user',
            'profession' => 'nullable|string|max:255',
            'domicile' => 'nullable|string|max:255',
            'age' => 'nullable|string',
            'gender' => 'nullable|string'
        ];
    }

    public function submit()
    {
        $this->validate();

        $data = [
            'name' => $this->name,
            'email' => $this->email,
            'role' => $this->role,
            'profession' => $this->profession,
            'domicile' => $this->domicile,
            'age' => $this->age ?: null,
            'gender' => $this->gender ?: null,
        ];

        if ($this->password) {
            $data['password'] = Hash::make($this->password);
        }

        if ($this->user) {

            $this->user->update($data);

            session()->flash('notify', [
                'type' => 'success',
                'title' => 'Success',
                'message' => 'User updated successfully'
            ]);

        } else {

            $data['password'] = Hash::make($this->password);

            User::create($data);

            session()->flash('notify', [
                'type' => 'success',
                'title' => 'Success',
                'message' => 'User created successfully'
            ]);
        }

        return redirect()->route('users.index');
    }

};
?>

<div class="space-y-6 max-w-xl">

    <flux:heading size="xl">
        {{ $user ? 'Edit User' : 'Create User' }}
    </flux:heading>

    <flux:card>

        <form wire:submit.prevent="submit" class="space-y-4">

            <flux:input
                label="Name"
                wire:model.live="name"
            />

            <flux:input
                label="Email"
                type="email"
                wire:model.live="email"
            />

            <flux:input
                label="Password"
                type="password"
                wire:model="password"
                placeholder="{{ $user ? 'Leave blank if not changing password' : '' }}"
            />

            <flux:select label="Role" wire:model="role">
                <option value="user">User</option>
                <option value="admin">Administrator</option>
            </flux:select>

            {{-- PROFESSION --}}
            <flux:select label="Profession" wire:model="profession">
                <option value="">Select Profession</option>
                @foreach ([
                    'PNS / ASN',
                    'Karyawan',
                    'Wiraswasta',
                    'Pelajar / Mahasiswa',
                    'Ibu Rumah Tangga',
                    'Guru / Dosen',
                    'Seniman',
                    'Budayawan',
                    'Lainnya'
                ] as $item)
                    <option value="{{ $item }}">{{ $item }}</option>
                @endforeach
            </flux:select>

            {{-- DOMICILE --}}
            <flux:select label="Domicile" wire:model="domicile">
                <option value="">Select Domicile</option>
                @foreach ([
                    'Jawa',
                    'Sumatera',
                    'Kalimantan',
                    'Papua Maluku',
                    'Bali Nusa',
                    'Sulawesi'
                ] as $item)
                    <option value="{{ $item }}">{{ $item }}</option>
                @endforeach
            </flux:select>

            {{-- AGE --}}
            <flux:select label="Age" wire:model="age">
                <option value="">Select Age</option>
                @foreach ([
                    '< 15 tahun',
                    '15 - 25 tahun',
                    '25 - 35 tahun',
                    '35 - 45 tahun',
                    '> 45 tahun'
                ] as $item)
                    <option value="{{ $item }}">{{ $item }}</option>
                @endforeach
            </flux:select>

            <flux:select label="Gender" wire:model="gender">
                <option value="">Select Gender</option>
                <option value="Laki-laki">Laki-laki</option>
                <option value="Perempuan">Perempuan</option>
            </flux:select>


            <div class="flex justify-end gap-2">

                <flux:button
                    href="{{ route('users.index') }}"
                    variant="ghost"
                >
                    Cancel
                </flux:button>

                <flux:button
                    type="submit"
                    variant="primary"
                >
                    Save
                </flux:button>

            </div>

        </form>

    </flux:card>

</div>
