<?php
$rows = isset($rows) ? $rows : [];
$button_class = isset($button_class) ? $button_class : 'cek_transaksi';

$get_row_value = function($row, $key, $default = '') {
    if (is_array($row) && array_key_exists($key, $row)) {
        return $row[$key];
    }

    if (is_object($row) && isset($row->{$key})) {
        return $row->{$key};
    }

    return $default;
};

$text = function($value) {
    return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
};

$total_order = 0;
$total_pajak = 0;
$total_bayar = 0;
$total_diskon = 0;
$total_piutang = 0;
?>

<div class="card-body">
    <div class="table-responsive-sm table-shell">
        <table class="table table-modern">
            <thead class="thead-section thead-dark">
                <tr>
                    <th class="text-center col-no">No</th>
                    <th class="text-center col-trx">No Trx</th>
                    <th class="text-center col-tanggal">Tanggal Order</th>
                    <th class="text-center col-kasir">Kasir</th>
                    <th class="text-center col-pelanggan">Pelanggan</th>
                    <th class="text-center col-rincian">Rincian Penjualan Barang</th>
                    <th class="text-center col-money">Pajak</th>
                    <th class="text-center col-money">Total Transaksi</th>
                    <th class="text-center col-money">Total Bayar</th>
                    <th class="text-center col-money">Diskon</th>
                    <th class="text-center col-money">Piutang</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($rows)) {
                    $no = 1;
                    foreach ($rows as $row) {
                        $id_invoice = $get_row_value($row, 'id_invoice', 0);
                        $rows_bayar = bayar($id_invoice);
                        $total_bayar_invoice = isset($rows_bayar['Totalbayar']) ? (float) $rows_bayar['Totalbayar'] : 0;

                        $t_omset = (float) sumOrder($id_invoice);
                        $diskon_detail = (float) totalDiskon($id_invoice);
                        $diskon_invoice = (float) $get_row_value($row, 'potongan_harga', 0);
                        $t_omset = $t_omset - $diskon_detail;

                        $pajak_persen = (float) $get_row_value($row, 'pajak', 0);
                        $piutang_raw = sumPiutang($id_invoice);
                        $bayar_piutang = (is_array($piutang_raw) && isset($piutang_raw[0]['piutang'])) ? (float) $piutang_raw[0]['piutang'] : (float) $piutang_raw;

                        if ($pajak_persen > 0) {
                            $sub_pajak = ($t_omset * $pajak_persen) / 100;
                            $piutang = $t_omset + $sub_pajak - $total_bayar_invoice - $diskon_invoice;
                        } else {
                            $sub_pajak = 0;
                            $piutang = $t_omset - $bayar_piutang - $diskon_invoice;
                        }

                        $oto = (int) $get_row_value($row, 'oto', 0);
                        $status_order = $get_row_value($row, 'status', '');
                        if ($status_order === 'batal' || $oto === 5) {
                            $total_bayar_invoice = 0;
                            $t_omset = 0;
                            $sub_pajak = 0;
                            $diskon_invoice = 0;
                            $piutang = 0;
                        }

                        if ($oto === 0) {
                            $view = 'edit';
                        } elseif ($oto === 1) {
                            $view = 'edit';
                        } elseif ($oto === 2) {
                            $view = 'view';
                        } elseif ($oto === 3) {
                            $view = 'edit';
                        } elseif ($oto === 4) {
                            $view = 'view';
                        } elseif ($oto === 5) {
                            $view = 'view';
                        } else {
                            $view = 'view';
                        }

                        $nama_pribadi = $get_row_value($row, 'nama', $get_row_value($row, 'nama_konsumen', ''));
                        $nama_perusahaan = $get_row_value($row, 'perusahaan', '');
                        $tampil = (int) $get_row_value($row, 'tampil', 0);
                        $nama_full = ($tampil !== 0 && $nama_perusahaan !== '') ? $nama_perusahaan : $nama_pribadi;
                        if ($nama_full === '') {
                            $nama_full = $get_row_value($row, 'pelanggan', $get_row_value($row, 'nama_lengkap', '-'));
                        }

                        $kasir = $get_row_value($row, 'kasir', $get_row_value($row, 'nama_lengkap', '-'));
                        $id_transaksi = $get_row_value($row, 'id_transaksi', '-');
                        $tgl_trx = $get_row_value($row, 'tgl_trx', '');
                        $no_hp = $get_row_value($row, 'no_hp', '');
                        $row_state = ($status_order === 'batal' || $oto === 5) ? 'row-cancelled' : '';

                        $total_order += $t_omset;
                        $total_pajak += $sub_pajak;
                        $total_bayar += $total_bayar_invoice;
                        $total_diskon += $diskon_invoice;
                        $total_piutang += $piutang;
                ?>
                <tr class="row-master <?=$row_state;?>">
                    <td class="text-center"><?=$no++;?></td>
                    <td class="text-center nowrap col-trx-cell">
                        <?=$text($id_transaksi);?>
                    </td>
                    <td class="text-center nowrap col-tanggal-cell"><?=dtimes($tgl_trx, false, false);?></td>
                    <td class="text-center col-kasir-cell"><?=$text($kasir);?></td>
                    <td class="text-left col-pelanggan-cell">
                        <?php if ($no_hp !== '') { ?>
                            <a data-toggle="tooltip"
                               data-original-title="Kirim Ke <?=$text($nama_full . ' : ' . phone_number($no_hp));?>"
                               data-placement="left"
                               class="text-success kirim_wa"
                               data-id="<?=encrypt_url($id_invoice);?>"
                               data-nomor="<?=$text($no_hp);?>"
                               data-trx="<?=$text($id_transaksi);?>"
                               data-tgl="<?=$text($tgl_trx);?>"
                               href="javascript:void(0)">
                                <i class="fa fa-whatsapp"></i> &nbsp;<?=$text($nama_full);?>
                            </a>
                        <?php } else { ?>
                            <?=$text($nama_full);?>
                        <?php } ?>
                    </td>
                    <td class="p-0 cell-rincian">
                        <table class="table nested-table w-100 mb-0">
                            <colgroup>
                                <col style="width:50%">
                                <col style="width:18%">
                                <col style="width:10%">
                                <col style="width:22%">
                            </colgroup>
                            <thead>
                                <tr>
                                    <th>Nama Barang</th>
                                    <th>Harga</th>
                                    <th>Qty</th>
                                    <th>Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $detail = detail_order($id_invoice, 'semua');
                                if (!empty($detail)) {
                                    foreach ($detail as $val) {
                                        $subtotal = (float) $val->jumlah * (float) $val->harga;
                                        if ((float) $val->diskon > 0) {
                                            $diskon_item = ($subtotal * (float) $val->diskon) / 100;
                                            $subtotal = $subtotal - $diskon_item;
                                        }

                                        $bahan_obj = getDetailBahan($val->id_bahan);
                                        $nama_barang = isset($bahan_obj->title) ? $bahan_obj->title : '';
                                        if ($nama_barang === '' || $nama_barang === '-') {
                                            $nama_barang = getBahan($val->id_bahan);
                                        }
                                        if ($nama_barang === '' || $nama_barang === '-') {
                                            $nama_barang = nama_produk($val->id_produk);
                                        }
                                ?>
                                <tr class="row-detail">
                                    <td class="text-left col-nama" title="<?=$text($nama_barang);?>"><?=$text($nama_barang);?></td>
                                    <td class="text-center nowrap"><?=rp($val->harga);?></td>
                                    <td class="text-center nowrap"><?=$text($val->jumlah);?></td>
                                    <td class="text-center nowrap"><?=rp($subtotal);?></td>
                                </tr>
                                <?php } } else { ?>
                                <tr><td colspan="4" class="text-center text-muted">Tidak ada item</td></tr>
                                <?php } ?>
                            </tbody>
                        </table>
                    </td>
                    <td class="text-right nowrap cell-money"><?=rp($sub_pajak);?></td>
                    <td class="text-right nowrap cell-money"><?=rp($t_omset);?></td>
                    <td class="text-right nowrap cell-money"><?=rp($total_bayar_invoice);?></td>
                    <td class="text-right nowrap cell-money"><?=rp($diskon_invoice);?></td>
                    <td class="text-right nowrap cell-money"><?=rp($piutang);?></td>
                </tr>
                <?php } } else { ?>
                <tr>
                    <td colspan="11" class="text-center py-4">Data belum ada</td>
                </tr>
                <?php } ?>
            </tbody>
            <tfoot>
                <tr class="row-total">
                    <th class="text-left" colspan="6">
                        <span class="text-muted small d-none d-md-inline-block">Total keseluruhan</span>
                    </th>
                    <th class="text-right"><?=rp($total_pajak);?></th>
                    <th class="text-right"><?=rp($total_order);?></th>
                    <th class="text-right"><?=rp($total_bayar);?></th>
                    <th class="text-right"><?=rp($total_diskon);?></th>
                    <th class="text-right"><?=rp($total_piutang);?></th>
                </tr>
            </tfoot>
        </table>

        <nav aria-label="Page navigation" class="p-2">
            <?php echo $this->ajax_pagination->create_links(); ?>
        </nav>
    </div>

    <div class="konsumen-result-count">Menampilkan: <?= !empty($rows) ? count($rows) : 0; ?></div>
</div>
