<?php include 'includes/header.php'; ?>
<?php include 'includes/sidebar.php'; ?>
<?php require 'includes/db_connect.php'; ?>

<?php
// Check if mail_id is passed
if (isset($_GET['mail_id'])) {
    $mail_id = intval($_GET['mail_id']);
    $mail_query = $conn->query("SELECT * FROM mails WHERE id = '$mail_id'");
    if ($mail_query->num_rows > 0) {
        $mail = $mail_query->fetch_assoc();
    } else {
        echo "<div class='alert alert-danger text-center'>Mail not found.</div>";
        exit();
    }
} else {
    echo "<div class='alert alert-warning text-center'>Invalid Request! No Mail Selected.</div>";
    exit();
}

// Handle Minute Form Submission
if (isset($_POST['minute_mail'])) {
    $staff_id = intval($_POST['staff_id']);
    $comment = trim($_POST['comment']);

    // Get Staff Details
    $staff_query = $conn->query("SELECT fullname, department FROM staff WHERE id = '$staff_id'");
    if ($staff_query->num_rows > 0) {
        $staff = $staff_query->fetch_assoc();
        $staff_name = $staff['fullname'];
        $staff_dept = $staff['department'];
 $reg_date = date('Y-m-d H:i:s');
        // Insert into mail_history
        $stmt = $conn->prepare("INSERT INTO mail_history (mail_id, staff_id, moved_by, moved_to, action, action_date) VALUES (?, ?, ?, ?, ?, NOW())");
        $action = "Mail Minuted to $staff_name ($staff_dept)";
        $moved_by = $_SESSION['fullname'];  // Assuming super admin is logged in
        $stmt->bind_param("iisss", $mail_id, $staff_id, $moved_by, $staff_dept, $action);
        $stmt->execute();

        // Update mails table with new department/status
        $conn->query("UPDATE mails SET department = '$staff_dept', status = 'Minuted', date_minuted = '$reg_date', action = '$comment' WHERE id = '$mail_id'");

        echo "<script>alert('Mail successfully minuted to $staff_name'); window.location='all_mails.php';</script>";
    } else {
        echo "<div class='alert alert-danger text-center'>Staff not found.</div>";
    }
}
?>

<div class="container mt-4">
    <h4 class="text-orange">Minute Mail - <?= htmlspecialchars($mail['subject']); ?></h4>

    <div class="card mt-3">
        <div class="card-header text-white" style="background-color: orange;">Tracking No: <?= htmlspecialchars($mail['tracking_no']); ?></div>
        <div class="card-body">
            <p><strong>Subject:</strong> <?= htmlspecialchars($mail['subject']); ?></p>
            <p><strong>Type:</strong> <?= htmlspecialchars($mail['mail_type']); ?></p>
            <p><strong>Current Status:</strong> <?= htmlspecialchars($mail['status']); ?></p>
            <p><strong>Last Moved To:</strong> <?= htmlspecialchars($mail['department']); ?></p>
        </div>
    </div>

    <div class="card mt-4">
        <div class="card-header text-white" style="background-color: orange;">Minute to Staff</div>
        <div class="card-body">
            <form method="POST">
                <div class="mb-3">
                    <label>Select Staff</label>
                    <select name="staff_id" class="form-control" required>
                        <option value="">-- Select Staff --</option>
                        <?php
                        $staffs = $conn->query("SELECT id, fullname, department FROM staff ORDER BY fullname ASC");
                        while ($s = $staffs->fetch_assoc()) {
                            echo "<option value='{$s['id']}'>{$s['fullname']} - {$s['department']}</option>";
                        }
                        ?>
                    </select>
                </div>
                <div class="mb-3">
                    <label>Comment/Instruction</label>
                    <textarea name="comment" class="form-control" rows="3" required></textarea>
                </div>
                <button type="submit" name="minute_mail" class="btn btn-warning text-white">Minute Mail</button>
            </form>
        </div>
    </div>
</div>

<?php include 'includes/footer.php'; ?>
