$(document).ready(function() {
    $('select[name="category_id"]').change(function(){
        var category_id = $(this).val();
        if(category_id != '') {
            $.ajax({
                async: false,
                url: base_url + 'inventory_store/get_items_by_cat_id',
                type: 'POST',
                data: {
                    category_id: category_id
                },
                dataType: "json"
            })
            .done(function(data) {
                var item_name ='';
                item_name += '<option value="">Select Item Name</option>';
                for(var i in data.items) {
                    var item = data.items[i];
                    item_name += '<option value="'+ item.id +'">'+ item.item_name+'</option>';
                }
                $('select[name="item_id"]').html(item_name);
                $('input[name="uom"]').val('');
                $('input[name="hidden_uom"]').val('');
                $('input[name="received_qty"]').val('');
            })
            .fail(function(XHR, status_code, error_thrown) {
                console.log(error_thrown);
            });
        } else {
            $('select[name="item_id"]').val('');
            $('input[name="uom"]').val('');
            $('input[name="hidden_uom"]').val('');
            $('input[name="received_qty"]').val('');
        }
    });
    
    $('select[name="item_id"]').change(function(){
        var item_id = $(this).val();
        var category_id = $('select[name="category_id"]').val();
        if(item_id != '') {
            $.ajax({
                async: false,
                url: base_url + 'inventory_general/get_unit_by_item_id',
                type: 'POST',
                data: {
                    item_id: item_id,
                    category_id: category_id
                },
                dataType: "JSON"
            })
            .done(function(data) {
                $('input[name="uom"]').val(data.item.unit_name);
                $('input[name="hidden_uom"]').val(data.item.id);
                
                $('input[name="received_qty"]').val(data.item.quantity);
            })
            .fail(function(XHR, status_code, error_thrown) {
                console.log(error_thrown);
            });
        } else {
            $('input[name="uom"]').val('');
            $('input[name="hidden_uom"]').val('');
            $('input[name="received_qty"]').val('');
        }
    });
    
    $('input[name="issued_qty"]').keyup(function(){
        var issued_qty = $(this).val();
        var received_qty = $('input[name="received_qty"]').val();
        var balance_qty = 0;
        if(parseFloat(received_qty) > parseFloat(issued_qty)) {
            balance_qty = parseFloat(received_qty) - parseFloat(issued_qty); 
        }
        $('input[name="balance_qty"]').val(balance_qty);
    });
    
    
    $.validator.addMethod("valid_issued_qty", function(value, element) {
        var return_val = true;
        var avail_qty = $('input[name="received_qty"]').val();
        var issue_qty = $('input[name="issued_qty"]').val();
        if(parseFloat(avail_qty) < parseFloat(issue_qty)) {
            return_val = false;
            return false;
        }
        return return_val;
    });
    
    $('form[name="inv_store_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            supplier_id: {
                required: true
            },
            category_id: {
                required: true
            },
            item_id: {
                required: true
            },
            issued_qty: {
                required: true,
                valid_issued_qty: true
            }
        },
        messages: {
            supplier_id: {
                required: 'Please select Supplier Name!'
            },
            category_id: {
                required: 'Please select Category Name!'
            },
            item_id: {
                required: 'Please select Item Name!'
            },
            issued_qty: {
                required: 'Please enter Issued Quantity!',
                valid_issued_qty: 'Issued Quantity should not be greater than Received Quantity!'
            }
        }
    });

});

function goBack() {
    window.history.back();
}