<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=<?= $this->config->item('api_map_key') ?>&sensor=false"></script>
<script type="text/javascript">

    $(document).ready(function () {
        loadfungsimodaldetail();
        dropdown_karyawan();
        dropdown_cabang();
        dropdown_departemen();

        // periode
        tanggal();
        pickrange();


        $(document).on('click','#btn_download',function(){
            var tgl_mulai   = $(this).attr("tglmulai");
            var tgl_selesai = $(this).attr("tglselesai");

            <?php
                if(in_array($this->level_user,["1","4"])){
            ?>
                    var id_karyawan   = $(this).attr("idkaryawan");
                    var id_cabang     = $(this).attr("idcabang");
                    var id_departemen = $(this).attr("iddepartemen");
            <?php
                }else if(in_array($this->level_user,["5"])){
            ?>
                    var id_karyawan   = $(this).attr("idkaryawan");
                    var id_cabang     = '<?= $this->id_cabang ?>';
                    var id_departemen = $(this).attr("iddepartemen");
            <?php
                }else if(in_array($this->level_user,["2","7"])){
            ?>
                    var id_karyawan   = $(this).attr("idkaryawan");
                    var id_cabang     = '<?= $this->id_cabang ?>';
                    var id_departemen = '<?= $this->id_departemen ?>';
            <?php
                }else{
            ?>
                    var id_karyawan   = '<?= $this->id_karyawan ?>';
                    var id_cabang     = '<?= $this->id_cabang ?>';
                    var id_departemen = '<?= $this->id_departemen ?>';
            <?php
                }
            ?>

            window.location.href = "<?= route('patroli.laporan.download.excel') ?>"+"/"+tgl_mulai+"/"+tgl_selesai+"/"+id_karyawan+"/"+id_cabang+"/"+id_departemen;
        });

        $(document).on('click','#btn_download2',function(){
            var tgl_mulai   = $(this).attr("tglmulai");
            var tgl_selesai = $(this).attr("tglselesai");

            <?php
                if(in_array($this->level_user,["1","4"])){
            ?>
                    var id_karyawan   = $(this).attr("idkaryawan");
                    var id_cabang     = $(this).attr("idcabang");
                    var id_departemen = $(this).attr("iddepartemen");
            <?php
                }else if(in_array($this->level_user,["5"])){
            ?>
                    var id_karyawan   = $(this).attr("idkaryawan");
                    var id_cabang     = '<?= $this->id_cabang ?>';
                    var id_departemen = $(this).attr("iddepartemen");
            <?php
                }else if(in_array($this->level_user,["2","7"])){
            ?>
                    var id_karyawan   = $(this).attr("idkaryawan");
                    var id_cabang     = '<?= $this->id_cabang ?>';
                    var id_departemen = '<?= $this->id_departemen ?>';
            <?php
                }else{
            ?>
                    var id_karyawan   = '<?= $this->id_karyawan ?>';
                    var id_cabang     = '<?= $this->id_cabang ?>';
                    var id_departemen = '<?= $this->id_departemen ?>';
            <?php
                }
            ?>

            window.location.href = "<?= route('patroli.laporan.download2.excel2') ?>"+"/"+tgl_mulai+"/"+tgl_selesai+"/"+id_karyawan+"/"+id_cabang+"/"+id_departemen;
        });

    });

    //btn_hapus
    function delete_laporan(e){
        var id_patroli = $(e).attr('id');
        $("#modal_hapus #id_patroli").val(id_patroli);
        $("#modal_hapus .card-body").show();
        $("#modal_hapus .card-footer").show();
        $('#modal_hapus').modal('show');
        $(".title").text("<?= $this->lang->line('title_form_hapus_data') ?>");
        $(".btn_hapus").removeClass("btn_hapus").addClass("btn_hapus btn btn-danger");
        $('.btn_hapus').attr('onClick','hapus_action(this)');
    };
    //btn_hapus

    //btn_delete
    function hapus_action(e){
        var loading = Ladda.create(e);
        loading.start();
        id_patroli = $("#modal_hapus #id_patroli").val();
        $.ajax({
            type: 'POST',
            url: "<?= route('patroli.laporan.delete') ?>",
            data: 'id_patroli='+id_patroli,
            dataType:'JSON',
            success: function(result){
                loading.stop();
                $("#modal_hapus .card-body").hide();
                $("#modal_hapus .card-footer").hide();
                $("#modal_hapus .title").html(result.message);
            },
            error:function(){
                loading.stop();
                $("#modal_hapus .card-body").hide();
                $("#modal_hapus .card-footer").hide();
                $("#modal_hapus .title").html('<font color="#eb3a28"><i class="fa fa-close(alias)">&nbsp;</i><strong><?= $this->lang->line('error') ?></strong></font>');
            }
        });
    };
    //btn_delete

    function dropdown_karyawan() {
        $('#karyawan').select2({
            placeholder: '<?= $this->lang->line("semua_petugas") ?>',
            allowClear: true,
            ajax: {
                url: '<?= route("dropdown.karyawan")  ?>',
                dataType: 'json',
                delay: 250,
                processResults: function (data) {
                return {
                        results: data
                    };
                },
                cache: false
            },
            minLength: 3
        });

    }

    function dropdown_cabang(){
        $('#cabang').select2({
            placeholder: '<?= $this->lang->line("semua_cabang") ?>',
            allowClear: true,
            ajax: {
                url: '<?= route("dropdown.cabang")  ?>',
                dataType: 'json',
                delay: 250,
                processResults: function (data) {
                return {
                        results: data
                    };
                },
                cache: false
            },
            minLength: 3
        });
    }

    function onchange_dropdown_cabang(){
        // $("#karyawan").val('').trigger('change');
        $("#departemen").val('').trigger('change');
        dropdown_departemen();
    }

    function dropdown_departemen(){

        <?php
            if(in_array($this->level_user,["1","4"])){
        ?>
                var id_cabang = $("#cabang").val();
        <?php
            }else{
        ?>
                var id_cabang = '<?= $this->id_cabang ?>';
        <?php
            }
        ?>

        $("#departemen").select2({
            placeholder: "<?= $this->lang->line("semua_departemen") ?>",
            allowClear: true,
            ajax: {
                url: '<?= route("dropdown.departemen.by.cabang") ?>'+'/'+id_cabang,
                dataType: 'json',
                delay: 250,
                processResults:function(data) {
                    return {
                            results: data
                        };
                },
                cache: false
            }
        });
    }

    function tanggal() {
        var d = new Date();
        d.setDate(d.getDate());
        $('.input-daterange input').each(function() {
            $(this).datepicker({
                orientation: "bottom",
                format: "dd-mm-yyyy",
                endDate: d,
                autoclose: true,
            });
        });
    }
