<?php
session_start();
include('config.php'); // Your existing connection file

// Ensure the employee is logged in (adjust session variable name as needed)
if (!isset($_SESSION['employee_id'])) {
    die("Please log in to continue.");
}
$employee_id = $_SESSION['employee_id'];

// Set today's date
$current_date = date('Y-m-d');

// Check if the employee has already answered today's question
$query = "SELECT id FROM habits_responses WHERE employee_id = ? AND response_date = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("is", $employee_id, $current_date);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows > 0){
    echo "You have already answered today's question.";
    exit;
}

// OPTIONAL: Enforce the 18-day period for this module
// Change $start_date to the actual date when the 18-day period starts.
$start_date = '2025-02-01';  
$day_count = (strtotime($current_date) - strtotime($start_date)) / (60 * 60 * 24) + 1;
if ($day_count > 18) {
    echo "The 18-day period for answering questions is over.";
    exit;
}

// Determine which question to show.
// Here we cycle through 5 questions. For example, day 1 shows question 1, day 2 shows question 2, … day 6 shows question 1 again.
$question_index = (($day_count - 1) % 5) + 1;

// Retrieve the question from habits_questions table
$query = "SELECT id, question_text FROM habits_questions WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $question_index);
$stmt->execute();
$question_result = $stmt->get_result();
if (!$question = $question_result->fetch_assoc()) {
    die("Question not found.");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Daily Habits &amp; Behavior</title>
</head>
<body>
    <h1>Daily Habits &amp; Behavior</h1>
    <p><strong>Question:</strong> <?php echo htmlspecialchars($question['question_text']); ?></p>
    <form method="post" action="submit_habit_response.php">
        <!-- Send the question id along with the answer -->
        <input type="hidden" name="question_id" value="<?php echo $question['id']; ?>">
        <textarea name="answer" rows="4" cols="50" placeholder="Type your answer here..." required></textarea>
        <br><br>
        <input type="submit" value="Submit Answer">
    </form>
    <!-- Optionally, add a link back to your main dashboard -->
    <p><a href="dashboard_employee.php">Back to Dashboard</a></p>
</body>
</html>
