<?php
// Siapkan data item untuk JavaScript
$arr_items = array();
foreach ($cash_items->result() as $it) {
    $arr_items[] = array(
        'id'   => $it->idproduction_cash_item,
        'name' => $it->item_name,
        'type' => ($it->item_type == 'income') ? 'debit' : 'credit'
    );
}
?>
<script type="text/javascript">
var CASH_ITEMS    = <?php echo json_encode($arr_items); ?>;
var PCB_IS_ADMIN  = <?php echo ($is_admin) ? 'true' : 'false'; ?>;
var PCB_MY_ID     = '<?php echo $this->session->userdata("iduser"); ?>';
var PCB_TODAY     = '<?php echo date("Y-m-d"); ?>';
var PCB_ASSET_URL = '<?php echo base_url().AZAPP."assets/production_cash/"; ?>';
</script>

<style type="text/css">
.pcb-summary-box { padding: 12px; border-radius: 5px; text-align: center; margin-bottom: 10px; }
.pcb-summary-box h5 { margin: 0 0 5px 0; font-weight: bold; }
.pcb-summary-box h3 { margin: 0; font-size: 20px; font-weight: bold; }
.pcb-bg-opening { background: #d9edf7; }
.pcb-bg-debit   { background: #dff0d8; }
.pcb-bg-credit  { background: #f2dede; }
.pcb-bg-closing { background: #fcf8e3; }
.pcb-thumb { width: 38px; height: 38px; object-fit: cover; margin-right: 3px; cursor: pointer; border: 1px solid #ddd; }
</style>

<div class="container-cash-book">
    <form class="form-horizontal" id="form_filter">
        <div class="form-group">
            <label class="control-label col-sm-1">Tanggal</label>
            <div class="col-sm-5">
                <table>
                    <tr>
                        <td>
                            <!-- KALENDER NATIVE: dari Senin minggu ini -->
                            <input type="date" class="form-control" id="date_1" name="date_1"
                                value="<?php echo date('Y-m-d', strtotime('monday this week')); ?>"
                                <?php echo (!$is_admin) ? 'min="'.date('Y-m-d', strtotime('-6 days')).'"' : ''; ?>>
                        </td>
                        <td>&nbsp;s/d&nbsp;</td>
                        <td>
                            <!-- KALENDER NATIVE: sampai hari ini -->
                            <input type="date" class="form-control" id="date_2" name="date_2"
                                value="<?php echo date('Y-m-d'); ?>"
                                <?php echo (!$is_admin) ? 'max="'.date('Y-m-d').'"' : ''; ?>>
                        </td>
                    </tr>
                </table>
            </div>
        </div>

        <?php if ($is_admin) { ?>
        <div class="form-group">
            <label class="control-label col-sm-1">User Produksi</label>
            <div class="col-sm-4">
                <select class="form-control" name="iduser_filter" id="iduser_filter">
                    <option value="">-- Pilih User Produksi --</option>
                    <?php foreach ($production_users->result() as $u) { ?>
                    <option value="<?php echo $u->iduser; ?>"><?php echo $u->name; ?></option>
                    <?php } ?>
                </select>
            </div>
        </div>
        <?php } ?>

        <div class="form-group">
            <label class="control-label col-sm-1">Tipe</label>
            <div class="col-sm-4">
                <select class="form-control" name="type_filter" id="type_filter">
                    <option value="">Semua</option>
                    <option value="debit">Pemasukan (Debit)</option>
                    <option value="credit">Pengeluaran (Kredit)</option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-1">Item Kas</label>
            <div class="col-sm-4">
                <select class="form-control" name="iditem_filter" id="iditem_filter">
                    <option value="">Semua Item</option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-1"></div>
            <div class="col-sm-10">
                <button class="btn btn-info" id="btn_filter" type="button"><i class="fa fa-filter"></i> Filter</button>
                <button class="btn btn-primary" id="btn_add_transaction" type="button"><i class="fa fa-plus"></i> Tambah Transaksi</button>
                <button class="btn btn-default" id="btn_print" type="button"><i class="fa fa-print"></i> Print</button>
                <button class="btn btn-success" id="btn_pdf" type="button"><i class="fa fa-file-pdf-o"></i> Download PDF</button>
            </div>
        </div>
    </form>
    <hr>

    <!-- RINGKASAN SALDO -->
    <div class="row">
        <div class="col-md-3 col-xs-6">
            <div class="pcb-summary-box pcb-bg-opening">
                <h5>SALDO AWAL</h5>
                <h3 id="txt_opening">Rp 0</h3>
            </div>
        </div>
        <div class="col-md-3 col-xs-6">
            <div class="pcb-summary-box pcb-bg-debit">
                <h5>TOTAL PEMASUKAN</h5>
                <h3 id="txt_total_debit">Rp 0</h3>
            </div>
        </div>
        <div class="col-md-3 col-xs-6">
            <div class="pcb-summary-box pcb-bg-credit">
                <h5>TOTAL PENGELUARAN</h5>
                <h3 id="txt_total_credit">Rp 0</h3>
            </div>
        </div>
        <div class="col-md-3 col-xs-6">
            <div class="pcb-summary-box pcb-bg-closing">
                <h5>SALDO AKHIR</h5>
                <h3 id="txt_closing">Rp 0</h3>
            </div>
        </div>
    </div>

    <!-- TABEL BUKU KAS -->
    <div class="table-responsive">
        <table class="table table-condensed table-bordered" id="table_cash_book">
            <thead>
                <tr>
                    <th style="text-align:center;width:30px;">No</th>
                    <th style="text-align:center;width:90px;">Tanggal</th>
                    <th style="text-align:center;width:50px;">Jam</th>
                    <th style="text-align:center;">Item</th>
                    <th style="text-align:center;">Keterangan</th>
                    <th style="text-align:center;width:110px;">Debit (+)</th>
                    <th style="text-align:center;width:110px;">Kredit (-)</th>
                    <th style="text-align:center;width:110px;">Saldo</th>
                    <th style="text-align:center;width:130px;">Bukti</th>
                    <th style="text-align:center;width:90px;">Aksi</th>
                </tr>
            </thead>
            <tbody>
                <tr><td colspan="10" align="center">Memuat data...</td></tr>
            </tbody>
        </table>
    </div>
</div>