<main id="main" class="main">

    <div class="pagetitle">
        <h1>Stok Gudang Harian</h1>
        <nav>
            <ol class="breadcrumb">

            </ol>
        </nav>
    </div><!-- End Page Title -->

    <section class="section">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        <br>
                        <!-- Table with stripped rows -->
                        <div class="datatable-wrapper datatable-loading no-footer sortable searchable fixed-columns">
                            <div class="row g-3">
                                <div class="col-md-3">
                                    <input type="date" onchange="tampildatatgl(this.value)" class="form-control" id="tgl_stok" value="<?= date('Y-m-d'); ?>">
                                </div>
                                <div class="col-md-4">
                                    <div class="btn-group" role="group" aria-label="Basic outlined example">
                                        <button type="button" onclick="tglsebelum()" class="btn btn-outline-primary" data-toggle='tooltip' title='Tgl Sebelum'><i class="bi bi-arrow-left"></i></button>
                                        <button type="button" onclick="tambahtgl()" class="btn btn-outline-primary" data-toggle='tooltip' title='Tgl Selanjutnya'><i class='bi bi-arrow-right'></i></button>
                                        <button type="button" onclick="tampildata()" id="btnview" class="btn btn-outline-primary" data-toggle='tooltip' title='View'><i class='bi bi-search'></i>&nbsp;View</button>
                                        <button type="button" style="display:none;" disabled="" id="btnloading" class="btn btn-outline-primary" data-toggle='tooltip' title='Loading'><span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>&nbsp;View</button>
                                        <?php if ($akses['unduh'] == 1) { ?>
                                            <button type="button" onclick="unduh()" class="btn btn-outline-primary" data-toggle='tooltip' title='Unduh'><i class='bi bi-download'></i>&nbsp;xls</button>
                                        <?php
                                        }
                                        ?>
                                    </div>
                                    <button type="button" style="display:none;float:left;margin: 2px;" class="btn btn-primary" type="button" disabled="">
                                        <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
                                        Loading...
                                    </button>
                                </div>
                            </div>
                            <br>
                            <div class="datatable-container">
                                <div id="tbfglist"></div>
                            </div>
                        </div>
                        <!-- End Table with stripped rows -->
                    </div>
                </div>
            </div>
        </div>
    </section>
</main>


<script>
    function tampildata() {
        var tgl = $("#tgl_stok").val();
        document.getElementById("btnloading").style.display = "block";
        document.getElementById("btnview").style.display = "none";

        $.get("<?= base_url(); ?>Gudang/tb_saldo_stok_harian?tgl=" + tgl, function(data, status) {
            $("#tbfglist").html(data);
            document.getElementById("btnloading").style.display = "none";
            document.getElementById("btnview").style.display = "block";
        });
    }

    function tambahtgl() {
        var dateInput = document.getElementById('tgl_stok');
        var currentDate = new Date(dateInput.value || new Date());
        currentDate.setDate(currentDate.getDate() + 1);
        var nextDay = currentDate.toISOString().split('T')[0];
        dateInput.value = nextDay;
        tampildata();
    }

    function tglsebelum() {
        var dateInput = document.getElementById('tgl_stok');
        var currentDate = new Date(dateInput.value || new Date());
        currentDate.setDate(currentDate.getDate() - 1);
        var nextDay = currentDate.toISOString().split('T')[0];
        dateInput.value = nextDay;
        tampildata();
    }

    function tampildatatgl(tgl) {
        document.getElementById("btnloading").style.display = "block";
        document.getElementById("btnview").style.display = "none";

        $.get("<?= base_url(); ?>Gudang/tb_saldo_stok_harian?tgl=" + tgl, function(data, status) {
            $("#tbfglist").html(data);
            document.getElementById("btnloading").style.display = "none";
            document.getElementById("btnview").style.display = "block";
        });
    }

    function unduh() {
        var tgl = $("#tgl_stok").val();
        window.open("<?= base_url(); ?>Gudang/tb_saldo_stok_harian_xls?tgl=" + tgl, "_self");
    }
</script>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        var searchInput = document.getElementById('tahun');

        searchInput.addEventListener('keyup', function(event) {
            if (event.keyCode === 13) { // 13 adalah kode key untuk Enter
                tampildata();
            }
        });
    });

    const inputTahun = document.getElementById('tahun');
    const tahunSekarang = new Date().getFullYear();
    inputTahun.value = tahunSekarang;
</script>

<script>
    $(function() {
        $('[data-toggle="tooltip"]').tooltip();
    });
</script>