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

@section('content')
<h2 class="my-6 text-2xl font-semibold text-gray-700">Manajemen Kategori Obat</h2>

@if(session('success'))
<div class="p-4 mb-4 text-sm text-emerald-700 bg-emerald-100 rounded-lg flex items-center gap-2" role="alert">
    <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

<div class="grid md:grid-cols-3 gap-6">
    <!-- Form Tambah -->
    <div class="bg-white rounded-lg shadow-sm border border-gray-100 p-6 h-fit">
        <h3 class="text-lg font-semibold text-gray-700 mb-4">Tambah Kategori</h3>
        <form action="{{ route('categories.store') }}" method="POST">
            @csrf
            <div class="mb-4">
                <label class="block text-sm font-medium text-gray-700 mb-1">Nama Kategori</label>
                <input type="text" name="name" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500 transition-colors" placeholder="Contoh: Bebas, Keras..." required>
            </div>
            <button type="submit" class="w-full bg-emerald-600 text-white py-2 rounded-lg hover:bg-emerald-700 transition font-semibold shadow-sm">Simpan</button>
        </form>
    </div>

    <!-- Tabel Data -->
    <div class="md:col-span-2 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 w-16">No</th>
                        <th class="px-4 py-3">Nama Kategori</th>
                        <th class="px-4 py-3 text-right">Aksi</th>
                    </tr>
                </thead>
                <tbody class="divide-y">
                    @foreach($categories as $cat)
                    <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-medium">{{ $cat->name }}</td>
                        <td class="px-4 py-3 text-sm text-right">
                            <div class="flex justify-end gap-2">
                                <!-- Edit using Alpine -->
                                <div x-data="{ openEdit: false }">
                                    <button @click="openEdit = true" class="px-3 py-1 text-sm 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-96 text-left shadow-xl transform transition-all">
                                            <h3 class="text-lg font-bold text-gray-800 mb-4">Edit Kategori</h3>
                                            <form action="{{ route('categories.update', $cat->id) }}" method="POST">
                                                @csrf @method('PUT')
                                                <div class="mb-4">
                                                    <label class="block text-sm font-medium text-gray-700 mb-1">Nama Kategori</label>
                                                    <input type="text" name="name" value="{{ $cat->name }}" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                                                </div>
                                                <div class="flex justify-end gap-2 mt-6">
                                                    <button type="button" @click="openEdit = false" class="px-4 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-4 py-2 text-sm font-medium text-white bg-emerald-600 rounded-lg hover:bg-emerald-700 transition-colors shadow-sm">Simpan Perubahan</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>

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