<?php include_once('../function/__data_app_db.php'); ?>
<?php
$edit = $name='';
$newLocation = 'href="add-track"';
if(isset($_GET['edit']) && !empty($_GET['edit'])){
    $edit = validate_jsnj_data($_GET['edit']);
    $edit_val = '<input type="hidden" value="'.$edit.'" name="edit">';
    $form_id = 'data-form';
    $button_name = 'Update';
    $action = 'function/edit/track-order-list';
    $buttonClass = 'btn-warning';
    $btnDisable = '';
    $viewAction = 'function/view/track-order-list';
    $title = 'Track Order';
    $tbl_id = 'track-order-list';
    $tbl_title = 'Track Order';
    $dlt_for = 'track-order';
} else {
    $edit = $edit_val = '';
    $form_id = 'data-form';
    $button_name = 'Save';
    $action = 'function/insert/track-order-list';
    $buttonClass = 'btn-success';
    $viewAction = 'function/view/track-order-list';
    $newLocation = 'href="add-track"';
    $btnDisable = 'enable';
    $title = 'Track Order';
    $tbl_id = 'quotation-pending';
    $tbl_title = 'Track Order';
    $dlt_for = 'track-order';
}
?>
<!DOCTYPE html>

<html class="loading" lang="en" data-textdirection="ltr">
<!-- BEGIN: Head-->
<?php include_once('head.php');?>
  <!-- END: Head-->

  <!-- BEGIN: Body-->
  <body class="vertical-layout vertical-menu-modern 2-columns navbar-sticky footer-static menu-expanded pace-done dark-layout" data-open="click" data-menu="vertical-menu-modern" data-col="2-columns">

    <!-- BEGIN: Header-->
    <?php include_once('header.php');?>
    <!-- END: Header-->

    <!-- BEGIN: Main Menu-->
    <?php include_once('sidemenu.php');?>
    <!-- END: Main Menu-->

    <!-- BEGIN: Content-->
    <div class="app-content content">
      <div class="content-overlay"></div>
      <div class="content-wrapper">
        <div class="content-header row"></div>
        <div class="content-body">
            <section id="basic-input">
                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-header">
                                <h4 class="card-title"><?php echo $title;?></h4>
                            </div>
                            <div class="card-content">
                                <div class="card-body">
                                        <div class="row datatable-btn">
                                            <div class="col-md-8 dataTables_wrapper dt-bootstrap4">
                                                <div class="btn-group btn-group-sm">
                                                    <a <?php echo $newLocation;?> class="btn btn-purple" <?php echo $btnDisable;?>><i class="demo-pli-add icon-fw"></i>Add <span class="hide-xs">New</span></a>
                                                    <button class="btn btn-default btn-icon add-tooltip" data-toggle="tooltip" data-original-title="DELETE" id="btn-delete" disabled><i class="bx bx-trash"></i></button>
                                                </div>
                                                <span id="tbl-btn"></span>

                                                <div class="dataTables_length"><label>Show <select class="custom-select custom-select-sm form-control form-control-sm" id="tbl-length"><option value="10">10</option><option value="25">25</option><option value="50">50</option><option value="100">100</option></select> entries</label></div>
                                            </div>
                                            <div class="col-md-4 dataTables_filter">
                                                <form id="search-tbl">
                                                    <div class="input-group input-group-sm">
                                                        <div class="input-group-prepend">
                                                        <a class="btn btn-warning btn-sm add-tooltip"  data-toggle="tooltip" data-original-title="RELOAD LIST" id="btn-tbl-reload"><i class="bx bx-reset"></i></a>
                                                        </div>
                                                        <input type="text" autocomplete="off" class="form-control" placeholder="Search" id="tbl-search">
                                                        <div class="input-group-append"><button class="btn btn-primary add-tooltip"  data-toggle="tooltip" data-original-title="SEARCH" id="btn-tbl-search"><i class="bx bx-search-alt"></i></button></div>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    <div class="table-responsive">
                                        <table id="<?php echo $tbl_id;?>" class="table table-bordered table-striped table-hover datatable">
                                            <thead>
                                            <tr>
                                                <th class="th-first noExport" title="SELECT ALL"><div class='checkbox'><input type='checkbox' class='checkbox-input' id="selectalldlt"><label for='selectalldlt'></label></div></th>
                                                <th>#</th>
                                                <th>Doc.No</th>
                                                <th>Date</th>
                                                <th>From Address</th>
                                                <th>To Address</th>
                                                <th class="min-desktop noExport">Action</th>
                                            </tr>
                                            </thead>
                                            <tbody id="<?php echo $tbl_id;?>_body">
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

        </div>
      </div>
    </div>
    <!-- END: Content-->


    <!-- BEGIN: Footer-->
    <?php include_once('footer.php');?>
    <!-- END: Footer-->

    <?php include_once('foot.php');?>
    <script type="text/javascript">
        //table
        var count = 0;
        var u_count = 0;
        datatbl();
        function datatbl() {
            $.notify({
                // options
                title: '<?php echo $title;?> data loading ',
                message: 'please wait...'
            },{
                // settings
                type: 'warning',
                animate: {
                    enter: 'animated bounceInDown',
                    exit: 'animated bounceOutUp'
                },
            });
            $.ajax({
                type: "POST",
                url: "<?php echo $viewAction;?>",//load page name
                data: {process: 'true'},
                cache: false,
                dataType: "json",
                success: function (tbl) {
                    $('#<?php echo $tbl_id;?>_body').html(tbl.tr);
                    tblDelete();
                    var OrderList = $('#<?php echo $tbl_id;?>').DataTable({
                        "dom": 'Brtip',
                        "responsive": true,
                        "buttons": [
                            {
                                "extend": 'copyHtml5',
                                "text": '<i class="bx bx-copy-alt add-tooltip" data-toggle="tooltip" data-original-title="COPY"></i>',
                                "titleAttr": 'Copy',
                                "title": '<?php echo $tbl_title;?>',
                                "exportOptions": {
                                    "columns": "thead th:not(.noExport)"
                                }
                            },
                            {
                                "extend": 'csvHtml5',
                                "text": '<i class="bx bxs-file add-tooltip" data-toggle="tooltip" data-original-title="CSV"></i>',
                                "titleAttr": 'CSV',
                                "title": '<?php echo $tbl_title;?>',
                                "exportOptions": {
                                    "columns": "thead th:not(.noExport)"
                                }
                            },
                            {
                                "extend": 'pdfHtml5',
                                "text": '<i class="bx bxs-file-pdf add-tooltip" data-toggle="tooltip" data-original-title="PDF"></i>',
                                "titleAttr": 'PDF',
                                "title": '<?php echo $tbl_title;?>',
                                "exportOptions": {
                                    "columns": "thead th:not(.noExport)"
                                }
                            },
                            {
                                "extend": 'print',
                                "text": '<i class="bx bx-printer add-tooltip" data-toggle="tooltip" data-original-title="PRINT"></i>',
                                "titleAttr": 'PRINT',
                                "title": '<?php echo $tbl_title;?>',
                                customize: function (win) {
                                    $(win.document.body)
                                        .css('font-size', '10pt');

                                    $(win.document.body).find('table')
                                        .addClass('compact')
                                        .css('font-size', 'inherit');
                                },
                                "exportOptions": {
                                    "columns": "thead th:not(.noExport)",
                                    "modifier": {
                                        "search": 'applied',
                                        "order": 'applied'
                                    }
                                }
                            }
                        ],
                        "columnDefs": [{
                            "targets": [0],
                            "orderable": false,
                            "searchable": false,
                        }],
                        "language": {
                            "sInfo": "Showing _START_ to _END_ of _TOTAL_ entries",
                            "sInfoFiltered": " ",
                        },
                    });


                    $('.add-tooltip').tooltip({
                        container: 'body'
                    });
                    $(".dt-buttons").appendTo("#tbl-btn");
                    //stable_search_manually

                    $('form#search-tbl').submit(function (e) {
                        e.preventDefault();
                        var tblSearch = $('#tbl-search').val();
                        if (tblSearch !== '') {
                            OrderList.search(tblSearch).draw();
                        }
                    });
                    //table_length_manually
                    $('#tbl-length').change(function () {
                        OrderList.page.len($(this).val()).draw();
                    });
                    //reload_tbl
                    $('#tbl-search').keyup(function () {
                        var tblSearch = $('#tbl-search').val();
                        OrderList.search(tblSearch).draw();
                    });
                }
            })
        }
        //reload_tbl
        $('#btn-tbl-reload').click(function () {
            $("#<?php echo $tbl_id;?>").dataTable().fnDestroy();
            datatbl();
        });
        //delete table multi
        function tblDelete(){
            count++;
            if(count === 1){
                $("#selectalldlt").click(function () {
                    if (this.checked) {
                        $('.dsdata').each(function () {
                            $(".dsdata").prop('checked', true).closest('tr').addClass('removeRow');
                        })
                    } else {
                        $('.dsdata').each(function () {
                            $(".dsdata").prop('checked', false).closest('tr').removeClass('removeRow');
                        })
                    }
                    countDltCheck();
                });
                $('.deleteable tr th:nth-child(2)').click(function (event) {
                    if (event.target.type !== 'checkbox') {
                        $(':checkbox', this).trigger('click');
                        countDltCheck();
                    }
                });
            };

            $('.dsdata').click(function (event) {
                if ($(this).is(':checked')) {
                    $(this).closest('tr').addClass('removeRow');
                } else {
                    $(this).closest('tr').removeClass('removeRow');
                }
                countDltCheck();
            });

            function countDltCheck() {
                if ($('.dsdata:checkbox:checked').length > 0) {
                    $('#btn-delete').prop('disabled', false).addClass('btn-danger');
                } else {
                    $('#btn-delete').prop('disabled', true).removeClass('btn-danger');
                }
            }

            $('.pagination a').click(function (event) {
                if ($('#selectalldlt').is(':checked')) {
                    $('#selectalldlt').prop('checked', false);
                    $('#btn-delete').prop('disabled', true).removeClass('btn-danger');
                }
            });
            $('.dsdata').closest('td').addClass('');

        };
        //end delete table multi

        $('#btn-delete').click(function () {
            var checkbox = $('.dsdata:checked');
            if (checkbox.length > 0) {
                var deleteData = [];
                $(checkbox).each(function () {
                    deleteData.push($(this).val());
                });
                Swal.fire( {
                        title: 'Are you sure?', text: "You want to delete selected data.", type: "warning", showCancelButton: !0, confirmButtonColor: "#f44336", cancelButtonColor: "#5e5e5e", confirmButtonText: "Delete", cancelButtonText: 'Cancel', confirmButtonClass: "btn btn-danger", cancelButtonClass: "btn btn-dark ml-1", buttonsStyling: !1, allowOutsideClick: function () {
                            Swal.isLoading()
                        }
                    }
                ).then(function (t) {
                        if(t.value){
                            $.ajax({
                                url: "function/deleteData",
                                method: "POST",
                                dataType: "json",
                                cache: false,
                                data: {deleteData: deleteData, for: '<?php echo $dlt_for;?>'},
                                success: function (res) {
                                    Swal.fire(res.notif_title, res.notif_disc, res.notif_type);

                                    if (res.res === 'true') {
                                        $("#<?php echo $tbl_id;?>").dataTable().fnDestroy();
                                        datatbl();
                                        <?php if(!$edit){ ?>
                                        location.reload();
                                        <?php };?>
                                    }else {
                                        Swal.fire('Oops', 'Something want wrong', 'warning');
                                    }

                                }
                            });
                        }
                    }
                );
            } else {
                    $.notify({
                        // options
                        title: 'Please select data',
                        message: '- Select at least one records'
                    },{
                        // settings
                        type: 'warning',
                        animate: {
                            enter: 'animated bounceInDown',
                            exit: 'animated bounceOutUp'
                        },
                    });
            }
        });
    </script>

  </body>
  <!-- END: Body-->
</html>