$(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 */

	/* ADD OPERATIONAL AREA */
	$('#add_are').on('click', function(e) {
		e.preventDefault();

		var alvo = $('#operational_area');
		var state = $('select[name="states"]').val();
		var city = $('select[name="city"] :selected').text();

		if(alvo.html() != '') {
			html = alvo.val();
			html += city +' - '+state+'\n';
		} else {
			html = (city +' - '+state+'\n');
		}
		alvo.html(html);
	});

	// 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.verifyExistsSalesman($(this).val(), 'cpf', 'CPF já cadastrado!');
			}
		}
	});

	// 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.verifyExistsSalesman($(this).val(), 'cnpj', 'CNPJ já cadastrado!');
			}
		}
	});

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

	$('#form_validator button').on('click', function(e) {
		e.preventDefault();
		if( BSValidator.handleSubmit() ) {	
			form.submit();
		}
	});

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

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


	$('#save_comission').on('click', function() {
		let provider = $('select[name="provider"]').val();
		let percentage = $('input[name="comission_percent"]').val();

		if(provider != '' && provider != 0) {

			saveComissionSalesman(provider, percentage);

			// if(percentage != '') {

			// 	saveComissionSalesman(provider, percentage);

			// } else {
			// 	utils.showAlerts('.message-add', 'Digite o valor da comissão!');
			// }

		} else {
			utils.showAlerts('.message-add', 'Selecione o Fornecedor!');
		}
	});

	function saveComissionSalesman(provider, percentage) {
		let salesman = $('input[name="id"]').val();

		$.ajax({
			url:BASE_URL+'savecomissionsalesman',
			type:'POST',
			data:{
				salesman:salesman,
				provider:provider,
				percentage:percentage
			},
			dataType:'json',
			success:function(json) {
				if(json == true) {
					$('select[name="provider"]').val('');
					$('input[name="comission_percent"]').val('');
					utils.showAlerts('.message-add', 'Salvo com sucesso!');

					setTimeout(function() {
						location.reload();
					}, 2000);					

				} else {
					utils.showAlerts('.message-add', 'Algo saiu errado!');
				}
			}
		});
	}

	// Exibir comissão atual ao selecionar fornecedor (Se existir)
	$('select[name="provider"]').on('change', function() {
		$('input[name="comission_percent"]').val('');
		let provider = $('select[name="provider"]').val();
		let salesman = $('input[name="id"]').val();

		$.ajax({
			url:BASE_URL+'showcomissionsalesman',
			type:'POST',
			data:{
				salesman:salesman,
				provider:provider
			},
			dataType:'json',
			success:function(json) {
				if(json.status == true) {					
					$('input[name="comission_percent"]').val(json.value);
				}
			}
		});
	});
});