<?php
  $Seccion      = 'Cursos';
  $SubSeccion   = 'Cursos';
  $NameSec      = 'Cursos';
  $UrlImgs      = 'images/cursos';
  $Position   = '../';
  require_once($Position.'config/config.php');
  require_once($Position.'config/conexion.php');
  require_once($Position.'config/user.php');
  require_once($Position.'funciones/acces.php');
  require_once($Position.'funciones/logout.php');
  require_once($Position.'funciones/funciones.php');
  require_once($Position.'funciones/extras.php');

  $SecLine      = array($NameSec);

  //Eliminar Cruso
  if( $_POST['Borrar']=='Ok' ){

    $Id = $_POST['Id'];

    $Image = $RootSite.'/'.$UrlImgs.'/'.$Item['Id'].'.jpg';
    unlink($RootSite."/".$UrlImgs."/".$Id.".jpg");

    //Del CrusoIdomas
    $Query = "
      DELETE FROM CMS_CursosIdiomas
      WHERE Fk_Cruso = '$Id'
    ";
    $Result = mysqli_query($mysqli, $Query);

    //Del Cruso
    $Query = "
      DELETE FROM CMS_Cursos
      WHERE Id = '$Id'
    ";
    $Result = mysqli_query($mysqli, $Query);

  }
  //End Eliminar Cruso

  //Cursos
  $Query = "
    SELECT
      cur.Id,
      cur.FechaInicio,
      cur.Precio,
      cur.Destacado,
      cur.Activo,
      cur.Titulo,
      tip.Tipo,
      CONCAT(exp.Nombre,' ',exp.Apellidos) AS Expositor
    FROM CMS_Cursos cur
    LEFT JOIN CMS_CursosTipos tip ON tip.Id = cur.Fk_CursoTipo
    LEFT JOIN CMS_Expositores exp ON exp.Id = cur.Fk_Expositor
    WHERE
      cur.Id != '0'
    ORDER BY cur.FechaInicio ASC
  ";
  $Cursos = $mysqli->query($Query);

?>

<!DOCTYPE html>
<html lang="en">

<?php require_once($Position.'head.php'); ?>

