<style>
.dataTables_length{
    color:black !important;
}
.postponedListTable_length{
    color:black !important;

}

</style>
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <h4 class="bold"><?php print $title; ?> </h4>
        </div>
        <div class="panel-body">
            <input type="hidden" id="is_order" value="<?php echo $is_order ? 1 : 0; ?>" />
            <div class="row form-group">
                <div  class="col-md-1 ">
                    <label><?php print CommonLanguage::getMessage('invoice_nb'); ?></label>
                    <input id="post_list_nb"  type="text" class="full-width form-control"  />
                </div>
                <div  class="col-md-2 ">
                    <label><?php print CommonLanguage::getMessage('customer'); ?></label>
                    <input id="post_cust"  type="hidden" class="full-width"  />
                </div>
                <div  class="col-md-2 <?php echo (!$is_order && smc_session()['default_setting']['add_pos_customer'] != 0) ? '' : 'hidden' ?>">
                    <label><?php print CommonLanguage::getMessage('road_customer'); ?></label>
                    <input id="post_pos_cust"  type="hidden" class="full-width"  />
                </div>
                <div  class="col-md-2 <?php echo!$is_order ? '' : 'hidden'; ?>">
                    <label ><?php print CommonLanguage::getMessage('payment_status'); ?></label>
                    <select id="post_status" class="full-width" multiple data-init-plugin="select2">
                        <!--<option value="all"><?php echo CommonLanguage::getMessage('all') ?></option>-->
                        <option value="0"><?php echo CommonLanguage::getMessage('unpaid') ?></option>
                        <option value="1"><?php echo CommonLanguage::getMessage('partially_paid') ?></option>
                        <option value="2"><?php echo CommonLanguage::getMessage('paid') ?></option>
                        <option value="3"><?php echo CommonLanguage::getMessage('park') ?></option>
                    </select>
                </div>
                <div  class="col-md-2 <?php echo!$is_order ? '' : 'hidden'; ?>">
                    <label><?php print CommonLanguage::getMessage('delivery'); ?></label>
                    <select id="post_is_delivery" class="full-width" data-init-plugin="select2">
                        <option value="all"><?php echo CommonLanguage::getMessage('all') ?></option>
                        <option value="0"><?php echo CommonLanguage::getMessage('no') ?></option>
                        <option value="1"><?php echo CommonLanguage::getMessage('yes') ?></option>
                    </select>
                </div>
                <div  class="col-md-3  ">
                    <label><?php print CommonLanguage::getMessage('Date'); ?></label>
                    <input id="post_list_date" name="post_list_date" class="form-control" autocomplete="off"/>
                </div>
            </div>
            <div class="row form-group">
                <div  class="col-md-3 ">
                    <label><?php print CommonLanguage::getMessage('posponted_status') ?></label>
                    <select class="full-width" id="posponted_status_filter" data-init-plugin="select2">
                        <option value="" ><?php echo CommonLanguage::getMessage('select') ?></option>
                        <option value="1" ><?php echo CommonLanguage::getMessage('Request') ?></option>
                        <option value="2" ><?php echo CommonLanguage::getMessage('production_line') ?></option>
                        <option value="3" ><?php echo CommonLanguage::getMessage('production_done') ?></option>
                        <option value="4" ><?php echo CommonLanguage::getMessage('delivered') ?></option>
                        <option value="5" ><?php echo CommonLanguage::getMessage('on_request') ?></option>
                        <option value="6" ><?php echo CommonLanguage::getMessage('canceled') ?></option>
                    </select>
                </div>
                <div  class="col-md-3 ">
                    <label><?php print CommonLanguage::getMessage('posponted_date'); ?></label>
                    <input id="post_list_due_date" name="post_list_due_date" class="form-control" autocomplete="off" />
                </div>
                <div  class="col-md-2 <?php echo!$is_order ? '' : 'hidden'; ?>">
                    <label><?php print CommonLanguage::getMessage('committed') . ' ?'; ?></label>
                    <select id="post_is_committed" class="full-width" data-init-plugin="select2">
                        <option value="all"><?php echo CommonLanguage::getMessage('All') ?></option>
                        <option value="0"><?php echo CommonLanguage::getMessage('no') ?></option>
                        <option value="1"><?php echo CommonLanguage::getMessage('yes') ?></option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label><?php echo CommonLanguage::getMessage('choose_branch') ?></label>
                    <input id="current_branch_id" type="hidden" class="full-width" name="current_branch_id" >
                </div>
            </div>
            <div class="row form-group m-t-10">
                <div class="col-md-12 table-repsonsive">
                    <table class="table table-hover demo-table-search full-width inventory" id="postponedListTable" >
                        <thead>
                            <tr>
                                <th class="text-center" style="width:2%">#</th>
                                <th class="text-center" style="width:5%" ><?php echo CommonLanguage::getMessage('customer'); ?></th>
                                <th class="text-center" style="width:5%" ><?php echo CommonLanguage::getMessage('Date'); ?></th>
                                <th class="text-center" style="width:5%" ><?php echo CommonLanguage::getMessage('postponed_date_time'); ?></th>
                                <th class="text-center" style="width:5%" ><?php echo CommonLanguage::getMessage('note'); ?></th>
                                <th class="text-center" style="width:5%" ><?php echo CommonLanguage::getMessage('total'); ?></th>
                                <?php if (!$is_order) { ?>
                                    <th class="text-center" style="width:5%" ><?php echo CommonLanguage::getMessage('paid_amount'); ?></th>
                                    <th class="text-center" style="width:5%" ><?php echo CommonLanguage::getMessage('remaining'); ?></th>
                                <?php } ?>
                                <th class="text-center" style="width:5%" ><?php echo CommonLanguage::getMessage('posponted_status'); ?></th>
                                <th class="text-center" style="width:5%" ><?php echo CommonLanguage::getMessage('Action'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                        <tfoot>
                        <th></th><th></th><th></th><th></th><th></th>
                        <th ><div class="text-center"><strong><span id="total" style="color:#000;"></span>&nbsp;<span id="currency_symbol" style="color:#000;"><?php echo smc_session()['local_currency_symbol'] ?></span></strong></div></th>
                        <?php if (!$is_order) { ?>
                            <th ><div class="text-center"><strong><span id="post_paid_total" style="color:#000;"></span>&nbsp;<span id="currency_symbol" style="color:#000;"><?php echo smc_session()['local_currency_symbol'] ?></span></strong></div></th>
                            <th ><div class="text-center"><strong><span id="post_rem_total" style="color:#000;"></span>&nbsp;<span id="currency_symbol" style="color:#000;"><?php echo smc_session()['local_currency_symbol'] ?></span></strong></div></th>
                        <?php } ?>
                        <th></th>
                        <th></th>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>




<div class="modal fade" id="inv_postpone_status_modal" data-backdrop="static" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm" >
        <div class="modal-content ">
            <div class="modal-header text-left">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="bold"><?php echo CommonLanguage::getMessage('change_status'); ?></h5>
            </div>
            <div class="modal-body">
                <div id="change_postpone_alert"></div>
                <form id="change-postpone-form" action="javascript:void(0);">
                    <input type="hidden" name="change_status_origin" id="change_status_origin" />
                    <div class="row form-group">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('posponted_status') ?></label>
                            <select class="full-width" id="change_inv_postpone_status" data-init-plugin="select2">
                                <option value="0" ><?php echo CommonLanguage::getMessage('select') ?></option>
                                <option value="1" ><?php echo CommonLanguage::getMessage('Request') ?></option>
                                <option value="2" ><?php echo CommonLanguage::getMessage('production_line') ?></option>
                                <option value="3" ><?php echo CommonLanguage::getMessage('production_done') ?></option>
                                <option value="4" ><?php echo CommonLanguage::getMessage('delivered') ?></option>
                                <option value="5" ><?php echo CommonLanguage::getMessage('on_request') ?></option>
                                <option value="6" ><?php echo CommonLanguage::getMessage('canceled') ?></option>

                            </select> 
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('note') ?></label>
                            <textarea id="status_postponed_note"  name="status_postponed_note"  class="form-control"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger text-white " data-dismiss="modal" ><?php print CommonLanguage::getMessage('Close') ?></button>
                <button type="button" class="btn  btn-info text-white " id="change_inv_postpone" ><?php print CommonLanguage::getMessage('Save') ?></button>
            </div>
            <?php echo Forms::ModalLoader('change_status_loader'); ?>
        </div>
    </div>
</div>
<script>
    var is_actual = <?php echo $is_actual ? "true" : "false"; ?>;
    var today = <?php echo json_encode($today) ?>, startDate = '', endDate = '',
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
            start_date = <?php echo json_encode(smc_session()['start_date']); ?>;
    $(function () {

        build_select2($("#post_cust"), "xhr/customerSearch")
        build_select2($("#post_pos_cust"), "xhr/poscustomerSearch")
        build_select2($("#current_branch_id"), "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches");

        rebuild_post_table()

        $("input[name=post_list_date],input[name=post_list_due_date]").daterangepicker({
            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            }, allowEmpty: true,
            //            autoApply: true,
            autoUpdateInput: false,
            locale: {
                cancelLabel: 'Clear'
            }
        }, function (start, end, label) {

        });
        $('input[name="post_list_date"]').on('apply.daterangepicker', function (ev, picker) {
            window.startDate = picker.startDate.format('YYYY-MM-DD'),
                    window.endDate = picker.endDate.format('YYYY-MM-DD');
            $(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
            rebuild_post_table()
        });
        $('input[name="post_list_date"]').on('cancel.daterangepicker', function (ev, picker) {
            startDate = ''
            endDate = ''
            $(this).val('');
            rebuild_post_table()
        });
        $('input[name="post_list_due_date"]').on('apply.daterangepicker', function (ev, picker) {
            window.startDueDate = picker.startDate.format('YYYY-MM-DD'),
                    window.endDueDate = picker.endDate.format('YYYY-MM-DD');
            $(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
            rebuild_post_table()
        });
        $('input[name="post_list_due_date"]').on('cancel.daterangepicker', function (ev, picker) {
            startDueDate = ''
            endDueDate = ''
            $(this).val('');
            rebuild_post_table()
        });
        $("#postponedListTable").on("click", ".change_postpone_status", function () {
            var status = $(this).attr("data-status");
            $("#status_postponed_note").val($('td:eq(4)', $(this).parents('tr')).html())
            $("#change_inv_postpone_status").val(status).select2();
            $('#change_status_origin').val($(this).attr('data-origin'))
            $("#inv_postpone_status_modal").attr("data-inv", $(this).parents(".Action").attr("data-id"))
            $("#inv_postpone_status_modal").modal("show")
        })
        $("#change_inv_postpone").on("click", function () {
            $("#change_status_loader").removeClass("hidden")
            $("#change_postpone_alert").addClass('hidden');
            $.ajax({
                url: $('#change_status_origin').val() == 'inv' ? "POSAjax/update_inv_postpone_status" : "InventoryAjax/update_order_postpone_status",
                method: "POST",
                data: {status: $("#change_inv_postpone_status").val(), id: $("#inv_postpone_status_modal").attr("data-inv"), postponed_note: $('#status_postponed_note').val()},
                complete: function (v1, v2) {
                    if (v2 == 'success') {
                        AddAlert('#change_postpone_alert', Submitted_Successfully, 'success')
                        hideModalAlert('inv_postpone_status_modal', 'change_postpone_alert');
                        //                        $("#change_postpone_alert").attr("class", "alert alert-success").html(Submitted_Successfully)
                        rebuild_post_table();
                    } else {
                        $("#change_postpone_alert").attr("class", "alert alert-danger").html(Failed_Submit)

                    }
                    $("#change_status_loader").addClass("hidden")


                }
            })
        });
        $("#post_cust,#post_status,#post_is_delivery,#post_pos_cust,#posponted_status_filter,#post_is_committed,#current_branch_id").on("change", function () {
            rebuild_post_table()
        })
        $("#post_list_nb").on("keyup", function () {
            rebuild_post_table()
        })
        $("#postponedListTable").on("click", ".printInv", function () {
            var myurl = "Inventory/pos_view_invoice/" + $(this).parents('.Action').attr("data-id") + '?is_posponed=1';
            printExternal(myurl)
        });

    })

    function rebuild_post_table() {
        var inv_data = {
            IsVendor: false,
            customer_supplier_id: $("#post_cust").val(),
            invoice_status: $("#post_status").val(),
            inv_is_delivered: $("#post_is_delivered").val(),
            inv_list_nb: $("#post_list_nb").val(),
            is_delivery: $("#post_is_delivery").val(),
            Date_From: window.startDate ? window.startDate : start_date,
            Date_To: window.endDate ? window.endDate : today,
            due_date_from: window.startDueDate,
            due_date_to: window.endDueDate,
            pos_customer: $("#post_pos_cust").val(),
            is_actual: is_actual ? 1 : 0,
            posponted_status: $("#posponted_status_filter").val(),
            is_committed: $("#post_is_committed").val(),
            type: 2,
            from_inventory: 1,
            branch_id: $("#current_branch_id").val()
        }
        if ($('#is_order').val() == 1) {
            $.ajax({
                url: "InventoryAjax/get_postponed_orders_total",
                data: inv_data,
                dataType: 'json',
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        RebuildDataTable('Postponed order', [0, 1, 2, 3, 4], "InventoryAjax/PostponedOrderListByLoggedUser", inv_data, "#postponedListTable", '', {"bSort": false});
                        $("#postponedListTable #total").html(AddCommas(parseFloat(v1.responseJSON['net_total'] || 0).toFixed(3)));
                    }
                }
            });

        } else {
            $.ajax({
                url: "POSAjax/get_sales_invoices_total",
                data: inv_data,
                dataType: 'json',
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        RebuildDataTable('Postponed invoices', [0, 1, 2, 3, 4], "POSAjax/PostponedListByLoggedUser", inv_data, "#postponedListTable", '', {"bSort": false});
                        $("#postponedListTable #total").html(AddCommas(parseFloat(v1.responseJSON['net_total'] || 0).toFixed(3)));
                        $("#postponedListTable #post_paid_total").html(AddCommas(parseFloat(v1.responseJSON['paid_amount'] || 0).toFixed(3)));
                        $("#postponedListTable #post_rem_total").html(AddCommas(parseFloat(v1.responseJSON['remaining'] || 0).toFixed(3)));
                    }
                }
            });
        }
    }

</script>