@extends('admin.layout')

@section('title', 'Menu Items - ' . $category->name)

@section('content')
<div class="max-w-7xl mx-auto">
    <!-- Back to Categories -->
    <div class="mb-6">
        <a href="{{ route('admin.categories') }}" 
           class="text-gold hover:text-dark-gold">
            <i class="fas fa-arrow-left mr-2"></i> Back to Categories
        </a>
    </div>
    
    <!-- Category Info -->
    <div class="bg-white rounded-lg shadow p-6 mb-8">
        <div class="flex items-center justify-between">
            <div>
                <h2 class="text-2xl font-bold">{{ $category->name }}</h2>
                <p class="text-gray-600">{{ $category->description }}</p>
            </div>
            <div>
                <span class="px-4 py-2 rounded-lg 
                    {{ $category->is_active ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800' }}">
                    {{ $category->is_active ? 'Active' : 'Inactive' }}
                </span>
            </div>
        </div>
    </div>
    
    <!-- Add Item Form -->
    <div class="bg-white rounded-lg shadow p-6 mb-8">
        <h3 class="text-xl font-bold mb-6">Add New Menu Item</h3>
        
        <form action="{{ route('admin.items.store', $category->id) }}" method="POST">
            @csrf
            
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Item Name *</label>
                    <input type="text" name="name" required
                           class="w-full p-3 border rounded-lg"
                           placeholder="Example: Sate Ayam">
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Price *</label>
                    <input type="number" name="price" required min="0" step="1000"
                           class="w-full p-3 border rounded-lg"
                           placeholder="Example: 25000">
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Display Order</label>
                    <input type="number" name="order" value="0"
                           class="w-full p-3 border rounded-lg">
                </div>
                
                <div class="md:col-span-3">
                    <label class="block text-sm font-medium text-gray-700 mb-2">Description</label>
                    <textarea name="description" rows="2"
                              class="w-full p-3 border rounded-lg"
                              placeholder="Describe this menu item"></textarea>
                </div>
                <div class="flex items-center">
    <input type="checkbox" name="is_active" id="is_active" value="1" checked
           class="h-5 w-5 text-gold rounded">
    <label for="is_active" class="ml-2 text-gray-700">Available for order</label>
</div>
            </div>
            
            <div class="mt-6">
                <button type="submit"
                        class="px-6 py-3 bg-gold text-black font-semibold rounded-lg hover:bg-dark-gold transition">
                    Add Menu Item
                </button>
            </div>
        </form>
    </div>
    
    <!-- Items List -->
    <div class="bg-white rounded-lg shadow overflow-hidden">
        <div class="px-6 py-4 border-b flex justify-between items-center">
            <h3 class="text-xl font-bold">Menu Items ({{ $items->count() }})</h3>
            <span class="text-sm text-gray-500">in {{ $category->name }}</span>
        </div>
        
        @if($items->isEmpty())
        <div class="p-8 text-center text-gray-500">
            <i class="fas fa-utensils text-4xl mb-4"></i>
            <p>No menu items yet. Add your first item above.</p>
        </div>
        @else
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">#</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Name</th>
                        <th class="px6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Description</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Price</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-200">
                    @foreach($items as $item)
                    <tr>
                        <td class="px-6 py-4">
                            <span class="px-3 py-1 bg-gray-100 rounded-full">{{ $item->order }}</span>
                        </td>
                        <td class="px-6 py-4">
                            <h4 class="font-medium">{{ $item->name }}</h4>
                        </td>
                        <td class="px-6 py-4">
                            <p class="text-sm text-gray-600">{{ $item->description }}</p>
                        </td>
                        <td class="px-6 py-4">
                            <span class="font-bold text-gold">{{ $item->formatted_price }}</span>
                        </td>
                                           <td class="px-6 py-4">
    @if($item->is_active)
        <span class="px-3 py-1 bg-green-100 text-green-800 rounded-full text-sm font-medium">
            <i class="fas fa-check-circle mr-1"></i> Available
        </span>
    @else
        <span class="px-3 py-1 bg-red-100 text-red-800 rounded-full text-sm font-medium">
            <i class="fas fa-times-circle mr-1"></i> Unavailable
        </span>
    @endif
</td>
                        <td class="px-6 py-4">
    <div class="flex space-x-2">
        <!-- Toggle Availability Button -->
        <form action="{{ route('admin.items.toggle', $item->id) }}" method="POST">
    @csrf
    <button type="submit"
            class="px-3 py-1 rounded text-white 
                   {{ $item->is_active ? 'bg-red-500 hover:bg-red-600' : 'bg-green-500 hover:bg-green-600' }}"
            title="{{ $item->is_active ? 'Mark as Unavailable' : 'Mark as Available' }}">
        @if($item->is_active)
            <i class="fas fa-times"></i>
        @else
            <i class="fas fa-check"></i>
        @endif
    </button>
</form>
        <!-- Edit Button -->
        <a href="{{ route('admin.items.edit', $item->id) }}"
           class="px-3 py-1 bg-yellow-500 text-white rounded hover:bg-yellow-600">
            <i class="fas fa-edit"></i>
        </a>
        
        <!-- Delete Button -->
        <form action="{{ route('admin.items.delete', $item->id) }}" 
              method="POST"
              onsubmit="return confirm('Delete this menu item?')">
            @csrf
            @method('DELETE')
            <button type="submit"
                    class="px-3 py-1 bg-red-500 text-white rounded hover:bg-red-600">
                <i class="fas fa-trash"></i>
            </button>
        </form>
    </div>
</td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        @endif
    </div>
</div>

<script>
function editItem(id) {
    // Simple edit - reload with form
    window.location.href = `/admin/items/${id}/edit`;
}
</script>
@endsection