<?php 
session_start();
?>
<div class="content-header" >
      <div class="container-fluid">
        <div class="row mb-2">
		 <div class="col-sm-6">
			<h1>
			Clientes
			<small>modulo</small>
			</h1>
		</div>	
		
		<div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
				<li class="breadcrumb-item" >Inicio</li>
				<li class="breadcrumb-item" >Menu</li>
				<li class="breadcrumb-item active">Clientes</li>
            </ol>
          </div>
		</div>
	</div>
</div>
<section class="content">
	<div class="container-fluid">
	<div class="row">
	    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="padding:0px;">
		    <div class="card">
		        <div class="card-header">
		          <button id="btn_agregar" onClick="cliente_form('insertar')" class="btn btn-primary">Agregar</button>
		          <button id="btn_actualizar" class="btn btn-default">Actualizar</button>	
		          
		          <span class="btn btn-success float-right" id="btn_excel"> <i class="fa fa-file-excel-o"></i> DESCARGAR EXCEL </span>
				
				
		        </div>
		    </div>
		</div>
	</div>
	</div>
</section>
	<!-- formulario de registro -->
			<div class="modal fade" id="div_cliente_form_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px;">
			<div class="modal-dialog modal-xl " style="width: 90% !important;" role="document" >
			<div class="modal-content">
			<div class="modal-body div_cliente_form" >
			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
			<div class="modal fade" id="div_clientedireccion_form_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px;">
			<div class="modal-dialog modal-lg "  role="document" >
			<div class="modal-content">
			<div class="modal-body div_clientedireccion_form" >
			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
			<div class="modal fade" id="div_clientedireccion_form_dialog2" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px;">
			<div class="modal-dialog modal-lg "  role="document" >
			<div class="modal-content">
			<div class="modal-body div_clientedireccion_form2" >
			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			<!-- vehiculooooooooooooooooooooooooooooooooooos-->
			
			
			<div class="modal fade" id="div_clientevehiculos_form_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px;">
			<div class="modal-dialog modal-lg "  role="document" >
			<div class="modal-content">
			<div class="modal-body div_clientevehiculos_form" >
			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			<!-- Correoooooooo-->
			
			<div class="modal " id="div_mensaje_dialog" role="dialog" data-keyboard="false" tabindex="-1"  style="padding:0px; overflow-y:scroll;">
			<div class="modal-dialog modal-md"  role="document" >
			<div class="modal-content" style="padding:0px;">
			<div class="modal-body div_mensaje"  >
			
		
			
			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
			
			<div class="modal fade" id="div_clientecorreo_form_dialog" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px;">
			<div class="modal-dialog modal-lg "  role="document" >
			<div class="modal-content">
			<div class="modal-body div_clientecorreo_form" >
			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
			<div class="modal fade" id="div_clientevehi_form_dialog2" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px;">
			<div class="modal-dialog modal-lg "  role="document" >
			<div class="modal-content">
			<div class="modal-body div_clientevehi_form2" >
			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
			<div class="modal fade" id="div_clientecorreo_form_dialog2" role="dialog" data-keyboard="false" data-backdrop="static" tabindex="-1"  style="padding:0px;">
			<div class="modal-dialog modal-lg "  role="document" >
			<div class="modal-content">
			<div class="modal-body div_clientecorreo_form2" >
			</div>
			<br />
			<br />
			</div>
			</div>
			</div>
			
			
			
			
				
<!-- Buscar registros. -->
<div id="div_cliente_filtro"></div>
<!-- Listar registros -->
<div id="div_cliente_tabla"  ></div>




<script type="text/javascript">
$("#file_archivo").fileinput({
        browseClass: "btn btn-success ",
		 browseLabel:"",
		 browseIcon: "<i class=\"fa fa-file-excel-o\"></i> ",
         showPreview : false,
		showCaption: false,
        showRemove: false,
        showUpload: false
    });
	
	
$("#file_archivo").change(function(){
	
	var file = $("#file_archivo")[0].files[0];
	var fileName = file.name;
	fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
	
	var formData = new FormData($("#formularioarchivo")[0]);
	if(fileExtension == "xlsx" || fileExtension == "xls"){
		
		$.ajax({
		type: "POST",
		url: "../cliente/importar_datos.php",
		cache: false,
        contentType: false,
        processData: false,                
		data: formData,
		beforeSend: function() {
			$("#msj_imporcompra").html('Procesando el archivo subido...');	
			$("#msj_imporcompra").show();	
        },
		success: function(rpt){
			
		$("#msj_imporcompra").hide();	
		toastr["info"]('Archivo procesado correctamente');
			
		},
		complete: function(){			
			cliente_tabla();
		},
		error:function (response)
		{
			  $("#msj_imporcompra").html('ERROR!! RECARGAR LA PAGINA Y VUELVA A SUBIR EL ARCHIVO.');	
		}
	});     
		
	}else{
		toastr["error"]('Seleccione un archivo Excel');
	}
});
	
