<script type="text/javascript">
    $(document).ready(function () {
        dropdown_departemen();


        $(document).on('click', '#btn_filter', function () {
            var loading = Ladda.create(this);
            var cabang_pusat    = "<?= $this->cabang_pusat ?>";
            var id_cabang       = (cabang_pusat == 1)?$('#cabang').val():"<?= $this->id_cabang ?>";
            var id_departemen   = (!$('#departemen').val())?"all":$('#departemen').val();
            var tgl_mulai       = $('#tanggal_mulai').val();
            var tgl_selesai     = $('#tanggal_selesai').val();
            loading.start();
            $('#view_data').load("<?= route('reimburse.rekap.listdata') ?>"+"/"+id_cabang+"/"+id_departemen+"/"+tgl_mulai+"/"+tgl_selesai, function(){
                loading.stop();
            });
        });

        $(document).ready(function () {
            $('#cabang').select2({
                placeholder: 'Pilih Cabang',
                allowClear: true,
                ajax: {
                    // type: 'POST',
                    url: '<?= route("dropdown.cabang")  ?>',
                    dataType: 'json',
                    delay: 250,
                    processResults: function (data) {
                    return {
                            results: data
                        };
                    },
                    cache: false
                },
                minLength: 3
            });
        });

        $(document).on('click','#btn_download_pdf',function(e){
            var cabang_pusat    = "<?= $this->cabang_pusat ?>";
            var id_cabang       = (cabang_pusat == 1)?$('#cabang').val():"<?= $this->id_cabang ?>";
            var id_departemen   = (!($('#departemen').val()))?"all":$('#departemen').val();
            var id_karyawan     = "-";
            var tgl_mulai       = $('#tanggal_mulai').val();
            var tgl_selesai     = $('#tanggal_selesai').val();

            var url = "<?= route('reimburse.rekap.download.pdf') ?>"+"/"+id_cabang+"/"+id_departemen+"/"+id_karyawan+"/"+tgl_mulai +"/"+tgl_selesai;
            window.open(url);
        });

    });


    function onchange_cabang() {
        $('#departemen').val('');
        $('#departemen').select2({
            placeholder: 'Semua Departemen',
            allowClear: true,
            ajax: {
              // type: 'POST',
                url: '<?= route("dropdown.departemen.by.cabang")  ?>'+'/'+$('#cabang').val(),
                dataType: 'json',
                delay: 250,
                processResults: function (data) {
                return {
                        results: data
                    };
                },
                cache: false
            },
            minLength: 3
        });
    }

    function dropdown_departemen(){
        var cabang_pusat    = "<?= $this->cabang_pusat ?>";
        var id_cabang       = (cabang_pusat == 1)?$('#cabang').val():"<?= $this->id_cabang ?>";
        $('#departemen').select2({
            placeholder: 'Semua Departemen',
            allowClear: true,
            ajax: {
                 // type: 'POST',
                url: '<?= route("dropdown.departemen.by.cabang")  ?>'+'/'+id_cabang,
                dataType: 'json',
                delay: 250,
                processResults: function (data) {
                return {
                        results: data
                    };
                },
                cache: false
            },
            minLength: 3
        });

    }

</script>
<div class="content-header">
    <div class="row align-items-center">
        <div class="content-header-left col-md-6 col-12 mb-md-0 mb-1">
            <h3 class="content-header-title"><?= $judul ?></h3>
            <div class="row breadcrumbs-top">
                <div class="breadcrumb-wrapper col-12">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="#">Rekap Reimburse</a></li>
                        <li class="breadcrumb-item active"><?= $judul ?></li>
                    </ol>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="card card-rounded">
    <div class="card-content">
        <div class="card-body">
            <div class="row filter">
                <div class="col-md-2">
                    <label>Tanggal Mulai</label>
                    <?php
                        $tgl_mulai   = date('d-m-Y', strtotime('-7 day'));
                        $tgl_selesai = date('d-m-Y', strtotime('+1 month'));
                    ?>
                    <input type="text" name="tanggal_mulai" id="tanggal_mulai" class="form-control datepicker" autocomplete="off" placeholder="dd-mm-yyyy" value="<?= $tgl_mulai ?>">
                </div>
                <div class="col-md-2">
                    <label>Tanggal Selesai</label>
                    <input type="text" name="tanggal_selesai" id="tanggal_selesai" class="form-control datepicker" autocomplete="off" placeholder="dd-mm-yyyy" value="<?= $tgl_selesai ?>">
                </div>
                <?php
                    if(in_array($this->level_user, array("1","4","6")) && $this->cabang_pusat == true) {
                ?>
                        <div class="col-md-3">
                            <label>Cabang</label>
                            <select id="cabang" class="form-control" class="select2 form-control" onchange="onchange_cabang()">
                                <option>Pilih</option>
                            </select>
                        </div>
                <?php
                    }
                ?>
                <div class="col-md-3">
                    <label>Departemen</label>
                    <select id="departemen" class="select2 form-control">
                        <option>Semua Departemen</option>
                    </select>
                </div>
                <div class="col-md-1 align-self-end">
                    <button type="button" id="btn_filter" class="btn btn-primary ladda-button" data-style="expand-left">FILTER</button>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="card card-rounded">
    <div class="card-content">
        <div class="card-body">
            <div class="row">
                <div id="view_data" class="col-md-12"></div>
            </div>
        </div>
    </div>
</div>

<!-- START TAMPIL MODAL -->
<div id="modal_form" class="modal fade in">
    <div class="modal-dialog modal-md">
        <div id="tampil_form" class="modal-content"></div>
    </div>
</div>
<!-- END TAMPIL MODAL -->


