<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>
            <div class="row form-group">
                <div class="col-md-12">
                    <div class=" col-md-3 input-daterange" id="datepicker-range-invoice">
                        <label><?php print CommonLanguage::getMessage('Delivery_Date'); ?></label>
                        <input id="From_To_Date" type="text" autocomplete="off" class="form-control" name="From_To_Date" >
                    </div>
                    <div class=" col-md-3">
                        <label><?php print CommonLanguage::getMessage($is_sales ? 'choose_customer' : 'choose_supplier'); ?></label>
                        <input id="customer_id" type="hidden"  class="full-width" name="customer_id" >
                    </div>


                    <div class=" col-md-3 input-daterange" id="datepicker-range">
                        <label><?php print CommonLanguage::getMessage('range_date_created'); ?></label>
                        <input id="created_date" type="text" autocomplete="off" class="form-control" name="created_date" >
                    </div>

                </div>
                <div class="col-md-12 form-group">
                    <div class="col-md-3">
                        <label><?php echo CommonLanguage::getMessage('users'); ?></label>
                        <input type="hidden" class="full-width" name="users" multiple id="users" autocomplete="off"/>
                    </div>
                    <div class=" col-md-3">
                        <label><?php print CommonLanguage::getMessage('branch'); ?></label>
                        <input id="branch_id" name="branch_id" type="hidden"  class="full-width"  >
                    </div>
                    <div class=" col-md-3">
                        <label><?php print CommonLanguage::getMessage('Category'); ?></label>
                        <input type="hidden" name="category" id="category" class="full-width"/>
                    </div>
                    <div class="  col-md-3" >
                        <label><?php echo CommonLanguage::getMessage('status'); ?></label>
                        <select required name="status_id"  class="full-width status_id " id="status_id" data-init-plugin="select2">
                            <option value="0"><?php echo CommonLanguage::getMessage('All'); ?></option>
                            <option value="1" selected><?php echo CommonLanguage::getMessage('pending'); ?></option>
                            <option value="2"><?php echo CommonLanguage::getMessage('converted'); ?></option>
                        </select>
                    </div>
                </div>
            </div>

        </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:10%" ><?php print CommonLanguage::getMessage('quantity'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('Price'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                        <tfoot>
                        <th></th>
                        <th></th>
                        <th></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>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var ViewLang = <?php echo json_encode(CommonLanguage::getMessage('view')); ?>,
            title = <?php echo json_encode(CommonLanguage::getMessage('total_sales_ordered_items')); ?>,
            is_actual = <?php echo json_encode($is_actual); ?>,
            is_sales = <?php echo json_encode($is_sales); ?>;

    $(function () {
        build_select2($("#customer_id"), is_sales ? "xhr/customerSearch" : "xhr/supplierSearch");
        build_select2($("#category"), "xhr/CommonSearch?col_id=Cat_ID&col_name=Cat_Name&table=tbl_product_cat");
        build_select2($("#users"), "xhr/CommonSearch?col_id=User_ID&col_name=User_Name&table=tbl_common_users", 1);
        build_select2($("#branch_id"), "xhr/CommonSearch?col_id=branch_id&col_name=branch_name&table=tbl_organization_branches");

        $("input[name=From_To_Date]").daterangepicker({
//            locale: {
//                format: 'YYYY-MM-DD'
//            },
//            startDate: moment(moment().format('YYYY-MM-DD')),
//            endDate: 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'))]
            }, allowEmpty: true,
            //            autoApply: true,
            autoUpdateInput: false,
            locale: {
                cancelLabel: 'Clear'
            }
        }, function (start, end, label) {
//            window.startDate = start.format("YYYY-MM-DD"),
//                    window.endDate = end.format("YYYY-MM-DD")
//            ActualAjax()
        });
        $('input[name="From_To_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'));
            ActualAjax()
        });
        $('input[name="From_To_Date"]').on('cancel.daterangepicker', function (ev, picker) {
            startDate = ''
            endDate = ''
            $(this).val('');
            ActualAjax()
        });
        $("input[name=created_date]").daterangepicker({
//            locale: {
//                format: 'YYYY-MM-DD'
//            },
//            startDate: moment(moment().format('YYYY-MM-DD')),
//            endDate: 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'))]
            }, allowEmpty: true,
            //            autoApply: true,
            autoUpdateInput: false,
            locale: {
                cancelLabel: 'Clear'
            }
        }, function (start, end, label) {
//            window.start_created = start.format("YYYY-MM-DD"),
//                    window.end_created = end.format("YYYY-MM-DD")
//            ActualAjax()
        });
        $('input[name="created_date"]').on('apply.daterangepicker', function (ev, picker) {
            window.start_created = picker.startDate.format('YYYY-MM-DD'),
                    window.end_created = picker.endDate.format('YYYY-MM-DD');
            $(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
            ActualAjax()
        });
        $('input[name="created_date"]').on('cancel.daterangepicker', function (ev, picker) {
            window.end_created = ''
            window.start_created = ''
            $(this).val('');
            ActualAjax()
        });
        ActualAjax()
        $("#category,#customer_id,#users,#branch_id,#status_id").on("change", function () {
            ActualAjax();
        })
    })
    function ActualAjax() {
//        var From_To_Date = $("input[name=From_To_Date]").val().split(' - ');
//        var from_to_created = $("input[name=created_date]").val().split(' - ');
        var CurrentDate = {
            From_Date: window.startDate ? window.startDate : '',
            To_Date: window.endDate ? window.endDate : '',
            date_created_from: window.start_created ? window.start_created : '',
            date_created_to: window.end_created ? window.end_created : '',
            customer_id: $("#customer_id").val(),
            category: $("#category").val(),
            users: $("#users").val(),
            is_actual: is_actual,
            is_sales: is_sales,
            branch_id: $("#branch_id").val(),
            status_id: $("#status_id").val()
        };
        console.log('CurrentDate')
        console.log(CurrentDate)
//        if (CurrentDate.customer_id != '') {
//            $('#customer_name').val($("#s2id_customer_id span").text());
//        }

//        RebuildDataTable(title, [0, 1, 2], "InventoryAjax/total_ordered_items_report", CurrentDate)
        $.ajax({
            url: "InventoryAjax/get_total_sales_ordered_items_totals",
            data: CurrentDate,
            dataType: "json",
            complete: function (v1, v2) {
                if (v2 == "success") {
                    var response = v1.responseJSON;
//                    console.log(response)
                    RebuildDataTable(title, [0, 1, 2], "InventoryAjax/total_ordered_items_report", CurrentDate)
//                    $("#total_quatity").html(response.total_quantity);
                    $("#total").html(response.total);
                }
            }
        })
    }
</script>