</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="#"><?= $this->lang->line('patroli') ?></a>
                        </li>
                        <li class="breadcrumb-item active"><?= $judul ?>
                        </li>
                    </ol>
                </div>
            </div>
        </div>
        <div class="content-header-right col-md-6 col-12">
        </div>
    </div>
</div>

<div class="card card-rounded">
    <div class="card-content">
        <div class="card-body filter">
            <div class="row">
                <div class="col-md-1">
                    <label><?= strtoupper($this->lang->line('periode')) ?></label>
                </div>
                <div class="col-md-5">
                    <div class="input-group input-daterange">
                        <input type="text" id="tanggal_mulai" class="form-control masktanggal" value="<?= $tgl_mulai ?>" placeholder="dd-mm-yyyy">
                        <div class="align-self-center">&nbsp;s/d&nbsp;</div>
                        <input type="text" id="tanggal_selesai" class="form-control masktanggal" value="<?= $tgl_selesai ?>" placeholder="dd-mm-yyyy">
                    </div>
                </div>
                <?php
                    if($level_user == "3") {
                ?>
                        <div class="col-md-1">
                            <button type="button" id="btn_filter" class="ladda-button btn-block btn btn-primary" data-style="expand-left">FILTER</button>
                        </div>
                <?php
                    }
                ?>
            </div>

            <?php
                if($level_user != "3") {
            ?>
                    <div class="row mt-1">
                        <div class="col-md-1">
                            <label><?= strtoupper($this->lang->line('petugas')) ?></label>
                        </div>
                        <div class="col-md-5">
                            <select id="karyawan" class="form-control"></select>
                        </div>
                        <div class="col-md-1">
                            <button type="button" id="btn_cari" class="btn btn-primary ladda-button" data-style="expand-left"><?= $this->lang->line('button_cari') ?></button>
                        </div>
                    </div>
            <?php
                }
            ?>

            <div class="row mt-1">
                <?php
                    if(in_array($level_user, array("1", "4")) || $multi_cabang == true) {
                ?>
                        <div class="col-md-1">
                            <label><?= strtoupper($this->lang->line('cabang')) ?></label>
                        </div>
                        <div class="col-md-2">
                            <select class="form-control" id="cabang" onchange="onchange_dropdown_cabang()"></select>
                        </div>
                <?php
                    }

                    if (in_array($level_user, array("1", "4", "5")) || $multi_departemen == true) {

                        if(in_array($level_user, array("1", "4"))){
                            $class = "col-md-2";
                        }else if($level_user == "5"){
                            if($multi_cabang == true){
                                $class = "col-md-2";
                            }else{
                                $class = "col-md-5";
                            }
                        }else{
                            $class = "col-md-5";
                        }
                ?>
                        <div class="col-md-1">
                            <label><?= strtoupper($this->lang->line('departemen')) ?></label>
                        </div>
                        <div class="<?= $class ?>">
                            <select id="departemen" class="form-control"></select>
                        </div>
                        <div class="col-md-1">
                            <button type="button" id="btn_filter" class="btn btn-primary btn-block ladda-button" data-style="expand-left">FILTER</button>
                        </div>
                <?php
                    }
                ?>
            </div>
        </div>
    </div>