<body class="fix-sidebar fix-header card-no-border">
    <div class="preloader">
        <svg class="circular" viewBox="25 25 50 50">
            <circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="2" stroke-miterlimit="10" /> </svg>
    </div>
    <div id="main-wrapper">
      <?php require_once($Position.'header.php'); ?>
      <?php require_once($Position.'menu.php'); ?>
      <div class="page-wrapper">
        <div class="container-fluid">
          <div class="row page-titles">

            <?php require_once($Position.'title.php'); ?>

            <!-- Botones -->
            <div class="col-md-7 align-self-center text-right">
              <div class="btn-group" role="group" aria-label="Basic example">
                <?php if( $Perms[28] ){ ?>
                  <a href="item">
                    <button type="button" class="btn btn-sm btn-secondary">Nuevo<i class="fa fa-plus mar-l-10"></i></button>
                  </a>
                <?php } ?>
              </div>
            </div>
            <!-- End Botones -->

          </div>
          <div class="row">
              <div class="col-12">
                <!-- Usuarios -->
                <div class="card">
                  <div class="card-body">
                    <div class="table-responsive m-t-20">
                      <table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
                        <thead>
                          <tr>
                            <!-- <th width="20">Id</th> -->
                            <th></th>
                            <th>Curso</th>
                            <th>Expositor</th>
                            <th>Tipo</th>
                            <th>Destacado</th>
                            <th width="20">Status</th>
                            <?php if( $Perms[29] ){ ?>
                              <th width="20"></th>
                            <?php } ?>
                            <?php if( $Perms[30] ){ ?>
                              <th width="20"></th>
                            <?php } ?>
                          </tr>
                        </thead>
                        <tbody>
                            <?php
                              mysqli_data_seek($Cursos, 0);
                              while($Cur = mysqli_fetch_array($Cursos)) {
                            ?>
                              <tr>

                                <!-- Id --
                                <td>
                                  <?php echo $Cur['Id'];?>
                                </td>
                                <!-- End Id -->

                                <!-- Imagen -->
                                <td style="width: 100px;">
                                  <?php
                                    $Image = $RootSite.'/'.$UrlImgs.'/'.$Cur['Id'].'m.jpg';
                                    if (file_exists($Image)) {
                                  ?>
                                    <div class="m-b-10">
                                      <img
                                        class="img-responsive"
                                        style="width:100px;"
                                        src="<?php echo $UrlSite.'/'.$UrlImgs.'/'.$Cur['Id'].'m.jpg'; ?>"
                                      >
                                    </div>
                                  <?php } ?>
                                </td>
                                <!-- End Imagen -->

                                <!-- Curso -->
                                <td>

                                  <!-- Titulo -->
                                  <div>
                                    <?php echo $Cur['Titulo']; ?>
                                  </div>
                                  <!-- End Titulo -->

                                  <!-- Categorias -->
                                  <div>
                                    <?php

                                      //Categorias
                                      $Cats = '';
                                      $Query = "
                                        SELECT
                                          cat.Id,
                                          cat.Fk_Categoria,
                                          ca.Nombre
                                        FROM CMS_Cursos_Categorias cat
                                        LEFT JOIN CMS_CrusosCategorias ca ON ca.Id = cat.Fk_Categoria
                                        WHERE cat.Fk_Curso = '$Cur[Id]'
                                      ";
                                      $Categorias = $mysqli->query($Query);
                                      mysqli_data_seek($Categorias, 0);
                                      while($Categoria = mysqli_fetch_array($Categorias)) {
                                        $Cats = $Cats.', '.$Categoria[Nombre];
                                      }
                                      echo ltrim($Cats,', ');
                                    ?>
                                  </div>
                                  <!-- End Categorias -->

                                  <!-- FechaInicio --->
                                  <div>
                                    <i class="fa fa-calendar m-r-5"></i>
                                    <?php
                                      //Fecha Inicio
                                      $Query = "
                                        SELECT
                                          ses.Id, ses.Fecha
                                        FROM CMS_CursosSesiones ses
                                        WHERE
                                          ses.Fk_Curso = '$Cur[Id]'
                                      ";
                                      $Result = mysqli_query($mysqli, $Query);
                                      $FechaInicio = mysqli_fetch_assoc($Result);
                                    ?>
                                    <?php echo CambioFechaInversa($FechaInicio['Fecha']); ?>
                                  </div>
                                  <!-- End FechaInicio --->

                                  <!-- Sesiones -->
                                  <div>
                                    <?php
                                      //Total Fechas
                                      $Query = "
                                        SELECT
                                          COUNT(ses.Id) AS Total
                                        FROM CMS_CursosSesiones ses
                                        WHERE
                                          ses.Fk_Curso = '$Cur[Id]'
                                      ";
                                      $Result = mysqli_query($mysqli, $Query);
                                      $Sesiones = mysqli_fetch_assoc($Result);
                                      if( $Sesiones[Total] ){
                                    ?>
                                      <?php echo $Sesiones[Total]; ?>
                                      <?php
                                        if( $Sesiones[Total] == 1 ){
                                          echo "sesión";
                                        }else{
                                          echo "sesiones";
                                        }
                                      ?>
                                    <?php } ?>
                                  </div>
                                  <!-- End Sesiones -->

                                  <?php if( $Cur[Precio] ){ ?>
                                    <!-- Precio -->
                                    <div>
                                      $
                                      <?php
                                        echo number_format($Cur[Precio],2,'.',',');
                                      ?>
                                      usd
                                    </div>
                                    <!-- End Precio -->
                                  <?php } ?>

                                </td>
                                <!-- End Curso -->

                                <!-- Expositor -->
                                <td>
                                  <div><?php echo $Cur['Expositor']; ?></div>
                                </td>
                                <!-- End Expositor -->

                                <!-- Tipo -->
                                <td>
                                  <div><?php echo $Cur['Tipo']; ?></div>
                                </td>
                                <!-- End Tipo -->

                                <!-- Destacado -->
                                <td>
                                  <?php if( $Cur[Destacado] == 1 ){ ?>
                                    <span class="label label-success">
                                      Destacado
                                    </span>
                                  <?php } ?>
                                </td>
                                <!-- End Destacado -->

                                <!-- Status -->
                                <td>
                                  <?php if( $Cur[Activo] == 1 ){ ?>
                                    <span class="label label-success">
                                      Visible
                                    </span>
                                  <?php } ?>
                                </td>
                                <!-- End Status -->

                                <?php if( $Perms[29] ){ ?>
                                  <!-- Editar -->
                                  <td>
                                    <a href="item?Id=<?php echo $Cur['Id']; ?>">
                                      <button class="btn btn-secondary btn-sm" data-toggle="tooltip" data-placement="top" title="" data-original-title="Editar">
                                        Editar<i class="fa fa-pencil m-l-10"></i>
                                      </button>
                                    </a>
                                  </td>
                                  <!-- End Editar -->
                                <?php } ?>

                                <?php if( $Perms[30] ){ ?>
                                  <!-- Eliminar -->
                                  <td>
                                    <a data-toggle="modal" href="#Borrar<?php echo $Cur[Id];?>">
                                      <button class="btn btn-danger btn-sm ml-2" data-toggle="tooltip" data-placement="top"  title="Eliminar" data-original-title="Eliminar">
                                      <i class="fa fa-times"></i></button>
                                    </a>

                                    <!--Modal Eliminar-->
                                    <form method="POST">
                                      <div id="Borrar<?php echo $Cur[Id];?>" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
                                        <div class="modal-dialog">
                                          <div class="modal-content">
                                            <div class="modal-header">
                                              <h4 class="modal-title">Eliminar</h4>
                                              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                            </div>
                                            <div class="modal-body p-4">
                                              <div class="row">
                                                <div class="col-md-12">
                                                  <p>¿Desea eliminar esté elemento?</p>
                                                  <p>
                                                    <strong>
                                                      <?php echo $Cur[Titulo]; ?>
                                                    </strong>
                                                  </p>
                                                </div>
                                                <div class="col-md-12">
                                                  <p>
                                                    <strong>
                                                      <?php echo $Cur['Nombre']; ?>
                                                    </strong>
                                                  </p>
                                                </div>
                                              </div>
                                            </div>
                                            <div class="modal-footer">
                                                <input name="Borrar" type="hidden" value="Ok">
                                                <input name="Id" type="hidden" value="<?php echo $Cur[Id];?>">
                                                <button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">Cancelar</button>
                                                <button type="submit" class="btn btn-danger waves-effect waves-light">Eliminar</button>
                                              </div>
                                          </div>
                                        </div>
                                      </div>
                                    </form>
                                    <!-- End Modal Eliminar-->
                                  </td>
                                  <!-- End Eliminar -->
                                <?php } ?>

                              </tr>
                            <?php } ?>
                          </tbody>
                      </table>
                    </div>
                  </div>
                </div>
                <!-- End Usuarios -->
              </div>
            </div>
        </div>
        <?php require_once($Position.'footer.php'); ?>
      </div>
    </div>
    <?php require_once($Position.'scripts.php'); ?>
    <script>

    </script>
    <script src="<?php echo $Position; ?>assets/plugins/styleswitcher/jQuery.style.switcher.js"></script>

</body>
</html>
