@extends('layouts.admin')
@section('title', 'Pengguna - SI Apotek')

@section('content')
<div class="flex justify-between items-center mb-6">
    <h2 class="text-2xl font-semibold text-gray-700">Manajemen Pengguna</h2>
    <div x-data="{ openAdd: false }">
        <button @click="openAdd = true" class="bg-emerald-600 text-white px-4 py-2 rounded-lg font-medium hover:bg-emerald-700 transition flex items-center gap-2 shadow-sm">
            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M18 9v3m0 0v3m0-3h3m-3 0h-3m-2-5a4 4 0 11-8 0 4 4 0 018 0zM3 20a6 6 0 0112 0v1H3v-1z"></path></svg>
            Tambah Pengguna
        </button>
        
        <!-- Modal Tambah -->
        <div x-show="openAdd" class="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50 backdrop-blur-sm" x-cloak>
            <div @click.away="openAdd = false" class="bg-white rounded-2xl p-6 w-[500px] text-left shadow-xl">
                <div class="flex justify-between items-center mb-6">
                    <h3 class="text-xl font-bold text-gray-800">Tambah Pengguna</h3>
                    <button @click="openAdd = false" class="text-gray-400 hover:text-gray-600"><svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg></button>
                </div>
                <form action="{{ route('users.store') }}" method="POST">
                    @csrf
                    <div class="space-y-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Nama Lengkap</label>
                            <input type="text" name="name" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Username</label>
                            <input type="text" name="username" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Password</label>
                            <input type="password" name="password" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Hak Akses</label>
                            <select name="role" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                                <option value="admin">Admin</option>
                                <option value="cashier">Kasir</option>
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Status Akun</label>
                            <select name="status" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                                <option value="1">Aktif</option>
                                <option value="0">Non-Aktif</option>
                            </select>
                        </div>
                    </div>
                    <div class="flex justify-end gap-3 mt-8 border-t pt-5">
                        <button type="button" @click="openAdd = false" class="px-5 py-2 text-sm font-medium text-gray-600 bg-gray-100 rounded-lg hover:bg-gray-200 transition-colors">Batal</button>
                        <button type="submit" class="px-5 py-2 text-sm font-medium text-white bg-emerald-600 rounded-lg hover:bg-emerald-700 transition-colors shadow-sm">Simpan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

@if(session('success'))
<div class="p-4 mb-4 text-sm text-emerald-700 bg-emerald-100 rounded-lg flex items-center gap-2">
    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>
    {{ session('success') }}
</div>
@endif

@if($errors->any())
<div class="p-4 mb-4 text-sm text-red-700 bg-red-100 rounded-lg">
    <ul class="list-disc pl-5">
        @foreach($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
@endif

<div class="bg-white rounded-lg shadow-sm border border-gray-100 overflow-hidden">
    <div class="overflow-x-auto">
        <table class="w-full whitespace-no-wrap">
            <thead>
                <tr class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b bg-gray-50">
                    <th class="px-4 py-3">No</th>
                    <th class="px-4 py-3">Nama</th>
                    <th class="px-4 py-3">Username</th>
                    <th class="px-4 py-3">Role</th>
                    <th class="px-4 py-3 text-center">Status</th>
                    <th class="px-4 py-3 text-right">Aksi</th>
                </tr>
            </thead>
            <tbody class="divide-y">
                @foreach($users as $user)
                <tr class="text-gray-700 hover:bg-gray-50 transition-colors">
                    <td class="px-4 py-3 text-sm">{{ $loop->iteration }}</td>
                    <td class="px-4 py-3 text-sm font-semibold text-gray-800">
                        <div class="flex items-center gap-2">
                            <div class="w-8 h-8 rounded-full bg-emerald-100 flex items-center justify-center text-emerald-600 font-bold">
                                {{ substr($user->name, 0, 1) }}
                            </div>
                            {{ $user->name }}
                        </div>
                    </td>
                    <td class="px-4 py-3 text-sm">{{ $user->username }}</td>
                    <td class="px-4 py-3 text-sm">
                        <span class="px-2 py-1 leading-tight rounded-full {{ $user->role == 'admin' ? 'bg-purple-100 text-purple-700' : 'bg-blue-100 text-blue-700' }}">
                            {{ ucfirst($user->role) }}
                        </span>
                    </td>
                    <td class="px-4 py-3 text-sm text-center">
                        @if($user->status)
                            <span class="px-2 py-1 leading-tight text-emerald-700 bg-emerald-100 rounded-full">Aktif</span>
                        @else
                            <span class="px-2 py-1 leading-tight text-red-700 bg-red-100 rounded-full">Non-Aktif</span>
                        @endif
                    </td>
                    <td class="px-4 py-3 text-sm text-right">
                        <div class="flex justify-end gap-2">
                            <!-- Edit -->
                            <div x-data="{ openEdit: false }">
                                <button @click="openEdit = true" class="px-3 py-1 text-xs font-medium leading-5 text-emerald-600 bg-emerald-100 rounded-lg hover:bg-emerald-200 transition-colors">Edit</button>
                                
                                <!-- Modal Edit -->
                                <div x-show="openEdit" class="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50 backdrop-blur-sm" x-cloak>
                                    <div @click.away="openEdit = false" class="bg-white rounded-2xl p-6 w-[500px] text-left shadow-xl">
                                        <div class="flex justify-between items-center mb-6">
                                            <h3 class="text-xl font-bold text-gray-800">Edit Pengguna</h3>
                                            <button @click="openEdit = false" class="text-gray-400 hover:text-gray-600"><svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg></button>
                                        </div>
                                        <form action="{{ route('users.update', $user->id) }}" method="POST">
                                            @csrf @method('PUT')
                                            <div class="space-y-4">
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-1">Nama Lengkap</label>
                                                    <input type="text" name="name" value="{{ $user->name }}" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500" required>
                                                </div>
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-1">Username</label>
                                                    <input type="text" name="username" value="{{ $user->username }}" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500" required>
                                                </div>
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-1">Password <span class="text-gray-400 font-normal">(Kosongkan jika tidak diubah)</span></label>
                                                    <input type="password" name="password" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500">
                                                </div>
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-1">Hak Akses</label>
                                                    <select name="role" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500" required>
                                                        <option value="admin" {{ $user->role == 'admin' ? 'selected' : '' }}>Admin</option>
                                                        <option value="cashier" {{ $user->role == 'cashier' ? 'selected' : '' }}>Kasir</option>
                                                    </select>
                                                </div>
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-1">Status Akun</label>
                                                    <select name="status" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500" required>
                                                        <option value="1" {{ $user->status ? 'selected' : '' }}>Aktif</option>
                                                        <option value="0" {{ !$user->status ? 'selected' : '' }}>Non-Aktif</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="flex justify-end gap-3 mt-8 border-t pt-5">
                                                <button type="button" @click="openEdit = false" class="px-5 py-2 text-sm font-medium text-gray-600 bg-gray-100 rounded-lg">Batal</button>
                                                <button type="submit" class="px-5 py-2 text-sm font-medium text-white bg-emerald-600 rounded-lg">Simpan</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>

                            @if(auth()->id() != $user->id)
                            <!-- Delete -->
                            <form action="{{ route('users.destroy', $user->id) }}" method="POST" onsubmit="return confirm('Yakin ingin menghapus pengguna ini?');" class="inline">
                                @csrf @method('DELETE')
                                <button type="submit" class="px-3 py-1 text-xs font-medium leading-5 text-red-600 bg-red-100 rounded-lg hover:bg-red-200 transition-colors">Hapus</button>
                            </form>
                            @endif
                        </div>
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>
@endsection
