<?php echo $this->extend('/template'); ?>
<?php echo $this->section('content') ?>

<head>
    <title>KELOLA ARMADA</title>
    <!-- <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script> -->

</head>
<div class="row">
    <div class="col-md-5">
        <h3>
            <img src="<?php echo base_url(); ?>/assets/images/pemprovsmr/header_icon.png" width="35px">&nbsp;<b>KELOLA ARMADA</b>
        </h3>
    </div>
    <div class="col-md-7" style="text-align:right;">
    <a href="/armada_cetak/<?php echo $balik; ?>" target="_blank"><span class="fa fa-print fa-3x"></span></a>
    </div>
</div>

<div class="containers">

    <table id="dg" title="DAFTAR ARMADA" class="easyui-datagrid" style="width:100%;height:600px" url="/armadadata" toolbar="#toolbar" rownumbers="true" fitColumns="true" singleSelect="true" data-options="
            pagination:true,
            scrollbarSize:0,
            nowrap:false,
            remoteFilter:true,
            pageSize:20,
            noWrap:false,
            queryParams: {'<?php echo csrf_token() ?>': '<?php echo csrf_hash() ?>','balik':'<?php echo $balik; ?>'},
            ">
        <thead>
            <tr>
                <th field="perusahaan" width="50" align="left">PENYEDIA</th>
                <th field="nama" width="50" align="left">NAMA ARMADA</th>
                <th field="nopol" width="30">NO PLAT</th>
                <th field="jenist" width="30">JENIS ARMADA</th>
                <th field="nobus" width="15" align="center">NO BUS</th>

                <th field="kapasitas" width="35" align="center">KAPASITAS<br />PENUMPANG</th>
                <th field="kontakwa" width="35" align="left">NOMOR KONTAK</th>
                <th field="statust" width="45" align="center">STATUS</th>
                <th field="aksi" width="30" align="center">AKSI</th>
            </tr>
        </thead>
    </table>

    <div id="toolbar">
        <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="armada_baru()">Menambah Armada</a>
    </div>

    <div id="dlg" class="easyui-dialog" modal="true" style="width:600px;height:450px;padding:10px 20px" closed="true" buttons="#dlg-buttons">

        <form id="fm" method="post" enctype="multipart/form-data">
            <div class="fitem">
                <input name="nama" id="nama" labelPosition="left" label="NAMA BUS" class="easyui-textbox" labelWidth="110px" style="width:90%">
            </div>
            <div class="fitem">
                <input name="nobus" id="nobus" style="width:50%" label="NO BUS" labelWidth="110px" labelPosition="left" class="easyui-textbox">
            </div>
            <div class="fitem">
                <input name="nopol" id="nopol" style="width:50%" label="NO PLAT" labelWidth="110px" labelPosition="left" class="easyui-textbox">
            </div>
            <div class="fitem">
                <input id="jenis" name='jenis' class="easyui-combobox" data-options="
                label:'JENIS',
                value: '<?php //echo $kec_kode; 
                        ?>',
                queryParams: {'<?php echo csrf_token() ?>': '<?php echo csrf_hash() ?>'},
                labelPosition:'left',
                labelWidth:'110px',
                width:'50%',
                panelHeight:'auto',
                editable:false,
                valueField: 'id',
                textField: 'jenis',
                url: '/jenisarmada_combo',
            ">
                <div class="fitem">
                    <input name="kapasitas" id="kapasitas" style="width:50%" label="KAPASITAS" labelWidth="110px" labelPosition="left" class="easyui-numberbox">
                </div>
                <div class="fitem">
                    <input name="perusahaan" id="perusahaan" style="width:100%" label="PENYEDIA" labelWidth="110px" labelPosition="left" class="easyui-textbox">
                </div>
                <div class="fitem">
                    <input name="kontakwa" id="kontakwa" style="width:100%" label="No. WA" labelWidth="110px" labelPosition="left" class="easyui-textbox">
                </div>

            </div>
        </form>
    </div>


    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="armada_simpan()" style="width:90px">Simpan</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">Cancel</a>
    </div>

</div>

