@extends('layouts.app')

@section('title', 'Edit User Account')

@section('content')
<div class="max-w-4xl mx-auto space-y-6">

    <div class="flex items-center justify-between">
        <div>
            <h1 class="text-xl font-bold text-slate-900 dark:text-white">Edit User: {{ $user->name }}</h1>
            <p class="text-xs text-slate-500 dark:text-slate-400">Update user credentials, assigned role, and status.</p>
        </div>
        <a href="{{ route('app.users.index') }}" class="px-4 py-2 bg-slate-200 dark:bg-slate-700 text-slate-700 dark:text-slate-200 font-semibold text-xs rounded-xl">Back to Users</a>
    </div>

    <form action="{{ route('app.users.update', $user->id) }}" method="POST" class="space-y-6">
        @csrf
        @method('PUT')

        <div class="bg-white dark:bg-slate-800 rounded-3xl p-6 border border-slate-200 dark:border-slate-700/60 shadow-sm space-y-4">
            <h3 class="text-sm font-bold text-slate-900 dark:text-white uppercase tracking-wider">Account Information</h3>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-xs">
                <div>
                    <label for="name" class="block font-semibold text-slate-700 dark:text-slate-300 mb-1">Full Name *</label>
                    <input type="text" name="name" id="name" required value="{{ $user->name }}" class="w-full px-3 py-2 bg-slate-50 dark:bg-slate-900 border border-slate-300 dark:border-slate-700 rounded-xl text-slate-900 dark:text-white">
                </div>

                <div>
                    <label for="username" class="block font-semibold text-slate-700 dark:text-slate-300 mb-1">Username *</label>
                    <input type="text" name="username" id="username" required value="{{ $user->username }}" class="w-full px-3 py-2 bg-slate-50 dark:bg-slate-900 border border-slate-300 dark:border-slate-700 rounded-xl text-slate-900 dark:text-white">
                </div>

                <div>
                    <label for="email" class="block font-semibold text-slate-700 dark:text-slate-300 mb-1">Email Address *</label>
                    <input type="email" name="email" id="email" required value="{{ $user->email }}" class="w-full px-3 py-2 bg-slate-50 dark:bg-slate-900 border border-slate-300 dark:border-slate-700 rounded-xl text-slate-900 dark:text-white">
                </div>

                <div>
                    <label for="mobile" class="block font-semibold text-slate-700 dark:text-slate-300 mb-1">Mobile Number</label>
                    <input type="text" name="mobile" id="mobile" value="{{ $user->mobile }}" class="w-full px-3 py-2 bg-slate-50 dark:bg-slate-900 border border-slate-300 dark:border-slate-700 rounded-xl text-slate-900 dark:text-white">
                </div>
            </div>
        </div>

        <div class="bg-white dark:bg-slate-800 rounded-3xl p-6 border border-slate-200 dark:border-slate-700/60 shadow-sm space-y-4">
            <h3 class="text-sm font-bold text-slate-900 dark:text-white uppercase tracking-wider">Role & Branch Assignments</h3>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-xs">
                <div>
                    <label for="role_id" class="block font-semibold text-slate-700 dark:text-slate-300 mb-1">Role *</label>
                    <select name="role_id" id="role_id" required class="w-full px-3 py-2 bg-slate-50 dark:bg-slate-900 border border-slate-300 dark:border-slate-700 rounded-xl text-slate-900 dark:text-white">
                        @foreach($roles as $role)
                            <option value="{{ $role->id }}" {{ $user->hasRole($role->slug) ? 'selected' : '' }}>{{ $role->name }}</option>
                        @endforeach
                    </select>
                </div>

                <div>
                    <label for="default_branch_id" class="block font-semibold text-slate-700 dark:text-slate-300 mb-1">Default Branch *</label>
                    <select name="default_branch_id" id="default_branch_id" required class="w-full px-3 py-2 bg-slate-50 dark:bg-slate-900 border border-slate-300 dark:border-slate-700 rounded-xl text-slate-900 dark:text-white">
                        @foreach($branches as $b)
                            <option value="{{ $b->id }}" {{ $user->default_branch_id == $b->id ? 'selected' : '' }}>{{ $b->name }}</option>
                        @endforeach
                    </select>
                </div>

                <div>
                    <label for="status" class="block font-semibold text-slate-700 dark:text-slate-300 mb-1">Account Status *</label>
                    <select name="status" id="status" required class="w-full px-3 py-2 bg-slate-50 dark:bg-slate-900 border border-slate-300 dark:border-slate-700 rounded-xl text-slate-900 dark:text-white">
                        <option value="active" {{ $user->status === 'active' ? 'selected' : '' }}>Active</option>
                        <option value="suspended" {{ $user->status === 'suspended' ? 'selected' : '' }}>Suspended</option>
                        <option value="inactive" {{ $user->status === 'inactive' ? 'selected' : '' }}>Inactive</option>
                    </select>
                </div>
                <label class="flex items-center gap-2 rounded-xl border border-slate-200 px-4 py-3 font-semibold text-slate-700">
                    <input type="checkbox" name="allow_web_login" value="1" class="h-4 w-4 rounded text-sky-600" @checked($user->allow_web_login)>
                    Allow web browser login
                </label>
                <label class="flex items-center gap-2 rounded-xl border border-slate-200 px-4 py-3 font-semibold text-slate-700">
                    <input type="checkbox" name="allow_mobile_login" value="1" class="h-4 w-4 rounded text-sky-600" @checked($user->allow_mobile_login)>
                    Allow mobile login
                </label>
            </div>
        </div>

        <div class="flex justify-end space-x-3 rtl:space-x-reverse">
            <a href="{{ route('app.users.index') }}" class="px-5 py-2.5 bg-slate-200 dark:bg-slate-700 text-slate-700 dark:text-slate-200 font-bold text-xs rounded-xl">Cancel</a>
            <button type="submit" class="px-6 py-2.5 bg-sky-600 hover:bg-sky-700 text-white font-bold text-xs rounded-xl shadow-lg shadow-sky-600/25">
                Update Account
            </button>
        </div>
    </form>
</div>
@endsection
