<style>
label{
	font-size:16px;
	font-family:Arial;
	@text-decoration:underline;
	color:sky-blue;
	font-wight:lighter;
}
</style>


  <?php
	include_once("../header.php");
  ///if($_GET['filtro']!=""){
        include_once("../php/config.php");
        $sql = mysql_query("select * from clientes where nombre like '%".$_GET['filtro']."%' order by nombre asc") or die(mysql_error());
    ////}
	?>

<style>
  .form-control{
    font-size: 20px;
  }


b{
    font-size: 18px;
  }

  th{
    font-size:20px;
    text-align: left;
  }

    td{
    font-size:16px;
  }
</style>
    <div class="container-fluid">

        
		<div class="row">
			<div class="col-md-12 ">

		<div class="row">
    <div class="col-md-3   mt-2 mb-2">	   
				<a class="btn btn-success" href="javascript:void(0)" onClick="modalcliente()"><i class="fa fa-plus"></i> Nuevo</a>
        
        <a class="btn btn-default  text-dark" href="creditos.php" ><i class="fa fa-calendar"></i> Creditos</a>
    </div>

     <div class="col-md-3 mb-2  mt-2">
   <input type="search" id="txt_buscar" placeholder="Filtrar registros" class="form-control" value="<?php echo $_GET['filtro']?>">
 </div>
  <div class="col-md-3 mb-2  mt-2">
        <a class="btn btn-default" href="#" onClick="filtrar()"><i class="fa fa-search"></i><span style="font-weight:bolder;"> Filtrar</span></a>

		 <a class="btn btn-default text-dark" href="../menu/menu.php" ><i class="fa fa-undo"></i> Salir</a>
 </div>
  </div>

   <script>
   function filtrar(){
    var t = $("#txt_buscar").val();
    window.location.href="clientes.php?filtro="+t;
   }
 </script>    


        <script type="text/javascript">
          function modalcliente(){
            $("#modalcliente").modal('show');
            setTimeout(function(){
             $("#frm").trigger('reset'); 
            },500)
            
        }
        
        </script>
       
				<div class="table-responsive  p-3" >
					<table class="table  table-hover table-striped" data-page-length='8' style="font-family:calibri">
					
							<th>Id cliente</th>
							<th>Nombre</th>
             <th>Cedula</th>
              <th>Telefono</th>
			   <th>Maximo credito</th>
              <th>Editar</th>
					
						<tbody>
            <?php while(@$r=mysql_fetch_array($sql)){?>
							<tr>
								<td><?php echo $r["id_cliente"]?></td>
                <td><a class="" href="#modalcliente" data-toggle="modal" onClick='ver(<?php echo $r["id_cliente"]?>)'><?php echo $r["nombre"]?></a></td>
               <td><?php echo $r["cedula"]?></td>
               <td><?php echo $r["telefono"]?></td>
				 <td><?php echo @decimal($r["max_credito"])?></td>
              
             
               <td><a class="" href="#modalcliente" data-toggle="modal" onClick='ver(<?php echo $r["id_cliente"]?>)'><i class="fa fa-edit"></i></a></td>
							</tr>
                                                        <?php }?>
						</tbody>
					</table>
				</div>
			</div>
		</div>
		
        </div>
      </div>
      


          
