
<style>
    .QuantityVal,.CostVal{
        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;
        }
    }
</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 CommonLanguage::getMessage('multi_product_location') ?></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 print CommonLanguage::getMessage('Depot'); ?></label>
                    <span class="red-star">*</span>
                    <input class=" full-width " id="depot_id" name="depot_id"  type="hidden" />
                </div>
                <div class="col-md-3">
                    <label><?php print CommonLanguage::getMessage('currency'); ?></label>
                    <span class="red-star">*</span>
                    <input class=" full-width " id="currency_id" name="currency_id"  type="hidden" />
                </div>
                <div class="col-md-3">
                    <label><?php print CommonLanguage::getMessage('Date'); ?></label>
                    <span class="red-star">*</span>
                    <input class=" form-control " id="assign_date" name="assign_date"  type="text" value="<?php echo date('Y-m-d');?>" />
                </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('cost'); ?></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="row form-group">
                            <div class="col-md-6">
                                <label><?php echo CommonLanguage::getMessage('trans_note') ?></label>
                                <textarea id="trans_note" class="form-control" rows="4" style="resize:vertical;"></textarea>
                            </div>
                            <div class="col-md-2 pull-right">
                                <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('Assign_Product_Location'); ?></span></button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
<script>
    var main = 1,
            is_expiry_active = <?php echo json_encode(smc_session()['default_setting']['activate_expiry_date']); ?>,
            is_actual = 0;
    $(function () {

        var data_session = get_storage_element(page_link);
        if (data_session.depot_name != '') {
            $("#depot_id").val(data_session.depot_id).attr("data-select-name", data_session.depot_name);
        }
        if (data_session.category_name != '') {
            $("#category_id").val(data_session.category_id).attr("data-select-name", data_session.category_name);
        }
        if (data_session.currency_name != '') {
            $("#currency_id").val(data_session.currency_id).attr("data-select-name", data_session.currency_name);
        }
        $("#assign_date").datepicker({autoclose:true});
        var table = $('#tableWithSearch').DataTable();
        table.on('draw.dt', function (e, settings) {
            $('input.expiry_date_picker').datepicker({autoclose: true})
        });
        build_select2($("#category_id"), "xhr/catSearch")
        build_select2($("#currency_id"), "xhr/CommonSearch?col_id=Currency_ID&col_name=concat_ws(' ',Currency_Name,Currency_Symbol)&table=tbl_common_currency");
        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 = {
                note: $("#trans_note").val(),
                is_actual: is_actual,
                Depot_ID: $("#depot_id").val(),
                Currency_ID: $("#currency_id").val(),
                assign_date: $("#assign_date").val(),
                assign_type:0,
                Products: []
            }
            var row = $("#tableWithSearch").dataTable().fnGetNodes(),
                    is_error = false;
            $("input.QuantityVal,input.CostVal").removeClass("wrong")
            for (var i = 0; i < row.length; i++) {
                var product_id = $(row[i]).find("input.QuantityVal").attr("data-product-id"),
                        uom_id = $(row[i]).find("input.QuantityVal").attr("data-uom-id"),
                        quantity = parseFloat($(row[i]).find("input.QuantityVal").val()),
                        cost = parseFloat($(row[i]).find("input.CostVal").val());
                if (!isNaN(quantity) && quantity > 0 && !isNaN(cost) && cost >= 0) {
                    Data.Products.push({
                        Product_ID: product_id,
                        UOM_ID: uom_id,
                        quantity:0,
                        diff_qty: Math.abs(quantity),
                        unit_cost: Math.abs(cost),
                        lot_nb: $(row[i]).find('input.lot_nb').val(),
                        serial_nb: $(row[i]).find('input.serial_nb').val(),
                        is_service:0,
                        note:'',
                        expiry_date: (is_expiry_active == 1 && $(row[i]).find("input.QuantityVal").attr('data-is-expiry') == 1) ? $(row[i]).find('input.expiry_date_picker').val() : ''
                    });
                } else {
                    if ((isNaN(quantity) && !isNaN(cost) && cost >= 0) || quantity <= 0) {
                        $(row[i]).find("input.QuantityVal").addClass("wrong")
                        is_error = true;
                    }
                    if ((isNaN(cost) && !isNaN(quantity) && quantity > 0) || cost < 0) {
                        $(row[i]).find("input.CostVal").addClass("wrong")
                        is_error = true;
                    }
                }
            }

            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 (!$('#currency_id').val()) {
                $("#multi_product_location_alert").attr("class", "alert alert-danger").html('<?php echo CommonLanguage::getMessage('choose_currency_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/add_assign_ajax",
                method: "POST",
                data: Data,
                dataType:'json',
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        if (v1.responseJSON[0] == 'OK') {
                            set_data_store();
                            $('#trans_note').val('')
                            $("#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.responseJSON)
                    } 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 = {
            currency_name: ($("#currency_id").select2('data') == null ? '' : $("#currency_id").select2('data').text),
            depot_name: ($("#depot_id").select2('data') == null ? '' : $("#depot_id").select2('data').text),
            category_name: ($("#category_id").select2('data') == null ? '' : $("#category_id").select2('data').text),
            depot_id: $("#depot_id").val(),
            currency_id: $("#currency_id").val(),
            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/multiProductLocation', data, '', [0, 1, 2]);
    }
</script>
