<style>
    .table thead tr >th{
        background-color:#4c0870;
        color:#fff;
    }
</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>
            <form  target="_blank" action="Inventory/view_user_invoices" method="post" >
                <div class="row">
                    <div class="col-md-2">
                        <label><?php echo CommonLanguage::getMessage('created_by'); ?></label>
                        <select  id="created_by" name="created_by"  class="full-width " data-init-plugin="select2">
                            <option value="0"><?php echo CommonLanguage::getMessage('All'); ?></option>
                            <?php foreach ($users as $user): ?>
                            <option value="<?php echo $user['User_ID']; ?>"><?php echo $user['User_Name']; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label><?php echo CommonLanguage::getMessage('modified_by'); ?></label>
                        <select  id="users" name="user"  class="full-width Account_Code " data-init-plugin="select2">
                            <option value="0"><?php echo CommonLanguage::getMessage('All'); ?></option>
                            <?php foreach ($users as $user): ?>
                            <option value="<?php echo $user['User_ID']; ?>"><?php echo $user['User_Name']; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label><?php echo CommonLanguage::getMessage('salesman'); ?></label>
                        <input  id="salesman" name="salesman" type="hidden" class="full-width salesman " />
                    </div>
                    <div class="col-md-2">
                        <label><?php echo CommonLanguage::getMessage('Select_Type'); ?></label>
                        <select  id="select_type" name="type"  class="full-width select_type " data-init-plugin="select2">
                            <option value="0"><?php echo CommonLanguage::getMessage('All'); ?></option>
                            <option value="Invoice"><?php echo CommonLanguage::getMessage('Invoice'); ?></option>
                            <option value="Return"><?php echo CommonLanguage::getMessage('Return'); ?></option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label><?php echo CommonLanguage::getMessage('select_class'); ?></label>
                        <input  id="class_id" name="class_id" type="hidden" class="full-width class_id " />
                    </div>
                    <div class="col-md-2 <?php echo $permissions->hasPermission('Branches') ? '':'hidden'; ?>">
                        <label><?php echo CommonLanguage::getMessage('branch'); ?></label>
                        <input  id="branch_id" name="branch_id" type="hidden" class="full-width branch_id " />
                    </div>
                </div>
                <div class="row m-t-10 <?php  echo count($shipping_company) > 0 ? '' : 'hidden';?>">
                    <div class="col-md-2">
                        <label><?php echo CommonLanguage::getMessage('shipping_company_name') ?></label>
                        <select name="shipping_company_id" id="shipping_company_id" class="full-width" data-init-plugin="select2">
                            <option value=""><?php  echo CommonLanguage::getMessage('All');?></option>
                            <?php  foreach($shipping_company as $value){?>
                            <option value="<?php  echo $value['shipping_company_id'];?>" ><?php  echo $value['shipping_company_name'];?></option>
                            <?php }?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label><?php  echo CommonLanguage::getMessage('status');?></label>
                        <select name="shipping_status_id" id="shipping_status_id" class="full-width" data-init-plugin="select2">
                            <option value=""><?php  echo CommonLanguage::getMessage('All');?></option>
                            <?php  foreach($shipping_status as $value){?>
                            <option value="<?php  echo $value['shipping_status_id'];?>" ><?php  echo $value['shipping_status_name'];?></option>
                            <?php }?>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label><?php echo CommonLanguage::getMessage('pick_up_date') ?></label>
                        <div class="input-group">
                            <span class="input-group-addon bg-primary"><i class="fa fa-calendar"></i></span>
                            <input id="pick_up_from_to_date" type="text" class="form-control date_range_picker" name="pick_up_from_to_date" autocomplete="off" >
                        </div>
                    </div>  
                    <div class="col-md-4">
                        <label><?php echo CommonLanguage::getMessage('drop_off_date') ?></label>
                        <div class="input-group">
                            <span class="input-group-addon bg-primary"><i class="fa fa-calendar"></i></span>
                            <input id="drop_off_from_to_date" type="text" class="form-control date_range_picker" name="drop_off_from_to_date" autocomplete="off" >
                        </div>
                    </div> 
                </div>
                <div class="row m-t-10">
                    <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-3">
                        <label><?php echo CommonLanguage::getMessage('invoice_date') ?></label>
                        <div class="input-group">
                            <span class="input-group-addon bg-primary"><i class="fa fa-calendar"></i></span>
                            <input id="invoice_date" type="text" class="form-control" name="invoice_date" autocomplete="off" />
                        </div>
                    </div> 
                    <div class="col-md-3 ">
                        <label><?php echo CommonLanguage::getMessage('choose_customer') ?></label>
                        <input id="customer_id" type="hidden" class="full-width" name="customer_id" >
                    </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-3 m-t-10">
                         <label><?php print CommonLanguage::getMessage('without_shipping'); ?></label>
                        <input type='checkbox' id='exclude_shipping' name='exclude_shipping' />
                    </div>  <div class="col-md-3 m-t-10">
                         <label><?php print CommonLanguage::getMessage('include_receipt_payment'); ?></label>
                        <input type='checkbox' id='include_receipt_payment' name='include_receipt_payment' />
                    </div>
                    <div class="col-md-3  pull-right m-t-10">
                        <button type="submit" class="btn btn-primary pull-right " id="print_user_invoices"><?php echo CommonLanguage::getMessage('print');?></button>
                        <button type="button" class="btn btn-primary pull-right m-r-5 text-white" id="search_list"><?php echo CommonLanguage::getMessage('search');?></button>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <div class="col-md-3"><h3><strong><?php echo CommonLanguage::getMessage('total') . ": " ?></strong><span class="ReportTotal"></span><span class="ReportCurrency">&nbsp;<?php echo $local_currency_symbol; ?></span></h3></div>
                        <div class="col-md-3"><h3><strong><?php echo CommonLanguage::getMessage('cash') . ": " ?></strong><span class="ReportTotalCash"></span><span class="ReportCurrency">&nbsp;<?php echo $local_currency_symbol; ?></span></h3></div>
                        <div class="col-md-3"><h3><strong><?php echo CommonLanguage::getMessage('debit') . ": " ?></strong><span class="ReportTotalDebit"></span><span class="ReportCurrency">&nbsp;<?php echo $local_currency_symbol; ?></span></h3></div>
                        <div class="col-md-3"><h3><strong><?php echo CommonLanguage::getMessage('discount') . ": " ?></strong><span class="ReportDiscount"></span><span class="ReportCurrency">&nbsp;<?php echo $local_currency_symbol; ?></span></h3></div>

                    </div>
                </div>
            </form>
            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table table-bordered inventory" id="tableWithSearch">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th><?php print CommonLanguage::getMessage('customer_name') ?></th>
                            <th><?php print CommonLanguage::getMessage('invoice_date') ?></th>
                            <th><?php print CommonLanguage::getMessage('Date') ?></th>
                            <th><?php print CommonLanguage::getMessage('total').' ('.  smc_session()['local_currency_symbol'].')' ?></th>
                            <th><?php print CommonLanguage::getMessage('discount').' ('.  smc_session()['local_currency_symbol'].')' ?></th>
                            <th><?php print CommonLanguage::getMessage('type') ?></th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</div>
