<div class="wrapper">
    <div class="panel invoice">
        <div class="panel-body">
            <h3 class="panel-title bold">
                <?php print $title; ?> 
            </h3>
            <form  target="_blank" action="Inventory/view_total_sold_items_brand" method="post" >
                <div class="row form-group">
                    <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($is_brand ? 'brand_name' : 'category_name'); ?></label>
                        <input type="hidden" class="full-width" name="brand_id" multiple id="brand_id"/>
                    </div>
                    <div class=" col-md-2 <?php echo $permissions->hasPermission('Branches') ? '':'hidden'; ?>">
                        <label><?php print CommonLanguage::getMessage('branch'); ?></label>
                        <input id="branch_id" type="hidden" name="branch_id"  class="full-width"   >
                    </div> 
                    <div class=" col-md-4 input-daterange" id="datepicker-range">
                        <label><?php print CommonLanguage::getMessage('range_date'); ?></label>
                        <input id="From_To_Date" type="text" autocomplete="off" class="form-control" name="From_To_Date" >
                    </div>
                    <div class="col-md-2  pull-right m-t-20">
                        <button type="submit" class="btn btn-primary pull-right"><?php echo CommonLanguage::getMessage('print'); ?></button>
                    </div>
                </div>
                <div class="row form-group">
                    <div class=" col-md-2">
                        <label><?php print CommonLanguage::getMessage('salesman'); ?></label>
                        <input id="sales_man_id" type="hidden" name="sales_man_id"  class="full-width"  >
                    </div>
                    <div class="col-md-1">
                        <div><label><?php print CommonLanguage::getMessage('return'); ?></label></div>
                        <input type="checkbox" id="with_return" name="with_return"/>
                    </div>

                </div>
            </form>
            <div class="row m-t-10">    
                <div class="col-md-12 table-responsive" id="MyTable" >
                    <table class="table table-hover inventory" id="tableWithSearch" >
                        <thead>
                            <tr>
                                <th class="text-center" style="width:5%" ><?php print CommonLanguage::getMessage($is_brand ? 'brand_name' : 'category_name'); ?></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>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                        <tfoot>
                        <th></th>
                        <th><div class="text-center"><span id="total_quatity"></span></div></th>
                        <th><div class="text-center"><span id="total"></span><span id="currency_symbol"><?php echo smc_session()['local_currency_symbol'] ?></span></div></th>
                        <th><div class="text-center"><span id="vat1_total"></span><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><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><span id="currency_symbol"><?php echo smc_session()['local_currency_symbol'] ?></span></div></th>


                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var is_actual = <?php echo json_encode($is_actual); ?>, is_brand = <?php echo json_encode($is_brand ? 1 : 0); ?>;
    $(function () {
        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");
        build_select2($('#sales_man_id'), 'xhr/employeeSearch');

        new Switchery($("#with_return").get(0), {color: $.Pages.getColor("primary")});
        if (is_brand)
            build_select2($("#brand_id"), "xhr/CommonSearch?col_name=brand_name&col_id=brand_id&table=tbl_brand_products", 1);
        else
            build_select2($("#brand_id"), "xhr/CommonSearch?col_name=Cat_Name&col_id=Cat_ID&table=tbl_product_cat", 1);
        var default_end_time = subtractFromTime(default_start_time, 1, 'seconds'),
                twelve_hours = subtractFromTime(default_start_time, 12, 'hours');
        $("input[name=From_To_Date]").daterangepicker({
            timePicker: true,
            locale: {
                format: 'YYYY-MM-DD HH:mm:ss'
            },
            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();
        });
        ActualAjax();
        $("#with_return,#users,#brand_id,#branch_id,#sales_man_id").on("change", function () {
            ActualAjax();
        });
    });
    function ActualAjax() {
        var date_from_to_value = $("input[name=From_To_Date]").val().split(' - ');
        var CurrentDate = {
            with_return: $("#with_return").is(":checked") ? 1 : 0,
            From_Date: window.startDate ? window.startDate : date_from_to_value[0],
            To_Date: window.endDate ? window.endDate : date_from_to_value[1],
            users: $("#users").val(),
            brand_id: $("#brand_id").val(),
            is_actual: is_actual,
            is_brand: is_brand,
            branch_id: $('#branch_id').val(),
            sales_man_id: $('#sales_man_id').val(),
        };
        $.ajax({
            url: "InventoryAjax/get_total_sold_brand_items_totals",
            data: CurrentDate,
            dataType: "json",
            complete: function (v1, v2) {
                if (v2 == "success") {
                    var response = v1.responseJSON;
                    RebuildDataTable($('.panel-body .panel-title').html(), [0, 1, 2], "InventoryAjax/TotalItemsPerBrandAjax", CurrentDate);
                    $("#total_quatity").html(response[0]);
                    $("#total").html(response[1]);
                    $("#vat1_total").html(response[2]);
                    $("#vat2_total").html(response[3]);
                    $("#total_with_vat").html(response[4]);
                }
            }
        })
    }
</script>