<div class="page_title row">
    <div class="col-md-7">
        <h2>Riwayat</h2>
        <p>Riwayat semua transaksi</p>
    </div>
    <div class="col-md-5">
        <div class="input-group">
            <?php 
                if(isset($_GET['filter']) && $_GET['filter'] == 'hari_ini') {
                    $start = date('Y-m-d');
                    $end = date('Y-m-d');
                } else {
                    $start = date('Y-m-d', strtotime('-1 month'));
                    $end = date('Y-m-d');
                }
            ?>
            <input type="date" id="filter_start" 
                    class="form-control"
                    data-bs-toggle="tooltip"
                    title="Masukan filter tgl mulai"
                    value="<?= $start ?>"
            >
            <input type="date" id="filter_end" 
                    class="form-control"
                    data-bs-toggle="tooltip"
                    title="Masukan filter tgl selesai"
                    value="<?= $end ?>"
            >
        </div>
    </div>
</div>
<?php if(isset($_GET['filter']) && $_GET['filter'] == 'hari_ini') : ?>
    <div class="card mb-2">
        <div class="card-body">
            <span class="me-1">
                Menampilkan semua transaksi hari ini 
            </span>
            <a href="<?= site_url('riwayat/semua') ?>" class="badge text-bg-light border">Tampilkan semua</a>
        </div>
    </div>
<?php endif ?>

<div class="row">
    <div class="col-sm-12">
        <div class="card shadow-none" >
            <div class="card-header">
                <div class="row justify-content-between mt-3">
                    <div class="col-sm-2 mb-2">
                        <select class="form-select w-px-80 select2-nosrc" id="limiter">
                            <option>10</option>
                            <option selected>25</option>
                            <option>50</option>
                            <option>100</option>
                        </select>
                    </div>
                    <div class="col-sm-6 mb-2">
                        <div class="row">
                            <div class="col-md-4 mb-2">
                                <select class="form-select select2-nosrc" id="jenis">
                                    <option value="">Semua</option>
                                    <option value="1">Penjualan</option>
                                    <option value="2">Pembelian</option>
                                </select>

                            </div>
                            <div class="col-md-8">
                                <input type="search" id="pencarian" class="form-control" placeholder="Cari Transaksi">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card-body p-0" id="data">
                
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="modal_detail">
    <div class="modal-dialog modal-xl modal-dialog-scrollable modal-fullscreen-sm-down">
        <div class="modal-content">
            <div class="modal-header border-0">
                <div>
                    <h5 class="modal-header-title mb-0">
                        <strong>
                            Riwayat
                        </strong>
                    </h5>
                    <small>
                        Detail riwayat pembelian
                    </small>
                </div>
                <a href="javascript:void(0)" 
                    class="btn-close" 
                    data-bs-dismiss="modal"
                ></a>
            </div>
            <div class="modal-body">
                
            </div>
        </div>
    </div>
</div>

<script>
    $(function() {
        load_data(1);
    })

    function load_data(page) {
        $.post({
            url: base_url + 'proses/load_riwayat_semua',
            data: {
                start: $('#filter_start').val(),
                end: $('#filter_end').val(),
                limit: $('#limiter').val(),
                jenis: $('#jenis').val(),
                cari: $('#pencarian').val(),
                active: page,
            },
            dataType: 'html',
            success: data => {
                $('#data').html(data);
                tooltip()
            }
        })
    }

    function detail_riwayat(id, url) {
        $.post({
            url: url,
            data: {id:id},
            success: data => {
                $('#modal_detail').modal('show')
                                    .find('.modal-body')
                                    .html(data)
                ;
            }
        })
    }

    function cetak_struk(url) {
        window.open(url);
    }

    $(document).on('change', '#limiter, #filter_start, #filter_end, #jenis, #pencarian', function() {
        load_data(1);
    })

</script>