
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <input type="hidden" id="origin" value="<?php echo $origin; ?>"/>
            <div class="panel-title"> <?php print $title; ?></div>
            <div class="row form-group">
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('Date') ?></label>
                    <div class="input-group">
                        <span class="input-group-addon bg-<?php echo $origin == 'stock' ? 'complete-darker' : '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-3 <?php echo smc_session()['default_setting']['activate_expiry_date'] == 1 ? '' : 'hidden'; ?>">
                    <label><?php echo CommonLanguage::getMessage('Expiry_Date') ?></label>
                    <div class="input-group">
                        <span class="input-group-addon bg-complete-darker"><i class="fa fa-calendar"></i></span>
                        <input id="From_To_ExDate" type="text" class="form-control" name="From_To_ExDate" autocomplete="off" >
                    </div>
                </div> 
                <div class="col-md-2">
                    <div><label><?php print CommonLanguage::getMessage('Category'); ?></label></div>
                    <input class=" full-width " id="category_main_filter"  type="hidden" />
                </div>
                <div class="col-md-2">
                    <div><label><?php print CommonLanguage::getMessage('product'); ?></label></div>
                    <input class=" full-width " value="<?php echo isset($get_paramters['product_id']) ? $get_paramters['product_id'] : ''; ?>" data-select-name="<?php echo isset($get_paramters['product_name']) ? $get_paramters['product_name'] : ''; ?>" id="product_filter"  type="hidden" />
                </div>
                <div class="col-md-2">
                    <label><?php echo CommonLanguage::getMessage('Depot') ?></label>

                    <input id="depot_id" type="hidden" class="full-width" name="depot_id" >
                </div>
                </div> 
            <div class="row form-group">
            <div class="col-md-2  <?php echo smc_session()['default_setting']['activate_second_currency'] == 1 ? '':'hidden'; ?>">
                    <label><?php echo CommonLanguage::getMessage('currency') ?></label>
                    <select class="full-width" id="currency_id" 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="clearfix"></div>
        </div>
        <div class="panel-body" >
            <style>
                span{
                    color: black;
                }
                span:hover{
                    color: #ed9421;
                }
            </style>
            <table class="table table-bordered <?php echo $origin == 'stock' ? 'stock' : 'inventory'; ?>" id="tableWithSearch" >
                <thead>
                    <tr>
                        <th class="text-center"><?php print CommonLanguage::getMessage('Code'); ?></th>
                        <th class="text-center"  ><?php print CommonLanguage::getMessage('customer_name'); ?></th>
                        <th class="text-center"  ><?php print CommonLanguage::getMessage('supplier_name'); ?></th>
                        <th class="text-center"  ><?php print CommonLanguage::getMessage('Name'); ?></th>
                        <th class="text-center"  ><?php print CommonLanguage::getMessage('Date'); ?></th>
                        <?php if ($permissions->hasPermission('show_cost_stock_transaction')) { ?>
                            <th class="text-center"  ><?php print CommonLanguage::getMessage('cost') . ' (<span id="currency_symbol">' . smc_session()['local_currency_symbol'] . '</span>)'; ?></th>
                        <?php } ?>
                        <th class="text-center"  ><?php print CommonLanguage::getMessage('quantity'); ?></th>
                        <th class="text-center"  ><?php print CommonLanguage::getMessage('balance'); ?></th>
                        <!--<th class="text-center"  ><?php print CommonLanguage::getMessage('Operation'); ?></th>-->
                        <th class="text-center"  ><?php print CommonLanguage::getMessage('type'); ?></th>
                        <th class="text-center"  ><?php print CommonLanguage::getMessage('Depot'); ?></th>
                        <?php if (smc_session()['default_setting']['activate_expiry_date'] == 1) { ?>
                            <th class="text-center"  ><?php print CommonLanguage::getMessage('Expiry_Date'); ?></th>
                        <?php } if (smc_session()['default_setting']['activate_product_lot_nb'] == 1) { ?>
                            <th class="text-center"  ><?php print CommonLanguage::getMessage('lot'); ?></th>
                        <?php } if (smc_session()['default_setting']['activate_product_serial_nb'] == 1) { ?>
                            <th class="text-center"  ><?php print CommonLanguage::getMessage('serial'); ?></th>
                            <?php } ?>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>
</div>
<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('Product_Location_list')); ?>,
            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($("#category_main_filter"), "xhr/catSearch")
        build_select2($("#product_filter"), "xhr/ProductSearch");
        build_select2($("#depot_id"), "xhr/depotSearch")
        change_table()
        $("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'));
            change_table();
        });
        $("input[name=From_To_Date]").on('cancel.daterangepicker', function (ev, picker) {
            window.startDate = ''
            window.endDate = ''
            $(this).val('');
            change_table();
        });
        $("input[name=From_To_ExDate]").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_ExDate]").on('apply.daterangepicker', function (ev, picker) {
            window.exstartDate = picker.startDate.format('YYYY-MM-DD'),
                    window.exendDate = picker.endDate.format('YYYY-MM-DD');
            $(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
            change_table();
        });
        $("input[name=From_To_ExDate]").on('cancel.daterangepicker', function (ev, picker) {
            window.exstartDate = ''
            window.exendDate = ''
            $(this).val('');
            change_table();
        });
        $("#category_main_filter,#product_filter,#depot_id,#currency_id").on("change", function () {
            change_table()
        })
    })
    function change_table() {
        var Data = {
            cat_id: $("#category_main_filter").val(),
            product_id: $("#product_filter").val(),
            depot_id: $("#depot_id").val(),
            from_date: window.startDate,
            to_date: window.endDate,
            expiry_from: window.exstartDate,
            expiry_to: window.exendDate,
            origin: $('#origin').val(),
            currency_id: $('#currency_id').val()
        };
        $("#currency_symbol").text($('#currency_id').val()==1 ?local_symbol : second_symbol )
        RebuildDataTable(title, [0, 1, 2], 'StockAjax/stock_transactions_ajax', Data);

    }
</script>
