app

	.component('appGesguia',{
		templateUrl: './views/gesguia/gesguia.view.html',
		controller: CtrlGesguia
	});

	CtrlGesguia.$inject = ['$scope','api.service','form.service','alert.service'];
	
	function CtrlGesguia(scope,api,form,alert) 
	{
		
		scope.progviaje = [];
		scope.gesguia = [];
		//scope.tipocert = [];
		//scope.cerchofer = [];
		//scope.archivo = null;
		
		Init = () => 
		{
			scope.spinner = true;

			/*
			api.list('Gesguia/parametros').then(res => {
				scope.progviaje = res.data.progviaje;
			})
			*/

			api.list('Gesguia/lista').then(res => {
				scope.gesguia = res.data;
			})
			.finally(fin => scope.spinner = false);
		}

		
		scope.openModal = (index,object = {}) => 
		{
			Clean();

			switch(index)
			{
				case 1:
					scope.modal.eguia = true;
					scope.modal.title = "Generar Guia Transportista SUNAT";
					
					scope.modal.model = angular.copy(object);

					form.modal('eguia');

					/*
					scope.modal.model.id_chofer=object.id_chofer;
					scope.modal.model.id_tipocertificado=object.id_tipocertificado;
					scope.modal.model.descripcion=object.descripcion;
					
					
					if (object.estado == 0){
						scope.modal.model.estado=true;
					}else{
						scope.modal.model.estado=false;
					}
					*/

			}
		}

		
		scope.dowloadfile = (cerchofe) => 
		{
			//console.log(cerchofe.documento);
			window.open(cerchofe.documento, '_blank');
		}



		scope.saveguia = () => 
		{
			scope.modal.spinner = true;
			scope.modal.fails   = {};
			
			console.log(scope.modal.model);
			/*
			let promise;
			let bs64 = convertToBase64(); 

			bs64.then((value) => 
			{
				const data = {
					fecha_emision: formatDate(scope.modal.model.fecha_emision),
					fecha_vencimiento: formatDate(scope.modal.model.fecha_vencimiento),
					descripcion: scope.modal.model.descripcion,
					id_tipocertificado: scope.modal.model.id_tipocertificado,
					id_chofer: scope.modal.model.id_chofer,
					archivo: value
				}
				

				if(scope.modal.model.id){
				}else{
					data.estado=0;
					promise = api.store('CertificadoChofer',data);
				}
				//console.log(data);	
				promise.then(res => Process(res)).catch(error => ManagerError(error)).finally(fin => scope.modal.spinner = false);
				
			});
			*/	
	
		}

		/*
		scope.saveup = () => 
		{
			
			scope.modal.spinner = true;
			scope.modal.fails   = {};

			let promise;
			
			var es_f 
			if (scope.modal.model.estado == false){
				es_f=1;
			}else
			{
				es_f=0;
			}
			const data = {
				
				id : scope.modal.model.id,
				id_tipocertificado: scope.modal.model.id_tipocertificado,
				id_chofer: scope.modal.model.id_chofer,
				estado :es_f
			}
			
			//console.log(data)
			promise = api.update('CertificadoChofer',data);
			promise.then(res => Process(res)).catch(error => ManagerError(error)).finally(fin => scope.modal.spinner = false);
			
	
		}


		scope.delete = (cerchofe) => 
		{
			scope.deleted = cerchofe;
			alert.confirm("¡Se eliminara a " + cerchofe.chofer.nombres +" - "+ cerchofe.tipocertificado.tipo + " de la lista de certificados, esta operación es irreversible!",Delete);
		}
		
		Delete = (cerchofe) => 
		{
			//console.log(scope.deleted.id);
			scope.spinner_delete = true;

			
			api.delete('CertificadoChofer',scope.deleted.id).then(res => {
				if(!res.data.success) return ManagerError(res.data.message);
				alert.success(res.data.message);
				Init();
			})
			.catch(error => ManagerError(error))
			.finally(fin => scope.spinner_delete = false);
		
		}

		*/
		Process = (res) => 
		{
			if(res.data.fails)
				return scope.modal.fails = res.data.fails;

			if(!res.data.success)
				return alert.warning(res.data.message);
			
			alert.success(res.data.message);
			form.modal('edit',false);
			form.modal('editdisebled',false);
			Init();
		}

		ManagerError = (error) => {
			alert.warning(error);
		}

		Clean = () => {
			scope.modal = {model: {},fails: {}};
			document.getElementById('formFile_pf').value= null;
		}
		

		Init();
	}

	/*
	function formatDate(date) {
		var d = new Date(date),
			month = '' + (d.getMonth() + 1),
			day = '' + d.getDate(),
			year = d.getFullYear();
	
		if (month.length < 2) 
			month = '0' + month;
		if (day.length < 2) 
			day = '0' + day;
	
		return [year, month, day].join('-');
	}

	async function convertToBase64(){
		var selectedFile = document.getElementById("formFile_pf").files;
		if (selectedFile.length > 0) {
			let file = selectedFile[0];
			let reader = new FileReader()
			reader.readAsDataURL(file)
			await new Promise(resolve => reader.onload = () => resolve())
			return reader.result.replace(/^data:.+;base64,/, '');
		}
	}
	*/