@extends('layout.app')

@section('content')
<div class="pagetitle">
    <h1>Pilih Alternatif</h1>
    <nav>
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ route('admindash') }}">Dashboard</a></li>
            <li class="breadcrumb-item active">Pilih Alternatif</li>
        </ol>
    </nav>
</div>

<section class="section">
    <div class="row">
        <div class="col-12">

            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">Pilih Alternatif (6 makanan)</h5>

                    @if(session('success'))
                        <div class="alert alert-success">{{ session('success') }}</div>
                    @endif

                    {{-- Form Filter & Search --}}
                        {{-- Input pencarian (lebih pendek) --}}
                        <form method="GET" class="d-flex align-items-end flex-wrap gap-2 mb-4">
                            {{-- Input pencarian --}}
                            <div style="min-width: 100px; max-width: 170px;">
                                <input type="text" name="q" class="form-control form-control-sm" placeholder="Cari nama makanan..." value="{{ $search }}">
                            </div>
                            {{-- Dropdown kategori --}}
                            <div style="min-width: 150px; max-width: 180px;">
                                <select name="kategori" class="form-select form-select-sm">
                                    <option value="">Semua Kategori</option>
                                    @foreach($kategoris as $kategori)
                                    <option value="{{ $kategori->id }}" {{ ($kategoriFilter == $kategori->id) ? 'selected' : '' }}>
                                        {{ $kategori->kategori }}
                                    </option>
                                    @endforeach
                                </select>
                            </div>
                            {{-- Dropdown urutkan --}}
                            <div style="min-width: 150px;">
                                <select name="sort" class="form-select form-select-sm">
                                    <option value="">Semua Kriteria</option>
                                    <option value="lemak" {{ $sort == 'lemak' ? 'selected' : '' }}>Lemak</option>
                                    <option value="natrium" {{ $sort == 'natrium' ? 'selected' : '' }}>Natrium</option>
                                    <option value="energi" {{ $sort == 'energi' ? 'selected' : '' }}>Energi</option>
                                    <option value="karbohidrat" {{ $sort == 'karbohidrat' ? 'selected' : '' }}>Karbohidrat</option>
                                </select>
                            </div>
                            {{-- Dropdown order --}}
                            <div style="min-width: 140px;">
                                <select name="order" class="form-select form-select-sm">
                                    <option value="desc" {{ $order == 'desc' ? 'selected' : '' }}>Tertinggi</option>
                                    <option value="asc" {{ $order == 'asc' ? 'selected' : '' }}>Terendah</option>
                                </select>
                            </div>
                            {{-- Tombol --}}
                            <div class="d-flex gap-2" style="min-width: 140px;">
                                <button type="submit" class="btn btn-primary btn-sm">Terapkan</button>
                                <a href="{{ route('alternatif.pilih') }}" class="btn btn-secondary btn-sm">Reset</a>
                            </div>
                        </form>
                    {{-- Form Pemilihan Alternatif --}}
                    <form action="{{ route('alternatif.pilih.simpan') }}" method="POST">
                        @csrf
                        <div class="row">
                            @foreach ($makanans as $makanan)
                            <div class="col-12 col-sm-6 col-lg-4 mb-4">
                                <div class="card h-100 border-0 shadow-sm hover-shadow" style="transition: 0.3s;">
                                    <div class="card-body d-flex align-items-start gap-2">
                                        <input 
                                        class="form-check-input mt-1 alternatif-checkbox" 
                                        type="checkbox" 
                                        name="alternatifs[]" 
                                        value="{{ $makanan->id }}" 
                                        id="alt{{ $makanan->id }}"
                                        style="transform: scale(1.2);"
                                        >
                                        <label class="form-check-label ms-2 w-100" for="alt{{ $makanan->id }}">
                                            <h6 class="fw-bold mb-1">{{ $makanan->nama }}</h6>
                                            <ul class="list-unstyled small text-muted mb-0">
                                                <li><i class="bi bi-droplet-half me-1"></i> Lemak: {{ $makanan->lemak }} g</li>
                                                <li><i class="bi bi-shield-check me-1"></i> Natrium: {{ $makanan->natrium }} mg</li>
                                                <li><i class="bi bi-lightning me-1"></i> Energi: {{ $makanan->energi }} kal</li>
                                                <li><i class="bi bi-activity me-1"></i> Karbohidrat: {{ $makanan->karbohidrat }} g</li>
                                            </ul>
                                        </label>
                                    </div>
                                </div>
                            </div>
                            @endforeach
                        </div>
                        <div class="d-flex justify-content-end">
                            <button type="submit" class="btn btn-success btn-lg px-4 mt-3" id="submitBtn" disabled>
                                <i class="bi bi-arrow-right-circle-fill me-1"></i> Lanjutkan
                            </button>
                        </div>
                    
                    </form>
                    <hr class="my-4">
                </div>
            </div>

        </div>
    </div>
</section>
@endsection

@push('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function () {
        const checkboxes = document.querySelectorAll('.alternatif-checkbox');
        const submitBtn = document.getElementById('submitBtn');
        const max = 6;
        const min = 6;

        function update() {
            const checked = document.querySelectorAll('.alternatif-checkbox:checked').length;
            submitBtn.disabled = !(checked >= min && checked <= max);
            checkboxes.forEach(cb => {
                cb.disabled = !cb.checked && checked >= max;
            });
        }

        checkboxes.forEach(cb => {
            cb.addEventListener('change', update);
        });
    });
</script>
@endpush
