<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-3">
                    <label><?php echo CommonLanguage::getMessage('users'); ?></label>
                    <input type="hidden" class="full-width" name="users" multiple id="users"/>
                </div>
                <div class=" col-md-3 " >
                    <label><?php print CommonLanguage::getMessage('From'); ?></label>
                    <input id="from_month" type="text" autocomplete="off" class="form-control" data-date-format="yyyy-mm" value="<?php echo($year . '-' . $month); ?>">
                </div>
                <div class=" col-md-3 " >
                    <label><?php print CommonLanguage::getMessage('To'); ?></label>
                    <input id="to_month" type="text" autocomplete="off" class="form-control" data-date-format="yyyy-mm" value="<?php echo($year . '-' . $month); ?>">
                </div>

                <div class=" col-md-3">
                    <label><?php print CommonLanguage::getMessage('choose_customer'); ?></label>
                    <input id="customer_id" type="hidden"  class="full-width" name="customer_id" >
                </div>


            </div>
            <div class="row form-group">
                <div class=" col-md-3 <?php echo $permissions->hasPermission('Branches') ? '':'hidden'; ?>">
                    <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('salesman'); ?></label>
                    <input type="hidden" name="sales_man_id" id="sales_man_id" class="full-width"/>
                </div>
                <div class=" col-md-3">
                    <label><?php print CommonLanguage::getMessage('Category'); ?></label>
                    <input type="hidden" name="category_filter" id="category_filter" class="full-width"/>
                </div>
                <div class=" col-md-3">
                    <label><?php print CommonLanguage::getMessage('customer_branch'); ?></label>
                    <input type="hidden" name="customer_branch_id" id="customer_branch_id" class="full-width"/>
                </div>

            </div>
            <div class=" row form-group">
                <div class=" col-md-3">
                    <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-3">
                    <label><?php print CommonLanguage::getMessage('main_supplier'); ?></label>
                    <input type="hidden" name="supplier_id" id="supplier_id" class="full-width"/>
                </div> 
                <div class=" col-md-3">
                    <label><?php print CommonLanguage::getMessage('brand'); ?></label>
                    <input type="hidden" name="brand_id" id="brand_id" class="full-width"/>
                </div>
                <div class=" col-md-3  <?php echo smc_session()['default_setting']['activate_second_currency'] == 1 ? '':'hidden'; ?>">
                    <label><?php print CommonLanguage::getMessage('currency'); ?></label>
                    <select id="currency_id" class="full-width" data-init-plugin="select2">
                        <option value="1"><?php echo smc_session()['local_currency_name'] . ' - ' . smc_session()['local_currency_symbol'] ?></option>
                        <option value="2"><?php echo smc_session()['second_currency']['name'] . ' - ' . smc_session()['second_currency']['symbol'] ?></option>
                    </select>
                </div>
                <div class=" col-md-3 m-t-25 pull-right">
                    <button class="btn btn-primary pull-right" id="generate_report"><?php echo CommonLanguage::getMessage('generate_report') ?></button>
                </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 class="main-head1">
                                <th><?php echo CommonLanguage::getMessage('Code'); ?></th>
                                <th rowspan="2"><?php echo CommonLanguage::getMessage('Name'); ?></th>
                                <th rowspan="2"><?php echo CommonLanguage::getMessage('UOM'); ?></th>
                            </tr>
                            <tr class="main-head2">
                                <th colspan="3"></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>

                    </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); ?>;
    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($("#supplier_id"), "xhr/supplierSearch");
        build_select2($("#brand_id"), "xhr/CommonSearch?col_id=brand_id&col_name=brand_name&table=tbl_brand_products");
        build_select2($("#users"), "xhr/CommonSearch?col_id=User_ID&col_name=User_Name&table=tbl_common_users", 1);
        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");


        $("#from_month,#to_month").datepicker({autoclose: true,
            viewMode: "months",
            minViewMode: "months"})
        ActualAjax()
//        $("#with_return,#users,#sales_man_id,#branch_id,#category_filter,#customer_branch_id,#customer_group_id").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();
//        });
        $("#generate_report").on("click", function () {
            ActualAjax();
        })
    });

    function ActualAjax() {
        var CurrentDate = {
            from_month: $("#from_month").val(),
            to_month: $("#to_month").val(),
            users: $("#users").val(),
            customer_id: $("#customer_id").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(),
            currency_id: $("#currency_id").val(),
            supplier_id: $("#supplier_id").val(),
            brand_id: $("#brand_id").val(),
        };
        var resultList = [],
                date = new Date($("#from_month").val() + '-15 14:00:00'),
                endDate = new Date($("#to_month").val() + '-15 14:00:00'),
                monthNameList = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
        var tr1 = '';
        var tr2 = '';
        var all_cols = [0, 1, 2];
        while (date <= endDate) {
            var stringDate = monthNameList[date.getMonth()] + " " + date.getFullYear();
            resultList.push(stringDate);
            tr1 += '<th colspan="2" class="dynamic-header-month">' + stringDate + '</th>';
            tr2 += '<th class="dynamic-header-month"><?php echo CommonLanguage::getMessage('qty') ?></th>' +
                    '<th class="dynamic-header-month"><?php echo CommonLanguage::getMessage('Price') ?></th>';
            date.setMonth(date.getMonth() + 1);
            all_cols.push(all_cols.length);
        }
        $("#tableWithSearch thead th.dynamic-header-month").remove();
        $(tr1).insertAfter($("#tableWithSearch thead .main-head1 th").last());
        $(tr2).insertAfter($("#tableWithSearch thead .main-head2 th").last());
        //$("#tableWithSearch thead").empty().append($(tr1));
        //$("#tableWithSearch thead").append($(tr2));
        //
        var mnthly_sls_list_dt = RebuildDataTable(title, all_cols, "InventoryAjax/monthly_sales_report_ajax", CurrentDate, '#tableWithSearch', '', {hide_search_row: true, ajax: {type: "post"}})
        mnthly_sls_list_dt.columns().visible(true);
    }
</script>