<?php

require_once "db.class.php";

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

    $cadena="<center><br><br><table style='font-size:10px;font-weight:bold;text-align:center;' 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-size:10px;font-weight:bold;text-align:center;' type='text' id='descripcion' size='40'></td>";

    $cadena.="<td><b>ABREVIAR</b></td><td><input style='font-size:10px;font-weight:bold;text-align:center;' type='text' id='abreviar'></td>";
    $cadena.="<td><b>I_H</b></td><td><input style='font-weight:bold;text-align:center;' type='text' id='i_h' size='2' ></td>";
    $cadena.="<td><b>PROMEDIAR</b></td><td><SELECT ID='promediar'><OPTION VALUE='SI'>SI</OPTION><OPTION VALUE='NO'>NO</OPTION></SELECT> </td>";
    
    $cadena.="<td><b>ORDEN</b></td>    <td><input style='font-size:10px;font-weight:bold;text-align:center;' type='text' id='orden' size='2'></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_area'){

    $obj = new database();

    $codigo=$_POST['codigo'];
    $descripcion=$_POST['descripcion'];
    $abreviar=$_POST['abreviar'];
    $i_h=$_POST['i_h'];
    $promediar=$_POST['promediar'];
    $orden=$_POST['orden'];

    $obj->conectar(); $obj->consulta("set names 'utf8'");
    
    $resultado=$obj->consulta("insert into areas values('".$codigo."','". $descripcion."','".$abreviar."','".$i_h."','".$promediar."',".$orden.")");
    $obj->disconnect();

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

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

    $obj = new database();

    $codigo=$_POST['codigo'];
    $descripcion=$_POST['descripcion'];
    $abreviar=$_POST['abreviar'];
    $i_h=$_POST['i_h'];
    $promediar=$_POST['promediar'];
    $orden=$_POST['orden'];
        
    $obj->conectar(); $obj->consulta("set names 'utf8'");
       
    $resultado=$obj->consulta("update areas set descripcion ='".$descripcion."',nombrecorto='".$abreviar."',i_h='".$i_h."',promediar='".$promediar."',orden=".$orden." where codigo='".$codigo."'");

    $obj->disconnect();

    if($resultado){
       echo "Area Actualizada";
       
    }
}

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

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

    $obj = new database();

    $codigo=$_POST['codigo'];
    $obj->conectar(); $obj->consulta("set names 'utf8'");
    
    $resultado=$obj->consulta("select * from catalogo where area='".$codigo."'");
    $n=$obj->numero_de_filas($resultado);

    if ($n!=0){
       echo "No se puede Eliminar Area - Contiene Asignaturas -";
    }
    else{
       $resultado1=$obj->consulta("delete from areas where codigo='".$codigo."'");

       if($resultado1){
          echo "Area Eliminada";
       }
    }

    $obj->disconnect();
    
}

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

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


    $obj = new database();

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

    $query=$obj->consulta("select * from areas where codigo='".$codigo."'");
    $obj->disconnect();
    
    $row = $obj->fetch_assoc($query);


    $cadena="<center><br><br><table style='font-size:10px;font-weight:bold;text-align:center;' 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-size:10px;font-weight:bold;text-align:center;' type='text' id='descripcion' value='".($row['descripcion'])."' size='40'></td>";

    $cadena.="<td><b>ABREVIAR</b></td><td><input style='font-size:10px;font-weight:bold;text-align:center;' type='text' id='abreviar' value='".($row['nombrecorto'])."'></td>";
    $cadena.="<td><b>I_H</b></td><td><input style='font-weight:bold;text-align:center;' type='text' id='i_h' value='".$row['i_h']."' size='2' ></td>";

    if ($row['promediar']=="SI"){
       $opcion= "<option value='SI' selected>SI</option><option value='NO'>NO</option>";
    }
    else{
        $opcion= "<option value='SI'>si</option><option value='NO' selected>NO</option>";
    }
      
    $cadena.="<td><b>PROMEDIAR</b></td><td><select id='promediar'>".$opcion."</select> </td>";
    $cadena.="<td><b>ORDEN</b></td><td><input style='font-size:10px;font-weight:bold;text-align:center;' type='text' id='orden' size='2' value='".$row['orden']."'></td>";
    $cadena.="</tr>";
    $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;

}


?>