@extends('layouts.admin')

@section('title', 'Staff Accounts & RBAC – Admin CRM')
@section('page_title', 'Staff & Role-Based Access Control (RBAC)')

@section('content')
<div class="space-y-8 max-w-6xl">
    
    <!-- Add Staff Form -->
    <div class="bg-slate-900 border border-white/10 rounded-2xl p-6 shadow-xl space-y-4" x-data="{ open: false }">
        <div class="flex items-center justify-between border-b border-white/10 pb-3">
            <div>
                <h3 class="text-base font-bold text-white font-heading">Staff & Department Accounts</h3>
                <p class="text-xs text-slate-400">Manage internal team permissions across showroom sales, auction desk, and accounts.</p>
            </div>
            <button type="button" @click="open = !open" class="px-3.5 py-1.5 rounded-xl bg-red-600 hover:bg-red-700 text-white font-bold text-xs transition flex items-center gap-1.5">
                <i class="fa-solid" :class="open ? 'fa-minus' : 'fa-plus'"></i>
                <span x-text="open ? 'Close' : 'Add Staff Member'"></span>
            </button>
        </div>

        <form method="POST" action="{{ route('admin.users.store') }}" x-show="open" class="space-y-4 pt-2 text-xs">
            @csrf

            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                <div>
                    <label class="block font-bold text-slate-300 uppercase text-[10px] mb-1">Full Name *</label>
                    <input type="text" name="name" required class="w-full bg-slate-950 border border-white/10 rounded-xl px-4 py-2 text-white text-xs font-semibold focus:border-red-500 focus:outline-none" placeholder="Staff Name">
                </div>
                <div>
                    <label class="block font-bold text-slate-300 uppercase text-[10px] mb-1">Email Address *</label>
                    <input type="email" name="email" required class="w-full bg-slate-950 border border-white/10 rounded-xl px-4 py-2 text-white text-xs font-semibold focus:border-red-500 focus:outline-none" placeholder="staff@motosolbd.com">
                </div>
            </div>

            <div class="grid grid-cols-1 sm:grid-cols-3 gap-4">
                <div>
                    <label class="block font-bold text-slate-300 uppercase text-[10px] mb-1">Phone Number *</label>
                    <input type="text" name="phone" required class="w-full bg-slate-950 border border-white/10 rounded-xl px-4 py-2 text-white text-xs font-semibold focus:border-red-500 focus:outline-none" placeholder="01711-XXXXXX">
                </div>
                <div>
                    <label class="block font-bold text-slate-300 uppercase text-[10px] mb-1">System Role *</label>
                    <select name="role" required class="w-full bg-slate-950 border border-white/10 rounded-xl px-3 py-2 text-white text-xs font-semibold">
                        <option value="admin">Administrator</option>
                        <option value="auction_manager">Auction Manager</option>
                        <option value="sales_manager">Sales Manager</option>
                        <option value="accounts">Accounts & Finance</option>
                        <option value="content_manager">Content Manager</option>
                    </select>
                </div>
                <div>
                    <label class="block font-bold text-slate-300 uppercase text-[10px] mb-1">Temporary Password *</label>
                    <input type="password" name="password" required minlength="8" class="w-full bg-slate-950 border border-white/10 rounded-xl px-4 py-2 text-white text-xs font-semibold focus:border-red-500 focus:outline-none">
                </div>
            </div>

            <button type="submit" class="px-5 py-2.5 rounded-xl bg-red-600 hover:bg-red-700 text-white font-bold text-xs shadow transition">
                Create Staff Account
            </button>
        </form>
    </div>

    <!-- Staff Accounts List -->
    <div class="bg-slate-900 border border-white/10 rounded-2xl overflow-hidden shadow-xl">
        <div class="p-4 border-b border-white/10">
            <h3 class="text-sm font-bold text-white font-heading">Active Staff & Roles ({{ $users->count() }})</h3>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full text-left text-xs">
                <thead class="bg-slate-950 text-slate-400 uppercase tracking-wider text-[10px] border-b border-white/10">
                    <tr>
                        <th class="p-4">Staff Member</th>
                        <th class="p-4">Contact</th>
                        <th class="p-4">Current Role</th>
                        <th class="p-4">Status</th>
                        <th class="p-4 text-right">Update Permissions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-white/5 text-slate-300">
                    @foreach($users as $user)
                        <tr class="hover:bg-white/5 transition">
                            <td class="p-4">
                                <div class="flex items-center gap-3">
                                    <div class="w-8 h-8 rounded-lg bg-red-600 text-white font-bold flex items-center justify-center text-xs">
                                        {{ strtoupper(substr($user->name, 0, 1)) }}
                                    </div>
                                    <div>
                                        <span class="font-bold text-white">{{ $user->name }}</span>
                                        <span class="text-[10px] text-slate-500 block">{{ $user->email }}</span>
                                    </div>
                                </div>
                            </td>
                            <td class="p-4 text-slate-300">
                                {{ $user->phone ?? 'N/A' }}
                            </td>
                            <td class="p-4">
                                <span class="px-2.5 py-0.5 rounded-full text-[10px] font-bold uppercase bg-white/10 text-red-400">
                                    {{ str_replace('_', ' ', $user->role) }}
                                </span>
                            </td>
                            <td class="p-4">
                                <span class="px-2 py-0.5 rounded text-[10px] font-bold uppercase {{ $user->status === 'active' ? 'bg-emerald-500/20 text-emerald-400' : 'bg-rose-500/20 text-rose-400' }}">
                                    {{ $user->status ?? 'active' }}
                                </span>
                            </td>
                            <td class="p-4 text-right">
                                <form method="POST" action="{{ route('admin.users.role', $user->id) }}" class="inline-flex items-center gap-2">
                                    @csrf
                                    <select name="role" class="bg-slate-950 border border-white/10 rounded-lg px-2 py-1 text-white text-[11px]">
                                        <option value="super_admin" {{ $user->role === 'super_admin' ? 'selected' : '' }}>Super Admin</option>
                                        <option value="admin" {{ $user->role === 'admin' ? 'selected' : '' }}>Admin</option>
                                        <option value="auction_manager" {{ $user->role === 'auction_manager' ? 'selected' : '' }}>Auction Manager</option>
                                        <option value="sales_manager" {{ $user->role === 'sales_manager' ? 'selected' : '' }}>Sales Manager</option>
                                        <option value="accounts" {{ $user->role === 'accounts' ? 'selected' : '' }}>Accounts</option>
                                    </select>
                                    <select name="status" class="bg-slate-950 border border-white/10 rounded-lg px-2 py-1 text-white text-[11px]">
                                        <option value="active" {{ $user->status === 'active' ? 'selected' : '' }}>Active</option>
                                        <option value="suspended" {{ $user->status === 'suspended' ? 'selected' : '' }}>Suspended</option>
                                    </select>
                                    <button type="submit" class="px-2.5 py-1 rounded-lg bg-blue-600 hover:bg-blue-500 text-white font-bold text-[11px]">
                                        Save
                                    </button>
                                </form>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>

</div>
@endsection
