@extends('layouts.admin.app')

@section('content')
<div class="container mx-auto px-4 py-8">
    <div class="flex justify-between items-center mb-6">
        <h1 class="text-2xl font-bold text-gray-800">Manajemen Menu</h1>
        <button type="button" onclick="openModal('addMenuModal')" class="bg-[#8B0000] text-white px-4 py-2 rounded-lg hover:bg-red-800">
            <i class="fas fa-plus mr-2"></i>Tambah Menu
        </button>
    </div>

    @if(session('success'))
    <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4" role="alert">
        <span class="block sm:inline">{{ session('success') }}</span>
    </div>
    @endif

    <!-- Display validation errors if any -->
    @if ($errors->any())
    <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
    @endif

    <!-- Category Filter Buttons -->
    <div class="flex flex-wrap gap-4 mb-6">
        <button class="category-filter px-6 py-2 rounded-full bg-[#8B0000] text-white hover:bg-red-800 transition-colors" data-category="all">
            Semua Menu
        </button>
        @foreach($categories as $category)
            <button class="category-filter px-6 py-2 rounded-full border border-[#8B0000] text-[#8B0000] hover:bg-[#8B0000] hover:text-white transition-colors" data-category="{{ $category->id }}">
                {{ $category->name }}
            </button>
        @endforeach
    </div>

    <div class="grid grid-cols-1 md:grid-cols-3 gap-6" id="menuGrid">
        @foreach($menuItems as $item)
        <div class="menu-item bg-white rounded-lg shadow-lg overflow-hidden" data-category="{{ $item->category_id }}">
            <img src="{{ Storage::url($item->image) }}" alt="{{ $item->name }}" class="w-full h-48 object-cover">
            <div class="p-4">
                <div class="flex justify-between items-start mb-2">
                    <h3 class="text-lg font-semibold text-gray-800">{{ $item->name }}</h3>
                    <span class="px-2 py-1 text-sm {{ $item->is_available ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800' }} rounded">
                        {{ $item->is_available ? 'Tersedia' : 'Tidak Tersedia' }}
                    </span>
                </div>
                <p class="text-gray-600 text-sm mb-2">{{ $item->description }}</p>
                <p class="text-[#8B0000] font-bold mb-2">Rp {{ number_format($item->price, 0, ',', '.') }}</p>
                <p class="text-sm text-gray-500 mb-4">Kategori: {{ $item->category->name }}</p>
                
                <div class="flex justify-end space-x-2">
                    <button onclick="openEditModal('{{ $item->id }}')" class="text-blue-600 hover:text-blue-800">
                        <i class="fas fa-edit"></i>
                    </button>
                    <form action="{{ route('admin.menu.destroy', $item) }}" method="POST" class="inline">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="text-red-600 hover:text-red-800" onclick="return confirm('Apakah Anda yakin ingin menghapus menu ini?')">
                            <i class="fas fa-trash"></i>
                        </button>
                    </form>
                </div>
            </div>
        </div>
        @endforeach
    </div>
</div>

<!-- Modal Tambah Menu -->
<div id="addMenuModal" class="modal fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
    <div class="bg-white rounded-lg p-8 max-w-md w-full m-4" onclick="event.stopPropagation();">
        <div class="flex justify-between items-center mb-4">
            <h2 class="text-xl font-bold">Tambah Menu Baru</h2>
            <button type="button" onclick="closeModal('addMenuModal')" class="text-gray-600 hover:text-gray-800">
                <i class="fas fa-times"></i>
            </button>
        </div>
        
        <form action="{{ route('admin.menu.store') }}" method="POST" enctype="multipart/form-data">
            @csrf
            <div class="space-y-4">
                <div>
                    <label class="block text-gray-700 text-sm font-bold mb-2">Nama Menu</label>
                    <input type="text" name="name" class="w-full border rounded px-3 py-2" required>
                </div>
                
                <div>
                    <label class="block text-gray-700 text-sm font-bold mb-2">Deskripsi</label>
                    <textarea name="description" class="w-full border rounded px-3 py-2" rows="3"></textarea>
                </div>
                
                <div>
                    <label class="block text-gray-700 text-sm font-bold mb-2">Harga</label>
                    <input type="number" name="price" class="w-full border rounded px-3 py-2" required>
                </div>
                
                <div>
                    <label class="block text-gray-700 text-sm font-bold mb-2">Kategori</label>
                    <select name="category_id" class="w-full border rounded px-3 py-2" required>
                        @foreach($categories as $category)
                        <option value="{{ $category->id }}">{{ $category->name }}</option>
                        @endforeach
                    </select>
                </div>
                
                <div>
                    <label class="block text-gray-700 text-sm font-bold mb-2">Gambar</label>
                    <input type="file" name="image" class="w-full border rounded px-3 py-2" required accept="image/*">
                </div>
                
                <div class="flex items-center">
                    <input type="checkbox" name="is_available" id="add_is_available" class="form-checkbox h-4 w-4 text-[#8B0000] rounded border-gray-300 focus:ring-[#8B0000]">
                    <label class="ml-2 text-gray-700 text-sm">Menu Tersedia</label>
                </div>
            </div>
            
            <div class="mt-6 flex justify-end">
                <button type="button" onclick="closeModal('addMenuModal')" class="bg-gray-300 text-gray-700 px-4 py-2 rounded mr-2">Batal</button>
                <button type="submit" class="bg-[#8B0000] text-white px-4 py-2 rounded">Simpan</button>
            </div>
        </form>
    </div>
