$(function() {

	$('#option').ready(function() {
		var searchoption = $('#option').val();

		if(searchoption == 3) {
			$('#providerList').hide();
			$('#prod_search').show();
		} else if(searchoption == 4) {
			$('#prod_search').hide();
			getProviders();
			$('#providerList').show();
		}
	});

	$('#option').change(function() {
		var searchoption = $(this).val();

		if(searchoption == 'clear' || searchoption == 1 || searchoption == 2) {
			setTimeout(function() {
				$('#prod_search').hide();
				$('#providerList').hide();
			}, 500);

		} else if(searchoption == 3) {
			setTimeout(function() {
				$('#providerList').hide();
				$('#prod_search').show();
				$('#prod_search').focus();
			}, 500);

		} else if(searchoption == 4) {
			setTimeout(function() {
				getProviders();
				$('#prod_search').hide();
				$('#providerList').show();
			}, 500);			
		}
	});

	function getProviders() {				
		utils.getListProvider('providerList');
	}

	$('button[type="submit"]').on('click', function(e) {
		e.preventDefault();

		if( BSValidator.handleSubmit() ) {

			if($('select[name="provider"]').val() != 0) {
				utils.errorImputs('select', 'provider', '#ced4da');
				utils.messageErrorImputs('div.pro_error', '', false);

				if($('select[name="group"]').val() != 0) {
					utils.errorImputs('select', 'group', '#ced4da');
					utils.messageErrorImputs('div.gro_error', '', false);

					if($('select[name="unity"]').val() != 0) {
						utils.errorImputs('select', 'unity', '#ced4da');
						utils.messageErrorImputs('div.uni_error', '', false);

						if($('select[name="presentation"]').val() != 0) {

							form.submit();

						} else {
							utils.errorImputs('select', 'presentation', '#FF0000');
							utils.messageErrorImputs('div.pre_error', 'Selecione a Apresentação!', true);	
						}

					} else {
						utils.errorImputs('select', 'unity', '#FF0000');
						utils.messageErrorImputs('div.uni_error', 'Selecione uma Unidade!', true);
					}


				} else {
					utils.errorImputs('select', 'group', '#FF0000');
					utils.messageErrorImputs('div.gro_error', 'Selecione o Grupo!', true);
				}

			} else {
				utils.errorImputs('select', 'provider', '#FF0000');
				utils.messageErrorImputs('div.pro_error', 'Selecione o Fornecedor!', true);
			}
		}
	});

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

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

});