$(function() {

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

	$('select[name="type"]').on('change', function() {
		let opt = parseInt($('select[name="type"]').val());
		let inputs = ['rg'];
		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('');
			}
		}
	});

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

	// 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');
			}
		}
	});

	// 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');
	});

	// Verificar formulário antes de envio
	$('form#form_validator button').on('click', function(e) {
		e.preventDefault();

		if( BSValidator.handleSubmit() ) {
			let providerType = parseInt($('select[name="provider_service"]').val());

			if(providerType != 0) {
				$('select[name="provider_service"]').css('border-color', '#ced4da');

				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('');

							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('');

							form.submit();
						}
					} else {
						utils.showError('cnpj', 'Campo Obrigatório!');
					}
				}

			} else {
				$('select[name="provider_service"]').css('border-color', '#FF0000');
				utils.showAlerts('.message-add', 'Selecione o segmento do Fornecedor!');
			}
		}
	});

	// Atualizar status de Fornecedores
	$('table#upStatus button').on('click', function() {
		let provider_of = $(this).attr('data-of');
		let astatus = $(this).attr('data-status');
		let ref = $(this).attr('data-button');

		utils.updateStatusProvider(ref, astatus, provider_of);
	});

	$('select[name="type"]').change();
});