<?php

require_once "db.class.php";

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

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

    $cadena.="<tr width='80%'><td>";

    $cadena.="<b>GRADO</b>&emsp;";
    $cadena.="<input style='font-weight:bold;text-align:center;' onclick=\"mostrar('g_id1','g_id2','grados',250,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='15' readonly=\"readonly\">&emsp;";

    $cadena.="<b>CURSO</b>&emsp;";
    $cadena.="<input style='font-weight:bold;text-align:center;' onclick=\"mostrar_curso_jornada('c_id1','c_id2','cursos',250,310);\" type='text' id='c_id1' size='1'>";
    $cadena.="<input style='font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='c_id2' size='15' readonly=\"readonly\">&emsp;";

    $cadena.="<b>JORNADA</b>&emsp;";
    $cadena.="<input style='font-weight:bold;text-align:center;' type='text' id='j_id1' size='1' readonly=\"readonly\">";
    $cadena.="<input style='font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='j_id2' size='15' readonly=\"readonly\">";
    $cadena.="</td>";

    $cadena.="<td width='20%' align='center'><input type='button' name='btngrabar' onclick='grabar();' value=' Grabar '></td></tr>";

    $cadena.="<tr><td><b>PROFESOR</b>&emsp;";
    $cadena.="<input style='font-weight:bold;text-align:center;' onclick=\"mostrar('p1','p2','profesores',450,310);\" type='text' id='p1' size='1'>";
    $cadena.="<input style='font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='p2' size='86' readonly=\"readonly\">";
    $cadena.="</td>";

    $cadena.="<td align='center'><input type='button' name='btnlimpiar' onclick='borrar();' value=' Limpiar '></td>";

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

    echo $cadena;
}

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

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


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

    $query=$obj->consulta("select titulares.profesor, profesores.nombres, titulares.grado, grados.descripcion as nom_grado, titulares.curso, cursos.descripcion as nom_curso, titulares.jornada, 
                               jornadas.descripcion as nom_jornada, titulares.numero from (jornadas inner join (grados inner join cursos on grados.codigo = cursos.grado) on jornadas.codigo = cursos.jornada) 
                               inner join (profesores inner join titulares on profesores.cedula = titulares.profesor) on (cursos.codigo = titulares.curso) and (cursos.grado = titulares.grado) where titulares.numero=".$numero);
    $obj->disconnect();

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

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


    $cadena.="<tr width='80%'><td>";

    $cadena.="<b>GRADO</b>&emsp;";
    $cadena.="<input style='font-weight:bold;text-align:center;' onclick=\"mostrar('g_id1','g_id2','grados',250,310);\" type='text' id='g_id1' value='".$row['grado']."' size='1'>";
    $cadena.="<input style='font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='g_id2' value='".($row['nom_grado'])."' size='15' readonly=\"readonly\">&emsp;";

    $cadena.="<b>CURSO</b>&emsp;";
    $cadena.="<input style='font-weight:bold;text-align:center;' onclick=\"mostrar_curso_jornada('c_id1','c_id2','cursos',250,310);\" type='text' id='c_id1' value='".$row['curso']."' size='1'>";
    $cadena.="<input style='font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='c_id2' value='".($row['nom_curso'])."' size='15' readonly=\"readonly\">&emsp;";

    $cadena.="<b>JORNADA</b>&emsp;";
    $cadena.="<input style='font-weight:bold;text-align:center;' type='text' id='j_id1' value='".$row['jornada']."' size='1' readonly=\"readonly\">";
    $cadena.="<input style='font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='j_id2' value='".($row['nom_jornada'])."' size='15' readonly=\"readonly\">";
    $cadena.="</td>";

    $cadena.="<td width='20%' align='center'><input type='button' name='btngrabar' onclick='actualizar(".$row['numero'].");' value=' Actualizar '></td></tr>";

    $cadena.="<tr><td><b>PROFESOR</b>&emsp;";
    $cadena.="<input style='font-weight:bold;text-align:center;' onclick=\"mostrar('p1','p2','profesores',450,310);\" type='text' id='p1' value='".$row['profesor']."' size='1'>";
    $cadena.="<input style='font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='p2' value='".($row['nombres'])."' size='86' readonly=\"readonly\">";
    $cadena.="</td>";

    $cadena.="<td align='center'><input type='button' name='btnlimpiar' onclick='borrar();' value='   Limpiar   '></td>";

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

     echo $cadena;
}

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

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

    $obj = new database();

    $profesor=$_POST['profesor'];
    $grado=$_POST['grado'];
    $curso=$_POST['curso'];
    $jornada=$_POST['jornada'];


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

    $query=$obj->consulta("select * from titulares where grado='".$grado."' and curso='".$curso."' and jornada='".$jornada."'");
    
    $n=$obj->numero_de_filas($query);
    
    if ($n==0){
        $resultado=$obj->consulta("insert into titulares (profesor,grado,curso,jornada) values('".$profesor."','".$grado."','".$curso."','".$jornada."')");
        echo "TITULAR ASIGNADO";
    }
    else{
        $obj->consulta("UPDATE titulares SET profesor='".$profesor."' WHERE grado='".$grado."' AND curso='".$curso."' AND jornada='".$jornada."'");
        echo "TITULAR ACTUALIZADO";
    }
    
    $obj->disconnect();

}
//------------------------------------

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

    $obj = new database();

    $profesor=$_POST['profesor'];
    $grado=$_POST['grado'];
    $curso=$_POST['curso'];
    $jornada=$_POST['jornada'];
    $numero=$_POST['numero'];

    $obj->conectar(); $obj->consulta("set names 'utf8'");
    
    $resultado=$obj->consulta("update titulares set profesor ='".$profesor."',grado='".$grado."',curso='".$curso."',jornada='".$jornada."' where numero=".$numero);

    $obj->disconnect();

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

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

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

    $obj = new database();

    $numero=$_POST['numero'];

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

    $resultado1=$obj->consulta("delete from titulares where numero=".$numero);

       if($resultado1){
          echo "Titular Eliminado";
       }

    $obj->disconnect();

}

?>