<?php
require_once "db.class.php";

if ($_POST['accion']=='mostrar_tabla'){

    $cadena="<center><br><br><table border='0' cellspacing='0' cellpadding='0' width='100%'>";
    $cadena.="<tr><td colspan='4'></td></tr>";
    $cadena.="<tr>";
    $cadena.="<td><b>CODIGO</b></td><td><input style='font-weight:bold;text-align:center;' type='text' id='codigo' size='2' ></td>";
    $cadena.="<td><b>DESCRIPCI&Oacute;N</b></td><td><input  style='font-weight:bold;text-align:center;' type='text' id='descripcion' size='25' onkeyup = 'this.value=this.value.touppercase()'></td>";

    $cadena.="<td><b>INFERIOR</b></td><td><input style='font-weight:bold;text-align:center;' type='text' id='inferior' onkeyup = 'this.value=this.value.touppercase()' size='10'></td>";
    $cadena.="<td><b>V1</b></td><td><input style='font-weight:bold;text-align:center;' type='text' id='v1' size='2' onclick=\"abrir_dialog('v1');\" readonly=\"readonly\"></td>";

    $cadena.="<td><b>SUPERIOR</b></td><td><input style='font-weight:bold;text-align:center;' type='text' id='superior' onkeyup = 'this.value=this.value.touppercase()' size='10'></td>";
    $cadena.="<td><b>V2</b></td><td><input style='font-weight:bold;text-align:center;' type='text' id='v2' size='2' onclick=\"abrir_dialog('v2');\" readonly=\"readonly\"></td>";


    $cadena.="</tr>";
    $cadena.="<tr><td colspan='10' align='center'><br><input type='button' name='btngrabar' onclick='grabar();' value=' Grabar '><input type='button' name='btnlimpiar' onclick='limpiar();' value=' Limpiar '></td></tr></table>";
    $cadena.="</center>";

    echo $cadena;

}

//------------------------------------

if ($_POST['accion']=='grabar_rango'){

    $obj = new database();

    $codigo=$_POST['codigo'];

    $descripcion=$_POST['descripcion'];
    $inferior=$_POST['inferior'];
    $v1=$_POST['v1'];
    $superior=$_POST['superior'];
    $v2=$_POST['v2'];

    $obj->conectar(); $obj->consulta("set names 'utf8'");
    
    $resultado=$obj->consulta("insert into rangos (codigo,descripcion,inferior,v1,superior,v2) values('".$codigo."','".$descripcion."','".$inferior."',".$v1.",'".$superior."',".$v2.")");
    $obj->disconnect();

    if($resultado){
       echo "Registro Grabado";
    }
}
//------------------------------------

if ($_POST['accion']=='actualizar_rango'){

    $obj = new database();

    $codigo=$_POST['codigo'];
    $descripcion=$_POST['descripcion'];
    $inferior=$_POST['inferior'];
    $v1=$_POST['v1'];
    $superior=$_POST['superior'];
    $v2=$_POST['v2'];

    $obj->conectar(); $obj->consulta("set names 'utf8'");
    
    $resultado=$obj->consulta("update rangos set descripcion ='".$descripcion."',inferior ='".$inferior."',v1=".$v1.",superior='".$superior."',v2=".$v2." where codigo='".$codigo."'");

    $obj->disconnect();

    if($resultado){
       echo "Rango Actualizado";
    }
}

//------------------------------------

if ($_POST['accion']=='borrar_rango'){

    $obj = new database();
    
    $codigo=$_POST['codigo'];

    $obj->conectar(); $obj->consulta("set names 'utf8'");

    $resultado=$obj->consulta("delete from rangos where codigo='".$codigo."'");

       if($resultado){
          echo "Rango Eliminado";
       }

    $obj->disconnect();

}

//------------------------------------

if ($_POST['accion']=='editar_rango'){


    $obj = new database();

    $codigo=$_POST['codigo'];
    $obj->conectar(); $obj->consulta("set names 'utf8'");

    $query=$obj->consulta("select * from rangos where codigo='".$codigo."'");

    $obj->disconnect();

    $row = $obj->fetch_assoc($query);

    $cadena="<center><br><br><table border='0' cellspacing='0' cellpadding='0' width='100%'>";
    $cadena.="<tr><td colspan='4'></td></tr>";
    $cadena.="<tr>";
    $cadena.="<td><b>CODIGO</b></td><td><input style='font-weight:bold;text-align:center;' type='text' id='codigo' value='".$row['codigo']."' size='2' readonly='readonly' ></td>";
    $cadena.="<td><b>DESCRIPCI&Oacute;N</b></td><td><input style='font-weight:bold;text-align:center;' type='text' id='descripcion' value='".($row['descripcion'])."' size='25'></td>";

    $cadena.="<td><b>INFERIOR</b></td><td><input style='font-weight:bold;text-align:center;' type='text' id='inferior' value='".$row['inferior']."' onkeyup = 'this.value=this.value.touppercase()' size='10'></td>";
    $cadena.="<td><b>V1</b></td><td><input style='font-weight:bold;text-align:center;' type='text' id='v1' value='".number_format($row['v1'],1)."' size='2' onclick=\"abrir_dialog('v1');\" readonly=\"readonly\"></td>";

    $cadena.="<td><b>SUPERIOR</b></td><td><input style='font-weight:bold;text-align:center;' type='text' id='superior' value='".$row['superior']."' onkeyup = 'this.value=this.value.touppercase()' size='10'></td>";
    $cadena.="<td><b>V2</b></td><td><input style='font-weight:bold;text-align:center;' type='text' id='v2' value='".number_format($row['v2'],1)."' size='2' onclick=\"abrir_dialog('v2');\" readonly=\"readonly\"></td>";

    $cadena.="<tr><td colspan='10' align='center'><br><input type='button' name='btngrabar' onclick='actualizar();' value=' Actualizar '><input type='button' name='btnlimpiar' onclick='Limpiar();' value=' Limpiar '></td></tr></table>";
    $cadena.="<center>";
    echo $cadena;
}


?>