<script>
    $(document).ready(function() {

    });

    function armada_baru() {
        $('#dlg').dialog('open').dialog('setTitle', 'Menambah Armada Baru');
        $('#fm').form('clear');
        url = '/armada_simpan';
    }

    function armada_edit(id) {
        $.ajax({
            type: 'post',
            url: "<?php echo base_url(); ?>/armadaeditdata",
            data: {
                id: id,
                "<?= csrf_token() ?>": "<?= csrf_hash() ?>"

            },
            success: function(data) {
                var respon = $.parseJSON(data);
                console.log(respon);
                $('#dlg').dialog('open').dialog('setTitle', 'Edit Armada');
                $('#fm').form('clear');
                $('#fm').form('load', respon);
                //id exdokumen diganti jika ada filenya 
                console.log(respon);
                url = "/armada_update/" + id;
            }

        });

    }

    function armada_simpan() {
        $('#fm').form('submit', {
            url: url,
            onSubmit: function(param) {
                param.csrftoken_erb2021 = '<?= csrf_hash() ?>';
                param.balik = '<?php echo $balik; ?>';
                return $(this).form('validate');
            },

            success: function(result) {
                var respon = $.parseJSON(result);
                console.log(respon);

                if (respon.errorMsg) {
                    alert(respon.errorMsg);
                } else {
                    $('#dg').datagrid('reload'); // reload the user data
                    $('#dlg').dialog('close'); // close the dialog
                    alert('Penyimpanan Berhasil');
                    $('#dlg').dialog('close'); // close the dialog
                }
            }
        });

    }

    function armada_destroy(id) {
        $.messager.confirm('Confirm', 'Yakin akan menghapus Data ini. Jika ya maka Data ini akan dihapus secara permanen?', function(r) {
            if (r) {
                $.ajax({
                    type: 'post',
                    url: "<?php echo base_url(); ?>/armada_destroy",
                    data: {
                        id: id,
                        "<?= csrf_token() ?>": "<?= csrf_hash() ?>"
                    },
                    success: function(data) {
                        var respon = $.parseJSON(data);
                        console.log(respon);
                        if (respon.code == 1) 
                            $('#dg').datagrid('reload');
                    },
                    error: function(){ alert('Tidak Dapat Dihapus karena Armada sudah di Tunjuk ke Tujuan Tertentu');}

                });
            }
        });
    }

    function hitungkuota() {
        $.ajax({
            type: 'post',
            url: "<?php echo base_url(); ?>/hitungkuota",
            data: {
                "<?= csrf_token() ?>": "<?= csrf_hash() ?>"
            },
            success: function(data) {
                var respon = $.parseJSON(data);
                if (respon.kuota > 0)
                    $('.boxinfo').html("<center>Selamat Datang di PEDA MATENG<br/>Saat ini masih tersedia kuota untuk :<br/><b>" + respon.kuota + " orang</b></center>");
                else {
                    $('.boxinfo').html("<center>Selamat Datang di PEDA MATENG<br/>Saat ini kuota telah Habis, silakan akses di lain waktu untuk melihat kemungkinan adanya penambahan kuota</b></center>");

                }
            }

        });

    }
</script>

<script type="text/javascript">
    $(function() {
        var dg = $('#dg').datagrid();
        dg.datagrid('enableFilter', [{
                field: 'perusahaan',
                type: 'textbox'
            }, {
                field: 'nopol',
                type: 'label'
            }, {
                field: 'nama',
                type: 'textbox'
            }, {
                field: 'kapasitas',
                type: 'label'
            }, {
                field: 'jenist',
                type: 'combobox',
                trigger: 'none',
                options: {
                    panelHeight: 'auto',
                    editable: false,
                    selectOnNavigation: false,
                    queryParams: {
                        '<?php echo csrf_token() ?>': '<?php echo csrf_hash() ?>'
                    },
                    url: '/jenisarmada2_combo',
                    onChange: function(value) {
                        if (value == '') {
                            dg.datagrid('removeFilterRule', 'jenis');
                        } else {
                            dg.datagrid('addFilterRule', {
                                field: 'jenis',
                                value: value
                            });
                        }

                        dg.datagrid('doFilter');
                    }

                }
            }, {
                field: 'aksi',
                type: 'label'
            }, {
                field: 'kontakwa',
                type: 'label'
            }, {
                field: 'statust',
                type: 'combobox',
                trigger: 'none',
                options: {
                    panelHeight: 'auto',
                    editable: false,
                    selectOnNavigation: false,
                    queryParams: {
                        '<?php echo csrf_token() ?>': '<?php echo csrf_hash() ?>'
                    },
                    url: '/statusa_combo',
                    onChange: function(value) {
                        if (value == '') {
                            dg.datagrid('removeFilterRule', 'status');
                        } else {
                            dg.datagrid('addFilterRule', {
                                field: 'status',
                                value: value
                            });
                        }

                        dg.datagrid('doFilter');
                    }

                }
            }, {
                field: 'wilayaht',
                type: 'combobox',
                trigger: 'none',
                options: {
                    editable: false,
                    panelHeight: 'auto',
                    selectOnNavigation: false,
                    queryParams: {
                        '<?php echo csrf_token() ?>': '<?php echo csrf_hash() ?>'
                    },
                    url: '/wilayah_combo',
                    onChange: function(value) {
                        if (value == '') {
                            dg.datagrid('removeFilterRule', 'wilayah_id');
                        } else {
                            dg.datagrid('addFilterRule', {
                                field: 'wilayah_id',
                                op: 'equal',
                                value: value
                            });
                        }
                        dg.datagrid('doFilter');
                    }
                }
            },




        ]);
    });
</script>
<script>
    function armada_status(i, s) {
        $.ajax({
            type: 'post',
            url: "<?php echo base_url(); ?>/armada_status",
            data: {
                i: i,
                s: s,
                "<?= csrf_token() ?>": "<?= csrf_hash() ?>"

            },
            success: function(data) {
                var respon = $.parseJSON(data);
                console.log(respon);
                if (respon.code == 1)
                    $('#dg').datagrid('reload');
                else
                    alert('TIDAK BISA UBAH STATUS. Armada ini sudah ditunjuk ke Tujuan Tertentu.');
            }

        });
    }
</script>



<style>
    .easyui-dialog {
        background-color: #f7d9d9;
    }
</style>

<?php echo $this->endSection() ?>