<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="clearfix"></div>
        </div>
        <div class="panel-body">
            <div class="row m-b-10 form-group">
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage($is_cust ? 'choose_customer' : 'choose_supplier'); ?><span class="red-star">*</span></label>
                    <input type="hidden" class="full-width" name="search_person"  id="search_person" autocomplete="off"/>
                </div>
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('category_name'); ?></label>
                    <input type="hidden" class="full-width" name="current_category_id"  id="current_category_id" autocomplete="off"/>
                </div>
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('family'); ?></label>
                    <input type="hidden" class="full-width" name="family_id"  id="family_id" autocomplete="off"/>
                </div>
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('brand'); ?></label>
                    <input type="hidden" class="full-width" name="brand_id"  id="brand_id" autocomplete="off"/>
                </div>


            </div>
            <div class="row">
                <div class="col-md-12 table-responsive">
                    <table class="table table-bordered inventory" id="tableWithSearch" >
                        <thead>
                            <tr>

                                <th class="text-center"  ><?php print CommonLanguage::getMessage('Item'); ?></th>
                                <th class="text-center"  ><?php print CommonLanguage::getMessage('Code'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="row">
                <div class="col-md-2 pull-right">
                    <button class="btn btn-primary full-width" id="submit_codes"><?php echo CommonLanguage::getMessage('Save') ?></button>

                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var title = <?php echo json_encode($title) ?>,
            is_cust =<?php echo json_encode($is_cust); ?>;
    $(function () {
        build_select2($("#search_person"), (is_cust ? ("xhr/customerSearch") : "xhr/supplierSearch"))
        build_select2($("#current_category_id"), "xhr/CommonSearch?col_name=Cat_Name&col_id=Cat_ID&table=tbl_product_cat");
        build_select2($("#family_id"), "xhr/CommonSearch?col_name=family_name&col_id=family_id&table=tbl_inventory_family");
        build_select2($("#brand_id"), "xhr/CommonSearch?col_name=brand_name&col_id=brand_id&table=tbl_brand_products");

//        change_table()
        $('#current_category_id,#search_person,#family_id,#brand_id').on('change', function () {
            change_table()
        })

        $("#submit_codes").on("click", function () {
            if ($("#search_person").val() == '') {
                toastr.error('<?php echo CommonLanguage::getMessage('Required_Fields'); ?>')
                return;
            }
            var data = {
                person_id: $("#search_person").val(),
                is_cust: is_cust,
                prod_arr: []
            };
            $("#tableWithSearch .product_code").each(function () {
                if ($(this).val() != $(this).attr("data-code")) {
                    data.prod_arr.push({
                        product_id: $(this).attr("data-id"),
                        product_code: $(this).val(),
                    })
                }
            })
            if (data.prod_arr.length == 0) {
                toastr.error('<?php echo CommonLanguage::getMessage('Required_Fields'); ?>')
                return;
            }
            $.ajax({
                url: "InventoryAjax/add_cust_supp_codes",
                data: data,
                complete: function (v1, v2) {
                    if (v2 == 'success' && v1.responseText == 'OK') {
                        toastr.success('<?php echo CommonLanguage::getMessage('Submitted_Successfully') ?>')
                        change_table()

                    } else {
                        toastr.error(v1.responseText)

                    }
                }
            })
        })


    });
    function change_table() {
     if ($("#search_person").val() == '') {
                toastr.error('<?php echo CommonLanguage::getMessage('Required_Fields'); ?>')
                return;
            }
        RebuildDataTable(title, [0, 1, 2, 3], "InventoryAjax/customer_supplier_codes",
                {category_id: $('#current_category_id').val(),
                    person_id: $("#search_person").val(),
                    brand_id: $("#brand_id").val(),
                    family_id: $("#family_id").val(),
                    is_cust: is_cust
                });

    }

</script>