function cliente_filtro(){			
	$.ajax({						
		url: "../cliente/cliente_filtro.php",
		async:true,
		dataType: "html",                      
		data: ({
		}),
		beforeSend: function() {
			$('#div_cliente_filtro').addClass("ui-state-disabled");
        },
		success: function(html){
			$('#div_cliente_filtro').html(html);
		},
		complete: function(){			
			$('#div_cliente_filtro').removeClass("ui-state-disabled");
			//cliente_tabla();
		}
	});        
}
function cliente_tabla(){			
	$.ajax({	
		type: "POST",					
		url: "../cliente/cliente_tabla.php",
		async:true,
		dataType: "html",                      
		data: ({
			cli_id: $("#hdd_fil_cli_id").val()
		}),
		beforeSend: function() {
			$('#div_cliente_tabla').addClass("ui-state-disabled");
    },
		success: function(html){
			$('#div_cliente_tabla').html(html);
		},
		complete: function(){			
			$('#div_cliente_tabla').removeClass("ui-state-disabled");
		}
	});        
}
	
function cliente_form(act,idf){
	$('#div_cliente_form_dialog').modal();
	$.ajax({
		type: "POST",
		url: "../cliente/cliente_form.php",
		async:true,
		dataType: "html",                      
		data: ({
			action: act,
			cli_id:	idf,
			vista:	'cliente_tabla'
		}),
		beforeSend: function() {
			var div = "<center> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></center>";	
			$('.div_cliente_form').html(div);
        },
		success: function(html){
			$('.div_cliente_form').html(html);		
		}
	});
}
function direccion_cliente(idf){
	$('#div_clientedireccion_form_dialog').modal();
	$.ajax({
		type: "POST",
		url: "../cliente/clientedireccion_form.php",
		async:true,
		dataType: "html",                      
		data: ({
			cli_id:	idf,
			vista:	'tabla'
		}),
		beforeSend: function() {
			var div = "<center> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></center>";	
			$('.div_clientedireccion_form').html(div);
        },
		success: function(html){
			$('.div_clientedireccion_form').html(html);		
		}
	});
}
function vehiculo_cliente(idf){
	$('#div_clientevehiculos_form_dialog').modal();
	$.ajax({
		type: "POST",
		url: "../cliente/clientevehiculo_form.php",
		async:true,
		dataType: "html",                      
		data: ({
			cli_id:	idf,
			vista:	'tabla'
		}),
		beforeSend: function() {
			var div = "<center> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></center>";	
			$('.div_clientevehiculos_form').html(div);
        },
		success: function(html){
			$('.div_clientevehiculos_form').html(html);		
		}
	});
}
function correo_cliente(idf){
	$('#div_clientecorreo_form_dialog').modal();
	$.ajax({
		type: "POST",
		url: "../cliente/clientecorreo_form.php",
		async:true,
		dataType: "html",                      
		data: ({
			cli_id:	idf,
			vista:	'tabla'
		}),
		beforeSend: function() {
			var div = "<center> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></center>";	
			$('.div_clientecorreo_form').html(div);
        },
		success: function(html){
			$('.div_clientecorreo_form').html(html);		
		}
	});
}
function eliminar_cliente(id)
{      
	if(confirm("Realmente desea eliminar?")){
		$.ajax({
			type: "POST",
			url: "../cliente/cliente_reg.php",
			async:true,
			dataType: "html",
			data: ({
				action: "eliminar",
				cli_id:		id
			}),
			beforeSend: function() {
				
			},
			success: function(html){
				toastr["success"](html);
			},
			complete: function(){
				cliente_tabla();
			}
		});
	}
}


	function abrirEnPestana(url) {
		var a = document.createElement("a");
		a.target = "_blank";
		a.href = url;
		a.click();
}
	
$(function() {
	
	
		
	$("#btn_excel").click(function(){
		
			abrirEnPestana('../cliente/cliente_excel.php');
});

	
	$('#btn_actualizar').click(function() {
		$("#contenido").load("../cliente/cliente_vista.php");
	});
		cliente_filtro();
		setTitle("CLIENTE - "+nombreEmpresa);
	
});
</script>