<style>
    .PriceVal{
        text-align: center;
        border:none;
        background: #eee;
    }
</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">
                <?php echo Forms::AlertDiv('product_branch_alert'); ?>
            </div>
            <div class="row">
                <div class="col-md-3 ">
                    <label><?php echo CommonLanguage::getMessage('options') ?></label>
                    <select name="product_options" id="product_options" multiple class="full-width" data-init-plugin="select2">
                        <option value="1"><?php print CommonLanguage::getMessage('branch'); ?></option>
                        <option value="2"><?php print CommonLanguage::getMessage('Printers'); ?></option>
                        <option value="3"><?php print CommonLanguage::getMessage('max_discount'); ?></option>
                        <option value="4"><?php print CommonLanguage::getMessage('family'); ?></option>
                        <option value="5"><?php print CommonLanguage::getMessage('Category'); ?></option>
                        <option value="6"><?php print CommonLanguage::getMessage('enabled'); ?></option>
                        <option value="7"><?php print CommonLanguage::getMessage('Brand'); ?></option>
                        <option value="8"><?php print CommonLanguage::getMessage('vat_percent'); ?></option>
                        <option value="9"><?php print CommonLanguage::getMessage('vat2_percent'); ?></option>
                        <option value="10"><?php print CommonLanguage::getMessage('Reorder_Point'); ?></option>
                        <option value="11" class="<?php echo smc_session()['default_setting']['points_for_products'] == 1 ? '' : 'hidden'; ?>"><?php print CommonLanguage::getMessage('positive_points'); ?></option>
                        <option value="12" class="<?php echo smc_session()['default_setting']['points_for_products'] == 1 ? '' : 'hidden'; ?>"><?php print CommonLanguage::getMessage('negative_points'); ?></option>
                    </select>
                </div> 
                <div class="col-md-3 ">
                    <label><?php echo CommonLanguage::getMessage('Category') ?></label>
                    <input id="Category"  name="Category" class="full-width Category" type="hidden"/>
                </div>
                <div class="col-md-3 ">
                    <label><?php echo CommonLanguage::getMessage('Product') ?></label>
                    <input id="product_name"  name="product_name" class="form-control product_name" type="text"/>
                </div>
                <div class="col-md-2 pull-right m-t-20 data-options hidden">
                    <button class=" btn btn-primary  btn-cons full-width " id="get_data"  type="button" ><?php print CommonLanguage::getMessage('generate'); ?></button>
                </div>
            </div>
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table  table-bordered  inventory" id="tableWithSearch" >
                    <thead>
                        <tr>
                            <th class="text-center" ><?php print CommonLanguage::getMessage('Code'); ?></th>
                            <th class="text-center"><?php print CommonLanguage::getMessage('Item'); ?></th>
                            <th class="text-center table_options_col"><?php print CommonLanguage::getMessage('options'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
            <?php if (smc_session()['default_setting']['sync_type'] != 2) { ?>
                <div class="container-sm-height data-options hidden">
                    <button class=" btn  btn-cons m-b-10 pull-right" id="submitMultiProductEdit" style="color:#fff;background-color:#4c0870  ;padding-top: 10px;top: 20px;min-width:253px;" type="button"><i class="pg-form"></i> <span class="bold"><?php print CommonLanguage::getMessage('Save'); ?></span></button>
                </div>
            <?php } ?>
        </div>
    </div>
</div>
<script>
    $(function () {
        $('#product_options').on('change', function () {
            ActualAjax();
        });
        build_select2($("#Category"), "xhr/catSearch");
        ActualAjax();
        $('#get_data').on('click', function () {
            ActualAjax();
        });
        $('#Category').on('change', function () {
            ActualAjax();
        });
        var table = $('#tableWithSearch').DataTable();
        table.on('draw.dt', function (e, settings) {
            $('select[name=product_printer_id],select[name=product_branch_id]').select2();
            build_select2($('input[name=product_category_id]'), 'xhr/CommonSearch?col_name=Cat_Name&col_id=Cat_ID&table=tbl_product_cat');
            build_select2($('input[name=brand_id]'), 'xhr/CommonSearch?col_name=brand_name&col_id=brand_id&table=tbl_brand_products');
            build_select2($('input[name=product_family_id]'), 'xhr/CommonSearch?col_name=family_name&col_id=family_id&table=tbl_inventory_family');
//            build_select2($('input[name=product_printer_id]'), 'xhr/CommonSearch?col_name=printer_title&col_id=printer_id&table=tbl_printers',1);
//            build_select2($('input[name=product_branch_id]'), 'xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches',1);
        });
    });
    $("#submitMultiProductEdit").on("click", function () {
        var Data = {products: [], product_options: $('#product_options').val()},
                row = $("#tableWithSearch").dataTable().fnGetNodes();
        for (var i = 0; i < row.length; i++) {
            Data.products.push({
                product_id: $("span.product_name_span", $(row[i])).attr("data-id"),
                branch_id: $("select[name=product_branch_id]", $(row[i])).val(),
                printer_id: $("select[name=product_printer_id]", $(row[i])).val(),
                family_id: $("input[name=product_family_id]", $(row[i])).val(),
                category_id: $("input[name=product_category_id]", $(row[i])).val(),
                brand_id: $("input[name=brand_id]", $(row[i])).val(),
                Product_Limit_Discount: $("input[name=Product_Limit_Discount]", $(row[i])).val(),
                Product_Reorder_Point: $("input[name=Product_Reorder_Point]", $(row[i])).val(),
                Status_ID: $("input[name=Status_ID]", $(row[i])).is(":checked") ? 1 : 0,
                vat_percent: $("input[name=vat_percent]", $(row[i])).val(),
                vat2_percent: $("input[name=vat2_percent]", $(row[i])).val(),
                positive_points: $("input[name=positive_points]", $(row[i])).val(),
                negative_points: $("input[name=negative_points]", $(row[i])).val(),
            });
        }
        $.ajax({
            url: "InventoryAjax/submitMultiProductEdit",
            method: "POST",
            data: Data,
            dataType: "json",
            complete: function (jqXHR, v2) {
                if (v2 == "success") {
                    if (jqXHR.responseJSON[1] == 'success') {
                        AddAlert("#product_branch_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                        ActualAjax()
                    }
                }
                $(document.body).animate({scrollTop: 0}, 800)
            }
        })
    });
    function ActualAjax() {
        var options_count = 0, options_data = [];
        if ($("#product_options").val()) {
            $(".data-options").removeClass('hidden')
            options_data = $('#product_options').select2('data');
            options_count = options_data.length;
        } else
            $(".data-options").addClass('hidden')


        var table_options_cols = $('#tableWithSearch thead th.table_options_col');
        var first_table_options_col = table_options_cols.first();
        table_options_cols.not(first_table_options_col).remove();
        var last_options_col;
        var prt_cols = [0, 1], options_val = [];
        for (var i = 0; i < options_count; i++) {
            if (i == 0)    //first one already exist so don't clone
                last_options_col = first_table_options_col;
            else
                last_options_col = first_table_options_col.clone().insertAfter(last_options_col);
            last_options_col.html(options_data[i]['text']);
            options_val.push(options_data[i]['id']);
            prt_cols.push(4 + i);
        }
        var product_list_dt = RebuildDataTable($('.panel-heading .panel-title').html(), prt_cols, 'InventoryAjax/ProductBranchesAjax', {Category_ID: $("#Category").val(), product_name: $('#product_name').val(), product_options: options_val/*$('#product_options').val()*/}, '#tableWithSearch');
        product_list_dt.columns().visible(true);
    }
</script>
