<?= $this->extend('template/layout') ?>

<?= $this->section('title') ?>Customer Engagement - <?= env('app.name') ?><?= $this->endSection() ?>

<!-- main section start-->
<?= $this->section('main') ?>
<?php
    $uri      = current_url(true);
    $segment1 = $uri->setSilent()->getSegment(1);
    $segment2 = $uri->setSilent()->getSegment(2);
    $segment3 = $uri->setSilent()->getSegment(3);
?>
<!-- Breadcumb -->
<ol class="breadcrumb page-breadcrumb">
    <li class="breadcrumb-item"><a href="javascript:void(0);"><?= env('app.name') ?></a></li>
    <li class="breadcrumb-item">Customer Engagement</li>
    <li class="breadcrumb-item">Browse Pesan</li>
    <li class="position-absolute pos-top pos-right d-none d-sm-block"><span class="js-get-date"></span></li>
</ol>

<div class="row">
    <div class="col-xl-6">
        <div id="panel-1" class="panel">
            <div class="panel-hdr border-faded border-top-1 border-right-0 border-left-0 shadow-0">
                <h2>Data Pesan</h2>
                <div class="panel-toolbar pr-3">
                    <ul class="nav nav-pills border-bottom-0" role="tablist">
                        <li class="nav-item">
                            <?= form_button('reload', '<i class="fal fa-sync"></i>', 'reload_datagrid_pesan()') ?>&nbsp;
                        </li>
                        <li class="nav-item">
                            <button type="button" id="show-modal" class="btn btn-default waves-effect waves-themed" onclick="show_data()"><i class="fal fa-eye"></i></button>&nbsp;
                        </li>
                        <?php if ($hapus == 1) { ?>
                        <li class="nav-item">
                            <?= form_button('delete', '<i class="fal fa-trash-alt"></i>', 'delete_pesan()') ?>&nbsp;
                        </li>
                        <?php } ?>
                    </ul>
                </div>
            </div><!-- end panel hdr -->

            <div class="panel-container show">
                <div class="panel-content">
                    <div id="panelfit-1" data-options="border:false">
                        <!-- grid start -->
                        <table id="datagrid_pesan" toolbar="#toolbar_pesan" url="<?= site_url('cusen/browse_pesan/datagrid_pesan') ?>" data-options="nowrap:true,striped:false,ctrlSelect:true,rownumbers:true,pagination:true,multiSort:true,remoteFilter:true,remoteSort:true,pageSize:10,pageList:[10,20,50,100],clientPaging:false,fit:true,fitColumns:true">
                            <thead data-options="frozen:true">
                                <tr>
                                    <th data-options="field:'ck',checkbox:true"></th>
                                </tr>
                            </thead>
                            <thead>
                                <tr>
                                    <th data-options="field:'judul',sortable:true,width:200">Judul</th>
                                    <th data-options="field:'isi',sortable:true,width:350">Pesan</th>
                                    <th data-options="field:'username',sortable:true,width:200">Pengirim</th>
                                    <th data-options="field:'wkt_rekam',sortable:true,width:130,align:'center'">Waktu Kirim</th>
                                </tr>
                            </thead>
                        </table>
                        <!-- grid end -->
                    </div>
                </div>
            </div><!-- end panel main content -->
        </div>
    </div>

    <div class="col-xl-6">
        <div id="panel-2" class="panel">
            <div class="panel-hdr border-faded border-top-1 border-right-0 border-left-0 shadow-0">
                <h2>Data Penerima</h2>
                <div class="panel-toolbar pr-3">
                    <ul class="nav nav-pills border-bottom-0" role="tablist">
                        <li class="nav-item">
                            <?= form_button('reload', '<i class="fal fa-sync"></i>', 'reload_datagrid_rcv()') ?>&nbsp;
                        </li>
                    </ul>
                </div>
            </div><!-- end panel hdr -->

            <div class="panel-container show">
                <div class="panel-content">
                    <div id="panelfit-2" data-options="border:false">
                        <!-- grid start -->
                        <table id="datagrid_rcv" toolbar="#toolbar_rcv" url="<?= site_url('cusen/browse_pesan/datagrid_rcv') ?>" data-options="nowrap:true,striped:false,ctrlSelect:true,rownumbers:true,pagination:true,multiSort:true,remoteFilter:true,remoteSort:true,pageSize:10,pageList:[10,20,50,100],clientPaging:false,fit:true,fitColumns:true">
                            
                            <thead>
                                <tr>
                                    <th data-options="field:'nama_calon',sortable:true,width:200">Nama</th>
                                    <th data-options="field:'agen',sortable:true,width:200">Agen</th>
                                    <th data-options="field:'wkt_kirim',sortable:true,width:250">Waktu Kirim</th>
                                </tr>
                            </thead>
                        </table>
                        <!-- grid end -->
                    </div>
                </div>
            </div><!-- end panel main content -->
        </div>
    </div>
</div>

