$(document).ready(function() {
    $.validator.addMethod("already_exist", function(value, element) {
        var return_val = true;
        var hidden_id = $('input[name="hidden_id"]').val();
        var field_name = 'passport_no';
       
        $.ajax({
            async: false,
            url: base_url + 'employee/check_fields_exists',
            type: 'POST',
            data: {
                field_name: field_name,
                val: $('.passport_no').val(),
                id: hidden_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;
    });
    
    $.validator.addMethod("already_exist_work_permit_no", function(value, element) {
        var return_val = true;
        var hidden_id = $('input[name="hidden_id"]').val();
        var field_name = 'work_permit_no';
        $.ajax({
            async: false,
            url: base_url + 'employee/check_fields_exists',
            type: 'POST',
            data: {
                field_name: field_name,
                val: $('.work_permit_no').val(),
                id: hidden_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;
    });
    
    $.validator.addMethod("already_exist_security_no", function(value, element) {
        var return_val = true;
        var hidden_id = $('input[name="hidden_id"]').val();
        var _this = $('.check_exist');
        var field_name = 'security_bond_no';
        $.ajax({
            async: false,
            url: base_url + 'employee/check_fields_exists',
            type: 'POST',
            data: {
                field_name: field_name,
                val: $('.security_bond_no').val(),
                id: hidden_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="employee_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            name: {
                required: true
            },
            address: {
                required: true
            },
            passport_no: {
                required: true,
                already_exist: true
            },
            work_permit_no: {
                required: true,
                already_exist_work_permit_no: true
            },
            security_bond_no: {
                required: true,
                already_exist_security_no: true
            }
        },
        messages: {
            name: {
                required: 'Please enter Employee Name!'
            },
            address: {
                required: 'Please enter Address!'
            },
            passport_no: {
                required: 'Please enter Passport No!',
                already_exist: 'Passport No already exists!'
            },
            work_permit_no: {
                required: 'Please enter Work Permit No!',
                already_exist_work_permit_no: 'Work Permit No already exists!'
            },
            security_bond_no: {
                required: 'Please enter Security Bond No!',
                already_exist_security_no: 'Security Bond No already exists!'
            }
        }
      });
      
      $('[type="file"]').on('change', function(){
            var prevImg = $(this).attr('data-value') || "";
            if(prevImg != '') {
                $(this).after('<input type="hidden" name="prev-image" value="'+ prevImg +'">');
            }
      });
      $('.img-preview .rm-btn').on('click', function(){
          var $this = $(this);
          if( window.confirm("Are you sure and want to remove..?") ){
              // Ok
              var $parent = $this.parent().closest('.form-group') //img-preview
              ,   $input = $parent.find('[type="file"]')
              ,   value  = $input.attr('data-value');  
              
              $this.after('<input type="hidden" name="rm-image" value="'+ value +'">');
              
              $this.remove();
              $input.val('');
              $parent.find('img').remove();
          };
          
          return false;
          
      })
});

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