</div>

<!-- Modal Edit Menu -->
<div id="editMenuModal" class="modal fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
    <div class="bg-white rounded-lg p-8 max-w-md w-full m-4" onclick="event.stopPropagation();">
        <div class="flex justify-between items-center mb-4">
            <h2 class="text-xl font-bold">Edit Menu</h2>
            <button type="button" onclick="closeModal('editMenuModal')" class="text-gray-600 hover:text-gray-800">
                <i class="fas fa-times"></i>
            </button>
        </div>
        
        <form id="editMenuForm" method="POST" enctype="multipart/form-data">
            @csrf
            @method('PUT')
            <div class="space-y-4">
                <div>
                    <label class="block text-gray-700 text-sm font-bold mb-2">Nama Menu</label>
                    <input type="text" name="name" id="edit_name" class="w-full border rounded px-3 py-2" required>
                </div>
                
                <div>
                    <label class="block text-gray-700 text-sm font-bold mb-2">Deskripsi</label>
                    <textarea name="description" id="edit_description" class="w-full border rounded px-3 py-2" rows="3"></textarea>
                </div>
                
                <div>
                    <label class="block text-gray-700 text-sm font-bold mb-2">Harga</label>
                    <input type="number" name="price" id="edit_price" class="w-full border rounded px-3 py-2" required>
                </div>
                
                <div>
                    <label class="block text-gray-700 text-sm font-bold mb-2">Kategori</label>
                    <select name="category_id" id="edit_category_id" class="w-full border rounded px-3 py-2" required>
                        @foreach($categories as $category)
                        <option value="{{ $category->id }}">{{ $category->name }}</option>
                        @endforeach
                    </select>
                </div>
                
                <div>
                    <label class="block text-gray-700 text-sm font-bold mb-2">Gambar Baru (Opsional)</label>
                    <input type="file" name="image" class="w-full border rounded px-3 py-2" accept="image/*">
                </div>
                
                <div class="flex items-center">
                    <input type="checkbox" name="is_available" id="edit_is_available" class="form-checkbox h-4 w-4 text-[#8B0000] rounded border-gray-300 focus:ring-[#8B0000]">
                    <label class="ml-2 text-gray-700 text-sm">Menu Tersedia</label>
                </div>
            </div>
            
            <div class="mt-6 flex justify-end">
                <button type="button" onclick="closeModal('editMenuModal')" class="bg-gray-300 text-gray-700 px-4 py-2 rounded mr-2">Batal</button>
                <button type="submit" class="bg-[#8B0000] text-white px-4 py-2 rounded">Simpan Perubahan</button>
            </div>
        </form>
    </div>
</div>

@push('scripts')
<script>
    // Make menu items available globally for the edit modal
    window.menuItems = @json($menuItems);

    // Category filter functionality
    document.addEventListener('DOMContentLoaded', function() {
        const filterButtons = document.querySelectorAll('.category-filter');
        const menuItems = document.querySelectorAll('.menu-item');

        filterButtons.forEach(button => {
            button.addEventListener('click', function() {
                // Remove active class from all buttons
                filterButtons.forEach(btn => {
                    btn.classList.remove('bg-[#8B0000]', 'text-white');
                    btn.classList.add('border', 'border-[#8B0000]', 'text-[#8B0000]');
                });

                // Add active class to clicked button
                this.classList.remove('border', 'border-[#8B0000]', 'text-[#8B0000]');
                this.classList.add('bg-[#8B0000]', 'text-white');

                const selectedCategory = this.dataset.category;
                
                menuItems.forEach(item => {
                    if (selectedCategory === 'all' || item.dataset.category === selectedCategory) {
                        item.style.display = 'block';
                    } else {
                        item.style.display = 'none';
                    }
                });
            });
        });

        // Set "Semua Menu" as active by default
        document.querySelector('[data-category="all"]').click();
    });
</script>
<script src="{{ asset('js/menu-modal.js') }}"></script>
@endpush
@endsection 