@extends('layouts.app')

@section('title', 'Grant ERP Access')

@section('content')
<div class="max-w-3xl 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">Grant ERP Access for: {{ $employee->full_name }}</h1>
            <p class="text-xs text-slate-500 dark:text-slate-400">Employee Code: <span class="font-mono font-bold text-sky-600 dark:text-sky-400">{{ $employee->employee_code }}</span> • {{ $employee->designation }}</p>
        </div>
        <a href="{{ route('app.employees.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 Directory</a>
    </div>

    <form action="{{ route('app.employees.store_access', $employee->id) }}" method="POST" class="space-y-6">
        @csrf

        <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 Credentials</h3>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-xs">
                <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="{{ strtolower(str_replace(' ', '.', $employee->first_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="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="{{ $employee->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="role_id" class="block font-semibold text-slate-700 dark:text-slate-300 mb-1">Assigned ERP 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 }}">{{ $role->name }} ({{ strtoupper($role->data_scope) }})</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 }}" {{ $employee->branch_id == $b->id ? 'selected' : '' }}>{{ $b->name }}</option>
                        @endforeach
                    </select>
                </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">Initial Password Setup</h3>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-xs">
                <div>
                    <label for="password" class="block font-semibold text-slate-700 dark:text-slate-300 mb-1">Password *</label>
                    <input type="password" name="password" id="password" 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">
                </div>

                <div>
                    <label for="password_confirmation" class="block font-semibold text-slate-700 dark:text-slate-300 mb-1">Confirm Password *</label>
                    <input type="password" name="password_confirmation" id="password_confirmation" 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">
                </div>
            </div>
        </div>

        <div class="flex justify-end space-x-3 rtl:space-x-reverse">
            <a href="{{ route('app.employees.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">
                Generate ERP Login Account
            </button>
        </div>
    </form>
</div>
@endsection
