<div class="card mb-2">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered mb-0">
                <thead>
                    <tr class="table-light">
                        <th class="w-px-80"></th>
                        <th>Item</th>
                        <th class="text-center  w-px-80">
                            Qty
                        </th>
                        <th class="text-center w-px-120">
                            Satuan
                        </th>
                        <th class="text-start w-px-120">
                            Harga Beli
                        </th>
                        <th class="text-start w-px-200">
                            Supplier
                        </th>
                        <th class="text-end w-px-150">
                            Total Harga
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                        $toko  = $this -> session -> mart_toko;
                        $this -> db -> where('id_toko', $toko);
                        $this -> db -> where('id_beli', 0);
                        $get   = $this -> db -> get('pembelian_detail') -> result();
                        $total = 0;

                        if($get) :
                            foreach($get as $item) :
                                $brg       = $this -> app -> get_barang_detail($item -> id_brg);
                                $sub_harga = $item -> qty * $item -> harga_beli;
                                $total += $sub_harga;
                    ?>
                    
                        <tr>
                            <td class="text-center">
                                <button class="btn btn-outline-danger border border-danger hps" 
                                        data-id="<?= $item -> id ?>"
                                >
                                    <i class="fa fa-trash"></i>
                                </button>
                            </td>
                            <td>
                                <?= $brg -> nama ?>
                            </td>
                            <td>
                                <input type="number" value="<?= $item -> qty ?>" data-id="<?= $item -> id ?>"
                                        class="form-control nochange w-px-80 text-center qty" 
                                >
                            </td>
                            <td>
                                <select class="form-select select2-nosrc w-px-120 id_satuan" 
                                        data-id="<?= $item -> id ?>"
                                >
                                    <option value="">Pilih</option>
                                    <?php 
                                        $get = $this -> app -> get_barang_satuan($item -> id_brg);
                                        foreach($get as $satuan) {
                                            $selected = $satuan -> id_satuan == $item -> id_satuan ? 'selected' : null;
                                            echo '
                                                <option value="'.$satuan -> id_satuan.'" '.$selected.'>
                                                    '.$satuan -> satuan.'
                                                </option>
                                            ';
                                        }
                                    ?>
                                </select>
                            </td>
                            <td class="w-px-120">
                                <input type="text" data-id="<?= $item -> id ?>"
                                        value="<?= form_uang($item -> harga_beli) ?>" 
                                        class="form-control form_uang w-px-120 harga_beli" 
                                        data-id="<?= $item -> id ?>" 
                                >
                            </td>
                            <td class="w-px-200">
                                <select class="form-select select2-nosrc w-px-200 id_supplier" 
                                        data-id="<?= $item -> id ?>"
                                >
                                    <option value="">Pilih</option>
                                    <?php
                                        $getsup = $this -> db -> get('supplier') -> result();
                                        foreach($getsup as $sup) {
                                            $selected = $sup -> id == $item -> id_supplier ? 'selected' : null;
                                            echo '
                                                <option value="'.$sup -> id.'" '.$selected.'>
                                                    '.$sup -> nama.'
                                                </option>
                                            ';
                                        } 
                                    ?>
                                </select>
                            </td>
                            <td class="w-px-150 text-end">
                                <strong>
                                    Rp<?= form_uang($sub_harga) ?>
                                </strong>
                            </td>
                        </tr>
                        
                    <?php endforeach ?>

                        <tr class="table-light">
                            <th colspan="6" class="text-end text-danger">
                                Total
                            </th>
                            <th class="text-end text-danger">
                                Rp<?= form_uang($total) ?>
                            </th>
                        </tr>

                    <?php else: ?>
                        <tr>
                            <td colspan="7" class="text-center">
                                Belum ada transaksi
                            </td>
                        </tr>
                    <?php endif ?>
                </tbody>
            </table>
        </div>
    </div>
</d>

<?php if($get) : ?>
    <div class="card">
        <div class="card-body text-end">
            <button class="btn btn-primary px-4 submit_beli">
                Simpan
            </button>
            <a href="javascript:reset_beli()" class="btn btn-danger px-4">
                Bersihkan
            </a>
        </div>
    </div>
<?php endif ?>