let delivery = {
	createNewCharge:(data) => {
		$.ajax({
	        url: BASE_URL+'createnewcharge',
	        type: 'POST',
	        data:{
	            delivery_charge:data
	        },
	        dataType: 'json',
	        success:function(json) {

	            if(json == true) {                            
	                location.reload();
	            }
	        }
	    });
	},
	getInfoDelivery:(ref) => {
		$.ajax({
	        url: BASE_URL+'getdeliveryinfo',
	        type: 'POST',
	        data:{
	            delivery:ref
	        },
	        dataType: 'json',
	        success:function(json) {
	            if(json) {

	            	delivery.verifyShowAndHide(json.itemInfo.status, json.itemInfo.product_group);
	            	delivery.showItemInfo(json, ref);
	            }
	        }
	    });
	},
	showItemInfo:(data, ref) => {
		$('input[name="id_item_delivery"]').val(ref);
		$('input[name="product_selected_group"]').val(data.itemInfo.product_group);
		$('input[name="edit_date"]').val(data.itemInfo.date_edition);
		$('input[name="balance"]').val(data.deliveryInfo.balance);
		$('input[name="amount_preview"]').val(data.itemInfo.amount);
		$('input[name="amount_balance"]').val(data.itemInfo.amount_balance);

		$('input[name="delivery_date"]').attr('status', false); // Negando o campo de data para forçar o usuário a inserir a data, caso não esteja entregue

		// Se estiver entregue, exibir data normal
		if(data.itemInfo['status'] == 6) {
			$('input[name="delivery_date"]').val(data.itemInfo.date_deliverie);
		}

		if(data.itemInfo.value_delivery_balance) {
			$('input[name="total_balance"]').val(data.itemInfo.value_delivery_balance);
		} else {
			$('input[name="total_balance"]').val(data.itemInfo.value_delivery);
		}

		$('input[name="shipping_invoice"]').val(data.itemInfo.shipping_invoice);
		$('textarea[name="note"]').val(data.itemInfo.note_delivery);

		$('.edit-charge').show(100);
	},
	saveDeliveryInfo:(data) => {
		$.ajax({
	        url: BASE_URL+'updatedeliveryinfo',
	        type: 'POST',
	        data:{
	            deliveryInfo:data
	        },
	        dataType: 'json',
	        success:function(json) {
	            if(json) {

	            	$('.edit-charge').hide(100);
	            	location.reload();
	            }
	        }
	    });
	},
	verifyDateDelivery:(date) => {
		$.ajax({
	        url: BASE_URL+'verifydeliverydate',
	        type: 'POST',
	        data:{
	            dateDelivery:date
	        },
	        dataType: 'json',
	        success:function(json) {

	            if(json) {
	            	$('input[name="delivery_date"]').attr('status', true);
	            	
	            } else {
	            	$('input[name="delivery_date"]').attr('status', false);
	            	utils.showAlerts('.message-add', 'Data da entrega não é válida!');
	            }
	        }
	    });
	},
	getUpdatedValueDelivery:(balance, ref) => {
		$.ajax({
	        url: BASE_URL+'getupdatedvaluedelivery',
	        type: 'POST',
	        data:{
	            balance:balance,
	            item:ref
	        },
	        dataType: 'json',
	        success:function(json) {

	        	if(json.status) {
	        		$('input[name="amount_balance"]').attr('status', true);
	        		$('input[name="total_balance"]').val(json.value);
	        	}
	            
	        }
	    });
	},
	setDeliveryComplete:(ref, p_group, amount_balance, total_value, shipping_invoice, dateDelivery, note) => {
		$.ajax({
	        url: BASE_URL+'setdeliverycomplete',
	        type: 'POST',
	        data:{
	        	item:ref,
	        	p_group:p_group,
	            balance:amount_balance,
	            value:total_value,
	            shipping_invoice:shipping_invoice,
	            dateDelivery:dateDelivery,
	            note:note
	        },
	        dataType: 'json',
	        success:function(json) {

	        	if(json) {
	        		location.reload();
	        	}
	            
	        }
	    });
	},
	verifyShowAndHide:(status, group) => {

		if(status == 6 || status == 7) {

			$('#delivery-complete').attr('disabled', true);
			$('#amount_balance').attr('readonly', true);

		} else {

			$('#amount_balance').attr('readonly', false);
			$('#save-info-delivery, #delivery-complete').attr('disabled', false);
		}

		if(group == 2) {
			$('#amount_balance').attr('readonly', true);
		}

	},
	existsShippingInvoice:(number, provider) => {
		$.ajax({
	        url: BASE_URL+'verifyshippinginvoice',
	        type: 'POST',
	        data:{
	        	value:number,
	        	provider:provider
	        },
	        dataType: 'json',
	        success:function(json) {

	        	if(json) {
	        		$('#shipping_invoice').attr('status', false);
	        		$('#save-info-delivery').attr('disabled', true);
	        		utils.showAlerts('.message-add', 'Nota Inválida e/ou já cadastrada para outro Fornecedor!');
	        	} else {
	        		$('#shipping_invoice').attr('status', true);
	        		$('#save-info-delivery').attr('disabled', false);
	        	}
	            
	        }
	    });
	},
	setDeliverieCanceled:(item, reason_canceled) => {
		$.ajax({
	        url: BASE_URL+'setdeliveriecanceled',
	        type: 'POST',
	        data:{
	        	item:item,
	        	text:reason_canceled
	        },
	        dataType: 'json',
	        success:function(json) {

	        	if(json) {
	        		location.reload();
	        	}
	            
	        }
	    });
	},
	zeroBalance:(ref) => {
		$.ajax({
	        url: BASE_URL+'zerobalance',
	        type: 'POST',
	        data:{
	        	delivery:ref
	        },
	        dataType: 'json',
	        success:function(json) {

	        	if(json) {
	        		location.reload();
	        	}
	            
	        }
	    });
	}
}