<style>
    .actionButton{
        background-color:#4c0870;
    }
</style>
<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">
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('Date') ?></label>
                    <div class="input-group">
                        <span class="input-group-addon bg-primary"><i class="fa fa-calendar"></i></span>
                        <input id="From_To_Date" type="text" class="form-control" name="From_To_Date" autocomplete="off" />
                    </div>
                </div> 
                <div class="col-md-2">
                    <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 echo CommonLanguage::getMessage('choose_branch') ?></label>
                    <input id="current_branch_id" type="hidden" class="full-width" name="current_branch_id" >
                </div> 
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('Depot') ?></label>
                    <input id="depot_filter_id" type="hidden" class="full-width" name="depot_filter_id" >
                </div> 
            </div>
        </div>
        <div class="panel-body">
            <div class="row ">
                <div class="col-md-12 table-responsive">
                    <table class="table table-bordered inventory" id="tableWithSearch" >
                        <thead>
                            <tr>
                                <th class="text-center" style="width:5%" >#</th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('customer_name'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('Date'); ?></th>
                                <th class="text-center no-print" style="width:10%" ><?php print CommonLanguage::getMessage('Action'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var is_actual= 1;
    $(function () {
        build_select2($("#depot_filter_id"), "xhr/depotSearch")

        var data_session = get_storage_element(page_link);
        if (data_session.branch_name != '') {
            $("#current_branch_id").val(data_session.branch_id).attr("data-select-name", data_session.branch_name)
        }
        if (data_session.person_name != '') {
            $("#customer_id").val(data_session.customer_id).attr("data-select-name", data_session.person_name)
        }
        if (data_session.Date != '') {
            window.startDate = data_session.from_date;
            window.endDate = data_session.to_date;
            $("input[name=From_To_Date]").val(data_session.Date)
        }

        build_select2($("#current_branch_id"), "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches");


        build_select2($('#customer_id'), 'xhr/customerSearch');

        ChangeTable();

        $("input[name=From_To_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=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'));
            ChangeTable();
        });
        $("input[name=From_To_Date]").on('cancel.daterangepicker', function (ev, picker) {
            window.startDate = ''
            window.endDate = ''
            $(this).val('');
            ChangeTable();
        });

        $('#current_branch_id,#depot_filter_id,#customer_id').on('change', function () {
            ChangeTable();
        });
    });
    function ChangeTable() {
        var Data = {
            is_actual: is_actual ? 1 : 0,
            customer_id: $('#customer_id').val(),
            person_name: ($("#customer_id").select2('data') == null ? '' : $("#customer_id").select2('data').text),
            branch_id: $('#current_branch_id').val(),
            branch_name: ($("#current_branch_id").select2('data') == null ? '' : $("#current_branch_id").select2('data').text),
            Date: $("input[name=From_To_Date]").val(),
            from_date: window.startDate,
            to_date: window.endDate,
            depot_id: $('#depot_filter_id').val()
        }
        set_storage_element(page_link, Data);
        RebuildDataTable('', [0, 1, 2, 3, 4, 5, 6], "InventoryAjax/consumption_list", Data);
    }
</script>