let request = {
	getClientInfo:(client) => {
		$.ajax({
			url:BASE_URL+'getclient',
			type:'POST',
			data:{
				client:client
			},
			dataType:'json',
			success:function(json) {
				if(json) {
					$('#c_name').val(json.name);

					if(json.cpf) {
						$('#c_cpf_cnpj').val(json.cpf);
					} else {
						$('#c_cpf_cnpj').val(json.cnpj);
					}

					$('#c_buyer_name').val(json.buyer);

					if(json.first_phone) {
						$('#c_buyer_phone').val(json.first_phone);
					} else {
						$('#c_buyer_phone').val(json.second_phone);
					}

					// Informações do vendedor
					if(json.id_salesman) {
						$('input[name="salesman_selected"]').val(json.id_salesman);
						$('input[name="search_salesman"]').val(json.salesman_name);
						$('#salesman_name').val(json.salesman_name);	
					} else {
						$('input[name="salesman_selected"]').val('');
						$('input[name="search_salesman"]').val('');
						$('#salesman_name').val('');
					}
				}
			}
		});
	},
	getClientFarms:(client, ref) => {
		$.ajax({
			url:BASE_URL+'getfarmsclient',
			type:'POST',
			data:{
				client:client
			},
			dataType:'json',
			success:function(json) {
				if(json) {
					var selectedValue = '';
					var change = false;
					let qt = json.length;
					if(qt == 1) {
						selectedValue = "selected";
						change = true;
					}
					

					var html = '<option value="0">Selecionar</option>';
					for(let i in json) {
						html += '<option '+selectedValue+' value="'+json[i].id+'">'+json[i].name+'</option>';
					}
					$('select[name='+ref+']').html(html);

					if(change) {
						$('select[name='+ref+']').change();
					}
				}
			}
		});
	},
	getFarmInfo:(farm) => {
		$.ajax({
			url:BASE_URL+'getfarminfo',
			type:'POST',
			data:{
				farm:farm
			},
			dataType:'json',
			success:function(json) {
				if(json) {
					$('#farm_name').val(json.name);
					$('#farm_responsible').val(json.responsible);
					$('#farm_phone').val(json.phone);
					$('#farm_state').val(json.state);
					$('#farm_city').val(json.city_name);
				}
			}
		});
	},
	getProviderInfo:(provider, salesman = '') => {
		$.ajax({
			url:BASE_URL+'getproviderinfo',
			type:'POST',
			data:{
				provider:provider,
				salesman:salesman
			},
			dataType:'json',
			success:function(json) {
				if(json) {
					$('#provider_name').val(json.company_name);

					if(json.cpf) {
						$('#provider_type').val(json.cpf);
					} else {
						$('#provider_type').val(json.cnpj);
					}

					$('#provider_contact').val(json.contact_name);

					if(json.first_phone) {
						$('#provider_phone').val(json.first_phone);
					} else {
						$('#provider_phone').val(json.second_phone);
					}
				}
			}
		});
	},
	getProducts:(provider, ref) => {
		$.ajax({
			url:BASE_URL+'productsproviderwithstock',
			type:'POST',
			data:{
				provider:provider
			},
			dataType:'json',
			success:function(json) {
				if(json) {
					var html = '<option value="0">Selecionar</option>';
					for(let i in json) {
						html += '<option value="'+json[i].id+'" data-groupid="'+json[i].group_name.id+'" data-group="'+json[i].group_name.name+'" data-unit="'+json[i].unity_name.name+'" data-presentation="'+json[i].presentation_name.name+'" data-costprice="'+json[i].cost_price+'" data-price="'+json[i].price+'"  data-comvalue="'+json[i].value_comission+'" >'+json[i].name+'</option>';
					}
					$('select[name='+ref+']').html(html);
				}
			}
		});
	},
	insertProductInRequest:(product_group, request_id, product_id, percent_supp, quantity, cost_price, price_sale, provider_id, type_request, reference_request) => {
		$.ajax({
			url:BASE_URL+'addproductinrequest',
			type:'POST',
			data:{
				product_group:product_group,
				request_id:request_id,
				product_id:product_id,
				percent_supp:percent_supp,
				quantity:quantity,
				cost_price:cost_price,
				price_sale:price_sale,
				provider_id:provider_id,
				type_request:type_request,
				reference_request:reference_request
			},
			dataType:'json',
			success:function(json) {
				if(json.status == true) {
					$('#percent_supp').val('');
					$('#product_group').val('');
					$('#product_unit').val('');
					$('#product_presentation').val('');
					$('#product_quantity').val('');
					$('#product_price_sale').val('');
					$('#product_price_coust').val('');
					$('#product_value_item').val('');
					$('#profitMargin').val('');
					$('select[name="product_list"]').val(0);
					$('input[name="total_amount"]').val(json.total_request);
					$('#insert_products').html(json.items_show);
					$('input[name="request_reference"]').val(json.reference);
					$('input[name="read_to_save"]').val(json.count);
					request.getCoustPerUnity();

					$('#salesman_percentage').keyup(); // acionar função que calcula comissão vendedor

				} else if(json.status == false) {
					utils.showAlerts('.message-add', json.msg);
				}
			}
		});
	},
	removeItem:(item, reference_request) => {
		$.ajax({
			url:BASE_URL+'removeitem',
			type:'POST',
			data:{
				product:item,
				request_id:reference_request
			},
			dataType:'json',
			success:function(json) {
				if (json.status == true) {

					$('#insert_products').html(json.items_show);					
					$('input[name="total_amount"]').val(json.total_request);

					if(json.count > 0) {
						$('input[name="read_to_save"]').val(1);
						request.getCoustPerUnity();
					} else {
						$('input[name="read_to_save"]').val('');
						$('#result_est_uniti input, #result_est_real input').val('');
					}
				}
			}
		});
	},
	getTotalItem:(qt, value) => {
		$.ajax({
			url:BASE_URL+'getvalueitem',
			type:'POST',
			data:{
				qt:qt,
				value:value
			},
			dataType:'json',
			success:function(json) {
				if(json) {
					$('#product_value_item').val(json);
				}
			}
		});
	},
	getCoustPerUnity:() => {
		$.ajax({
			url:BASE_URL+'getcoustperunity',
			type:'POST',
			dataType:'json',
			success:function(json) {
				if(json.status == true) {					
					$('#estimative_coust_for_unity').val(json.coust_per_unity);
					request.getTotalCoust();
				} else if(json.status == false) {
					utils.showAlerts('.message-add', json.msg);
				}
			}
		});
	},
	getTotalCoust:() => {
		var product_group = $('#product_groupid').val();
		var stmt_coust_for_unity = $('#estimative_coust_for_unity').val();
		var stmt_freight_price_unity = $('#estimative_freight_price_unity').val();
		var stmt_others_unity = $('#estimaive_others_unity').val();
		var stmt_commision_salesman = $('#estimative_commision_salesman').val();

		$.ajax({
			url:BASE_URL+'gettotalcoust',
			type:'POST',
			data:{
				product_group:product_group,
				stmt_coust_for_unity:stmt_coust_for_unity,
				stmt_freight_price_unity:stmt_freight_price_unity,
				stmt_others_unity:stmt_others_unity,
				stmt_commision_salesman:stmt_commision_salesman
			},
			dataType:'json',
			success:function(json) {
				if(json.status == true) {					

					$('#estimative_total_couste').val(json.totalCoust);
					request.getResultEstimate();

				} else if(json.status == false) {
					utils.showAlerts('.message-add', json.msg);
				}
			}
		});
	},
	getResultEstimate:() => {
		let product_group = $('#product_groupid').val();
		let estimative_total_couste = $('#estimative_total_couste').val();

		$.ajax({
			url:BASE_URL+'getresultestimate',
			type:'POST',
			data:{
				product_group:product_group,
				estimative_total_couste:estimative_total_couste
			},
			dataType:'json',
			success:function(json) {
				if(json.status == true) {
					
					$('#estimative_result').val(json.estimative_result);
					$('#estimative_result_real').val(json.estimative_result_real);

				} else if(json.status == false) {
					utils.showAlerts('.message-add', json.msg);
				}
			}
		});
	},
	getComissionSalesman:(percentage) => {
		$.ajax({
			url:BASE_URL+'getcomissionsalesman',
			type:'POST',
			data:{
				percentage:percentage
			},
			dataType:'json',
			success:function(json) {
				if(json.status == true){
					
					$('#real_to_unity, #estimative_commision_salesman').val(json.reais_to_unity);
					$('#salesman_comission_value').val(json.total_comission);

					request.getTotalCoust();

				} else if(json.status == false) {
					utils.showAlerts('.message-add', json.msg);
				}
			}

		});
	},
	getComissionPercentage:(provider, salesman, valueComission) => {
		$.ajax({
			url:BASE_URL+'getsalesmancomissionpercentage',
			type:'POST',
			data:{
				provider:provider,
				salesman:salesman,
				value:valueComission
			},
			dataType:'json',
			success:function(json) {
				if(json.status == true){
					
					$('input[name="salesman_percentage"]').val(json.value);
					$('#exist_comission_salesman').attr('checked', true);
				}
			}

		});
	},
	verifyDate:(date, type_requisition, ref) => {
		$.ajax({
			url:BASE_URL+'verifydate',
			type:'POST',
			data:{
				date:date,
				type_requisition:type_requisition
			},
			dataType:'json',
			success:function(json) {
				if(json) {
					$('#'+ref).attr('status', true);
				} else {
					$('#'+ref).attr('status', false);
					utils.showAlerts('.message-add', 'Data Inválida!');
				}
			}

		});
	},
	initDeliveries:(array) => {
		$.ajax({
			url:BASE_URL+'initdelivery',
			type:'POST',
			data:{
				delivery:array
			},
			dataType:'json',
			success:function(json) {
				if(json.status == true) {

					setTimeout(function() {
						$(location).attr('href', BASE_URL+'deliveryitem&d='+json.id_delivery);
					}, 1000);

				} else {
					utils.showAlerts('.message-add', 'Parece que algo deu errado!');	
				}
			}

		});
	},
	updateRequestNote:(note, ref) => {
		$.ajax({
			url:BASE_URL+'updatenoterequest',
			type:'POST',
			data:{
				note:note,
				ref:ref
			},
			dataType:'json',
			success:function(json) {
				if(json == true) {
					setTimeout(function() {
						$(location).attr('href', BASE_URL+'requests');
					}, 1000);
				}
			}
		});
	},
	cancelRequest:(note, ref) => {
		$.ajax({
			url:BASE_URL+'cancelrequest',
			type:'POST',
			data:{
				note:note,
				ref:ref
			},
			dataType:'json',
			success:function(json) {
				if(json == true) {
					setTimeout(function() {
						$(location).attr('href', BASE_URL+'requests');
					}, 1000);
				}
			}
		});
	},
	calculateValueProfit(costPrice, price) {
		$.ajax({
			url:BASE_URL+'getprofitvalue',
			type:'POST',
			data:{
				costprice:costPrice,
				pricesale:price
			},
			dataType:'json',
			success:function(json) {
				if(json) {
					$('#profitMargin').val(json);
				}
			}
		});
	}
}