@props(['mode', 'user' => null])

@php
    $isCreate = $mode === 'create';
    $isEdit = $mode === 'edit';
    $isDelete = $mode === 'delete';
    $isSelf = $mode === 'self';
    
    $actionUrl = match($mode) {
        'create' => route('admin.users.store'),
        'edit' => route('admin.users.update', $user?->msnv),
        'delete' => route('admin.users.destroy', $user?->msnv),
        default => null,
    };
    
    $method = match($mode) {
        'edit' => 'PUT',
        'delete' => 'DELETE',
        default => 'POST',
    };
@endphp

<div class="form-container flex flex-col h-full justify-between">
    <div class="form-header">
        <h3 class="form-header-title">
            {{ match($mode) {
                'create' => 'Thêm User Mới',
                'edit' => 'Quản Lý Thẻ: ' . ($user?->msnv),
                'delete' => 'Xác nhận nghỉ việc: ' . ($user?->msnv),
                'self' => 'Thông tin cá nhân: ' . ($user?->msnv),
            } }}
        </h3>
    </div>

    @if(!$isCreate && $user)
        <div class="form-body pb-0">
            <div class="mb-6 pb-6 border-b border-border-light space-y-3">
                <div class="flex justify-between items-center text-sm">
                    <span class="text-text-medium font-bold">Email:</span> 
                    <span class="font-semibold text-text-dark">{{ $user->mail }}</span>
                </div>
                <div class="flex justify-between items-center text-sm">
                    <span class="text-text-medium font-bold">Role:</span> 
                    <span class="font-semibold text-text-dark">{{ $user->role == \App\Models\User::ROLE_ADMIN ? 'Admin' : 'Member' }}</span>
                </div>
                <div class="flex justify-between items-center text-sm">
                    <span class="text-text-medium font-bold">Số dư thẻ:</span> 
                    <span class="font-extrabold text-orange-500 text-xl">{{ $user->card }}</span>
                </div>
            </div>
        </div>
    @endif

    @if($actionUrl)
        <form action="{{ $actionUrl }}" method="POST" class="flex flex-col flex-1 justify-between">
            @csrf
            @if($method !== 'POST')
                @method($method)
            @endif
    @endif

        <div class="form-body {{ !$isCreate ? 'pt-0' : '' }}">
            @if($isCreate)
                <div class="form-group">
                    <label class="form-label" for="msnv">Mã nhân viên (MSNV) <span class="text-red-500">*</span></label>
                    <div class="form-input-group">
                        <span class="form-input-icon">
                            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M15 9h3.75M15 12h3.75M15 15h3.75M4.5 19.5h15a2.25 2.25 0 002.25-2.25V6.75A2.25 2.25 0 0019.5 4.5h-15a2.25 2.25 0 00-2.25 2.25v10.5A2.25 2.25 0 004.5 19.5zm6-10.125a1.875 1.875 0 11-3.75 0 1.875 1.875 0 013.75 0zm1.294 6.336a6.721 6.721 0 01-3.17.789 6.721 6.721 0 01-3.168-.789 3.376 3.376 0 016.338 0z" />
                            </svg>
                        </span>
                        <input type="number" name="msnv" id="msnv" class="form-input form-input-with-icon bg-white" required value="{{ old('msnv') }}" placeholder="VD: 1001">
                    </div>
                    @error('msnv')<span class="error-text">{{ $message }}</span>@enderror
                </div>

                <div class="form-group">
                    <label class="form-label" for="name">Họ và tên <span class="text-red-500">*</span></label>
                    <div class="form-input-group">
                        <span class="form-input-icon">
                            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M15.75 6a3.75 3.75 0 1 1-7.5 0 3.75 3.75 0 0 1 7.5 0ZM4.501 20.118a7.5 7.5 0 0 1 14.998 0A17.933 17.933 0 0 1 12 21.75c-2.676 0-5.216-.584-7.499-1.632Z" />
                            </svg>
                        </span>
                        <input type="text" name="name" id="name" class="form-input form-input-with-icon bg-white" required value="{{ old('name') }}" placeholder="VD: Nguyễn Văn A">
                    </div>
                    @error('name')<span class="error-text">{{ $message }}</span>@enderror
                </div>

                <div class="form-group">
                    <label class="form-label" for="mail">Email <span class="text-red-500">*</span></label>
                    <div class="form-input-group">
                        <span class="form-input-icon">
                            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M21.75 6.75v10.5a2.25 2.25 0 01-2.25 2.25h-15a2.25 2.25 0 01-2.25-2.25V6.75m19.5 0A2.25 2.25 0 0019.5 4.5h-15a2.25 2.25 0 00-2.25 2.25m19.5 0v.243a2.25 2.25 0 01-1.07 1.916l-7.5 4.615a2.25 2.25 0 01-2.36 0L3.32 8.91a2.25 2.25 0 01-1.07-1.916V6.75" />
                            </svg>
                        </span>
                        <input type="email" name="mail" id="mail" class="form-input form-input-with-icon bg-white" required value="{{ old('mail') }}" placeholder="VD: nguyenvana@runsystem.net">
                    </div>
                    @error('mail')<span class="error-text">{{ $message }}</span>@enderror
                </div>

                <div class="form-group">
                    <label class="form-label" for="departments">Phòng ban <span class="text-red-500">*</span></label>
                    <select name="departments" id="departments" class="form-input bg-white" required>
                        <option value="1" {{ old('departments') == '1' ? 'selected' : '' }}>Phòng Phát Triển (Dev)</option>
                        <option value="2" {{ old('departments') == '2' ? 'selected' : '' }}>Phòng Nhân Sự (HR)</option>
                        <option value="3" {{ old('departments') == '3' ? 'selected' : '' }}>Phòng Kinh Doanh (Sale)</option>
                        <option value="4" {{ old('departments') == '4' ? 'selected' : '' }}>Ban Giám Đốc (Board)</option>
                        <option value="5" {{ old('departments') == '5' ? 'selected' : '' }}>Phòng Marketing</option>
                    </select>
                    @error('departments')<span class="error-text">{{ $message }}</span>@enderror
                </div>

                <div class="form-group">
                    <label class="form-label" for="password">Mật khẩu khởi tạo <span class="text-red-500">*</span></label>
                    <div class="form-input-group">
                        <span class="form-input-icon">
                            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M16.5 10.5V6.75a4.5 4.5 0 10-9 0V10.5m-2.812 10.5h14.625c.621 0 1.125-.504 1.125-1.125V11.25c0-.621-.504-1.125-1.125-1.125H3.75c-.621 0-1.125.504-1.125 1.125v7.875c0 .621.504 1.125 1.125 1.125z" />
                            </svg>
                        </span>
                        <input type="text" name="password" id="password" class="form-input form-input-with-icon bg-gray-50" readonly required value="1234@Dcba">
                    </div>
                    <span class="helper-text">Mật khẩu mặc định cho tất cả user mới tạo.</span>
                    @error('password')<span class="error-text">{{ $message }}</span>@enderror
                </div>

                <div class="form-group mb-0">
                    <label class="form-label" for="role">Quyền hạn <span class="text-red-500">*</span></label>
                    <select name="role" id="role" class="form-input bg-white" required>
                        <option value="{{ \App\Models\User::ROLE_MEMBER }}" {{ old('role') == \App\Models\User::ROLE_MEMBER ? 'selected' : '' }}>Nhân viên (Member)</option>
                        <option value="{{ \App\Models\User::ROLE_ADMIN }}" {{ old('role') == \App\Models\User::ROLE_ADMIN ? 'selected' : '' }}>Quản trị viên (Admin)</option>
                    </select>
                    @error('role')<span class="error-text">{{ $message }}</span>@enderror
                </div>
            @endif

            @if($isEdit && $user)
                <div class="form-group">
                    <label class="form-label" for="num_card">Nạp thêm thẻ</label>
                    <input type="number" name="num_card" id="num_card" min="1" class="form-input bg-white" placeholder="Nhập số lượng thẻ muốn nạp...">
                    @error('num_card')<span class="error-text">{{ $message }}</span>@enderror
                </div>

                <div class="form-group mb-0">
                    <label class="flex items-center gap-3 cursor-pointer p-4 border border-[#d9dfe7] rounded-lg hover:bg-gray-50 hover:border-gray-400 transition-colors shadow-sm bg-white">
                        <input type="checkbox" name="flag_send" id="flag_send" value="1" class="form-input !w-5 !h-5 !min-h-0" {{ $user->flag_send ? 'checked' : '' }}>
                        <span class="text-[14px] font-[600] text-text-dark">Bật quyền cho phép User gửi thẻ</span>
                    </label>
                </div>
            @endif

            @if($isDelete && $user)
                <p class="text-sm text-red-600 leading-relaxed font-semibold">
                    Bạn có chắc chắn muốn cho nhân viên {{ $user->msnv }} nghỉ việc không? Tài khoản này sẽ không thể đăng nhập hoặc thực hiện các giao dịch thẻ sau khi xác nhận.
                </p>
            @endif
        </div>

        <x-action-panel :mode="$mode" :user="$user" />

    @if($actionUrl)
        </form>
    @endif
</div>
