
<style>
    .QuantityVal,.damaged_reason{
        text-align: center;
        border:none;
        background: #eee;
    }
    .wrong{
        border: 1px solid red !important;
    }
    #tableWithSearch .Hint{
        position: relative;
        font-size: 10px;
        font-weight: bold;
        height: 20px;
        line-height: 20px;
        bottom: -3px;
        right: -144px;
        color: rgb(153, 153, 153);
    }
    .print-only{
        display:none;
    }
    @media print{
        .print-only{
            display:block;
        }
    }
    .expiry_width{
        width:300px!important;
    }
</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 id="multi_product_location_alert" class="alert alert-success hidden"></div>
            <div class="row">
                <div class="col-md-3">
                    <label><?php print CommonLanguage::getMessage('Category'); ?></label>
                    <input class=" full-width " id="category_id"  type="hidden" />
                </div>
                <div  class=" col-md-3 "> 
                    <label><?php echo CommonLanguage::getMessage('customer_name') ?></label>
                    <input type="hidden" name="customer_id"  id="customer_id" class="full-width customer_id " > 
                </div> 
                <div class="col-md-3 " >
                    <label><?php print CommonLanguage::getMessage('Depot'); ?></label>
                    <span class="red-star">*</span>
                    <input class=" full-width " id="depot_id" name="depot_id"  type="hidden" />
                </div>
            </div>
        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12">
                    <div class="table-responsive">
                        <table class="table table-bordered stock" id="tableWithSearch" >
                            <thead>
                                <tr>

                                    <th class="text-center" style="width:5%" ><?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 class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('Item'); ?></th>
                                    <th class="text-center" style="width:5%" ><?php print CommonLanguage::getMessage('barcode'); ?></th>
                                    <th class="text-center" style="width:5%" ><?php print CommonLanguage::getMessage('UOM'); ?></th>
                                    <th class="text-center" style="width:5%" ><?php print CommonLanguage::getMessage('quantity'); ?></th>
                                    <th class="text-center" style="width:5%" ><?php print CommonLanguage::getMessage('Damage_Reason'); ?></th>
                                    <?php if (smc_session()['default_setting']['activate_product_lot_nb'] == 1) { ?>
                                        <th class="text-center" style="width:5%" ><?php print CommonLanguage::getMessage('lot'); ?></th>
                                    <?php } if (smc_session()['default_setting']['activate_product_serial_nb'] == 1) { ?>
                                        <th class="text-center" style="width:5%" ><?php print CommonLanguage::getMessage('serial'); ?></th>
                                    <?php } if (smc_session()['default_setting']['activate_expiry_date'] == 1) { ?>
                                        <th class="text-center" style="width:5%" ><?php print CommonLanguage::getMessage('Expiry_Date'); ?></th>
                                    <?php } ?>

                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                        <div class="container-sm-height">
                            <button class=" btn  btn-cons m-b-10 pull-right" id="assign_product_location" style="color:#fff;background-color:#ed9421 ;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>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
