$(document).ready(function() {
    $.validator.addMethod("already_exist_category_name", function(value, element) {
        var return_val = true;
        var category_name = $('input[name="category_name"]').val();
        var id = $('input[name="hidden_id"]').val();
        $.ajax({
            async: false,
            url: base_url + 'statue_category/category_name_exists',
            type: 'POST',
            data: {
                category_name: category_name,
                category_id: id
            },
            dataType: "json"
        })
        .done(function(data) {
            if(data == 1) {
                return_val = false;
            }
        })
        .fail(function(jqXHR, status_code, error_thrown) {
            console.log(jqXHR.responseText);
        });
        
        return return_val;
    });
    
    $('form[name="category_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            code: {
                required: true
            },
            category_name: {
                required: true,
                already_exist_category_name: true
            }
        },
        messages: {
            code: {
                required: 'Please enter Category Code!'
            },
            category_name: {
                required: 'Please enter Category Name!',
                already_exist_category_name: 'Category Name already Exists!'
            }
        }
      });
      
      
    $.validator.addMethod("already_exist_item_name", function(value, element) {
        var return_val = true;
        var category_id = $('select[name="category_id"]').val();
        var id = $('input[name="hidden_id"]').val();
        var item_name = $('input[name="item_name"]').val();
        $.ajax({
            async: false,
            url: base_url + 'statue_item/item_name_exists',
            type: 'POST',
            data: {
                category_id: category_id,
                id: id,
                item_name: item_name
            },
            dataType: "json"
        })
        .done(function(data) {
            if(data == 1) {
                return_val = false;
            }
        })
        .fail(function(jqXHR, status_code, error_thrown) {
            console.log(jqXHR.responseText);
        });
        
        return return_val;
    });
    
    $('form[name="item_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            category_id: {
                required: true
            },
            item_name: {
                required: true,
                already_exist_item_name: true
            }
        },
        messages: {
            category_id: {
                required: 'Please select Category Name!'
            },
            item_name: {
                required: 'Please enter Item Name!',
                already_exist_item_name: 'Item name already Exists!'
            }
        }
    });
    
    $.validator.addMethod("already_exist_vendor_name", function(value, element) {
        var return_val = true;
        var id = $('input[name="hidden_id"]').val();
        var name = $('input[name="name"]').val();
        $.ajax({
            async: false,
            url: base_url + 'statue_vendor/vendor_name_exists',
            type: 'POST',
            data: {
                id: id,
                name: name
            },
            dataType: "json"
        })
        .done(function(data) {
            if(data == 1) {
                return_val = false;
            }
        })
        .fail(function(jqXHR, status_code, error_thrown) {
            console.log(jqXHR.responseText);
        });
        
        return return_val;
    });
    
    $('form[name="vendor_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            name: {
                required: true
                //already_exist_vendor_name: true
            },
            email: {
                email: true
            }
        },
        messages: {
            name: {
                required: 'Please enter Vendor Name!'
                //already_exist_item_name: 'Vendor name already Exists!'
            },
            email: {
                email: 'Please enter Valid Email Address!'
            }
        }
    });
    
    
    $.validator.addMethod("check_valid_qty", function(value, element) {
        var return_val = true;
        var item_id = $('input[name="item_id"]').val();
        var qty = parseFloat($('input[name="old_qty"]').val()) - parseFloat($('input[name="qty"]').val()); 
        if($('input[name="qty"]').val() != '') {
            if($('[name="hidden_id"]').val() != '') {        
                $.ajax({
                    async: false,
                    url: base_url + 'statue_books/check_valid_qty',
                    type: 'POST',
                    data: {
                        item_id: item_id
                    },
                    dataType: "json"
                })
                .done(function(data) {
                   if(data) {
                       var diff_qty = parseFloat(data.total_quantity) - parseFloat(qty);
                       var min_qty = parseFloat($('input[name="old_qty"]').val()) - parseFloat(data.total_quantity);
                        if( parseFloat(diff_qty) < parseFloat(0) ) {
                            $('input[name="qty"]').rules('add', {
                                messages: {
                                    check_valid_qty: 'Already used this Quantity. So,the minimum quantity is ' + min_qty
                                }
                            }); 
                            /* msgBox('Already used this Quantity. So,the minimum quantity is ' + data.total_quantity);*/
                            //$('input[name="qty"]').val('');
                            return_val = false;
                        } else {
                            return_val = true;
                        }
                   }
                })
                .fail(function(jqXHR, status_code, error_thrown) {
                    console.log(jqXHR.responseText);
                });
            }
        }
        return return_val;
    });
    
    $('form[name="statue_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            cat_id: {
                required: true
            },
            item_id: {
                required: true
            },
            vendor_id: {
                required: true
            },
            qty: {
                required: true,
                check_valid_qty: true
            },
            cost_per_item: {
                required: true
            }
        },
        messages: {
            cat_id: {
                required: 'Please select Category Name!'
            },
            item_id: {
                required: 'Please select Item Name!'
            },
            vendor_id: {
                required: 'Please select Vendor Name!'
            },
            qty: {
                required: 'Please enter Quantity!',
                check_valid_qty: 'Already used this Quantity'
            },
            cost_per_item: {
                required: 'Please enter the Cost per Item!'
            }
        }
    });
    
    $('.qty').keyup(function() {
        var _this = parseFloat($(this).val()) || 0;
        var cost = parseFloat($('.cost_per_item').val()) || 0;
        var total = parseFloat(_this) * parseFloat(cost);
        $('.total_amt').val((parseFloat(total)).toFixed(2));
    });
    
    $('.cost_per_item').keyup(function() {
        var _this = parseFloat($(this).val()) || 0;
        var qty = parseFloat($('.qty').val()) || 0;
        var total = parseFloat(_this) * parseFloat(qty);
        $('.total_amt').val((parseFloat(total)).toFixed(2));
    });
    
    $('[name="cat_id"]').change(function() {
        $.ajax({
                async: false,
                url: base_url + 'statue_books/get_item_by_cat_id',
                type: 'POST',
                data: {
                    cat_id: $(this).val()
                },
                dataType: "json"
            })
            .done(function(data) {
                var statue_data ='';
                var arraylength = data.items.length;
                statue_data += '<option value="">Selet Item</option>';
                for(var i =0; i < arraylength; i++) {
                    statue_data += '<option value="'+data.items[i]['id']+'">'+data.items[i]['item_name']+'</option>';
                }
                $('select[name="item_id"]').html(statue_data);               
            })
            .fail(function(jqXHR, status_code, error_thrown) {
                console.log(jqXHR.responseText);
            }); 
    });
    
});



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