</div>

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

<!-- START TAMPIL MODAL -->
<div id="modal_hapus" class="modal fade in">
    <div class="modal-dialog modal-md">
        <div id="tampil_form" class="modal-content">
            <input type="hidden" id="id_patroli">
            <div class="card-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                <h3 class="title modal-title"></h3>
            </div>
            <div class="card-footer">
                <div class="row">
                    <div class="col-md-7 text-left">
                        <div id="message"></div>
                    </div>
                    <div class="col-md-5 text-right">
                        <button class="btn_hapus ladda-button" id="btn_hapus" data-style="expand-left"><i class="fa fa-trash"></i> <?= $this->lang->line('button_form_hapus') ?></button>
                    </div>
                </div><!-- ./row -->
            </div>
        </div>
    </div>
</div>
<!-- END TAMPIL MODAL -->

<script type="text/javascript">
    $(function() {

        var level_user       = '<?= $level_user ?>';
        var multi_departemen = '<?= $multi_departemen ?>';
        var multi_cabang     = '<?= $multi_cabang ?>';

        var tgl_mulai   = $(".filter #tanggal_mulai").val();
        var tgl_selesai = $(".filter #tanggal_selesai").val();

        if(level_user == "3"){
            var id_karyawan = '<?= $this->id_karyawan ?>';
        }else{
            var id_karyawan = 0;
        }

        if(level_user == "1" || level_user == "4"){ // ADMIN HR DAN DIREKSI
            var id_cabang     = (!$("#cabang").val())?"0":$("#cabang").val();
            var id_departemen = (!$("#departemen").val())?"0":$("#departemen").val();
        }else if(level_user == "5"){ // KEPALA CABANG
            if(multi_cabang == '1'){
                var id_cabang   = (!$("#cabang").val())?"0":$("#cabang").val();
            }else{
                var id_cabang   = '<?= $this->session->userdata("id_cabang") ?>';
            }
            var id_departemen   = (!$("#departemen").val())?"0":$("#departemen").val();
        }else if(level_user == "2"){ // KEPALA DEPARTEMEN
            var id_cabang     = '<?= $this->session->userdata("id_cabang") ?>';
            if(multi_departemen == '1'){
                var id_departemen   = (!$("#departemen").val())?"0":$("#departemen").val();
            }else{
                var id_departemen   = '<?= $this->id_departemen ?>';
            }
        }else{
            var id_cabang       = '<?= $this->id_cabang ?>';
            var id_departemen   = '<?= $this->id_departemen ?>';
        }


       
        $('#view_data').load("<?= route('patroli.laporan.data.tabel') ?>"+"/"+tgl_mulai+"/"+tgl_selesai+"/"+id_karyawan+"/0/0");


        $('#btn_cari').click(function(){
            var loading = Ladda.create(this);
            loading.start();

            var tgl_mulai = $(".filter #tanggal_mulai").val();
            var tgl_selesai = $(".filter #tanggal_selesai").val();

            <?php
                if(in_array($this->level_user,["3"])){ // ADMIN HR DAN DIREKSI
            ?>
                    var id_karyawan = '<?= $this->id_karyawan ?>';
            <?php
                }else{
            ?>
                    var id_karyawan = ($("#karyawan").val() == null)?"0":$("#karyawan").val();
            <?php
                }
            ?>

            $('#view_data').load("<?= route('patroli.laporan.data.tabel') ?>"+"/"+tgl_mulai+"/"+tgl_selesai+"/"+id_karyawan+"/0/0", function(){
                // SET ATRIBUTE DOWNLOAD
                $("#btn_download").attr("tglmulai",tgl_mulai);
                $("#btn_download").attr("tglselesai",tgl_selesai);
                $("#btn_download").attr("idkaryawan",id_karyawan);
                $("#btn_download").attr("idcabang","0");
                $("#btn_download").attr("iddepartemen","0");

                $("#btn_download2").attr("tglmulai",tgl_mulai);
                $("#btn_download2").attr("tglselesai",tgl_selesai);
                $("#btn_download2").attr("idkaryawan",id_karyawan);
                $("#btn_download2").attr("idcabang","0");
                $("#btn_download2").attr("iddepartemen","0");

                loading.stop();
            });
        });

        $('#btn_filter').click(function(){
            $("#karyawan").val('').trigger('change');

            var loading = Ladda.create(this);
            loading.start();

            var tgl_mulai   = $(".filter #tanggal_mulai").val();
            var tgl_selesai = $(".filter #tanggal_selesai").val();
            if(level_user == "3"){
                var id_karyawan = '<?= $this->id_karyawan ?>';
            }else{
                var id_karyawan = 0;
            }

            if(level_user == "1" || level_user == "4"){ // ADMIN HR DAN DIREKSI
                var id_cabang     = (!$("#cabang").val())?"0":$("#cabang").val();
                var id_departemen = (!$("#departemen").val())?"0":$("#departemen").val();
            }else if(level_user == "5"){ // KEPALA CABANG
                if(multi_cabang == '1'){
                    var id_cabang   = (!$("#cabang").val())?"0":$("#cabang").val();
                }else{
                    var id_cabang   = '<?= $this->session->userdata("id_cabang") ?>';
                }
                var id_departemen   = (!$("#departemen").val())?"0":$("#departemen").val();
            }else if(level_user == "2"){ // KEPALA DEPARTEMEN
                var id_cabang     = '<?= $this->id_cabang ?>';
                if(multi_departemen == '1'){
                    var id_departemen   = (!$("#departemen").val())?"0":$("#departemen").val();
                }else{
                    var id_departemen   = '<?= $this->id_departemen ?>';
                }
            }else{
                var id_cabang       = '<?= $this->id_cabang ?>';
                var id_departemen   = '<?= $this->id_departemen ?>';
            }

            $('#view_data').load("<?= route('patroli.laporan.data.tabel') ?>"+"/"+tgl_mulai+"/"+tgl_selesai+"/"+id_karyawan+"/"+id_cabang+"/"+id_departemen, function(){
                // SET ATRIBUTE DOWNLOAD
                $("#btn_download").attr("tglmulai",tgl_mulai);
                $("#btn_download").attr("tglselesai",tgl_selesai);
                $("#btn_download").attr("idkaryawan",id_karyawan);
                $("#btn_download").attr("idcabang",id_cabang);
                $("#btn_download").attr("iddepartemen",id_departemen);

                $("#btn_download2").attr("tglmulai",tgl_mulai);
                $("#btn_download2").attr("tglselesai",tgl_selesai);
                $("#btn_download2").attr("idkaryawan",id_karyawan);
                $("#btn_download2").attr("idcabang",id_cabang);
                $("#btn_download2").attr("iddepartemen",id_departemen);
                loading.stop();
            });
        });

    });

</script>