

  <?php
	include_once("../header.php");
        include_once("../php/config.php");
        $sql = mysql_query("select * from rutas") or die(mysql_error());
        
        //$sql_cartera = mysql_query("select * from cartera where estado_cartera='1' order by nombre asc") or die(mysql_error());
	?>
<style>
th,td{
	@font-size:18px;
}

th{
	text-align:center;
}
</style>

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


b{
    font-size: 18px;
  }

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

    td{
    font-size:18px;
  }
</style>

<div class="container" style="background:white; padding:15px"> 
 <div class="row">
    
      <div class="col-md-12">
 
				        <a class="btn  btn-rojo mb-2 " href="javascript:void(0)" onClick="modalruta()"><i class="fa fa-plus"></i><b> Registrar una nueva ruta</b></a><br>


	   <script type="text/javascript">
          function modalruta(){
            $("#modalruta").modal('show');
            setTimeout(function(){
             $("#frm").trigger('reset'); 
            },500)
            
        }
        </script>
        <div class="table-responsive mt-2">
          <table class="table table-sm  table-hover table-striped">
           
              <th width="100"># ruta</th>
              <th>Nombre</th>

             
            
              <th width="45">Zona</th>
			  <th width="45">Opciones</th>
           
            <tbody>
           <?php while($r=mysql_fetch_array($sql)){?>
              <tr>
                <td><?php echo $r["id"]?></td>
                <td><?php echo $r["nombre"]?></td>
				 <td><?php echo $r["zona"]?></td>

               <td ><a class="text-info mb-2" href="#modalruta" data-toggle="modal" onClick='ver(<?php echo $r["id"]?>)'><i class="fa fa-edit"></i> Editar</a></td>
              </tr>
                                                        <?php }?>
            </tbody>
          </table>

	    </div>
		 </div>
		  </div>
    </div>
</div>
        </div>
      </div>
      


          
</div>
        


     </div>
   </div>
   
		
    </div>

	<!---Modal Cliente-->
	<div class="modal fade" id="modalruta" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <span class="modal-title text-rojo" id="exampleModalLabel"><i class="fa fa-w fa-key"></i> Registro de ruta</h3>
            <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" type="text"  class="form-control mb-2" id="nombre" name="nombre" value="">
          </div>
        </div>
			  
         <div class="row">
          <label for="id" class="col-sm-3 col-form-label" >Zona</label>
          <div class="col-sm-9">
            <input autocomplete="off" type="text"  class="form-control mb-2" id="zona" name="zona" value="">
          </div>
        </div>
		
		

        

          
			  
			  
		  </form>
		  
		  </div>
          <div class="modal-footer">
            <button class="btn btn-orange" type="button" onclick="grabar_ruta()"><i class="fa fa-w fa-save"></i> Grabar</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();
var id="";
function ver(id){
    $("#delete").show();
    dstry();
      $("#frm").trigger('reset');
      setTimeout(function(){
        $("#nombre").focus();
        $("#nombre").select();
    },1000);

            var sql="select * from rutas where id='"+id+"'"
            _enviar("../php/traer_datos.php",{"sql":sql})
            .done(function( data ){
                var rs = JSON.parse(data);
       
                for( var i in rs.data){
                     id= rs.data[i].id;             
                    $("#nombre").val(rs.data[i].nombre);
                    $("#zona").val(rs.data[i].zona);
                    $("#id").val(rs.data[i].id);
                 
                  
                  
                }
                $("#loading").modal('hide');
            })
        }
  
  function grabar_ruta(){
     if(!confirm("Seguro(a) que desea registrar esta ruta?")){return;}                   
    
     var nombre = $("#nombre").val();
     //var cartera = $("#cartera").val();
     var zona = $("#zona").val();
     
     id = $("#id").val();
     ///if( $('#estado').prop('checked') ) {
    /////   estado="1";
   //// }else{
    
   //// }



     if(zona=="" || nombre==""){msg('Faltan datos'); return}
    var sql="";                            
        if(id==""){
            sql="INSERT INTO rutas set ";
            sql+=" nombre='"+nombre+"'";
            sql+=", zona='"+zona+"'";
           
           

        }else{
            sql="UPDATE rutas set";
            sql+=" nombre='"+nombre+"'";
            sql+=", zona='"+zona+"'";
            sql+=" WHERE id='"+id+"'";
           
            
        }
    _enviar("../php/update.php",{"sql":sql})
    .done(function( data ){
      //var rs = JSON.parse(data);
      if(data=="1"){
        $("#modalruta").modal('hide');
        noti("success","Registro grabado con exito","success");
        $("#delete").hide();
       
        id="";
        setTimeout(function(){window.location.reload()},1000)

      }else{
          alert(data);
          
      }
        $("#loading").modal('hide');
    })
}
  

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

      }else{
          msg(data);
          
      }
        $("#loading").modal('hide');
    })
}



            ///$(".table").DataTable();
         
        </script>