<!-- MODAL SHOW -->
<div id="modal-show" class="modal fade default-example-modal-right" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog modal-dialog-right">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title h4">Detail Data Pesan</h5>
            </div>
         
            <div class="modal-body">
               <div class="table-responsive">
                    <table class="table">
                        <tr>
                            <td width="150">Judul</td>
                            <td width="10">:</td>
                            <td class="judul"></td>
                        </tr>
                        <tr>
                            <td width="150">Pengirim</td>
                            <td width="10">:</td>
                            <td class="pengirim"></td>
                        </tr>
                        <tr>
                            <td width="150">Waktu Kirim</td>
                            <td width="10">:</td>
                            <td class="dikirim"></td>
                        </tr>
                        <tr>
                            <td width="150">Isi Pesan</td>
                            <td width="10">:</td>
                            <td class="isi"></td>
                        </tr>
                    </table>
                </div>
                <hr/>
            </div><!-- end modal body -->
            <div class="modal-footer">
               <button type="button" class="btn btn-secondary waves-effect waves-themed" data-dismiss="modal">Close</button>
            </div>
         
        </div>
    </div>
</div><!-- END MODAL SHOW -->
<?= $this->endSection() ?>
<!-- main section end-->

<!-- pageScripts section start-->
<?= $this->section('pageScripts') ?>

<script type="text/javascript" src="<?= base_url('smart/dist/adlte/jquery-easyui-1.10.17/jquery.easyui.min.js') ?>"></script>
<script type="text/javascript" src="<?= base_url('smart/dist/adlte/jquery-easyui-1.10.17/datagrid-filter-1.0.6/datagrid-filter.js') ?>"></script>

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

    $(window).resize(function() {
        adjustpanelfit();
    });

    function adjustpanelfit(){
        var height = $(window).height();
        $('#panelfit-1, #panelfit-2').panel({
            height:height - 400
        });
    }
</script>

<script type="text/javascript">
    /*datagrid user*/
    var datagrid_pesan = $('#datagrid_pesan').datagrid({
        queryParams: {
            "<?= csrf_token() ?>": "<?= csrf_hash() ?>",
        },
        onDblClickRow : function(index,row){
            show_rcv();
        }
    });
    datagrid_pesan.datagrid('enableFilter');

    //show detail data pesan di modal
    function show_data()
    {
        var row = $('#datagrid_pesan').datagrid('getSelected');
        if (row) {
            $('.pengirim').html(row.username);
            $('.judul').html(row.judul);
            $('.isi').html(row.isi);
            $('.dikirim').html(row.wkt_rekam);
            $('#modal-show').modal('show');
        } else {
            $.messager.alert('<?= lang('App.warning') ?>','<?= lang('App.select_data') ?>','warning');
        }
    }

    /*function datagrid receiver*/
    //double klik row pesan tarik data child
    function show_rcv()
    {
        var row = $('#datagrid_pesan').datagrid('getSelected');
        if (row) {
            var id_pesan = row.id;
            $.ajax({
                url: '<?= base_url('cusen/browse_pesan/datagrid_rcv') ?>',
                type: "post",
                dataType: 'json',
                data: {
                    id_pesan: id_pesan,
                    "<?= csrf_token() ?>": "<?= csrf_hash() ?>",
                },
                error: function (xhr, status, message) {
                    $.messager.alert(status,message,'warning');
                },
                success: function(res) {
                    $('#datagrid_rcv').datagrid('loadData', res);
                }
            });
        } else {
            $.messager.alert('<?= lang('App.warning') ?>','<?= lang('App.select_data') ?>','warning');
        }
    }
    
    /*reload datagrid usergroup*/
    function reload_datagrid_pesan()
    {
        $('#datagrid_pesan').datagrid('removeFilterRule');
        $('#datagrid_pesan').datagrid('load');
    }

    /*datagrid rcv =====================================*/
    var datagrid_rcv = $('#datagrid_rcv').datagrid({
        queryParams: {
            "<?= csrf_token() ?>": "<?= csrf_hash() ?>",
        },

        onDblClickRow : function(index,row){
            edit_priv();
        }
    });
    datagrid_rcv.datagrid('enableFilter');

    /*reload datagrid am*/
    function reload_datagrid_rcv()
    {
        $('#datagrid_rcv').datagrid('removeFilterRule');
        $('#datagrid_rcv').datagrid('load');
    }
</script>

<?php if ($hapus == 1) { ?>
<!-- FUNCTION HAPUS -->
<script type="text/javascript">
    function delete_pesan()
    {
        var rows = $('#datagrid_pesan').datagrid('getSelections');
        var ids = [];
        for(var j=0;j<rows.length;j++){
            ids[j] = rows[j].id;
        }
        if (ids.length > 0) {
            $.messager.confirm('<?= lang('App.delete') ?>', '<?= lang('App.delete') ?> ' + rows.length + ' data ?', function(r){
                if (r){
                    $.ajax({
                        url: '<?= $action ?>',
                        type: "post",
                        dataType: 'json',
                        data: {
                            ids: ids,
                            "<?= csrf_token() ?>": "<?= csrf_hash() ?>",
                        },
                        error: function (xhr, status, message) {
                            $.messager.alert(status,message,'warning');
                        },
                        success: function(res) {
                            $('#datagrid_pesan').datagrid('reload');
                            $('#datagrid_rcv').datagrid('reload');
                            toastr.success(res.message, 'Updated!!', {timeOut: 3000});
                        }
                    });
                }
            });
        } else {
            $.messager.alert('<?= lang('App.warning') ?>','<?= lang('App.select_data') ?>','warning');
        }
    }
</script>
<?php } ?>
<?= $this->endSection() ?>
<!-- pageScripts section end-->