<?php
session_start();
include 'config/connexion.php';
$today = date('Y-m-d');
$agent = isset($_SESSION['user_name']) ? $_SESSION['user_name'] : 'inconnu';

// Traitement Effectué
if (isset($_POST['effectuer_id'])) {
    $id = intval($_POST['effectuer_id']);
    $stmt = $conn->prepare("DELETE FROM rappels_table WHERE id = ? AND agent = ?");
    $stmt->bind_param("is", $id, $agent);
    $stmt->execute();
    $stmt->close();
    header('Location: rappels_jour.php');
    exit;
}
// Traitement Reporter
if (isset($_POST['reporter_id'], $_POST['nouvelle_date'])) {
    $id = intval($_POST['reporter_id']);
    $nouvelle_date = $_POST['nouvelle_date'];
    $stmt = $conn->prepare("UPDATE rappels_table SET date_rappel = ? WHERE id = ? AND agent = ?");
    $stmt->bind_param("sis", $nouvelle_date, $id, $agent);
    $stmt->execute();
    $stmt->close();
    header('Location: rappels_jour.php');
    exit;
}
$res = $conn->prepare("SELECT id, date_rappel, objet, agent FROM rappels_table WHERE agent = ? AND date_rappel = ? ORDER BY date_rappel ASC");
$res->bind_param("ss", $agent, $today);
$res->execute();
$res->bind_result($id, $date_rappel, $objet, $agent_result);
$rappels = [];
while ($res->fetch()) {
    $rappels[] = [
        'id' => $id,
        'date_rappel' => $date_rappel,
        'objet' => $objet,
        'agent' => $agent_result
    ];
}
$res->close();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <?php include('includes/head.php'); ?>
    <title>Rappels du jour</title>
</head>
<body id="page-top">
<div id="wrapper">
    <?php include('includes/sidebar.php'); ?>
    <div id="content-wrapper" class="d-flex flex-column">
        <div id="content">
            <?php include('includes/topbar.php'); ?>
            <div class="container-fluid" id="container-wrapper">
                <div class="d-sm-flex align-items-center justify-content-between mb-4">
                    <h1 class="h3 mb-0 text-gray-800">Rappels du <?php echo date('d/m/Y'); ?></h1>
                </div>
                <div class="card shadow mb-4">
                    <div class="card-header py-3">
                        <h6 class="m-0 font-weight-bold text-primary">Liste des tâches à effectuer aujourd'hui</h6>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-bordered table-hover">
                                <thead class="thead-light">
                                    <tr>
                                        <th>Objet</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php if ($rappels): foreach ($rappels as $rappel): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($rappel['objet']); ?></td>
                                        <td>
                                            <form method="post" class="effectuer-form" style="display:inline;">
                                                <input type="hidden" name="effectuer_id" value="<?php echo $rappel['id']; ?>">
                                                <button type="button" class="btn btn-success btn-sm btn-effectuer" data-id="<?php echo $rappel['id']; ?>">Effectué</button>
                                            </form>
                        <!-- Modal Effectué -->
                        <div class="modal fade" id="effectuerModal" tabindex="-1" role="dialog" aria-labelledby="effectuerModalLabel" aria-hidden="true">
                          <div class="modal-dialog" role="document">
                            <div class="modal-content">
                              <div class="modal-header">
                                <h5 class="modal-title" id="effectuerModalLabel">Confirmer l'action</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                  <span aria-hidden="true">&times;</span>
                                </button>
                              </div>
                              <div class="modal-body">
                                <div id="rappelDetails" class="mb-2"></div>
                                Êtes-vous sûr de vouloir marquer ce rappel comme effectué ?
                              </div>
                              <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Annuler</button>
                                <button type="button" class="btn btn-success" id="confirmEffectuer">Oui, marquer comme effectué</button>
                              </div>
                            </div>
                          </div>
                        </div>
                                            <button type="button" class="btn btn-warning btn-sm" data-toggle="modal" data-target="#reporterModal" data-id="<?php echo $rappel['id']; ?>" data-date="<?php echo htmlspecialchars($rappel['date_rappel']); ?>">Reporter</button>
                                        </td>
                                    </tr>
                                <?php endforeach; else: ?>
                                    <tr><td colspan="2" class="text-center text-muted">Aucun rappel pour aujourd'hui.</td></tr>
                                <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                        <!-- Modal Reporter -->
                        <div class="modal fade" id="reporterModal" tabindex="-1" role="dialog" aria-labelledby="reporterModalLabel" aria-hidden="true">
                          <div class="modal-dialog" role="document">
                            <div class="modal-content">
                              <form method="post">
                                <div class="modal-header">
                                  <h5 class="modal-title" id="reporterModalLabel">Reporter le rappel</h5>
                                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                  </button>
                                </div>
                                <div class="modal-body">
                                  <input type="hidden" name="reporter_id" id="reporter_id">
                                  <div class="form-group">
                                    <label for="nouvelle_date">Nouvelle date</label>
                                    <input type="date" class="form-control" name="nouvelle_date" id="nouvelle_date" required>
                                  </div>
                                </div>
                                <div class="modal-footer">
                                  <button type="button" class="btn btn-secondary" data-dismiss="modal">Annuler</button>
                                  <button type="submit" class="btn btn-primary">Enregistrer</button>
                                </div>
                              </form>
                            </div>
                          </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php include('includes/footer.php'); ?>
    </div>
</div>
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>
<script src="js/ruang-admin.min.js"></script>
<script>
$('#reporterModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget);
  var id = button.data('id');
  var date = button.data('date');
  var modal = $(this);
  modal.find('#reporter_id').val(id);
  modal.find('#nouvelle_date').val(date);
});

// Gestion du bouton Effectué avec confirmation et affichage des détails
let formToSubmit = null;
$('.btn-effectuer').on('click', function() {
  formToSubmit = $(this).closest('form');
  // Chercher la ligne du rappel
  var row = $(this).closest('tr');
  var objet = row.find('td').eq(0).text();
  // Afficher les détails dans le modal
  var detailsHtml = '<strong>Objet :</strong> ' + objet;
  $('#rappelDetails').html(detailsHtml);
  $('#effectuerModal').modal('show');
});
$('#confirmEffectuer').on('click', function() {
  if (formToSubmit) {
    formToSubmit.submit();
  }
  $('#effectuerModal').modal('hide');
});
</script>
</body>
</html>
