@extends('layout.app')

@section('content')
    <div class="pagetitle">
        <h1>Edit Data Makanan</h1>
        <nav>
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="{{ route('admindash') }}">Home</a></li>
                <li class="breadcrumb-item active"><a href="{{ route('makanan') }}">Makanan></li>
                <li class="breadcrumb-item active"><a href="{{ route('editmakanan', $makanan->id) }}">Edit Makanan</a></li>
            </ol>
        </nav>
    </div><!-- End Page Title -->

    <div class="card">
        <div class="card-body">
            <h5 class="card-title">Edit Makanan</h5>

            <!-- General Form Elements -->
            <form id="editUserForm" method="POST" action="{{ url("/makanan/{$makanan->id}") }}">
                @method('PUT')
                @csrf

                <div class="row mb-3">
                    <label for="inputText" class="col-sm-2 col-form-label">Name</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="nama" value="{{ $makanan->nama }}">
                    </div>
                </div>
                <div class="row mb-3">
                    <label for="role" class="col-sm-2 col-form-label">Kategori</label>
                    <div class="col-sm-10">
                        <select id="role" class="form-select" name="kategori_id">
                            @foreach ($kategoris as $id => $kategori)
                                <option value="{{ $id }}" {{ $makanan->kategori_id == $id ? 'selected' : '' }}>{{ $kategori }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="row mb-3">
                    <label for="role" class="col-sm-2 col-form-label">Jenis Makanan</label>
                    <div class="col-sm-10">
                        <select id="role" class="form-select" name="jenis_id">
                            @foreach ($jenis_makanans as $id => $jenis)
                                <option value="{{ $id }}" {{ $makanan->jenis_id == $id ? 'selected' : '' }}>{{ $jenis }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>                
                <div class="row mb-3">
                    <label for="inputText" class="col-sm-2 col-form-label">Lemak</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="lemak" value="{{ $makanan->lemak }}">
                    </div>
                </div>
                <div class="row mb-3">
                    <label for="inputText" class="col-sm-2 col-form-label">Natrium</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="natrium" value="{{ $makanan->natrium }}">
                    </div>
                </div>
                <div class="row mb-3">
                    <label for="inputText" class="col-sm-2 col-form-label">Energi</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="energi" value="{{ $makanan->energi }}">
                    </div>
                </div>
                <div class="row mb-3">
                    <label for="inputText" class="col-sm-2 col-form-label">Karbohidrat</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="karbohidrat" value="{{ $makanan->karbohidrat }}">
                    </div>
                </div>
                <div class="row mb-3">
                    <div class="col-sm-12 text-center">
                        <a href="{{ route('makanan') }}" class="btn btn-secondary mx-3">Back</a>
                        <button type="button" class="btn btn-primary mx-3" onclick="confirmSave()">Save</button>
                        {{-- <button type="submit" class="btn btn-primary mx-3" onclick="return confirmSave()">Save</button> --}}
                        <!-- Reset button to reset form fields -->
                        <button type="reset" class="btn btn-secondary">Reset</button>
                    </div>
                </div>
            </form><!-- End General Form Elements -->

        </div>
    </div>

    <!-- SweetAlert2 -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <!-- Confirmation Dialog Script -->
    <script>
        function togglePasswordVisibility() {
            var passwordField = document.getElementById("passwordField");
            if (passwordField.type === "password") {
                passwordField.type = "text";
                document.querySelector('button[onclick="togglePasswordVisibility()"]').textContent = "Hide";
            } else {
                passwordField.type = "password";
                document.querySelector('button[onclick="togglePasswordVisibility()"]').textContent = "Show";
            }
        }

        function confirmSave() {
            Swal.fire({
                title: 'Are you sure?',
                text: "Do you want to save the changes?",
                icon: 'question',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, save it!'
            }).then((result) => {
                if (result.isConfirmed) {
                    document.getElementById('editUserForm').submit();
                }
            });
        }
    </script>
@endsection
