
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title">
                <h4 class="bold"><?php echo CommonLanguage::getMessage($IsVendor ? 'purchase_duedate_list' : 'invoice_duedate_list'); ?> </h4>
            </div>
            <div class="row form-group">
                <div class=" col-md-3">
                    <label><?php print CommonLanguage::getMessage('due_date'); ?></label>
                    <?php echo Forms::snippet_rangepicker(); ?>
                </div> 
                <div class="col-md-3 <?php echo $IsVendor ? 'hidden' : '' ?>">
                    <label><?php print CommonLanguage::getMessage('customer_group'); ?></label>
                    <input type="hidden"   name="customer_group_id" id="customer_group_id" class="full-width"/>
                </div>
                <div class="col-md-2">
                    <label><?php echo $IsVendor ? CommonLanguage::getMessage('choose_supplier') : CommonLanguage::getMessage('choose_customer') ?></label>
                    <input id="customer_supplier_id" type="hidden" class="full-width" name="customer_supplier_id" >
                </div> 
                <div class=" col-md-2 <?php echo!$IsVendor ? '' : 'hidden'; ?>">
                    <label><?php print CommonLanguage::getMessage('salesman'); ?></label>
                    <input type="hidden" name="salesman_id" id="salesman_id" class="full-width"/>
                </div>
                <div class=" col-md-2 <?php echo smc_session()['default_setting']['activate_second_currency'] == 1 ? '' : 'hidden' ;?>">
                    <label><?php print CommonLanguage::getMessage('currency'); ?></label>
                    <select id="currency_filter" class="full-width" data-init-plugin="select2">
                        <option value="1" data-symbol="<?php echo smc_session()['local_currency_symbol'] ?>" ><?php echo smc_session()['local_currency_name'] . ' - ' . smc_session()['local_currency_symbol'] ?></option>
                        <option value="2" data-symbol="<?php echo smc_session()['second_currency']['symbol'] ?>"><?php echo smc_session()['second_currency']['name'] . ' - ' . smc_session()['second_currency']['symbol'] ?></option>
                    </select>
                </div>

            </div>
        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12 table-responsive">
                    <table class="table table-bordered inventory" id="tableWithSearch">
                        <thead>
                            <tr>
                                <th class="text-center" style="width:7% !important"><?php echo CommonLanguage::getMessage($IsVendor ? "purchase" : "invoice") ?> #</th>
                                <th class="text-center"><?php print CommonLanguage::getMessage($IsVendor ? 'supplier' : 'customer'); ?></th>
                                <?php if ($IsVendor && !empty(smc_session()['default_setting']['activate_shipment_fields'])) { ?>
                                    <th class="text-center"><?php echo CommonLanguage::getMessage('shipping_nb') ?></th>
                                    <th class="text-center"><?php echo CommonLanguage::getMessage('shipping_date') ?></th>
                                <?php } ?>
                                <th class="text-center"><?php print CommonLanguage::getMessage('Date'); ?></th>
                                <th class="text-center" style="width:7% !important"> <?php print CommonLanguage::getMessage('due_date'); ?></th>
                                <th class="text-center" style="width:7% !important"> <?php print CommonLanguage::getMessage('diff'); ?></th>
                                <th class="text-center"  style="width:10% !important"> <?php print CommonLanguage::getMessage('amount'); ?></th>
                                <?php if ($IsVendor && !empty(smc_session()['default_setting']['activate_shipment_fields'])) { ?>
                                    <th class="text-center"><?php echo CommonLanguage::getMessage('shipping') ?></th>
                                    <th class="text-center"><?php echo CommonLanguage::getMessage('extra_cost') ?></th>
                                    <th class="text-center"><?php echo CommonLanguage::getMessage('net_total') ?></th>
                                <?php } ?>
                                <th class="text-center"  style="width:10% !important"> <?php print CommonLanguage::getMessage('paid_amount'); ?></th>
                                <th class="text-center"  style="width:10% !important"> <?php print CommonLanguage::getMessage('total_return'); ?></th>
                                <th class="text-center"  style="width:10% !important"> <?php print CommonLanguage::getMessage('remaining'); ?></th>
                                <th class="text-center"  style="width:10% !important"> <?php print CommonLanguage::getMessage('total'); ?>&nbsp;(<span class="currency_sym"><?php echo smc_session()['local_currency_symbol'] ?></span>)</th>
                                <th class="text-center no-print" style="width:7% !important"> <?php print CommonLanguage::getMessage('Action'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                        <tfoot>
                        <th></th>
                        <th></th>
                        <?php if ($IsVendor && !empty(smc_session()['default_setting']['activate_shipment_fields'])) { ?>
                            <th></th>
                            <th></th>
                        <?php } ?>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th class="text-center"><strong id="total"></strong><strong class="currency_sym"><?php echo smc_session()['local_currency_symbol'] ?></strong></th>
                        <?php if ($IsVendor && !empty(smc_session()['default_setting']['activate_shipment_fields'])) { ?>
                            <th></th>
                            <th></th>
                            <th></th>
                        <?php } ?>
                        <th class="text-center"><strong id="total_paid"></strong><strong class="currency_sym"><?php echo smc_session()['local_currency_symbol'] ?></strong></th>
                        <th class="text-center"><strong id="total_return"></strong><strong class="currency_sym"><?php echo smc_session()['local_currency_symbol'] ?></strong></th>
                        <th class="text-center"><strong id="total_remaining"></strong><strong class="currency_sym"><?php echo smc_session()['local_currency_symbol'] ?></strong></th>
                        <th></th>
                        <th></th>

                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var IsVendor = <?php echo $IsVendor ? "true" : "false" ?>,
            start_date = <?php echo json_encode(smc_session()['start_date']); ?>,
            local_symbol = <?php echo json_encode(smc_session()['local_currency_symbol']); ?>,
            second_symbol = <?php echo json_encode(smc_session()['second_currency']['symbol']); ?>,
            end_date = <?php echo json_encode(date('Y-m-d')); ?>;
    $(function () {
        build_select2($("#customer_group_id"), "xhr/general_group_Search?group_type=cust");
        build_select2($('#customer_supplier_id'), IsVendor ? 'xhr/supplierSearch' : 'xhr/customerSearch');
        build_select2($("#salesman_id"), "xhr/employeeSearch")

//        rangepicker_js(function () {
//            CallAjaxFunction()
//        },from_date,to_date);
        $("input[name=From_To_Date]").daterangepicker({
            locale: {
                format: 'YYYY-MM-DD'
            }, 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')]
            },
            startDate: start_date,
            endDate: end_date
        }, function (start, end, label) {
            window.startDate = start.format("YYYY-MM-DD"),
                    window.endDate = end.format("YYYY-MM-DD")
            CallAjaxFunction()
        });
        CallAjaxFunction()

        $("#customer_group_id,#customer_supplier_id,#salesman_id,#currency_filter").on('change', function () {
            CallAjaxFunction()
        });

    })
    function CallAjaxFunction() {
        var DataArr = {
            IsVendor: IsVendor ? 1 : 0,
            FromDate: window.startDate ? window.startDate : start_date,
            ToDate: window.endDate ? window.endDate : end_date,
            customer_group_id: $("#customer_group_id").val(),
            person_id: $('#customer_supplier_id').val(),
            salesman_id: $('#salesman_id').val(),
            currency_id: $('#currency_filter').val(),
        }
        $.ajax({
            url: "InventoryAjax/get_total_inv_pur_due_dates",
            data: DataArr,
            dataType: 'json',
            complete: function (v1, v2) {
                if (v2 == "success") {
//                    console.log(v1.responseJSON)
                    RebuildDataTable($('.panel-heading .panel-title'), [0, 1, 2, 3, 4, 5], 'InventoryAjax/DueDateInvoiceListRequest', DataArr, '', [IsVendor ? 14 : 9]);
                    $("#total").html(AddCommas(public_round_number(parseFloat(v1.responseJSON['total']) || 0)));
                    $("#total_paid").html(AddCommas(public_round_number(parseFloat(v1.responseJSON['total_paid']) || 0)));
                    $("#total_return").html(AddCommas(public_round_number(parseFloat(v1.responseJSON['total_return']) || 0)));
                    $("#total_remaining").html(AddCommas(public_round_number(parseFloat(v1.responseJSON['total_remaining']) || 0)));
                    $(".currency_sym").text($('#currency_filter').val() == 1 ? local_symbol : second_symbol)
                    $("#tableWithSearch_processing").hide();
                }
            }
        })

//        RebuildDataTable($('.panel-heading .panel-title'), [0, 1, 2, 3, 4, 5], 'InventoryAjax/DueDateInvoiceListRequest', DataArr)
    }
</script>
