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

    <div class="pagetitle">
        <h1>Stok Gudang</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-1">
                                    <label class="form-label">Bulan</label>
                                </div>
                                <div class="col-md-2">
                                    <select class="form-control" id="bln">
                                        <option value="1">Jan</option>
                                        <option value="2">Feb</option>
                                        <option value="3">Mar</option>
                                        <option value="4">Apr</option>
                                        <option value="5">May</option>
                                        <option value="6">Jun</option>
                                        <option value="7">Jul</option>
                                        <option value="8">Agu</option>
                                        <option value="9">Sep</option>
                                        <option value="10">Okt</option>
                                        <option value="11">Nov</option>
                                        <option value="12">Des</option>
                                    </select>
                                </div>
                                <div class="col-md-1">
                                    <label class="form-label">Tahun</label>
                                </div>
                                <div class="col-md-2">
                                    <input type="number" class="form-control" id="tahun">
                                </div>
                                <div class="col-md-4">
                                    <button type="button" style="float:left;margin: 2px;" class="btn btn-primary" id="btnview" onclick="tampildata()"><i class='bi bi-search'></i>&nbsp;Cek Stok</button>
                                    <button type="button" style="display:none;float:left;margin: 2px;" id="btnloading" class="btn btn-primary" type="button" disabled="">
                                        <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
                                        Loading...
                                    </button>
                                    <?php if ($akses['unduh'] == 1) { ?>
                                        <button type="button" style="float:left;margin: 2px;" class="btn btn-success" onclick="unduh()"><i class='bi bi-download'></i>&nbsp;Unduh</button>
                                    <?php
                                    }
                                    ?>
                                </div>
                            </div>
                            <br>
                            <div class="datatable-container">
                                <div id="tbfglist"></div>
                            </div>
                        </div>
                        <!-- End Table with stripped rows -->
                    </div>
                </div>
            </div>
        </div>
    </section>
</main>

<div class="modal fade" id="f_trace" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Trace Stok :&nbsp;</h5>
                <div id="nm_brg"></div>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <style>
                    .loader {
                        width: 200px;
                        height: 22px;
                        border-radius: 40px;
                        color: #514b82;
                        border: 2px solid;
                        position: relative;
                    }

                    .loader::before {
                        content: "";
                        position: absolute;
                        margin: 2px;
                        width: 25%;
                        top: 0;
                        bottom: 0;
                        left: 0;
                        border-radius: inherit;
                        background: currentColor;
                        animation: l3 3s infinite linear;
                    }

                    @keyframes l3 {
                        50% {
                            left: 100%;
                            transform: translateX(calc(-100% - 4px))
                        }
                    }
                </style>
                <center>
                    <div class="loader" id="loader_status"></div>
                </center>
                <div id="tb_trace"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<script>
    function tampildata() {
        var tahun = $("#tahun").val();
        var bln = $("#bln").val();
        document.getElementById("btnloading").style.display = "block";
        document.getElementById("btnview").style.display = "none";
        /*
        $.get("<?= base_url(); ?>Gudang/tb_stok_gudang?s=" + mulai + "&e=" + hingga, function(data, status) {
            $("#tbfglist").html(data);
            document.getElementById("btnloading").style.display = "none";
            document.getElementById("btnview").style.display = "block";
        });
        */
        $.get("<?= base_url(); ?>Gudang/tb_saldo_stok?thn=" + tahun + "&bln=" + bln, function(data, status) {
            $("#tbfglist").html(data);
            document.getElementById("btnloading").style.display = "none";
            document.getElementById("btnview").style.display = "block";
        });
    }

    function tracestok(kd, nm) {
        document.getElementById("loader_status").style.display = "block";
        $("#tb_trace").html('');
        $("#nm_brg").html(nm);
        $.get("<?= base_url(); ?>Gudang/tb_trace_stok?kd=" + kd, function(data, status) {
            document.getElementById("loader_status").style.display = "none";
            $("#tb_trace").html(data);
        });
    }

    function unduh() {
        var tahun = $("#tahun").val();
        var bln = $("#bln").val();
        window.open("<?= base_url(); ?>Gudang/tb_saldo_stok_xls?thn=" + tahun + "&bln=" + bln, "_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();
            }
        });
    });
</script>