

<?php
include_once("../header.php");
      include_once("../php/config.php");
      $sql = mysql_query("select * from tasas  where CONCAT(moneda,'',compra, ' ',venta) like '%".$_POST['txt_buscar']."%'order by id desc
   limit 20") or die(mysql_error());

?>



<style>
  .form-control{
    font-size: 16px;
	font-weight:bolder;
  }


label{
    font-size: 18px;
  }
.nav{
 @ display: none;
}
td, th{
	font-size:18px;

}


.container{
  background:white;
  padding: 15px;
}
</style>
    <div class="container">

 <hr>

		<div class="row">
			<div class="col-md-6 mt-2">



        <script type="text/javascript">
          function modalProducto(){
			    $("#frm").trigger('reset');
            $("#modalProducto").modal('show');


        }



        </script>


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

   
                  <span class="text-rojo h2"><i class="bi bi-cash-coin"></i>Remesas \ Recarga</span><br>
<hr>
			   <div class="row">
          <div class="col-xs-2">Codigo</div>
          <div class="col-xs-9">
            <input autocomplete="off" type="text"  class="form-control mb-2 bold" id="id" name="id" readonly>
          </div>
        </div>

        <div class="row">
         <div class="col-xs-2">Moneda</div>
         <div class="col-xs-9">
           <input autocomplete="off" type="text"  class="form-control mb-2 bold" id="moneda" name="moneda" value="">
         </div>
       </div>

       <div class="row">
        <div class="col-xs-2">Compra</div>
        <div class="col-xs-9">
          <input autocomplete="off" type="text"  class="form-control mb-2 bold" id="compra" name="compra" value="">
        </div>
       </div>


       <div class="row">
        <div class="col-xs-2">Venta</div>
        <div class="col-xs-9">
          <input autocomplete="off" type="text"  class="form-control mb-2 bold" id="venta" name="venta" value="">
        </div>
       </div>


       		  </form>





            <button class="btn btn-rojo" type="button" onclick="grabar_producto()" id="btngrabar"><i class="bi bi-download"></i> Actualizar datos</button>

  <button class="btn btn-secondary" type="button" onclick="window.location.href='tasas.php'" id="delete"><i class="bi bi-file"></i> Nuevo</button>
				<button class="btn btn-secondary" type="button" onclick="borrar()" id="delete"><i class="bi bi-trash"></i> Borrar</button>

            <button class="btn btn-secondary" data-dismiss="modal"> <i class="bi bi-arrow-clockwise"></i> Salir</button>
          </div>






      <div class="col-md-6 mt-2">
        <form action="" method="POST" id="frm">
          <div class="row">
            <div class="col-xs-2">Buscar</div>
            <div class="col-xs-10"><input type="text" class="form-control" id='txt_buscar' name='txt_buscar' placeholder='Buscar...'></div>
          </div>
          <br>
        <div class="table-responsive border p-3">

					<table class="table" data-page-length='100' style="width: 100%">


              <th>Moneda</th>

              <th>Compra</th>
              <th>Venta </th>

			   
              <th>Editar</th>


						<tbody>
            <?php while($r=mysql_fetch_array($sql)){?>
			<?php
			$c++;
			
      $id = $r["id"];
			?>
							<tr>

								<td><a class="" href="#modalProducto" data-toggle="modal" onClick='ver(<?php echo $r["id"]?>)'><i class="bi bi-stickies"></i> <?php echo $r["moneda"]?></a></td>
                <td><?php echo @decimal($r["compra"])?></td>
                <td><?php echo @decimal($r["venta"])?></td>

				

               <td><a class="text-primary" href="#"  onClick='ver(<?php echo $r["id"]?>)'><i class="bi bi-pen"></i> </a></td>


            </tr>
                                                        <?php }?>
						</tbody>
					</table>
        </form>
					<hr>
					<b>CANT. PRODCUTOS:</b> <?php echo $c;?>
					 
				</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;
        }
		$hoy = @date('Y-m-d');
		$fecha_a = date('Y-m-d', strtotime($hoy.  '+ 30 days'));

        ?>

<script>


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

            var sql="select * from tasas 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_producto= rs.data[i].id;
                    $("#moneda").val(rs.data[i].moneda);

                   
					           
                    $("#id").val(rs.data[i].id);
                    $("#compra").val(rs.data[i].compra);
                    $("#venta").val(rs.data[i].venta);

                  }

            })
        }




  function grabar_producto(){
			     var moneda = $("#moneda").val();
				var id = $("#id").val();
				if(id==""){
				var param = "moneda="+moneda;
				$.get("verificar_existe.php?"+param, function(data,st){
					if(st=="success"){
						if(data>0){
								alert("Este registro ya existe")
						}else{
							grabar_producto2()
						}
					}
				})
			}else{
				grabar_producto2()
			}
  }

  function grabar_producto2(){
    ///// if(!confirm("Seguro(a) que desea registrar este producto?")){return;}

     var id = $("#id").val()
     var moneda = $("#moneda").val();
     var compra = $("#compra").val();


     var venta = $("#venta").val();

    

	 if(compra==""){compra=0;}

	if(venta==""){venta=0;}

	$("#btngrabar").prop("disabled", true)

     if(moneda==""){alert('Faltan datos'); return}
    var sql="";
        if(id==""){
            sql="INSERT INTO tasas set ";
            sql+=" moneda='"+moneda+"'";
            sql+=", compra='"+compra+"'";
            sql+=", venta='"+venta+"'";
            sql+=", remesa='"+venta+"'";
           


        }else{

            sql="UPDATE tasas set";
            sql+=" moneda='"+moneda+"'";
            sql+=", compra='"+compra+"'";
            sql+=", venta='"+venta+"'";
            sql+=", remesa='"+venta+"'";
            sql+=" WHERE id='"+id+"'";

        }

    _enviar("../php/update.php",{"sql":sql})
    .done(function( data ){
      //var rs = JSON.parse(data);
      if(data=="1"){
	     window.location.reload();

      }else{
          alert(data);

      }

    })
}





 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 tasas where id='"+id+"'";

    _enviar("../php/update.php",{"sql":sql})
    .done(function( data ){
      //var rs = JSON.parse(data);
      if(data=="1"){
        $("#modalcobrador").modal('hide');
        noti("success","Registro eliminado con exito","Registro grabado");
        $("#delete").hide();
        dstry();
        id_producto="";
		
        setTimeout(function(){window.location.reload()},1000)

      }else{
          msg(data);

      }
        $("#loading").modal('hide');
    })
}

 
 function uniKeyCode(event) {
  var key = event.keyCode;
  //alert("Unicode KEY code: " + key);
  if(key=="13"){

		////if($("#txt_buscar").val()!=""){
			filtrar();
		///}

	  }

}


        </script>
