<?php 
include ("conexion.php");
session_start();

if (!isset($_SESSION['id'])) {
  header("Location: iniciar.php");
  exit();
}

$id= $_SESSION['id'];

$date = @$_POST['date'];
$hora = @$_POST['hora'];

if($date == TRUE && $hora == TRUE){
  $agregar = $conectado->prepare("INSERT INTO cita (datee, hora) VALUES ('$date','$hora')");
  $agregar->execute();
}

$mostrar = $conectado->prepare("SELECT * FROM cita");
$mostrar->execute();
$resul = [];
$resul = $mostrar->get_result();


$mostrar2 = $conectado->prepare("SELECT * FROM citas");
$mostrar2->execute();
$resultados = [];
$resultados = $mostrar2->get_result();

if (isset($_GET["id"])) {
  $id_obtenido = $_GET["id"];
  $borrar = "DELETE FROM cita WHERE id=$id_obtenido";
  $eliminar = $conectado->prepare($borrar);
  $eliminar->execute();
  header("location: citas2.php");
}

if (isset($_GET["asd"])) {
  $asd = $_GET["asd"];
  $borrar2 = "DELETE FROM citas WHERE id=$asd";
  $eliminar2 = $conectado->prepare($borrar2);
  $eliminar2->execute();
  header("location: citas2.php#table");
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
    <title>Mi Página Web</title>
    <link rel="stylesheet" type="text/css" href="Estilos/index.css">
    <script src="https://unpkg.com/feather-icons"></script>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" 
    integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/uikit@3.18.3/dist/js/uikit.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/uikit@3.18.3/dist/js/uikit-icons.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/uikit@3.18.3/dist/css/uikit.min.css" />
    <link rel="stylesheet" href="Estilos/citas.css">
  </head>
<body>
    <?php include ("menu.php") ?>
    
<div id="citas2">

  <div class="uk-container">

    <div class="uk-child-width-1-2 uk-text-center" uk-grid uk-height-match="target: > div > .uk-card">
      <div>
          <div class="uk-card uk-card-default uk-card-body">
              <form action="" onsubmit="return validaciond();" method="post">
                <div class="uk-margin">
                  <h4>Dias disponibles</h4>
                  <br>
                  <input class="uk-input uk-form-width-medium" type="date" id="dia" name="date">
                  <br>
                  <br>
                  <input class="uk-input uk-form-width-medium" placeholder="Formato 24 horas" type="number" id="hora" name="hora">
                  <br>
                  <button class="uk-button uk-button-default uk-margin-top">Submit</button>
                </div>
              </form>
          </div>
      </div>
      <div>
          <div id="table" class="uk-card uk-card-default uk-card-body">
                <h4>lista de dias disponibles: </h4>
                <table class="uk-table uk-table-middle uk-table-divider">
                  <thead>
                      <tr>
                          <th>dias disponibles:</th>
                          <th>hora: </th>
                          <th>Acciones:</th>
                      </tr>
                  </thead>
                  <tbody>
                    <?php foreach($resul as $valores) { ?>
                      <tr >
                          <td><?php echo $valores["datee"]; ?></td>
                          <td><?php echo $valores["hora"]; ?></td>
                          <td><a href="citas2.php?id=<?php echo $valores["id"]; ?>" class="uk-button uk-button-default" id="borrar" type="button">Borrar</a></td>
                      </tr>
                      <?php } ?>
                  </tbody>
            </table>
          </div>
      </div>
    </div>

  </div>

</div>
<div class="uk-container">
<div class="uk-card uk-card-body ">
    <div class="uk-width-1-1" uk-grid>
          <h2>Citas de usuarios</h2>
          <table class="uk-table uk-table-middle uk-table-divider">
                <thead>
                    <tr>
                        <th>nombre de usuario:</th>
                        <th>dia escojido: </th>
                        <th>hora:</th>
                        <th>Acciones:</th>
                    </tr>
                </thead>
                <tbody>
                  <?php foreach($resultados as $dolares) { ?>
                    <tr >
                        <td><?php echo $dolares["nombre"]; ?></td>
                        <td><?php echo $dolares["datee"]; ?></td>
                        <td><?php echo $dolares["hora"] ?></td>
                        <td><a class="uk-button uk-button-default" href="citas2.php?asd=<?php echo $dolares["id"]; ?>">Borrar</a></td>
                    </tr>
                    <?php } ?>
                </tbody>
          </table>
    </div>
          </div>
        </div>

        <div class="chat-icon">
    <?php include("boticon.php") ?>
    </div>
 
<script>
  function validaciond() {
    let borrar = document.getElementById('borrar').value;
    let dia = document.getElementById('dia').value;
    let hora = document.getElementById('hora').value;
    
    if (borar === 0) {
        alert('quiere borrar este apartado?');
        return false;
    }
    if (dia === 0) {
        alert('no hay fecha seleccionanda');
        return false;
    }
    if (hora.length >= 1) {
        alert('No ha elegido hora');
        return false;
    }
    alert('Se agrego la cita correctamente');
    return true;
}
</script>
</body>
</html>

<?php