</div>
    </div>

	<!---Modal cliente-->
	<div class="modal fade" id="modalcliente" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-md" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h2 class="modal-title text-dark" id="exampleModalLabel"><i class="fa fa-w fa-edit"></i> Registro de clientes</h2>
            <button class="close" type="button" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">×</span>
            </button>
          </div>
          <div class="modal-body">


		  <form name="frm" action="" method="POST" id="frm">
			 
     

        <div class="row">
  				<label for="id" class="col-sm-3 col-form-label" >Id</label>
  				<div class="col-sm-3">
  				  <input autocomplete="off" type="text"  class="form-control  mb-2" id="id" value="" disabled>
  				</div>
			  </div>
			  
			   <div class="row">
          <label for="id" class="col-sm-3 col-form-label" >Nombre</label>
          <div class="col-sm-9">
            <input autocomplete="off" style="font-size:18px;" type="text"  class="form-control mb-2 font-weight-bolde" id="nombre" name="nombre" value="">
          </div>
        </div>

         <div class="row">
          <label for="cedula" class="col-sm-3 col-form-label" >Cedula</label>
          <div class="col-sm-9">
            <input autocomplete="off" type="text"  class="form-control mb-2" id="cedula" name="cedula" value="">
          </div>
        </div>


  <div class="row mt-2">
    <label for="cedula" class="col-sm-3 col-form-label" >Tipo NCF</label>
        <div class="col-sm-9">
            <select class="form-control input-sm" id="ncf" onchange="get_ncf()" style="font-size:14px">

    <option value="" selected></option>
    <?php $sql2 = mysql_query("select * from ncf order by prefijo desc") ?>

    <?php while ($r = mysql_fetch_array($sql2)) { ?>
      <option value="<?php echo $r['prefijo'] ?>" <?php if ($r['prefijo'] == 'B02') {
                                                    echo 'selected';
                                                  } ?>><?php echo $r['tipo'] ?><?php echo $r['nombre'] ?></option>
    <?php } ?>
    </select>
    </div>
</div>

<div class="row mt-2 mb-2">
    <label for="cedula" class="col-sm-3 col-form-label" >Termino venta</label>
        <div class="col-sm-9">
            <select class="form-control input-sm" id="termino" onchange="get_ncf()" style="width:200px">
              <option value="Efectivo" selected>Efectivo</option>
              <option value="Credito">Credito</option>
             </select>
        </div>
</div>

			  
         <div class="row">
          <label for="telefono" class="col-sm-3 col-form-label" >Telefono</label>
          <div class="col-sm-9">
            <input autocomplete="off" type="text"  class="form-control mb-2" id="telefono" name="telefono" value="">
          </div>
        </div>

        <div class="row">
          <label for="direccion" class="col-sm-3 col-form-label" >Direccion</label>
          <div class="col-sm-9">
            <input autocomplete="off" type="text"  class="form-control mb-2" id="direccion" name="direccion" value="">
          </div>
        </div>
		 <div class="row">
          <label for="direccion" class="col-sm-3 col-form-label" >Maximo credito</label>
          <div class="col-sm-3">
            <input autocomplete="off" type="number"  class="form-control mb-2" id="max_credito" name="max_credito" value="">
          </div>
        </div>


  


			  
			  
		  </form>
		  
		  </div>
          <div class="modal-footer">
            <button class="btn btn-danger" type="button" onclick="grabar_cliente()"><i class="fa fa-w fa-save"></i> ACTUALIZAR DATOS </button>

                  <button class="btn btn-default" type="button" onclick="borrar()" id="delete"><i class="fa fa-w fa-trash"></i> BORRAR</button>

            <a class="btn btn-default" data-dismiss="modal"> <i class="fa fa-w fa-home"></i> SALIR</a>
          </div>
        </div>
      </div>
    </div>

        <?php
        include_once("../footer.php");

        function state($est) {

            if ($est == "1") {
                $b = "<span class='badge badge-success'>Activo</span>";
            } else {

                $b = "<span class='badge badge-danger'>Inactivo</span>";
            }

            return $b;
        }
        ?>
            
<script>
   


