<?php
$this->load->helper('az_core');
// Ambil data payment status dengan aman untuk menghindari error Undefined Index
$payment_status = isset($data['payment_status']) ? $data['payment_status'] : 'BELUM BAYAR';
$total_pay = isset($data['total_pay']) ? $data['total_pay'] : 0;
$total_panjar = isset($data['total_panjar']) ? $data['total_panjar'] : 0;
$grand_total_final = isset($data['grand_total_final']) ? $data['grand_total_final'] : 0;

// Hitung kembalian
$total_change = $total_pay - $grand_total_final;
if ($payment_status == 'PANJAR') {
    $total_change = $total_pay - $total_panjar;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Detail</title>
<style>
    .trx-item { font-weight: bold; }
    .trx-item-chs { font-size: 11px; color: #555; margin-bottom: 5px; }
    .img-item-list { max-width: 60px; max-height: 60px; margin-right: 5px; border: 1px solid #ddd; }
    .status-badge { padding: 4px 8px; border-radius: 4px; font-weight: bold; color: #fff; font-size: 12px; }
    .status-cash { background-color: #28a745; }
    .status-panjar { background-color: #ffc107; color: #333; }
    .status-belum { background-color: #dc3545; }
</style>
</head>
<body>
<input type="hidden" id="hd_idtrx" value="<?php echo az_encode_url($data['idtransaction_group']);?>">
<table width="100%">
<tr>
<td width="50%" valign="top">
<table>
<tr>
<td>Outlet</td>
<td width="10px" align="center">:</td>
<td><?php echo $outlet['outlet_name'];?></td>
</tr>
<tr>
<td>Tgl Transaksi</td>
<td align="center">:</td>
<td><?php echo Date('d-m-Y H:i', strtotime($data['date']));?></td>
</tr>
<tr>
<td>Tgl Selesai</td>
<td align="center">:</td>
<td>
<?php
$duedate = '';
if (strlen($data['duedate']) > 0) {
$duedate = Date('d-m-Y H:i', strtotime($data['duedate']));
}
echo $duedate;
?>
</td>
</tr>
<tr>
<td valign="top">Pelanggan</td>
<td valign="top" width="10px" align="center">:</td>
<td valign="top">
<?php echo $data['customer_name'].'<br>'.$data['customer_address'];?>
</td>
</tr>
<tr>
<td valign="top">Nomor WA</td>
<td valign="top" width="10px" align="center">:</td>
<td valign="top">
<?php echo $data['customer_phone'];?>
</td>
</tr>
</table>
</td>
<td valign="top">
<table>
<tr>
<td>Kode Transaksi</td>
<td width="10px" align="center">:</td>
<td><?php echo $data['code'];?></td>
</tr>
<tr>
<td>Status Transaksi</td>
<td align="center">:</td>
<td><?php echo $data['transaction_group_status'];?></td>
</tr>
<tr>
<td>Hal Pembayaran</td>
<td align="center">:</td>
<td>
    <?php 
    // Tampilkan Badge Status Pembayaran
    if ($payment_status == 'CASH') {
        echo '<span class="status-badge status-cash">CASH / LUNAS</span>';
    } elseif ($payment_status == 'PANJAR') {
        echo '<span class="status-badge status-panjar">PANJAR / DP</span>';
    } else {
        echo '<span class="status-badge status-belum">BELUM BAYAR</span>';
    }
    ?>
</td>
</tr>
<tr>
<td>Total Transaksi</td>
<td align="center">:</td>
<td><?php echo az_thousand_separator($grand_total_final);?></td>
</tr>
<tr>
<td>Posisi Stock</td>
<td align="center">:</td>
<td style="font-size: 22px; font-weight:700;padding-left: 30px;"><?php echo isset($data['stock_position']) ? $data['stock_position'] : '-'; ?></td>
</tr>
<?php
if (isset($data['transaction_group_status']) && $data['transaction_group_status'] == 'Pick Up') {
?>
<tr>
<td>No Gantungan</td>
<td align="center">:</td>
<td><?php echo isset($data['hanger_no']) ? $data['hanger_no'] : '';?></td>
</tr>
<tr>
<td>Rak</td>
<td align="center">:</td>
<td><?php echo isset($data['rack']) ? $data['rack'] : '';?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<br>

<!-- Tabel Detail Barang -->
<table class="table table-condensed" border="1" style="width:100%; border-collapse: collapse;">
<thead>
    <tr style="background-color: #f2f2f2;">
        <th style="padding: 5px; width: 30px;">#</th>
        <th style="padding: 5px;">Barang / Keterangan Pakaian</th>
        <th style="padding: 5px; width: 50px; text-align: center;">Qty</th>
        <th style="padding: 5px; width: 100px; text-align: right;">Harga</th>
        <!-- Kolom Pajak dihapus sesuai permintaan -->
        <th style="padding: 5px; width: 100px; text-align: right;">Total</th>
    </tr>
</thead>
<tbody>
<?php
$total_qty_display = 0; // Variabel penghitung Qty
foreach ($trx as $key => $value) {
    $total_qty_display++; // Setiap item dihitung 1 pcs
?>
<tr>
    <td style="padding: 5px; text-align: center;"><?php echo ($key + 1); ?></td>
    <td style="padding: 5px; vertical-align: top;">
        <div class="trx-item">
            <?php echo $value['item_code'].' - '.$value['product_name'];?>
        </div>
        <div class="trx-item-chs">
            <?php
            // Menampilkan Keterangan Pakaian (Warna, Corak, Noda)
            $keterangan = array();
            if (strlen($value['colour']) > 0) $keterangan[] = 'Warna: '.$value['colour'];
            if (strlen($value['hue']) > 0) $keterangan[] = 'Corak: '.$value['hue'];
            if (strlen($value['spot']) > 0) $keterangan[] = 'Noda: '.$value['spot'];
            
            if (!empty($keterangan)) {
                echo implode(', ', $keterangan).'. ';
            }
            
            if (strlen($value['trx_note']) > 0) {
                echo '<br><i>Ket: '.$value['trx_note'].'</i>';
            }
            ?>
        </div>
        <div>
            <?php echo $value['img'];?>
        </div>
    </td>
    <td style="padding: 5px; text-align: center; vertical-align: top;">1</td>
    <td style="padding: 5px; text-align: right; vertical-align: top;"><?php echo az_thousand_separator($value['price']);?></td>
    <td style="padding: 5px; text-align: right; vertical-align: top;"><?php echo az_thousand_separator($value['total']);?></td>
</tr>
<?php
// Tampilkan Biaya Tambahan per item jika ada
foreach ((array) $value['arr_cost'] as $cost_key => $cost_value) {
?>
<tr>
    <td></td>
    <td style="padding: 5px;">+ <?php echo $cost_value['name'];?></td>
    <td style="text-align: center;">-</td>
    <td style="padding: 5px; text-align: right;"><?php echo az_thousand_separator($cost_value['price']);?></td>
    <td style="padding: 5px; text-align: right;"><?php echo az_thousand_separator($cost_value['price']);?></td>
</tr>
<?php
}
?>
<?php
}
?>
<tr style="background-color: #f9f9f9; font-weight: bold;">
    <td colspan="2" style="padding: 5px; text-align: right;">TOTAL QTY</td>
    <td style="padding: 5px; text-align: center;"><?php echo $total_qty_display; ?></td>
    <td colspan="2"></td>
</tr>
</tbody>
</table>

<br>
<table width="100%">
<?php
if (isset($data['grand_discount']) && $data['grand_discount'] > 0 || isset($data['grand_add_cost']) && $data['grand_add_cost'] > 0) {
?>
<tr>
<td align="right" width="70%">SUBTOTAL</td>
<td align="right"><?php echo az_thousand_separator($data['grand_total']);?></td>
</tr>
<?php
if (isset($data['grand_discount']) && $data['grand_discount'] > 0) {
?>
<tr>
<td align="right">DISKON</td>
<td align="right"><?php echo az_thousand_separator($data['grand_discount']);?></td>
</tr>
<?php
}
?>
<?php
if (isset($data['grand_add_cost']) && $data['grand_add_cost'] > 0) {
?>
<tr>
<td align="right"><?php echo isset($data['add_cost_note']) ? $data['add_cost_note'] : 'Biaya Tambahan';?></td>
<td align="right"><?php echo az_thousand_separator($data['grand_add_cost']);?></td>
</tr>
<?php
}
?>
<?php
}
?>
<tr>
<td align="right" style="font-weight:bold; font-size: 16px;">GRAND TOTAL</td>
<td align="right" style="font-weight:bold; font-size: 16px;"><?php echo az_thousand_separator($grand_total_final);?></td>
</tr>
<?php
if ($payment_status == 'PANJAR') {
?>
<tr>
<td align="right">Panjar (DP)</td>
<td align="right"><?php echo az_thousand_separator($total_panjar);?></td>
</tr>
<tr>
<td align="right">Sisa Tagihan</td>
<td align="right" style="color: red;"><?php echo az_thousand_separator($grand_total_final - $total_panjar);?></td>
</tr>
<?php
}
if ($payment_status != 'BELUM BAYAR') {
?>
<tr>
<td align="right">Dibayar</td>
<td align="right"><?php echo az_thousand_separator($total_pay);?></td>
</tr>
<tr>
<td align="right">Kembalian</td>
<td align="right"><?php echo az_thousand_separator($total_change);?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>