<script>
    var is_actual =<?php echo json_encode($is_actual); ?>,
            activate_expiry_date = <?php echo json_encode(smc_session()['default_setting']['activate_expiry_date']); ?>,
            activate_lot = <?php echo json_encode(smc_session()['default_setting']['activate_product_lot_nb']); ?>,
            activate_serial = <?php echo json_encode(smc_session()['default_setting']['activate_product_serial_nb']); ?>;
    $(function () {
        var data_session = get_storage_element(page_link);

        if (data_session.category_name != '') {
            $("#category_id").val(data_session.category_id).attr("data-select-name", data_session.category_name);
        }

        build_select2($("#category_id"), "xhr/catSearch");
        build_select2($("#customer_id"), "xhr/customerSearch")
        build_select2($("#depot_id"), "xhr/CommonSearch?col_id=Depot_ID&col_name=Depot_Name&table=tbl_stock_depot_warehouse")
        build_table()

        $("#category_id").on("change", function () {
            build_table()
        });
        $("#assign_product_location").on("click", function () {
            var validate = true;
            var Data = {
                Depot_ID: $("#depot_id").val(),
                customer_id: $("#customer_id").val(),
                is_actual: is_actual,
                note: '',
                Products: [],
            }
            var row = $("#tableWithSearch").dataTable().fnGetNodes(),
                    is_error = false;
            $("input.QuantityVal").removeClass("wrong")
            for (var i = 0; i < row.length; i++) {
//                console.log(i)
                var product_id = $(row[i]).find("input.QuantityVal").attr("data-product-id"),
                        uom_id = $(row[i]).find("input.QuantityVal").attr("data-uom-id"),
                        is_serial = $(row[i]).find("input.QuantityVal").attr("data-is-serial"),
                        is_lot = $(row[i]).find("input.QuantityVal").attr("data-is-lot"),
                        is_expiry = $(row[i]).find("input.QuantityVal").attr("data-is-expiry"),
                        quantity = parseFloat($(row[i]).find("input.QuantityVal").val()),
                        damaged_reason = $(row[i]).find("input.damaged_reason").val();
                if (!isNaN(quantity) && quantity > 0) {
                    Data.Products.push({
                        Product_ID: product_id,
                        UOM_ID: uom_id,
                        quantity: Math.abs(quantity),
                        note: damaged_reason,
                        lot_nb: $(row[i]).find("input.lot_nb").val(),
                        serial_nb: $(row[i]).find("input.serial_nb").val(),
                        expiry_date: $(row[i]).find("input.expiry_date").val(),
                    });
                    if (activate_expiry_date == 1 && is_expiry == 1 && $(row[i]).find("input.expiry_date") == "") {
                        $(row[i]).find("input.expiry_date").addClass("wrong")
                        is_error = true;
                    }
                    if (activate_lot == 1 && is_lot == 1 && $(row[i]).find("input.lot_nb")) {
                        $(row[i]).find("input.lot_nb").addClass("wrong")
                        is_error = true;
                    }
                    if (activate_serial == 1 && is_serial == 1 && $(row[i]).find("input.serial_nb")) {
                        $(row[i]).find("input.serial_nb").addClass("wrong")
                        is_error = true;
                    }
                } else {
                    if ($(row[i]).find("input.QuantityVal").val() != '') {
                        $(row[i]).find("input.QuantityVal").addClass("wrong")
                        is_error = true;
                    }
                }
            }

//            console.log(Data)
            if (!$('#depot_id').val()) {
                $("#multi_product_location_alert").attr("class", "alert alert-danger").html('<?php echo CommonLanguage::getMessage('choose_depot_first'); ?>')
                $(document.body).animate({scrollTop: 0}, 800)
                return false;
            }

            if (Data.Products.length == 0) {
                $("#multi_product_location_alert").attr("class", "alert alert-danger").html('<?php echo CommonLanguage::getMessage('input_option'); ?>')
                $(document.body).animate({scrollTop: 0}, 800)
                return false;
            }
            if (is_error) {
                $("#multi_product_location_alert").attr("class", "alert alert-danger").html('<?php echo CommonLanguage::getMessage('Required_Fields'); ?>')
                $(document.body).animate({scrollTop: 0}, 800)
                return false;
            }


            $.ajax({
                url: "StockAjax/multiProductDamagedAjax",
                method: "POST",
                data: Data,
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        if (v1.responseText == 'OK') {
                            $("#multi_product_location_alert").attr("class", "alert alert-success").html('<?php echo CommonLanguage::getMessage('Submitted_Successfully'); ?>')
//                            build_table();

                        } else
                            $("#multi_product_location_alert").attr("class", "alert alert-danger").html(v1.responseText)
                    } else {
                        $("#multi_product_location_alert").attr("class", "alert alert-danger").html(Failed_Submit)
                    }
                    $(document.body).animate({scrollTop: 0}, 800)
                }
            });
        });
    });

    //    function set_data_store() {
    //        var data_arr = {
    //            category_name: ($("#category_id").select2('data') == null ? '' : $("#category_id").select2('data').text),
    //            category_id: $("#category_id").val(),
    //        };
    //        set_storage_element(page_link, data_arr);
    //    }
    function build_table() {
        var data = {
            category_id: $("#category_id").val(),
        }
        RebuildDataTable('', [0, 1, 2], 'StockAjax/multiProductDamaged', data, '', [0, 1, 2]);
        $('#tableWithSearch').on('draw.dt', function () {
            var row = $("#tableWithSearch").dataTable().fnGetNodes();
            for (var i = 0; i < row.length; i++) {
                var product_id = $("input.QuantityVal", $(row[i])).attr("data-product-id");
                build_select2($('input.lot_nb', $(row[i])), function () {
                    return 'xhr/get_product_lot?product_id=' + $('input.QuantityVal',this.parents('tr')).attr('data-product-id') + '&depot=' + $("#depot_id").val();
                }, 0, 'expiry_width');
                build_select2($('input.serial_nb', $(row[i])), function () {
                    return 'xhr/get_product_serial?product_id=' + $('input.QuantityVal',this.parents('tr')).attr('data-product-id') + '&depot=' + $("#depot_id").val();
                }, 0, 'expiry_width');
                build_select2($('input.expiry_date', $(row[i])), function () {
                    return 'xhr/get_product_expiry?product_id=' + $('input.QuantityVal',this.parents('tr')).attr('data-product-id') + '&depot=' + $("#depot_id").val();
                }, 0, 'expiry_width');

            }
        })
    }
</script>
