@extends('component.app')
@section('title', 'Riwayat Pembelian')
@section('active15', 'active')
@section('menu2', '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">
                    <h5 class="card-header">Riwayat Pembelian Bahan Baku</h5>
                    <button type="button" class="btn btn-primary me-2" data-bs-toggle="modal"
                        data-bs-target="#staticBackdrop"><i class="bx bx-plus bx-sm"></i> Add</button>
                </div>


                <div class="table-responsive">
                    <table class="table table-hover" id="Mytable">
                        <thead class="table-primary text-center">
                            <tr>
                                <th scope="col">Laundry</th>
                                <th scope="col">Tgl. Beli</th>
                                <th scope="col">SKU</th>
                                <th scope="col">Nama Barang</th>
                                <th scope="col">Volume/Qty</th>
                                <th scope="col">Total Harga</th>
                                <th scope="col">Harga Susut</th>
                                <th scope="col">Volume Awal</th>
                                <th scope="col">Volume Akhir</th>
                                <th scope="col">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($data as $data)
                                <tr>
                                    <td>{{ $data->laundry->nama_laundry }}</td>
                                    <td>{{ Carbon\Carbon::parse($data->tgl_beli)->format('d M Y') }}</td>
                                    <td>{{ $data->barang->sku }}</td>
                                    <td>{{ $data->barang->nama_barang }}</td>
                                    <td>{{ $data->qty . ' ' . $data->satuan }}</td>
                                    <td>{{ 'Rp. ' . number_format($data->total_harga, 0, ',', '.') }}</td>
                                    <td>{{ 'Rp. ' . number_format($data->harga_susut, 0, ',', '.') . ' /' . $data->satuan_susut }}
                                    </td>
                                    <td>
                                        <span class="badge bg-secondary">
                                            {{ $data->volume_awal }} {{ $data->barang->satuan }}
                                        </span>

                                    </td>
                                    <td>
                                        <span class="badge bg-warning">
                                            {{ $data->volume_akhir }} {{ $data->barang->satuan }}
                                        </span>

                                    </td>


                                    <td><a href="{{ route('paket.edit', $data->id) }}"
                                            class="btn btn-sm btn-label-secondary"> <i class="bx bx-edit-alt"></i></a>
                                        <form action="{{ route('paket.aktifasi', $data->id) }}" method="POST">
                                            @csrf
                                            @if ($data->status == 'tidak aktif')
                                                <button type="submit" class="btn btn-sm btn-label-success"
                                                    onclick="return confirm('Apakah Anda yakin ingin mengaktifkan paket ini?')">
                                                    <i class="bx bx-check"></i></button>
                                            @elseif ($data->status == 'aktif')
                                                <button type="submit" class="btn btn-sm btn-label-danger"
                                                    onclick="return confirm('Apakah Anda yakin ingin menonaktifkan paket ini?')">
                                                    <i class="bx bx-x"></i></button>
                                            @endif

                                        </form>

                                    </td>

                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </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-lg">
                    <div class="modal-content ">
                        <div class="modal-header">
                            <h5 class="modal-title" id="staticBackdropLabel">Tambah Data Pembelian Bahan Baku</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <form action="{{ route('pembelian-bahan-baku.store') }}" method="POST"
                                enctype="multipart/form-data">
                                @csrf
                                <div class="row">
                                    <!-- Kolom Kiri -->
                                    <div class="col-md-12">
                                        <!-- Pilih Laundry -->
                                        <div class="mb-3">
                                            <label for="users_id" class="form-label">Pilih Laundry</label>
                                            <select id="pelanggan" name="id_laundry" class="form-select form-control-sm"
                                                required>
                                                <option value="">Pilih Laundry</option>
                                                @foreach ($laundry as $laundry)
                                                    <option value="{{ $laundry->id }}">{{ $laundry->nama_laundry }} -
                                                        {{ $laundry->email }}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                        <!-- 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 form-control-sm"
                                                required>
                                                <option value="">Pilih Barang</option>


                                                @foreach ($barang as $barangnya)
                                                    <option value="{{ $barangnya->id }}">{{ $barangnya->id }} -
                                                        {{ $barangnya->sku }} -
                                                        {{ $barangnya->nama_barang }}</option>
                                                @endforeach
                                            </select>
                                        </div>



                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="card shadow-lg border-0 rounded-lg card-custom d-none"
                                                    id="card-custom">
                                                    <div class="card-header bg-primary text-white">
                                                        <h5 class="card-title mb-0">Detail Barang</h5>
                                                    </div>
                                                    <div class="card-body p-4">
                                                        <div class="table-responsive">
                                                            <table class="table table-borderless">
                                                                <tbody>
                                                                    <!-- SKU -->
                                                                    <tr>
                                                                        <td><strong>
                                                                                SKU
                                                                            </strong></td>
                                                                        <td>:</td>
                                                                        <td>
                                                                            <span id="sku" name="sku"
                                                                                required></s>
                                                                        </td>
                                                                    </tr>

                                                                    <!-- Nama Barang -->
                                                                    <tr>
                                                                        <td><strong>Nama Barang</strong></td>
                                                                        <td>:</td>
                                                                        <td>
                                                                            <span id="nama_barang" name="nama_barang"
                                                                                required></span>
                                                                        </td>
                                                                    </tr>

                                                                    <!-- Merek -->
                                                                    <tr>
                                                                        <td><strong>Merek</strong></td>
                                                                        <td>:</td>
                                                                        <td>
                                                                            <span id="merek" name="merek"
                                                                                required></span>
                                                                        </td>
                                                                    </tr>

                                                                    <!-- Jumlah (Qty) -->
                                                                    <tr>
                                                                        <td><strong>Persediaan (Qty/Volume)</strong></td>
                                                                        <td>:</td>
                                                                        <td
                                                                            class="d-flex align-items-center justify-content-start">
                                                                            <span id="volume_total" name="volume_total"
                                                                                required></span>
                                                                            &nbsp;
                                                                            <span name="satuan" id="satuan"
                                                                                required></span>
                                                                        </td>
                                                                    </tr>

                                                                </tbody>
                                                            </table>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="row">
                                                    <div class="col-md-8">
                                                        <!-- Jumlah (Qty) -->
                                                        <div class="mb-3">
                                                            <label for="qty" class="form-label">Jumlah (Qty)</label>
                                                            <input type="number" class="form-control" id="qty"
                                                                name="qty" placeholder="Masukkan Jumlah Barang"
                                                                required>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-4">
                                                        <!-- Satuan -->
                                                        <div class="mb-3">
                                                            <label for="satuan" class="form-label">Satuan</label>
                                                            <select name="satuan" id="satuan" class="form-select">
                                                                <option value="">Pilih Satuan</option>
                                                                <option value="liter">Liter</option>
                                                                <option value="kg">Kg</option>
                                                            </select>
                                                        </div>
                                                    </div>
                                                </div>





                                                <!-- Total Harga -->
                                                <div class="mb-3">
                                                    <label for="total_harga" class="form-label">Total Harga</label>
                                                    <input type="number" class="form-control" id="total_harga"
                                                        name="total_harga" placeholder="Masukkan Total Harga" required>
                                                </div>
                                                <div class="mb-4" id="metode-pembayaran">
                                                    <label class="form-label mb-2">Metode Pembayaran</label>
                                                    <br>
                                                    <div class="form-check-inline">
                                                        <input class="form-check-input" type="radio" name="is_cash"
                                                            id="cash" value="1">
                                                        <label class="form-check-label" for="flexRadioCash">
                                                            Cash
                                                        </label>
                                                    </div>
                                                    <div class="form-check-inline">
                                                        <input class="form-check-input" type="radio" name="is_cash"
                                                            value="0" id="transfer">
                                                        <label class="form-check-label" for="flexRadioTransfer">
                                                            Transfer
                                                        </label>
                                                    </div>
                                                </div>

                                                <div class="mb-4 d-none" id="pilih-pembayaran-transfer">
                                                    <label for="smallSelect" class="form-label">Jenis
                                                        Pembayaran</label>
                                                    <select id="pelanggan" class="form-select form-select-sm"
                                                        name="metode_bayar_id">
                                                        <option value="">--Pilih Metode Pembayaran--
                                                        </option>
                                                        @if ($metode != null)
                                                            @foreach ($metode as $metode)
                                                                <option value="{{ $metode->id }}">
                                                                    {{ $metode->jenis_pembayaran }} -
                                                                    {{ $metode->no_rek }}</option>
                                                            @endforeach
                                                        @endif
                                                    </select>
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label mb-2">Catatan</label>
                                                    <textarea class="form-control" name="catatan" id="catatan" rows="5"></textarea>
                                                </div>

                                            </div>

                                        </div>







                                    </div>

                                </div>


                        </div>

                        <div class="modal-footer">
                            <button type="submit" class="btn btn-primary">Simpan</button>
                        </div>
                        </form>
                    </div>
                </div>
            </div>
        @endsection

        @push('js')
            <script>
                $(document).ready(function() {
                    // ambil data dari barang yang dipilih
                    $('#admin').on('change', function() {
                        var id = $(this).val();
                        $('#card-custom').removeClass('d-none');
                        $.get('/barangnya/' + id, function(data) {
                            console.log(data);
                            $('#sku').text(data.sku);
                            $('#nama_barang').text(data.nama_barang);
                            $('#merek').text(data.merek);
                            $('#qty').text(data.qty);
                            $('#satuan').text(data.satuan);
                            // harga format rupiah
                            // ubah value data.harga jadi rupiah
                            // ubah value data.harga jadi rupiah
                            // ubah value data.harga jadi rupiah

                            $('#harga').text('Rp. ' + data.harga);
                            $('#volume_total').text(data.volume_total);
                        });
                    });

                });

                // jika yang dipilih sudahbayar tarnsfer
                $('#transfer').on('change', function() {
                    if ($(this).is(':checked')) {
                        $('#pilih-pembayaran-transfer').removeClass('d-none');
                    } else {
                        $('#pilih-pembayaran-transfer').addClass('d-none');
                    }
                })
                // jika yang dipilih sudahbayar tunai
                $('#cash').on('change', function() {
                    if ($(this).is(':checked')) {
                        $('#pilih-pembayaran-transfer').addClass('d-none');
                    } else {

                        $('#pilih-pembayaran-transfer').removeClass('d-none');
                    }
                })
            </script>
        @endpush
