@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),
        'self' => route('user.update_profile'),
        default => null,
    };
    
    $method = match($mode) {
        'edit' => 'PUT',
        'delete' => 'DELETE',
        default => 'POST',
    };
@endphp

<div class="bg-white rounded-[24px] shadow-[0_10px_30px_rgba(15,23,42,0.05)] border border-white overflow-hidden flex flex-col justify-between">
    @if($mode !== 'edit')
        <div class="bg-gradient-to-r from-blue-50/70 via-indigo-50/50 to-purple-50/30 px-6 py-6 sm:px-8 border-b border-gray-100">
            <h3 class="text-[17px] font-extrabold text-[#1a2b49] leading-tight flex items-center gap-2">
                <span>{{ match($mode) {
                    'create' => 'Thêm User Mới',
                    'delete' => 'Xác nhận nghỉ việc: ' . ($user?->name),
                    'self' => 'Thông tin cá nhân',
                } }}</span>
            </h3>
        </div>
    @endif

    @if(!$isCreate && $user && !$isEdit && !$isSelf)
        <div class="p-6 sm:p-8 pb-0">
            <div class="mb-2 p-5 rounded-2xl bg-gradient-to-br from-slate-50 to-gray-50 border border-gray-100 flex items-center gap-4 shadow-sm">
                <div class="w-12 h-12 rounded-full bg-blue-50 text-[#3462f7] flex items-center justify-center font-extrabold text-lg shadow-inner flex-shrink-0">
                    {{ strtoupper(substr($user->name ?? $user->msnv, 0, 2)) }}
                </div>
                <div class="flex-1">
                    <h4 class="text-base font-extrabold text-[#1a2b49]">{{ $user->name }}</h4>
                    <p class="text-xs text-gray-500 font-semibold mt-1 flex items-center gap-2">
                        <span>MSNV: {{ $user->msnv }}</span>
                        <span class="w-1 h-1 bg-gray-300 rounded-full"></span>
                        <span>{{ $user->mail }}</span>
                    </p>
                </div>
            </div>
        </div>
    @endif

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

        <div class="p-6 sm:p-8 {{ !$isCreate && !$isEdit && !$isSelf ? 'pt-0' : '' }} space-y-5">
            @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->name }} (MSNV: {{ $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>
            @else
                <!-- 1. User Avatar Section -->
                <x-user-avatar-section :mode="$mode" :user="$user" />

                <!-- 2. User Basic Information Section -->
                <x-user-basic-info :mode="$mode" :user="$user" />

                <!-- 3. User Security Section -->
                <x-user-security-section :mode="$mode" :user="$user" />

                <!-- 4. User Administration Fields Section -->
                <x-user-admin-section :mode="$mode" :user="$user" />
            @endif
        </div>

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

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

@if($isCreate || $isEdit)
    @push('scripts')
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            if (document.getElementById('departments') && typeof TomSelect === 'function') {
                new TomSelect("#departments", {
                    create: false,
                    sortField: {
                        field: "text",
                        direction: "asc"
                    },
                    placeholder: "Tìm phòng ban...",
                    render: {
                        item: function(data, escape) {
                            return '<div class="item" title="' + escape(data.text) + '">' + escape(data.text) + '</div>';
                        },
                        no_results: function(data, escape) {
                            return '<div class="no-results p-3 text-gray-500">Không tìm thấy phòng ban nào</div>';
                        }
                    }
                });
            }
        });
    </script>
    @endpush
@endif