$("#delete").hide();
id_cliente="";
function ver(id){
    $("#delete").show();
    dstry();
      $("#frm").trigger('reset');
      setTimeout(function(){
        $("#nombre").focus();
        $("#nombre").select();
    },1000);

            var sql="select * from clientes where id_cliente='"+id+"'"
            _enviar("../php/traer_datos.php",{"sql":sql})
            .done(function( data ){
                var rs = JSON.parse(data);
       
                for( var i in rs.data){
                    id_cliente= rs.data[i].id_cliente;             
                    $("#nombre").val(rs.data[i].nombre);
                    $("#cedula").val(rs.data[i].cedula);
                    
                    $("#id").val(rs.data[i].id_cliente);
                    $("#telefono").val(rs.data[i].telefono);
                    $("#direccion").val(rs.data[i].direccion);
				          	$("#max_credito").val(rs.data[i].max_credito);
                    $("#ncf option[value='"+rs.data[i].tipo_ncf+"']").prop('selected', true);
                    $("#termino option[value='"+rs.data[i].termino+"']").prop('selected', true);
                    opener.getCliente(rs.data[i].nombre)
					self.close();
					opener.set_ticket();
                }
                
            })
        }
  
  
  
  
  
  
  
  
  
  
  
  
    function grabar_cliente(){
				var nombre = $("#nombre").val();
				var id = $("#id").val();
				if(id==""){
				var param = "nombre="+nombre
				$.get("verificar_existe.php?"+param, function(data,st){
					if(st=="success"){
						if(data>0){
								alert("Ya existe un cliente con esta referencia")
						}else{
							grabar_cliente2()
						}
					}
				})
				
			}else{
				grabar_cliente2()
			}
  }
  
  
  
  
  
  function grabar_cliente2(){
     if(!confirm("Seguro(a) que desea registrar este cliente?")){return;}                   
    
     var nombre = $("#nombre").val();
     var cedula = $("#cedula").val();
     var telefono = $("#telefono").val();
     var direccion = $("#direccion").val();
     var ncf = $("#ncf").val();
     var termino = $("#termino").val();
	 var max_credito = $("#max_credito").val();
   id_cliente = $("#id").val();
    



     if(nombre==""){alert('Faltan datos'); return}
    var sql="";                            
        if(id_cliente==""){
            sql="INSERT INTO clientes set ";
            sql+="nombre='"+nombre+"'";
            sql+=", cedula='"+cedula+"'";
            sql+=", telefono='"+telefono+"'";
            sql+=", direccion='"+direccion+"'"; 
            sql+=", termino='"+termino+"'"; 
            sql+=", tipo_ncf='"+ncf+"'"; 
			sql+=", max_credito='"+max_credito+"'"; 
           
        }else{
            sql="UPDATE clientes set";
            sql+=" nombre='"+nombre+"'";
            sql+=", cedula='"+cedula+"'";
            sql+=", telefono='"+telefono+"'";
            sql+=", direccion='"+direccion+"'"; 
            sql+=", termino='"+termino+"'"; 
            sql+=", tipo_ncf='"+ncf+"'"; 
			sql+=", max_credito='"+max_credito+"'"; 
            sql+=" WHERE id_cliente='"+id_cliente+"'";
           
            
        }
    _enviar("../php/update.php",{"sql":sql})
    .done(function( data ){
      //var rs = JSON.parse(data);
      if(data=="1"){
       
        noti("success","Registro grabado con exito","Registro grabado");
        $("#delete").hide();
        dstry();
        id_cliente="";
         $("#modalcliente").modal('hide');
        setTimeout(function(){window.location.reload()},2000)

      }else{
          alert(data);
          
      }
       
    })
}
  

 function borrar(){
  
     if(id_cliente==""){alert('Seleccione un registro'); return}
     if(!confirm("Seguro(a) que desea eliminar este registro?")){return;}
    var sql="";                            
    sql="delete from clientes where id_cliente='"+id_cliente+"'";
        
    _enviar("../php/update.php",{"sql":sql})
    .done(function( data ){
      //var rs = JSON.parse(data);
      if(data=="1"){
        $("#modalcliente").modal('hide');
        noti("success","Registro eliminado con exito","Registro grabado");
        $("#delete").hide();
        dstry();
        id_cliente="";
        setTimeout(function(){window.location.reload()},1000)

      }else{
          alert(data);
          
      }
       
    })
}


   $(".form-control").addClass("auto-save");      
        </script>