<script>
    var default_start_time = <?php echo json_encode(smc_session()['default_setting']['default_start_time']);?>;
    var is_actual = <?php echo json_encode($is_actual);?>;
    $(function () {
        var default_end_time = subtractFromTime(default_start_time,1,'seconds'),
            twelve_hours = subtractFromTime(default_start_time,12,'hours');
        build_select2($('#customer_id'), 'xhr/customerSearch');
        build_select2($("#salesman"), "xhr/employeeSearch");
        build_select2($("#class_id"), "xhr/PricingClassSearch");
        build_select2($("#branch_id"), "xhr/CommonSearch?col_id=branch_id&col_name=branch_name&table=tbl_organization_branches");
        build_select2($('#customer_branch_id'), 'xhr/searchCustomerBranch?customer_id='+$("#customer_id").val());
        $('#customer_id').on('change',function(){
            $('#customer_branch_id').val('');
            build_select2($('#customer_branch_id'), 'xhr/searchCustomerBranch?customer_id='+$("#customer_id").val()); 
        });
        new Switchery($("#exclude_shipping").get(0), {color: $.Pages.getColor("primary")});
        new Switchery($("#include_receipt_payment").get(0), {color: $.Pages.getColor("primary")});
        $("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().add(1, 'days').format('YYYY-MM-DD') + ' ' + default_end_time)],
                'Yesterday': [moment(moment().subtract(1, 'days').format('YYYY-MM-DD') + ' ' + default_start_time),
                    moment(moment(new Date().getTime()).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")
        });
//        $("input[name=invoice_date]").daterangepicker({
//            autoApply: true,
//            singleDatePicker: false,
//            showDropdowns: true,
//            locale: {
//                format: 'YYYY-MM-DD'
//            },
//            ranges: {
//                'Today': [moment(), moment()],
//                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
//                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
//                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
//                'This Month': [moment().startOf('month'), moment().endOf('month')],
//                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
//            }
//        });
 $("input[name=invoice_date]").daterangepicker({
            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            }, locale: {
                cancelLabel: 'Clear'
            }, allowEmpty: false,
            autoUpdateInput: false
        });

        $("input[name=invoice_date]").on('apply.daterangepicker', function (ev, picker) {
            window.startInvDate = picker.startDate.format('YYYY-MM-DD'),
                    window.endInvDate = picker.endDate.format('YYYY-MM-DD');
            $(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
         
        });
        $("input[name=invoice_date]").on('cancel.daterangepicker', function (ev, picker) {
            window.startInvDate = '';
            window.endInvDate = '';
            $(this).val('');
            
        });

        ActualAjax();

        $("input.date_range_picker").daterangepicker({
            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            }, locale: {
                cancelLabel: 'Clear'
            }, allowEmpty: false,
            autoUpdateInput: false
        });
        $("input.date_range_picker").on('apply.daterangepicker', function (ev, picker) {
            var current_id = $(this).attr('id');
            if(current_id == 'pick_up_from_to_date'){
                window.startPickUpDate = picker.startDate.format('YYYY-MM-DD');
                window.endPickUpDate = picker.endDate.format('YYYY-MM-DD');
            }else{
                window.startDropOffDate = picker.startDate.format('YYYY-MM-DD');
                window.endDropOffDate = picker.endDate.format('YYYY-MM-DD');
            }
            $(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
            //            changeTable();
        });
        $("input.date_range_picker").on('cancel.daterangepicker', function (ev, picker) {
            var current_id = $(this).attr('id');
            if(current_id == 'invoice_from_to_date'){
                window.startDate = '';
                window.endDate = '';
            }else if(current_id == 'pick_up_from_to_date'){
                window.startPickUpDate = '';
                window.endPickUpDate = '';
            }else{
                window.startDropOffDate = '';
                window.endDropOffDate = '';
            }
            $(this).val('');
            //            changeTable();
        });
        $("#search_list").on("click", function () {
            ActualAjax();
        });
    });
    function ActualAjax(){
        var date_from_to_value = $("input[name=From_To_Date]").val().split(' - '),
            invoice_date_from_to_value = $("input[name=invoice_date]").val().split(' - '),
            current_data = {user: $("#users").val(),created_by:$('#created_by').val(),
                            From_Date: window.startDate ? window.startDate : date_from_to_value[0],
                            To_Date: window.endDate ? window.endDate : date_from_to_value[1],
                            invoice_from_date: window.startInvDate ? window.startInvDate : invoice_date_from_to_value[0],
                            invoice_to_date: window.endInvDate ? window.endInvDate : invoice_date_from_to_value[1],
                            salesman: $('#salesman').val(), is_actual: is_actual,
                            type: $('#select_type').val(), class_id: $("#class_id").val(),
                            branch_id:$('#branch_id').val(),
                            shipping_company_id:$('#shipping_company_id').val(),
                            shipping_status_id:$('#shipping_status_id').val(),
                            customer_id:  $('#customer_id').val(),
                            customer_branch_id:  $('#customer_branch_id').val(),
                            drop_off_from_date: window.startDropOffDate,
                            drop_off_to_date: window.endDropOffDate,
                            pick_up_from_date: window.startPickUpDate,
                            pick_up_to_date: window.endPickUpDate,
                            exclude_shipping: $('#exclude_shipping').is(':checked') ? 1 : 0,
                            include_receipt_payment: $('#include_receipt_payment').is(':checked') ? 1 : 0};
                        console.log(current_data)
        $.ajax({
            url: "InventoryAjax/getUserInvoiceTotals",
            method: "GET",
            dataType: 'json',
            data: current_data,
            complete: function (v1, v2) {
                $(".ReportTotal").html(AddCommas(v1.responseJSON['total'].toFixed(3)));
                $(".ReportTotalCash").html(AddCommas(v1.responseJSON['total_cash'].toFixed(3)));
                $(".ReportTotalDebit").html(AddCommas((v1.responseJSON['total'] - v1.responseJSON['total_cash'] - v1.responseJSON['discount']).toFixed(3)));
                $(".ReportDiscount").html(AddCommas(v1.responseJSON['discount'].toFixed(3)));
                RebuildDataTable($('.panel-heading .panel-title').html(), [0, 1, 2, 3, 4, 5], "InventoryAjax/userInvoicesReport", current_data);
            }
        });
    }
</script>