<style>
    .bg-gray{
        background-color: #e5e5e5 !important;
    }
</style>
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print $title ?></div>
            <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-3 input-daterange" id="datepicker-range-invoice">
                    <label><?php print CommonLanguage::getMessage('range_date_invoice'); ?></label>
                    <input id="From_To_Date" type="text" autocomplete="off" class="form-control" name="From_To_Date" >
                </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 class=" col-md-2">
                    <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-2 <?php echo smc_session()['default_setting']['activate_second_currency'] == 1 ? '':'hidden'; ?>" >
                    <label><?php echo 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>
            <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 <?php echo!$is_sales ? 'hidden' : '' ?>">
                    <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 echo CommonLanguage::getMessage('type') ?></label>
                    <select id="type" class="full-width" data-init-plugin="select2">
                        <option value="0"><?php echo CommonLanguage::getMessage('totals') ?></option>
                        <option value="1"><?php echo CommonLanguage::getMessage('only_cat') ?></option>
                        <option value="2"><?php echo CommonLanguage::getMessage('only_products') ?></option>
                    </select>
                </div>
                <div class="col-md-2 " >
                    <label><?php echo CommonLanguage::getMessage('period') ?></label>
                    <select id="period" class="full-width" data-init-plugin="select2">
                        <option value="1"><?php echo CommonLanguage::getMessage('daily') ?></option>
                        <option value="2"><?php echo CommonLanguage::getMessage('monthly') ?></option>
                        <option value="3"><?php echo CommonLanguage::getMessage('yearly') ?></option>
                    </select>
                </div>
                <div class="col-md-2 " style="    margin-top: 24px;">
                    <button id="Generate_Report" type="button" class="btn btn-primary pull-right" data-toggle="dropdown" aria-expanded="false">
                        <?php print CommonLanguage::getMessage('generate_report'); ?></button>
                </div>
            </div>
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table  inventory" id="tableWithSearch">
                    <thead>
                        <tr>
                            <th class="text-left"><?php print CommonLanguage::getMessage('Date') ?></th>
                            <th class="text-left"><?php print CommonLanguage::getMessage('Name') ?></th>
                            <th class="text-left"><?php print CommonLanguage::getMessage('quantity') ?></th>
                            <th class="text-left"><?php print CommonLanguage::getMessage('UOM') ?></th>
                            <th class="text-center"><?php print CommonLanguage::getMessage('total') ?>&nbsp;(<span id="mysymbol"></span>)</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</div>
<script>
    var title = <?php echo json_encode($title); ?>;
    var is_actual = <?php echo $is_actual ? 1 : 0; ?>,
            is_sales = <?php echo $is_sales ? 1 : 0; ?>,
            local_symbol=<?php echo json_encode(smc_session()['local_currency_symbol'])?>,
            second_symbol=<?php echo json_encode(smc_session()['second_currency']['symbol'])?>;
    $(function () {
        build_select2($("#users"), "xhr/CommonSearch?col_id=User_ID&col_name=User_Name&table=tbl_common_users", 1);
        build_select2($("#customer_id"), is_sales ? "xhr/customerSearch" : "xhr/supplierSearch");
        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');
        ChangeTable();
        $("input[name=From_To_Date]").daterangepicker({
            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,
            autoUpdateInput: false,
            locale: {
                cancelLabel: 'Clear'
            }
        }, function (start, end, label) {

        });
        $('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'));

        });
        $('input[name="From_To_Date"]').on('cancel.daterangepicker', function (ev, picker) {
            startDate = ''
            endDate = ''
            $(this).val('');

        });
        $("input[name=created_date]").daterangepicker({
            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,
            autoUpdateInput: false,
            locale: {
                cancelLabel: 'Clear'
            }
        }, function (start, end, label) {
        });
        $('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'));

        });
        $('input[name="created_date"]').on('cancel.daterangepicker', function (ev, picker) {
            window.end_created = ''
            window.start_created = ''
            $(this).val('');

        });

        $("#Generate_Report").on("click", function () {
            ChangeTable()
        })
    });
    function ChangeTable() {
        var data = {
            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(),
            users: $("#users").val(),
            is_actual: is_actual,
            branch_id: $("#branch_id").val(),
            sales_man_id: $("#sales_man_id").val(),
            type: $("#type").val(),
            period: $("#period").val(),
            currency_id: $("#currency_id").val(),
            is_sales: is_sales
        };
        if($("#currency_id").val()==1)
            $("#mysymbol").text(local_symbol)
        else
            $("#mysymbol").text(second_symbol)
        RebuildDataTable(title, [], "InventoryAjax/periodic_sales_purchase_mov", data)

//        $.ajax({
//            url: "InventoryAjax/periodic_sales_purchase_mov",
//            data: data,
//            complete: function (v1, v2) {
//                if (v2 == 'success') {
//                    $("#tableWithSearch").DataTable().destroy();
//                    $("#tableWithSearch tbody").empty().html(v1.responseText)
//                    RebuildDataTable(title, [], "InventoryAjax/periodic_sales_purchase_mov",data)
//                }
//            }
//        });
    }
</script>