<?php

include ("header.php");

$guardado=0;

$edt=0;

$fecha=date("Y-m-d");

if (!empty($_POST))

            {

               

                $opc = $mysqli->real_escape_string($_POST['opc']);

                

                if($opc==1)

                {

                    $id = $mysqli->real_escape_string($_POST['id']);

                    $edt = $mysqli->real_escape_string($_POST['edt']);

                    $age_id = $mysqli->real_escape_string($_POST['age_id']);

                    $fecha = $mysqli->real_escape_string($_POST['fecha']);

                    $hora = $mysqli->real_escape_string($_POST['hora']);

                    

                    $sql2 = "SELECT * FROM `agenda` WHERE `age_fecha`='$fecha' and `age_hora` = '$hora'";

                        $result = $mysqli->query($sql2); 

                        $row = $result->fetch_assoc();

                        $r=$row['age_id'];

                    

                    if ($r>0)

                        $error=1;

                    else{

                    

                        $consulta="UPDATE `agenda` SET `age_fecha`='$fecha',`age_hora`='$hora' WHERE age_id=$age_id";



                    $stmt = $mysqli->prepare($consulta);

                    if ($stmt->execute()){

                        $guardado=1;

                    }

                    }

                }



                    $fecha = $mysqli->real_escape_string($_POST['fecha']);



            }

            

if (!empty($_GET))

            {

                $age_id = $_GET['age_id'];

                $edt= $_GET['edt'];

                

                $id = $_GET['id'];

                

                $sql2 = "SELECT * FROM pacientes WHERE pac_id = $id";

                        $result = $mysqli->query($sql2); 

                        $row = $result->fetch_assoc();

-                     

                       $nombre=$row['pac_nombre'];

                        $sexo=$row['pac_sexo'];

            }

            

$sqltabla="SELECT * FROM `agenda`,pacientes WHERE `age_fecha` = '$fecha' and age_pac_id = pac_id order by age_hora asc ";

$resultadotabla = mysqli_query($mysqli, $sqltabla);

$row_cnt = mysqli_num_rows($resultadotabla);



