<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print CommonLanguage::getMessage('stock_value') ?></div>
            <div class="row form-group">
                <div class="col-md-2">
                    <label><?php echo CommonLanguage::getMessage('method'); ?></label>
                    <select id="method" class="full-width " data-init-plugin="select2">
                        <?php foreach ($methods as $method) : ?>
                            <option <?php echo smc_session()['stock_method'] == $method['Pricing_Method_ID'] ? 'selected' : '' ?> data-type-id="<?php echo $method['Pricing_Method_ID']; ?>" type="method" data-currency="<?php echo smc_session()['local_currency_symbol'] ?>"><?php echo $method['Pricing_Method_Name']; ?></option>
                        <?php endforeach; ?>
                        <?php foreach ($classes as $class) : ?>
                            <option data-type-id="<?php echo $class['Class_ID']; ?>" type="price" data-currency="<?php echo $class['Currency_Symbol']; ?>"><?php echo $class['Class_Name']; ?></option>
                        <?php endforeach; ?>
                        <option data-type-id="0" type="last_cost" data-currency="<?php echo smc_session()['local_currency_symbol'] ?>"><?php echo CommonLanguage::getMessage('last_cost') . ' ' . smc_session()['local_currency_symbol'] ?></option>
                        <option data-type-id="0" type="last_cost_second" data-currency="<?php echo smc_session()['second_currency']['symbol'] ?>"><?php echo CommonLanguage::getMessage('last_cost') . ' ' . smc_session()['second_currency']['symbol'] ?></option>
                    </select>
                </div>
                <div class="  col-md-2 ">
                    <label><?php echo CommonLanguage::getMessage('to_date'); ?></label>
                    <input id="date" type="text" class="form-control" name="date" value="<?php echo $today; ?>">
                </div>
                
                <div class="  col-md-2 <?php echo $permissions->hasPermission('Depots') ? '':'hidden'; ?>">
                    <label><?php echo CommonLanguage::getMessage('Depot'); ?></label>
                    <input id="depot_id" name="depot_id" class="full-width depot_id" type="hidden" value="<?php echo smc_session()['user_accounts']['depot_id'] ?>" data-select-name="<?php echo smc_session()['user_accounts']['Depot_Name']; ?>"  />
                </div>
                <div class="  col-md-2 ">
                    <label><?php echo CommonLanguage::getMessage('Category'); ?></label>
                    <input required id="Category_ID" name="Category_ID" class="full-width Category_ID" type="hidden" />
                </div>
                <div class="  col-md-2 ">
                    <label><?php echo CommonLanguage::getMessage('operation'); ?></label>
                    <select id="operation" class="full-width " data-init-plugin="select2">
                        <option value='0'><?php echo CommonLanguage::getMessage('all') ?></option>
                        <option value='pos'><?php echo CommonLanguage::getMessage('positive') ?></option>
                        <option value='neg'><?php echo CommonLanguage::getMessage('negative') ?></option>
                        <option value='zero'><?php echo CommonLanguage::getMessage('zero') ?></option>
                    </select>
                </div>
                <div class="col-md-1">
                    <label><?php print CommonLanguage::getMessage('show_zero_qty'); ?></label>
                    <div><input type="checkbox" id="show_zero_qty" /></div>
                </div>
            </div>
            <div class="row form-group">
                <div class="  col-md-2 ">
                    <label><?php echo CommonLanguage::getMessage('brand'); ?></label>
                    <input required id="brand_id" name="brand_id" class="full-width brand_id" type="hidden" />
                </div>
                <div class="  col-md-2 ">
                    <label><?php echo CommonLanguage::getMessage('main_supplier'); ?></label>
                    <input required id="supplier_id" name="supplier_id" class="full-width supplier_id" type="hidden" />
                </div>
                <div class="  col-md-2 ">
                    <label><?php echo CommonLanguage::getMessage('cost'); ?></label>
                    <select id="cost_filter" class="full-width " data-init-plugin="select2">
                        <option value='0'><?php echo CommonLanguage::getMessage('all') ?></option>
                        <option value='zero'><?php echo CommonLanguage::getMessage('zero') ?></option>
                        <option value='non_zero'><?php echo CommonLanguage::getMessage('non_zero') ?></option>
                    </select>
                </div>
                <div class="col-md-2 <?php echo $permissions->hasPermission('Branches') ? '':'hidden'; ?> ">
                    <label><?php echo CommonLanguage::getMessage('choose_branch') ?></label>
                    <input id="search_branch_id" type="hidden" class="full-width" name="search_branch_id" value="<?php echo smc_session()['user_accounts']['branch_id'] ?>" data-select-name="<?php echo smc_session()['user_accounts']['branch_name']; ?>">
                </div>

            </div>
        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12">
                    <div class="table-responsive">
                        <table class="table table-bordered inventory" id="tableWithSearch">
                            <thead>
                                <tr>
                                    <th><?php print CommonLanguage::getMessage('Code') ?></th>
                                    <?php if (smc_session()['default_setting']['activate_search_product_second_code'] == 1) { ?>
                                        <th><?php print CommonLanguage::getMessage('product_second_code') ?></th>
                                    <?php } ?>
                                    <th><?php print CommonLanguage::getMessage('Name') ?></th>
                                    <th><?php print CommonLanguage::getMessage('qty') ?></th>
                                    <th><?php print CommonLanguage::getMessage('UOM') ?></th>
                                    <th><?php print CommonLanguage::getMessage('cost') ?></th>
                                    <th><?php print CommonLanguage::getMessage('total_costs') ?></th>
                                </tr>
                            </thead>
                            <tbody></tbody>
                            <tfoot>
                                <th></th>
                                <?php if (smc_session()['default_setting']['activate_search_product_second_code'] == 1) { ?>
                                    <th></th>
                                <?php } ?>
                                <th></th>
                                <th></th>
                                <th></th>
                                <th></th>
                                <th>
                                    <div class="text-center">
                                        <span id="stock_total"></span>
                                        <span id="currency_symbol"></span>
                                        <br>

                                        <span style="<?php echo smc_session()['default_setting']['activate_second_currency'] == 1 ? '' : 'display: none;'; ?>" id="stock_total_sec"></span>
                                        <span style="<?php echo smc_session()['default_setting']['activate_second_currency'] == 1 ? '' : 'display: none;'; ?>" id="sec_currency_symbol"></span>


                                    </div>
                                </th>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('stock_value')); ?>,
        is_actual = 0;
    var second_currency = <?php echo json_encode(smc_session()['second_currency']['symbol']) ?>,
        datatable_msg = <?php echo json_encode(isset(smc_session()['datatable_header_temp']['stock_value_report']) ? smc_session()['datatable_header_temp']['stock_value_report'] : null) ?>;

    $(function() {
        new Switchery($("#show_zero_qty").get(0), {
            color: $.Pages.getColor("primary")
        });
        //        var table = $('#tableWithSearch').DataTable();

        //        $('#tableWithSearch').on('page.dt', function (e, setting) {
        //            var info = table.page.info();
        //            console.log(e)
        //        });
        build_select2($("#depot_id"), "xhr/depotSearch")
        build_select2($("#Category_ID"), "xhr/catSearch");
        build_select2($("#brand_id"), "xhr/CommonSearch?col_id=brand_id&col_name=brand_name&table=tbl_brand_products")
        build_select2($("#supplier_id"), "xhr/CommonSearch?col_id=Supplier_ID&col_name=Supplier_Name&table=tbl_stock_suppliers")
        build_select2($("#search_branch_id"), "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches");

        $("#date").datepicker({
            autoclose: true
        });

        ChangeTable();
        $("#method,#date,#depot_id,#operation,#Category_ID,#show_zero_qty,#brand_id,#supplier_id,#cost_filter,#search_branch_id").on("change", function() {
            ChangeTable()
        });
    });

    function ChangeTable() {
        var depot_name = $('#depot_id').select2('data');
        if (depot_name) {
            depot_name = depot_name.text;
        } else {
            depot_name = '';
        }
        var data = {
            date: $("#date").val(),
            depot_id: $("#depot_id").val(),
            type: $("#method option:selected").attr("type"),
            type_id: $("#method option:selected").attr("data-type-id"),
            operation: $("#operation").val(),
            category: $("#Category_ID").val(),
            show_zero_qty: $("#show_zero_qty").is(":checked") ? 1 : 0,
            brand_id: $("#brand_id").val(),
            supplier_id: $("#supplier_id").val(),
            search_branch_id: $("#search_branch_id").val(),
            cost_filter: $("#cost_filter").val(),
        };

        $.ajax({
            url: "InventoryAjax/get_costing_report",
            data: data,
            dataType: 'JSON',
            complete: function(v1, v2) {
                if (v2 == 'success') {
                    $("#tableWithSearch").DataTable().destroy();
                    $("#tableWithSearch tbody").empty().html(v1.responseJSON['html'])
                    var msg = '';
                    //                    console.log(CurrentDate.From_Date_invoice+'==='+CurrentDate.To_Date_invoice)
                    if (datatable_msg != null) {
                        msg = datatable_msg.replace("[[to_date]]", data.date)
                        msg = msg.replace("[[date_class]]", data.date != '' ? '' : 'hidden')

                        if ($("#depot_id").val() != '')
                            msg = msg.replace("[[depot]]", $("#depot_id").select2("data").text)
                        msg = msg.replace("[[depot_class]]", $("#depot_id").select2("data") != null ? '' : 'hidden')
                        if ($("#Category_ID").val() != '')
                            msg = msg.replace("[[category]]", $("#Category_ID").select2("data").text)
                        msg = msg.replace("[[category_class]]", $("#Category_ID").select2("data") != null ? '' : 'hidden')
                        if ($("#brand_id").val() != '')
                            msg = msg.replace("[[brand]]", $("#brand_id").select2("data").text)
                        msg = msg.replace("[[brand_class]]", $("#brand_id").select2("data") != null ? '' : 'hidden')
                        if ($("#supplier_id").val() != '')
                            msg = msg.replace("[[supplier]]", $("#supplier_id").select2("data").text)
                        msg = msg.replace("[[supplier_class]]", $("#supplier_id").select2("data") != null ? '' : 'hidden')
                    }
                    RebuildDataTable(title + '<br/>' + depot_name, [], "", {}, '', '', {
                        msg: msg,
                        initComplete: function(settings, json) {
                            $("#stock_total").html(AddCommas(public_round_number(v1.responseJSON['stock_total'])));
                            $("#stock_total_sec").html(AddCommas(public_round_number(v1.responseJSON['stock_total_sec'])));
                            $("#currency_symbol").html($("#method option:selected").attr("data-currency"));
                            $("#sec_currency_symbol").html(second_currency);
                            if ($("#method option:selected").attr("data-currency") == second_currency)
                                $('#stock_total_sec,#sec_currency_symbol').addClass('hidden');
                            else
                                $('#stock_total_sec,#sec_currency_symbol').removeClass('hidden');

                        }
                    });
                }
            }
        });
    }
</script>