$(function() {
	// Verificação de campos antes de enviar formulário
	$('form button').on('click', function(e) {
		e.preventDefault();
		verifyBeforeSend();
	});

	// Campo de rg, caso seja digitado algo.
	$('input[name="rg"]').on('blur', function() {
		let type_view = $('input[name="type_view"]').val();
		if(type_view != 'edit') {
			if($(this).val() != '') {
				let result = utils.verifyExistsRg($(this).val(), 'rg');
			}
		}
	});

	// Campo de cpf, caso seja digitado algo.
	$('input[name="cpf"]').on('blur', function() {
		let type_view = $('input[name="type_view"]').val();
		if(type_view != 'edit') {
			if($(this).val() != '') {
				utils.verifyExistsCpf($(this).val(), 'cpf');
			}
		}
	});

	// Campo de cnpj, caso seja digitado algo.
	$('input[name="cnpj"]').on('blur', function() {
		let type_view = $('input[name="type_view"]').val();
		if(type_view != 'edit') {
			if($(this).val() != '') {
				utils.verifyExistsCnpj($(this).val(), 'cnpj');
			}
		}
	});

	/* CPF OR CNPJ */
	$('input[name="cnpj"]').addClass('oculted');

	$('select[name="type"]').on('change', function() {
		let opt = parseInt($('select[name="type"]').val());
		let inputs = ['rg', 'birthdate'];
		let type_view = $('input[name="type_view"]').val();

		if(opt == 1) {			
			enableDisable(inputs, false);
			$('input[name="cpf"]').removeClass('oculted');
			$('input[name="cpf"]').addClass('viewed');
			$('input[name="cnpj"]').removeClass('viewed');
			$('input[name="cnpj"]').addClass('oculted');
		}
		if(opt == 2) {
			enableDisable(inputs, true);
			$('input[name="cnpj"]').removeClass('oculted');
			$('input[name="cnpj"]').addClass('viewed');
			$('input[name="cpf"]').removeClass('viewed');
			$('input[name="cpf"]').addClass('oculted');

			if(type_view != 'edit') {
				$('input[name="rg"]').val('');
				$('input[name="cnpj"]').val('');
				$('input[name="cpf"]').val('');
				$('input[name="birthdate"]').val('');
			}
		}
	});
	/* END CPF OR CNPJ */

	function enableDisable(ref, status) {
		for(let i in ref) {
			$('input[name='+ref[i]+']').prop('disabled', status);
		}
	}

	// Load cep
	$('#inputCep').on('keyup', function() {
		if($(this).val().length == 9) {
			utils.changeEnder($(this).val(), 'public_place');
		}
	});

	// List Cities
	$('select[name="state"]').on('change', function() {
		utils.getListCity($(this).val(), 'city');
	});

	/*Auto complete Buyer Data*/
	$('#gridCheck').click(function() {
		if($('#gridCheck').is(':checked')) {			
			$('input[name=buyer_name]').val($('input[name=name]').val());
			$('input[name=buyer_phone]').val($('input[name=first_phone]').val());
			$('input[name=buyer_email]').val($('input[name=email]').val());
			$('input[name=buyer_birthdate]').val($('input[name=birthdate]').val());
		} else {
			$('input[name=buyer_name]').val('');
			$('input[name=buyer_phone]').val('');
			$('input[name=buyer_email]').val('');
			$('input[name=buyer_birthdate]').val('');
		}
	});

	// Verificar formulário antes de envio
	function verifyBeforeSend() {
		let typeCLient = parseInt($('select[name="type"]').val());

		if(typeCLient == 1) {			
			if($('input[name="cpf"]').val() != '') {
				if($('select[name="city"]').val() == null) {
					$(this).css('border-color', '#FF0000');
					$('div.errorc').html('Cidade não selecionada!');
				} else {
					$(this).css('border-color', '#ced4da');
					$('div.errorc').html('');

					if( BSValidator.handleSubmit() ) {	
						form.submit();
					}
				}
			} else {
				utils.showError('cpf', 'Campo Obrigatório!');
			}
		} else {
			if($('input[name="cnpj"]').val() != '') {
				if($('select[name="city"]').val() == null) {
					$(this).css('border-color', '#FF0000');
					$('div.errorc').html('Cidade não selecionada!');
				} else {
					$(this).css('border-color', '#ced4da');
					$('div.errorc').html('');

					if( BSValidator.handleSubmit() ) {	
						form.submit();
					}
				}
			} else {
				utils.showError('cnpj', 'Campo Obrigatório!');
			}
		}
	}
	
	$('select[name="type"]').change();
});