$(function() {

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

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

		if(opt == 1) {
			$('input[name="cpf"]').removeClass('oculted');
			$('input[name="cpf"]').addClass('viewed');
			$('input[name="cnpj"]').removeClass('viewed');
			$('input[name="cnpj"]').addClass('oculted');
		}
		if(opt == 2) {
			$('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="cnpj"]').val('');
				$('input[name="cpf"]').val('');
			}
		}
	});
	/* END CPF OR CNPJ */

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

		utils.updateStatusShipping(ref, astatus);
	});

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

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