$(document).ready(function() {
    $('.add_type').click(function() {
        show_popup();
    });
    
    $('.cancel_popup').click(function(){
        $(".modal-backdrop").remove();
    });
    
    $('#type_modal').on('hidden.bs.modal', function () {
        $('form[name="addType"] .error_container').html('');
    });
    
    $.validator.addMethod('type_already_exists', function(value, element) {
        var return_val = true;
        var type = $('#type_modal input[name="new_type"]').val();
        $.ajax({
            async: false,
            url: base_url + 'inventory_gold/check_type_exists',
            type: 'POST',
            data: {
                type: type
            },
            dataType: "json"
        })
        .done(function(data) {
            if(data == 1)
                return_val = false;
            else
                return_val = true;
        })
        .fail(function(XHR, status_code, error_thrown) {
            console.log(error_thrown);
        });
        return return_val;
    });
    
    
    /* $.validator.addMethod('temple_type_exists', function(value, element) {
        var return_val = true;
        var type = $('input[name="type"]').val();
        var id = $('input[name="hidden_id"]').val();
        $.ajax({
            async: false,
            url: base_url + 'inventory_temple/type_exists',
            type: 'POST',
            data: {
                id: id,
                type: type
            },
            dataType: "json"
        })
        .done(function(data) {
            if(data == 1)
                return_val = false;
            else
                return_val = true;
        })
        .fail(function(XHR, status_code, error_thrown) {
            console.log(error_thrown);
        });
        return return_val;
    }); */
    
    $('form[name="addType"]').validate({
        errorContainer:'form[name="addType"] .error_container',
        errorLabelContainer:'form[name="addType"]  .error_container',
        errorElement: 'li',
        invalidHandler: function(event, validator) {
            $(this).animate(
                {
                    scrollTop: $(validator.currentForm).offset().top
                },
                '500',
                'swing'
            );
        },
        rules: {
            new_type: {
                required: true,
                type_already_exists: true
            }
        },
        messages: {
            new_type: {
                required: 'Please enter Type!',
                type_already_exists: 'Type already Exists!'
            }
        },
        submitHandler: function(form) {
            add_new_type();
            return false;
        }
    });
    
    
    
    $('form[name="inv_gold_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            /* name: {
                required: true
            },
            contact_no: {
                required: true
            }, */
            type_id: {
                required: true
            },
            item: {
                required: true
            },
            weight_in_grams: {
                required: true
            }
        },
        messages: {
            /* name: {
                required: 'Please enter Name!'
            },
            contact_no: {
                required: 'Please enter Contact No!'
            }, */
            type_id: {
                required: 'Please select Type!'
            },
            item: {
                required: 'Please enter Item!'
            },
            weight_in_grams: {
                required: 'Please enter Weight in Grams!'
            }
        }
    });
    
    
    $('form[name="inv_temple_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            type: {
                required: true,
                temple_type_exists: true
            }
        },
        messages: {
            type: {
                required: 'Please enter Type!',
                temple_type_exists: 'Type already exists!'
            }
        }
    });

});

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

function add_new_type() {
    var type = $('input[name="new_type"]').val();
    $.ajax({
        async: false,
        url: base_url + 'inventory_gold/add_new_type',
        type: 'POST',
        data: {
            type: type
        },
        dataType: "json"
    })
    .done(function(data) {
        if(data) {
            $("#type_modal").css('display','none');
            $(".modal-backdrop").remove();
            var option = '<option value="' + data + '" selected>' + type + '</option>';
            $('select[name="type_id"]:last').append(option);
        }
    })
    .fail(function(XHR, status_code, error_thrown) {
        console.log(error_thrown);
    });
}

function show_popup(){
    $('body').append('<div class="modal-backdrop fade in"></div>');
    $('.modal-backdrop').addClass('in');
    $("#type_modal").css('display','block');
    $("#type_modal").modal({
        show: true      
    });
    $('#type_modal input[name="new_type"]').val('');
}