<?php

require_once "db.class.php";

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

    $cadena="<center><br><table border='0' cellspacing='10' cellpadding='0' width='100%'>";

    $cadena.="<tr><td>";
    $cadena.="<b>AREA</b>";
    $cadena.="<input style='font-weight:bold;text-align:center;' onblur=\"al_salir('g_id1','g_id2','areas');\" onclick=\"mostrar('g_id1','g_id2','areas',450,310);\" type='text' id='g_id1' size='1'>";
    $cadena.="<input style='font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='g_id2' size='45' readonly=\"readonly\">&emsp;&emsp;";
    $cadena.="<b>C&Oacute;DIGO</b>";
    $cadena.="<input style='font-weight:bold;text-align:center;' type='text' id='codigo' size='1'>&emsp;&emsp;";
    $cadena.="<b>NOMBRE</b>";
    $cadena.="<input style='font-weight:bold;text-align:center;' type='text' id='descripcion' size='45'>";
    $cadena.="</td></tr>";

    $cadena.="<tr><td>";
    $cadena.="<b>ABREVIAR</b>&emsp;&emsp;<input  style='font-weight:bold;text-align:center;' type='text' id='abreviar' size='20' onkeyup = 'this.value=this.value.touppercase()'>&emsp;&emsp;";
    $cadena.="<b>PROMEDIAR</b>&emsp;";
    $cadena.="<input style='font-weight:bold;text-align:center; type='text' id='promediar' size='5'>&emsp;&emsp;";
    $cadena.="<b>ORDEN</b>&emsp;";
    $cadena.="<input style='font-weight:bold;text-align:center; type='text' id='orden' size='1'>";
    $cadena.="&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;<input type='button' name='btngrabar' onclick='grabar();' value=' Grabar '>&emsp;<input type='button' name='btnlimpiar' onclick='borrar();' value=' Limpiar '>";

    $cadena.="</td></tr></table></center>";
    $cadena.="<div id='dialog' style='background-color:#003366;' title='SELECCIONE AREA' style='display:none;'></div>";

    echo $cadena;
}

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

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

    $obj = new database();

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

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

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

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

    $obj = new database();

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

    $obj->disconnect();

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

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

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

    $obj = new database();

    $codigo=$_POST['codigo'];

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

    $resultado=$obj->consulta("select * from boletin where asignatura='".$codigo."'");

    $n=$obj->numero_de_filas($resultado);

    if ($n!=0){
       echo "No se puede Eliminar el Asignatura - Esta contenida en la Tabla Boletin -";
    }
    else{
       $resultado1=$obj->consulta("delete from catalogo where codigo=".$codigo);

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


    }

    $obj->disconnect();

}

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

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


    $obj = new database();

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

    $query=$obj->consulta("select catalogo.area, areas.descripcion as nom_area, catalogo.codigo, catalogo.descripcion as nom_asign, catalogo.nombrecorto, catalogo.promediar, catalogo.orden from areas inner join catalogo on areas.codigo = catalogo.area where catalogo.codigo='".$codigo."'");

    $obj->disconnect();

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

    $cadena="<center><br><table border='0' cellspacing='10' cellpadding='0' width='100%'>";

    $cadena.="<tr><td>";
    $cadena.="<b>AREA</b>";
    $cadena.="<input style='font-weight:bold;text-align:center;' onblur=\"al_salir('g_id1','g_id2','areas');\" onclick=\"mostrar('g_id1','g_id2','areas',450,310);\" type='text' id='g_id1' value='".$row['area']."' size='1'>";
    $cadena.="<input style='font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='g_id2' value='".($row['nom_area'])."' size='45' readonly=\"readonly\">&emsp;&emsp;";
    $cadena.="<b>C&Oacute;DIGO</b>";
    $cadena.="<input style='font-weight:bold;text-align:center;' type='text' id='codigo' value='".$row['codigo']."' size='1' readonly=\"readonly\">&emsp;&emsp;";
    $cadena.="<b>NOMBRE</b>";
    $cadena.="<input style='font-weight:bold;text-align:center;' type='text' id='descripcion' value='".($row['nom_asign'])."' size='45'>";
    $cadena.="</td></tr>";

    $cadena.="<tr><td>"; 
    $cadena.="<b>ABREVIAR</b>&emsp;&emsp;<input  style='font-weight:bold;text-align:center;' type='text' id='abreviar' value='".($row['nombrecorto'])."' size='20' onkeyup = 'this.value=this.value.touppercase()'>&emsp;&emsp;";
    $cadena.="<b>PROMEDIAR</b>&emsp;";
    $cadena.="<input style='font-weight:bold;text-align:center;' type='text' id='promediar' value='".$row['promediar']."' size='5'>&emsp;&emsp;";
    $cadena.="<b>ORDEN</b>&emsp;";
    $cadena.="<input style='font-weight:bold;text-align:center; type='text' id='orden' value='".$row['orden']."' size='1'>";
    $cadena.="&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;<input type='button' name='btngrabar' onclick='actualizar();' value=' Actualizar '>&emsp;<input type='button' name='btnlimpiar' onclick='borrar();' value=' Limpiar '>";

    $cadena.="</td></tr></table></center>";    
    $cadena.="<div id='dialog' style='background-color:#003366;' title='SELECCIONE AREA' style='display:none;'></div>";

    echo $cadena;

}


?>