?>



        <div class="content-wrapper">

            <!-- START PAGE CONTENT-->

            <div class="page-heading">

                <h1 class="page-title">Agenda de Citas</h1>

            </div>

            <div class="row">

 

                    <div class="col-lg-8">

                        <div class="ibox">



                            <div class="ibox-body">

                             <?php

                            if ($guardado == 1){?>

                        <div class="alert alert-success " role="alert">

                            Cita guardada con exito... 

                            

                        </div>

                        <?php

                        }

                        if ($error == 1){?>

                            <div class="alert alert-danger " role="alert">

                                Error el horario para la fecha seleccionada ya esta ocupado, verifique los datos...

                              </div>   

                            

                            <?php

                            }

                        ?>                               

                                <div class="flexbox mb-4">

                                    <div>

                                        <h3 class="m-0">Citas</h3>

                                        <div><?echo date('d-m-Y', strtotime($fecha));?> </div>

                                    </div>

                                    

                                </div>

                                <?if($row_cnt>0){?>

                                <div>

                                    <?php while($row = mysqli_fetch_array($resultadotabla)) { ?>

                                        <div class="row align-items-center">

                                        <div class="col-lg-<?if($fecha==date("Y-m-d"))echo '10'; else echo '10';?>">

                                            <div class="alert alert-success" role="alert">

                                                <?php echo $row['age_hora']. ' hrs. Paciente: ' .$row['pac_nombre']; ?>

                                                

                                            </div>

                                        </div>

                                        

                                         <div class="col-lg-2">

                                            <div class="alert alert-primary" role="alert">

                                              <div class="row">

                                               <?if($fecha==date("Y-m-d")){?> 

                                               <a href="nexpediente.php?id=<?php echo $row['pac_id']; ?>"> <div class="icon-item"><i class="fa fa-file-text-o"></i></div></a> </td>

                                               &nbsp;&nbsp;&nbsp;<?}?>

                                                <a href="verpaciente.php?id=<?php echo $row['pac_id'].'&fecha='.$fecha; ?>"> <div class="icon-item"><i class="fa fa-search-plus" aria-hidden="true"></i></div></a> </td>

                                              &nbsp;&nbsp;&nbsp;

                                              <a href="eagenda.php?age_id=<?php echo $row['age_id'].'&edt=1&id='.$row['age_pac_id']; ?>"> <div class="icon-item"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></div></a> </td>

                                               

                                         </div>

                                               

                                            </div>

                                        </div> 

                                        

                                        

                                        

                                        

                                        

                                        </div>

                                        



                                    <?php } ?>

                                </div>

                                <?}

                                else{?>

                                <div class="alert alert-danger" role="alert">

                                    No hay citas agendadas

                                </div>

                                <?}?>

                                

                                <button onclick="location.href='pacientes.php'" type="button" class="btn btn-primary">Nueva Cita</button>

                            </div>

                        </div>

                    </div>

                    <div class="col-lg-4">

                        <div class="ibox">

                            <div class="ibox-head">

                                <div class="ibox-title">Consultar Fecha</div>

                            </div>

                            <div class="ibox-body">

                                <div class="row align-items-center">

                                <form id="signupform" class="form-horizontal" role="form" action="eagenda.php?age_id=<?php echo 'edt=1&id='.$id;?>" method="POST" autocomplete="off">

                                    <div class="row">

                                    <div class="col-sm-12 form-group">

                                            <label>Fecha:</label>

                                            <input type="date" name="fecha" step="1" min="2021-01-01" max="2050-12-31" value="<?php echo $fecha;?>">        

                                    </div>

                                        

                                    

                                    <input id="tipo" name="tipo" type="hidden" value="insumo">

                                    <div class="col-sm-12 form-group">

                                    <?if ($edt==0){?>

                                    <button class="btn btn-info" type="submit">Consultar</button>

                                    <?}?>

                                    </div>

                                </form> 

                            </div>

                        </div>

                    </div>

                </div>

                    

                    

                





            </div></div>

                    

   <?if ($guardado == 0){?>                 

                    <div class="row">

                    <div class="col-md-8">

                        <div class="ibox">

                            <div class="ibox-head">

                                <div class="ibox-title">Datos del Paciente</div>

                                <div class="ibox-tools">

                                    <a class="ibox-collapse"><i class="fa fa-minus"></i></a>

                                    <a class="fullscreen-link"><i class="fa fa-expand"></i></a>

                                </div>

                            </div>

                            <div class="ibox-body">

                                    <form id="signupform" class="form-horizontal" role="form" action="nagenda.php?id=<?echo $id;?>" method="POST" autocomplete="off">

                                    <div class="row">

                                            <div class="col-sm-4 form-group">

                                                    <label>Nombre</label>

                                                    <input name="nombre" class="form-control" type="text" placeholder="Nombre Completo" value="<?php echo $nombre; ?>" readonly>

                                                </div>

                                            <div class="col-sm-4 form-group">

                                                    <label>Sexo</label>

                                                    <input name="sexo" class="form-control" type="text" placeholder="Nombre Completo" value="<?php echo $sexo; ?>" readonly>

                                                </div>



                                            <div class="col-sm-4 form-group">

                                                <label>Fecha de solicitud de cita:</label>

                                                <input type="date" name="fecha" step="1" min="2020-01-01" max="2025-12-31" value="<?php echo $fecha;?>" <?if ($edt==0){?>readonly <?}?>>

                                                

                                            </div>

                                                <div class="col-sm-4 form-group">

                                                    <label>Hora de solicitud de cita:</label><br>

                                                  <select name="hora" class="form-select" aria-label="Default select example">

                                                    <option value="<?echo $hora;?>" selected><?echo $hora;?></option>

                                                    <option value="09:00">09:00</option>

                                                    <option value="09:30">09:30</option>

                                                    <option value="10:00">10:00</option>

                                                    <option value="10:30">10:30</option>

                                                    <option value="11:00">11:00</option>

                                                    <option value="11:30">11:30</option>

                                                    <option value="12:00">12:00</option>

                                                    <option value="12:30">12:30</option>

                                                    <option value="13:00">13:00</option>

                                                    <option value="13:30">13:30</option>

                                                    <option value="14:00">14:00</option>

                                                    <option value="14:30">14:30</option>

                                                    <option value="15:00">15:00</option>

                                                    <option value="15:30">15:30</option>

                                                    <option value="16:00">16:00</option>

                                                    <option value="16:30">16:30</option>

                                                    <option value="17:00">17:00</option>

                                                    <option value="17:30">17:30</option>

                                                    <option value="18:00">18:00</option>

                                                    <option value="18:30">18:30</option>

                                                    <option value="19:00">19:30</option>

                                                    <option value="19:30">19:30</option>

                                                  </select>

                                                </div>

                                               

                                    <input id="edt" name="edt" type="hidden" value="<?php echo $edt;?>">

                                    <input id="age_id" name="age_id" type="hidden" value="<?php echo $age_id;?>">

                                                 <input id="opc" name="opc" type="hidden" value="1">

                                                <input id="id" name="id" type="hidden" value="<?echo $id;?>">

                                    <div class="col-sm-12 form-group">

                                    <button class="btn btn-info" type="submit">Modificar Cita</button>

                                   

                                    </div>

                                </form> 

                                    </div>

                                    

                                 

                                   

                        </div>

                       

                            

                            

                            

                    </div>

            <?}?>  

            <div class="page-content fade-in-up">

                

            </div>

            

            





<?php 

    include ('footer.php');

?>