@extends('component.app')
@section('title', 'Paket')
@section('active8', 'active')
@section('menu1', 'open')
@section('content')
    <div class="content-wrapper">
        <div class="container-xxl flex-grow-1 container-p-y">
            <!-- Invoice List Table -->
            <div class="card">
                <div class="header-menu d-flex justify-content-between align-items-center">
                    <h3 class="card-header">Edit Data Paket</h3>
                    <form action="{{ route('paket.update', $data->id) }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        <button type="submit" class="btn btn-primary me-2"><i class="bx bx-save bx-sm"></i> Simpan</button>
                </div>

                <div class="card-body">
                    <div class="row">
                        <!-- Kolom Kiri -->
                        <div class="col-md-4">
                            @if (Auth::user()->role == 'superadmin')
                                <div class="mb-3">
                                    <label for="users_id" class="form-label">Pilih Laundry</label>
                                    <select id="pelanggan" name="laundry_id" class="form-select form-control-sm" required>
                                        <option value="{{ $data->laundry_id }}"> {{ $data->laundry->nama_laundry }} -
                                            {{ $data->laundry->email }}</option>
                                        <option value="">Pilih Laundry</option>
                                        @foreach ($laundry as $laundry)
                                            <option value="{{ $laundry->id }}">{{ $laundry->nama_laundry }} -
                                                {{ $laundry->email }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            @else
                                <div class="mb-3">
                                    <label for="users_id" class="form-label">Nama Laundry</label>
                                    <input type="hidden" name="laundry_id" value="{{ $laundry->id }}">
                                    <input type="text" class="form-control form-control-sm"
                                        value="{{ $laundry->nama_laundry }}">
                                </div>
                            @endif
                            <!-- Nama Brand -->
                            <div class="mb-3">
                                <label for="nama_brand" class="form-label">Nama Paket</label>
                                <input type="text" class="form-control" id="nama_paket" name="nama_paket"
                                    placeholder="Masukkan Nama Paket" value="{{ $data->nama_paket }}" required>
                            </div>

                            <!-- Logo Brand -->
                            <div class="mb-3">
                                <label for="foto_paket" class="form-label">Foto Paket</label>
                                <input type="file" class="form-control" id="foto" name="foto">
                                <span class="text-danger">*Isi jika mengganti foto</span>
                            </div>
                            <div class="nb-3">
                                <img src="{{ asset('storage/img/' . $data->foto) }}" alt="" width="100px"
                                    height="100px">
                            </div>

                        </div>
                        <div class="col-md-4">

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="mb-3">
                                        <label for="harga" class="form-label">Harga</label>
                                        <input type="number" class="form-control" id="harga" name="harga"
                                            placeholder="Masukkan harga" value="{{ $data->harga }}" required>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="mb-3">
                                        <label for="alamat" class="form-label">Satuan</label>

                                        <select name="satuan" id="satuan" class="form-select" required>
                                            <option value="{{ $data->satuan }}">{{ $data->satuan }}</option>
                                            <option value="">--Pilih Satuan--</option>
                                            <option value="pcs">Pcs</option>
                                            <option value="kg">Kg</option>
                                            <option value="meter">Meter</option>
                                        </select>
                                    </div>
                                </div>
                            </div>



                            <div class="mb-3">
                                <label for="keterangan" class="form-label">Keterangan</label>
                                <textarea class="form-control" id="keterangan" name="keterangan" placeholder="Masukkan Keterangan" rows="3"
                                    required>{{ $data->keterangan }}</textarea>
                            </div>
                        </div>
                        </form>

                        <div class="col-md-4">

                            <div class="boxnya">
                                <div class="mb-3">
                                    <div class="box d-flex justify-content-between align-items-center">
                                        <label for="status" class="form-label">List Bahan Baku & Min. Qty</label>
                                        <button type="button" class="bg-white border-0 text-secondary" title="tambah"
                                            data-bs-toggle="modal" data-bs-target="#staticBackdrop"><i
                                                class="bx bx-plus-circle"></i></button>
                                    </div>


                                    @php
                                        $bahan_baku_paket = [];
                                        $bahan_baku_paket = App\Models\BahanBakuPaket::where(
                                            'paket_id',
                                            $data->id,
                                        )->get();

                                    @endphp
                                    @foreach ($bahan_baku_paket as $paket)
                                        @php
                                            $bahan_baku = App\Models\Barang::find($paket->barang_id);
                                        @endphp
                                        <div class="box d-flex justify-content-between align-items-center mt-2">

                                            <div class="mb-3 d-flex align-items-start flex-column">
                                                <form action="{{ route('editBahanBakuPaket', $paket->id) }}"
                                                    method="POST" style="display: inline;">
                                                    @csrf
                                                    @method('POST')


                                                    <p class="m-0" style="font-size: 12px">
                                                        {{ '*' . $paket->barang->nama_barang . '(' . $paket->barang->satuan . ')' }}
                                                    </p>
                                                    <input type="hidden" name="paket_id"
                                                        value="{{ $paket->paket_id }}">

                                                    <div class="box d-flex align-items-center justify-content-between">
                                                        <input type="text" class=" form-control form-control-sm"
                                                            value="{{ $paket->qty }}" name="qty">

                                                    </div>
                                            </div>
                                            <div class="boxnya d-flex justify-content-center align-items-center">
                                                <button type="submit" class="btn btn-link p-0 m-0 align-baseline">
                                                    <i class="bx bx-check text-warning" title="simpan"></i>
                                                </button>
                                                </form>

                                                <form action="{{ route('deleteBahanBaku', $paket->id) }}" method="POST">
                                                    @csrf
                                                    @method('POST')
                                                    <button type="submit" class="bg-white border-0 text-danger"
                                                        title="hapus"
                                                        onclick="return confirm('Apakah Anda yakin ingin menghapus item ini?')">
                                                        <i class="bx bx-trash"></i>
                                                    </button>
                                                </form>
                                            </div>
                                        </div>
                                    @endforeach
                                </div>



                            </div>
                        </div>
                    </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Modal -->
        <div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
            aria-labelledby="staticBackdropLabel" aria-hidden="true">
            <div class="modal-dialog modal-sm">
                <div class="modal-content ">
                    <div class="modal-header">
                        <h5 class="modal-title" id="staticBackdropLabel">Tambah Paket Pegawai</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>


                    <div class="modal-body">
                        <form action="{{ route('tambahBahanBaku', $data->id) }}" method="POST"
                            enctype="multipart/form-data">
                            @csrf
                            @php
                                $barang = App\Models\Barang::where('id_laundry', $data->laundry_id)->get();
                            @endphp
                            <div class="row">
                                <p>Pemakaian Minimal Bahan Baku </p>
                                <div class="col-md-12">




                                    <!-- Pilih Barang -->
                                    <div class="mb-3">
                                        <label for="barang_id" class="form-label">Pilih Barang</label>
                                        <select id="admin" name="barang_id[]" class="form-select" multiple required
                                            style="width: 100%">
                                            <option value="">Pilih Barang</option>
                                            @foreach ($barang as $item)
                                                <option value="{{ $item->id }}">
                                                    {{ $item->nama_barang }}</option>
                                            @endforeach
                                        </select>
                                    </div>



                                    <div class="row">
                                        <div class="col-md-6">
                                            <!-- QTY -->
                                            <div class="mb-3">
                                                <label for="qty" class="form-label">QTY
                                                    <span class="text-muted">*cth: 100,200</span>
                                                </label>
                                                <input type="text" class="form-control" id="qty" name="qty"
                                                    placeholder="Masukkan jumlah" required>

                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <label for="satuan" class="form-label">Satuan <span
                                                    class="text-muted fs-6">*terisi otomatis</span></label>
                                            <input type="text" id="satuan_list" name="satuan_list"
                                                class="form-control" placeholder="Satuan" readonly>
                                        </div>
                                        <span class="text-muted fs-6 mt-3 mb-3">*jika barang lebih dari
                                            satu,
                                            masing masing qty dan satuan mewakili sesuai urutan dan
                                            dipisahkan oleh koma (,)</span>
                                    </div>
                                </div>
                            </div>

                    </div>

                    <div class="modal-footer">
                        {{-- <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> --}}
                        <button type="submit" class="btn btn-primary">Simpan</button>
                    </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

@endsection

@push('js')
    <script>
        $(document).ready(function() {
            // Event listener ketika nilai barang dipilih
            $('#admin').on('change', function() {
                var selectedValues = $(this).val();
                var satuanList = []; // Array untuk menyimpan satuan

                if (selectedValues.length === 0) {
                    $('#satuan_list').val('');
                    return;
                }


                var requests = selectedValues.map(function(value) {
                    return $.get('/barangnya/' + value, function(data) {
                        // Gunakan properti 'paket' sebagai alternatif jika 'satuan' tidak tersedia
                        if (data.satuan) {
                            satuanList.push(data.satuan);
                        }
                    });
                });

                $.when.apply($, requests).done(function() {
                    $('#satuan_list').val(satuanList.join(
                        ','));

                    // Gabungkan dan tampilkan satuan/paket
                });
            });



        });
    </script>
@endpush
