<?php

require_once "db.class.php";

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

    $cadena="<center><br><table border='0' cellspacing='0' cellpadding='0' width='100%'>";
    $cadena.="<tr><td colspan='4'></td></tr>";

    $cadena.="<tr>";

    $cadena.="<td><b>GRADO</b></td><td>";
    $cadena.="<input style='border-radius:15px;font-weight:bold;text-align:center;' onblur=\"consecutivo();\" onclick=\"mostrar('g_id1','g_id2','grados',250,310);\" type='text' id='g_id1' size='2'>";
    $cadena.="<input style='border-radius:15px;font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='g_id2' size='20' readonly=\"readonly\"></td>";

    $cadena.="<td><b>C&Oacute;DIGO</b></td><td><input  style='border-radius:15px;font-weight:bold;text-align:center;' type='text' id='codigo' size='10'></td>";
    $cadena.="<td><b>DESCRIPCI&Oacute;N</b></td><td><input  style='border-radius:15px;font-weight:bold;text-align:center;' type='text' id='descripcion' size='31'></td>";

    $cadena.="</tr>";

    $cadena.="<tr>";

    $cadena.="<td><b>JORNADA</b></td><td>";
    $cadena.="<input style='border-radius:15px;font-weight:bold;text-align:center;' onclick=\"mostrar('j_id1','j_id2','jornadas',250,200);\" type='text' id='j_id1' size='2'>";
    $cadena.="<input style='border-radius:15px;font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='j_id2' size='20' readonly=\"readonly\"></td>";

    $cadena.="<td><b>ABREVIAR</b></td><td><input  style='border-radius:15px;font-weight:bold;text-align:center;' type='text' id='abreviar' size='20' onkeyup = 'this.value=this.value.touppercase()'></td>";

    $cadena.="<td><b>SEDE</b></td><td>";
    $cadena.="<input style='border-radius:15px;font-weight:bold;text-align:center;' onclick=\"mostrar('s_id1','s_id2','sedes',250,200);\" type='text' id='s_id1' size='2'>";
    $cadena.="<input style='border-radius:15px;font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='s_id2' size='24' 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>";
    $cadena.="<div id='dialog' style='background-color:#003366;' title='SELECCIONE ITEM' style='display:none;'></div>";
    echo $cadena;

}

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

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

    $obj = new database();
    $obj->conectar(); $obj->consulta("set names 'utf8'");
    
    
    $grado=$_POST['grado'];
    $resultado=$obj->consulta("SELECT  MAX(CAST(codigo AS UNSIGNED)) codigo FROM cursos where grado='".$grado."'");

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

    $cod_curso=($row['codigo']+1);

    $sw="true";
    
    while (sw=="true"){

    $resul1=$obj->consulta("SELECT  codigo FROM cursos where grado='".$grado."' and codigo='".$cod_curso."'");    
    $n=$obj->numero_de_filas($resul1);

      if ($n==1)
          $cod_curso=($row['codigo']+1);
      else 
         $sw="false";
    }


    echo $cod_curso;

    $obj->disconnect();
}

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


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

    $obj = new database();

    $grado=$_POST['grado'];
    $codigo=$_POST['codigo'];
    $jornada=$_POST['jornada'];
    $descripcion=$_POST['descripcion'];
    $abreviar=$_POST['abreviar'];
    $sede=$_POST['sede'];

    $obj->conectar(); $obj->consulta("set names 'utf8'");
    
    $resultado=$obj->consulta("insert into cursos (grado,codigo,jornada,descripcion,abreviar,sede) values('".$grado."','".$codigo."','".$jornada."','".$descripcion."','".$abreviar."','".$sede."')");
    $obj->disconnect();

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

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

    $obj = new database();

    $orden=$_POST['orden'];
    $grado=$_POST['grado'];
    $codigo=$_POST['codigo'];
    $jornada=$_POST['jornada'];
    $descripcion=$_POST['descripcion'];
    $abreviar=$_POST['abreviar'];
    $sede=$_POST['sede'];

    $obj->conectar(); $obj->consulta("set names 'utf8'");
    
    $obj->consulta("update cursos set grado ='".$grado."',codigo ='".$codigo."',jornada='".$jornada."',descripcion='".$descripcion."',abreviar='".$abreviar."',sede='".$sede."' where orden=".$orden);

    $obj->consulta("update boletin set jornada='".$jornada."' WHERE grado ='".$grado."' AND curso ='".$codigo."'");
    $obj->consulta("update titulares set jornada='".$jornada."' WHERE grado ='".$grado."' AND curso ='".$codigo."'");


    $obj->disconnect();

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

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

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

    $obj = new database();

    $orden=$_POST['orden'];
    $grado=$_POST['grado'];
    $cod_curso=$_POST['curso'];

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

    $resultado=$obj->consulta("select * from boletin where grado='".$grado."' and curso='".$cod_curso."'");

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

    if ($n!=0){
       echo "No se puede Eliminar el Curso - Contiene Registros -";
    }
    else{
       $resultado1=$obj->consulta("delete from cursos where orden=".$orden);

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

    $obj->disconnect();

}

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

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


    $obj = new database();

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

    $query=$obj->consulta("select cursos.grado, grados.descripcion as nom_gra, cursos.codigo, cursos.descripcion, cursos.jornada, jornadas.descripcion as nom_jor, cursos.abreviar, cursos.sede, sedes.descripcion as nom_sed, cursos.orden from jornadas inner join (grados inner join (sedes inner join cursos on sedes.codigo = cursos.sede) on grados.codigo = cursos.grado) on jornadas.codigo = cursos.jornada where cursos.orden=".$orden);

    $obj->disconnect();

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


    $cadena="<center><br><table border='0' cellspacing='0' cellpadding='0' width='100%'>";
    $cadena.="<tr><td colspan='4'></td></tr>";

    $cadena.="<tr>";

    $cadena.="<td><b>GRADO</b></td><td>";
    $cadena.="<input style='border-radius:15px;font-weight:bold;text-align:center;' onblur=\"al_salir('g_id1','g_id2','grados');\"  ondblclick=\"mostrar('g_id1','g_id2','grados',250,310);\" type='text' id='g_id1' value='".$row['grado']."' size='2'>";
    $cadena.="<input style='border-radius:15px;font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='g_id2' value='".($row['nom_gra'])."' size='20' readonly=\"readonly\"></td>";

    $cadena.="<td><b>CODIGO</b></td><td><input  style='border-radius:15px;font-weight:bold;text-align:center;' type='text' id='codigo' value='".$row['codigo']."' size='10'></td>";
    $cadena.="<td><b>DESCRIPCION</b></td><td><input  style='border-radius:15px;font-weight:bold;text-align:center;' type='text' id='descripcion' value='".($row['descripcion'])."' size='31'></td>";
    $cadena.="</tr>";

    $cadena.="<tr>";

    $cadena.="<td><b>JORNADA</b></td><td>";
    $cadena.="<input style='border-radius:15px;font-weight:bold;text-align:center;' onblur=\"al_salir('j_id1','j_id2','jornadas');\" ondblclick=\"mostrar('j_id1','j_id2','jornadas',250,200);\" type='text' id='j_id1' value='".$row['jornada']."' size='2'>";
    $cadena.="<input style='border-radius:15px;font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='j_id2' value='".($row['nom_jor'])."' size='20' readonly=\"readonly\"></td>";

    $cadena.="<td><b>ABREVIAR</b></td><td><input  style='border-radius:15px;font-weight:bold;text-align:center;' type='text' id='abreviar' value='".($row['abreviar'])."' size='20'></td>";
 
    $cadena.="<td><b>SEDE</b></td><td>";
    $cadena.="<input style='border-radius:15px;font-weight:bold;text-align:center;' onblur=\"al_salir('s_id1','s_id2','sedes');\" ondblclick=\"mostrar('s_id1','s_id2','sedes',250,200);\" type='text' id='s_id1' value='".$row['sede']."' size='2'>";
    $cadena.="<input style='border-radius:15px;font-weight:bold;text-align:center;background-color: #dfdcdc' type='text' id='s_id2' value='".($row['nom_sed'])."' size='24' readonly=\"readonly\"></td>";

    $cadena.="</tr>";

    $cadena.="<tr><td colspan='10' align='center'><br><input type='button' name='btngrabar' onclick='actualizar(".$orden.");' value=' Actualizar '><input type='button' name='btnlimpiar' onclick='limpiar_c();' value=' Limpiar '></td></tr></table>";
    $cadena.="<center>";
    $cadena.="<div id='dialog' style='background-color:#003366;' title='SELECCIONE ITEM' style='display:none;'></div>";
    echo $cadena;

}


?>
