<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 $title; ?> </h4>
            </div>
            <form  target="_blank" action="Inventory/view_total_sold_items" method="post" >
                <input type="hidden" name='is_day' id='is_day' value='0'/>
                <div class="col-md-2">
                    <input type="hidden" id="customer_name" name="customer_name"/>
                </div>
                <div class="row form-group">
                    <div class="col-md-10">
                        <div class="col-md-2">
                            <label><?php echo CommonLanguage::getMessage('users'); ?></label>
                            <input type="hidden" class="full-width" name="users" multiple id="users"/>
                        </div>
                        <div class="col-md-2">
                            <label><?php echo CommonLanguage::getMessage('Barcode'); ?></label>
                            <input type="hidden" class="full-width" name="barcode"  id="barcode"/>
                        </div>

                        <div class=" col-md-3 input-daterange" id="datepicker-range-invoice">
                            <label><?php print CommonLanguage::getMessage('range_date_invoice'); ?></label>
                            <input id="From_To_Date_invoice" type="text" autocomplete="off" class="form-control" name="From_To_Date_invoice" >
                        </div>
                        <div class=" col-md-3 input-daterange" id="datepicker-range">
                            <label><?php print CommonLanguage::getMessage('range_date_created'); ?></label>
                            <input id="From_To_Date" type="text" autocomplete="off" class="form-control" name="From_To_Date" >
                        </div>
                        <div class=" col-md-2">
                            <label><?php print CommonLanguage::getMessage('choose_customer'); ?></label>
                            <input id="customer_id" type="hidden"  class="full-width" name="customer_id" >
                        </div>
                        <div class="col-md-2">
                            <div><label><?php print CommonLanguage::getMessage('return'); ?></label></div>
                            <input type="checkbox" id="with_return" name="with_return"/>
                        </div>
                    </div>
                    <div class="col-md-2 m-t-10">
                        <div class="col-md-6  m-t-10">
                            <button type="submit" class="btn btn-primary pull-right" name='per_day' onclick="ClckBtn(this)"><?php echo CommonLanguage::getMessage('print_per_days'); ?></button>
                        </div>
                        <div class="col-md-6  m-t-10">
                            <button type="submit" class="btn btn-primary pull-right" name='all' onclick="ClckBtn(this)"><?php echo CommonLanguage::getMessage('print'); ?></button>
                        </div>
                    </div>
                </div>
                <div class="row form-group">
                    <div class=" col-md-2">
                        <label><?php print CommonLanguage::getMessage('branch'); ?></label>
                        <input id="branch_id" name="branch_id" type="hidden"  class="full-width"  >
                    </div>
                    <div class=" col-md-2">
                        <label><?php print CommonLanguage::getMessage('salesman'); ?></label>
                        <input type="hidden" name="sales_man_id" id="sales_man_id" class="full-width"/>
                    </div>
                    <div class=" col-md-2">
                        <label><?php print CommonLanguage::getMessage('Category'); ?></label>
                        <input type="hidden" name="category_filter" id="category_filter" class="full-width"/>
                    </div>
                    <div class=" col-md-2">
                        <label><?php print CommonLanguage::getMessage('customer_branch'); ?></label>
                        <input type="hidden" name="customer_branch_id" id="customer_branch_id" class="full-width"/>
                    </div>
                    <div class=" col-md-2">
                        <label><?php print CommonLanguage::getMessage('customer_group'); ?></label>
                        <input type="hidden" name="customer_group_id" id="customer_group_id" class="full-width"/>
                    </div>
                </div>
            </form>
        </div>
        <div class="panel-body">
            <div class="row ">    
                <div class="col-md-12 table-responsive" id="MyTable" >
                    <table class="table table-bordered inventory" id="tableWithSearch" >
                        <thead>
                            <tr>
                                <th class="text-center" style="width:5%" ><?php print CommonLanguage::getMessage('Name'); ?></th>
                                <th class="text-center" style="width:5%" ><?php print CommonLanguage::getMessage('Code'); ?></th>
                                <th class="text-center" style="width:5%" ><?php print CommonLanguage::getMessage('barcode'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('quantity'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('Price'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('vat'); ?></th>
                                <?php if (smc_session()['default_setting']['activate_second_vat'] == 1) { ?>
                                    <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('vat2_amount'); ?></th>
                                <?php } ?>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('total_amount'); ?></th>
                                <?php if($permissions->hasPermission('effect_qty_link_product') == 1){ ?>
                                    <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('diesel'); ?></th>
                                <?php } ?>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                        <tfoot>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th><div class="text-center"><span id="total_quatity"></span></div></th>
                        <th><div class="text-center"><span id="total"></span>&nbsp;<span id="currency_symbol"><?php echo smc_session()['local_currency_symbol'] ?></span></div></th>
                        <th><div class="text-center"><span id="vat1_total"></span>&nbsp;<span id="currency_symbol"><?php echo smc_session()['local_currency_symbol'] ?></span></div></th>
                        <?php if (smc_session()['default_setting']['activate_second_vat'] == 1) { ?>
                            <th><div class="text-center"><span id="vat2_total"></span>&nbsp;<span id="currency_symbol"><?php echo smc_session()['local_currency_symbol'] ?></span></div></th>
                        <?php } ?>
                        <th><div class="text-center"><span id="total_with_vat"></span>&nbsp;<span id="currency_symbol"><?php echo smc_session()['local_currency_symbol'] ?></span></div></th>
                        <?php if($permissions->hasPermission('effect_qty_link_product') == 1){ ?>
                            <th><div class="text-center"><span id="total_diesel"></span>&nbsp; </div></th>
                        <?php } ?>
                    </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var ViewLang = <?php echo json_encode(CommonLanguage::getMessage('view')); ?>;
    var title = <?php echo json_encode(CommonLanguage::getMessage('invoice_per_time')); ?>;
    var today = <?php echo json_encode($today) ?>;
    var is_actual = <?php echo json_encode($is_actual); ?>,
            datatable_msg =<?php echo json_encode(isset(smc_session()['datatable_header_temp']['total_sold_items']) ? smc_session()['datatable_header_temp']['total_sold_items'] : null) ?>;

    function ClckBtn(who) {
        var name = $(who).attr('name');
        if (name != 'all')
            $('#is_day').val(1);
        else
            $('#is_day').val(0);
    }
    $(function () {
        var default_end_time = subtractFromTime(default_start_time, 1, 'seconds'),
                twelve_hours = subtractFromTime(default_start_time, 12, 'hours');
        build_select2($("#users"), "xhr/CommonSearch?col_id=User_ID&col_name=User_Name&table=tbl_common_users", 1);
        build_select2($("#barcode"), "xhr/CommonSearch?col_id=Product_PLU&col_name=Product_PLU&table=tbl_stock_products");
        build_select2($("#customer_id"), "xhr/CommonSearch?col_id=Customer_ID&col_name=Customer_Name&table=tbl_customers");
        build_select2($("#branch_id"), "xhr/CommonSearch?col_id=branch_id&col_name=branch_name&table=tbl_organization_branches");
        build_select2($("#customer_group_id"), "xhr/general_group_Search?group_type=cust");
        build_select2($('#sales_man_id'), 'xhr/employeeSearch');
        build_select2($('#customer_branch_id'), 'xhr/searchCustomerBranch?customer_id=' + $("#customer_id").val());
        build_select2($("#category_filter"), "xhr/CommonSearch?col_name=Cat_Name&col_id=Cat_ID&table=tbl_product_cat");

        new Switchery($("#with_return").get(0), {color: $.Pages.getColor("primary")});
        $("input[name=From_To_Date]").daterangepicker({
            timePicker: true,
            autoUpdateInput: false,
            locale: {
                format: 'YYYY-MM-DD HH:mm:ss',
                cancelLabel: 'Clear'
            },
            startDate: moment(moment().format('YYYY-MM-DD') + ' ' + default_start_time),
            endDate: moment(moment().add(1, 'days').format('YYYY-MM-DD') + ' ' + default_end_time),
            ranges: {
                'Today': [moment(moment(new Date().getTime()).format('YYYY-MM-DD') + ' ' + default_start_time),
                    moment(moment(new Date().getTime()).format('YYYY-MM-DD') + ' ' + default_end_time)],
                'Yesterday': [moment(moment().subtract(1, 'days').format('YYYY-MM-DD') + ' ' + default_start_time),
                    moment(moment().subtract(1, 'days').format('YYYY-MM-DD') + ' ' + default_end_time)],
                'Last 7 Days': [moment(moment().subtract(6, 'days').format('YYYY-MM-DD') + ' ' + default_start_time),
                    moment(moment(new Date().getTime()).format('YYYY-MM-DD') + ' ' + default_end_time)],
                'Last 30 Days': [moment(moment().subtract(29, 'days').format('YYYY-MM-DD') + ' ' + default_start_time),
                    moment(moment(new Date().getTime()).format('YYYY-MM-DD') + ' ' + default_end_time)],
                'This Month': [moment(moment().startOf('month').format('YYYY-MM-DD') + ' ' + default_start_time),
                    moment(moment().endOf('month').format('YYYY-MM-DD') + ' ' + default_end_time)],
                'Last Month': [moment(moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD') + ' ' + default_start_time),
                    moment(moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD') + ' ' + default_end_time)]
            }
        }, function (start, end, label) {
            //            window.startDate = start.format("YYYY-MM-DD HH:mm:ss"),
            //                    window.endDate = end.format("YYYY-MM-DD HH:mm:ss")
            //            ActualAjax()
        });
        $('input[name=From_To_Date]').on('apply.daterangepicker', function (ev, picker) {
            $(this).val(picker.startDate.format('YYYY-MM-DD HH:mm:ss') + ' - ' + picker.endDate.format('YYYY-MM-DD HH:mm:ss'));
            ActualAjax()
        });
        $('input[name=From_To_Date]').on('cancel.daterangepicker', function (ev, picker) {
            $(this).val('');
            ActualAjax();
        });
        $("input[name=From_To_Date_invoice]").daterangepicker({
            locale: {
                format: 'YYYY-MM-DD'
            },
            startDateinvoice: moment(moment().format('YYYY-MM-DD')),
            endDateinvoice: moment(moment().add(1, 'days').format('YYYY-MM-DD')),
            ranges: {
                'Today': [moment(moment(new Date().getTime()).format('YYYY-MM-DD')),
                    moment(moment(new Date().getTime()).format('YYYY-MM-DD'))],
                'Yesterday': [moment(moment().subtract(1, 'days').format('YYYY-MM-DD')),
                    moment(moment().subtract(1, 'days').format('YYYY-MM-DD'))],
                'Last 7 Days': [moment(moment().subtract(6, 'days').format('YYYY-MM-DD')),
                    moment(moment(new Date().getTime()).format('YYYY-MM-DD'))],
                'Last 30 Days': [moment(moment().subtract(29, 'days').format('YYYY-MM-DD')),
                    moment(moment(new Date().getTime()).format('YYYY-MM-DD'))],
                'This Month': [moment(moment().startOf('month').format('YYYY-MM-DD')),
                    moment(moment().endOf('month').format('YYYY-MM-DD'))],
                'Last Month': [moment(moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD')),
                    moment(moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD'))]
            }
        }, function (start, end, label) {
            window.startDateinvoice = start.format("YYYY-MM-DD"),
                    window.endDateinvoice = end.format("YYYY-MM-DD")
            ActualAjax()
        });
        ActualAjax()
        $("#with_return,#users,#sales_man_id,#branch_id,#category_filter,#customer_branch_id,#customer_group_id,#barcode").on("change", function () {
            ActualAjax();
        });
        $('#customer_id').on('change', function () {
            $('#customer_branch_id').val('');
            build_select2($('#customer_branch_id'), 'xhr/searchCustomerBranch?customer_id=' + $("#customer_id").val());
            ActualAjax();
        });
    })
    function ActualAjax() {
        var date_from_to_value = $("input[name=From_To_Date]").val().split(' - ');
        var From_To_Date_invoice = $("input[name=From_To_Date_invoice]").val().split(' - ');
        var CurrentDate = {
            with_return: $("#with_return").is(":checked") ? 1 : 0,
            From_Date: date_from_to_value[0] == undefined ? '' : date_from_to_value[0],
            To_Date: date_from_to_value[1] == undefined ? '' : date_from_to_value[1],
            From_Date_invoice: window.startDateinvoice ? window.startDateinvoice : From_To_Date_invoice[0],
            To_Date_invoice: window.endDateinvoice ? window.endDateinvoice : From_To_Date_invoice[1],
            users: $("#users").val(),
            barcode: $("#barcode").val(),
            customer_id: $("#customer_id").val(),
            is_day: $("#is_day").val(),
            is_actual: is_actual,
            sales_man_id: $("#sales_man_id").val(),
            customer_branch_id: $('#customer_branch_id').val(),
            category_id: $("#category_filter").val(),
            branch_id: $("#branch_id").val(),
            customer_group_id: $("#customer_group_id").val()
        };
        if (CurrentDate.customer_id != '') {
            $('#customer_name').val($("#s2id_customer_id span").text());
        }
        $.ajax({
            url: "InventoryAjax/get_total_sold_items_totals",
            data: CurrentDate,
            dataType: "json",
            complete: function (v1, v2) {
                if (v2 == "success") {
                    var response = v1.responseJSON;
                    console.log('response')
                    console.log(response)
                    var msg = '';
//                    console.log(CurrentDate.From_Date_invoice+'==='+CurrentDate.To_Date_invoice)
                    if (datatable_msg != null) {
                        msg = datatable_msg.replace("[[from_date]]", CurrentDate.From_Date_invoice)
                        msg = msg.replace("[[to_date]]", CurrentDate.To_Date_invoice)
                        msg = msg.replace("[[date_class]]", CurrentDate.From_Date_invoice != '' && CurrentDate.To_Date_invoice != '' ? '' : 'hidden')

                        msg = msg.replace("[[from_date_created]]", CurrentDate.From_Date)
                        msg = msg.replace("[[to_date_created]]", CurrentDate.To_Date)
                        msg = msg.replace("[[date_created_class]]", CurrentDate.From_Date != '' && CurrentDate.To_Date != '' ? '' : 'hidden')

                        var users_text = '';
                        if ($("#users").val() != '' && $("#users").select2('data') != null) {
                            $.each($("#users").select2('data'), function (key, val) {
                                users_text += ((users_text == '' ? '' : ', ') + val['text']);
                            })
                        }
                        msg = msg.replace("[[users]]", users_text)
                        msg = msg.replace("[[users_class]]", users_text != '' ? '' : 'hidden')

                        if ($("#customer_id").val() != '')
                            msg = msg.replace("[[customer]]", $("#customer_id").select2("data").text)
                        msg = msg.replace("[[customer_class]]", $("#customer_id").select2("data") != null ? '' : 'hidden')
                        if ($("#branch_id").val() != '')
                            msg = msg.replace("[[branch]]", $("#branch_id").select2("data").text)
                        msg = msg.replace("[[branch_class]]", $("#branch_id").select2("data") != null ? '' : 'hidden')
                        if ($("#sales_man_id").val() != '')
                            msg = msg.replace("[[salesman]]", $("#sales_man_id").select2("data").text)
                        msg = msg.replace("[[salesman_class]]", $("#sales_man_id").select2("data") != null ? '' : 'hidden')
                        if ($("#category_filter").val() != '')
                            msg = msg.replace("[[category]]", $("#category_filter").select2("data").text)
                        msg = msg.replace("[[category_class]]", $("#category_filter").select2("data") != null ? '' : 'hidden')


                        if ($("#customer_branch_id").val() != '')
                            msg = msg.replace("[[customer_branch]]", $("#customer_branch_id").select2("data").text)
                        msg = msg.replace("[[customer_branch_class]]", $("#customer_branch_id").select2("data") != null ? '' : 'hidden')
                        if ($("#customer_group_id").val() != '')
                            msg = msg.replace("[[customer_group]]", $("#customer_group_id").select2("data").text)
                        msg = msg.replace("[[customer_group_class]]", $("#customer_group_id").select2("data") != null ? '' : 'hidden')
                    }
                    RebuildDataTable(title, [0, 1, 2], "InventoryAjax/TotalItemsPerDateAjax", CurrentDate, '', [], {msg: msg})
                    $("#total_quatity").html(response.total_quantity);
                    $("#total").html(response.sub_total);
                    $("#vat1_total").html(response.vat1_total);
                    $("#vat2_total").html(response.vat2_total);
                    $("#total_with_vat").html(response.total_with_vat);
                    $("#total_diesel").html(response.total_qty_link);
            }
            }
        })
    }
</script>