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

<?php
if (isset($_POST['issue_query'])) {
    $staff_id = intval($_POST['staff_id']);
    $query_text = $conn->real_escape_string($_POST['query_text']);
    $issued_by = $_SESSION['user_id'];

    $conn->query("INSERT INTO staff_queries (staff_id, query_text, issued_by, date_issued) 
                  VALUES ($staff_id, '$query_text', '$issued_by', NOW())");

    echo "<script>alert('Query successfully issued.'); window.location='staff_list.php';</script>";
}
?>

<div class="container mt-4">
    <h4 class="text-orange">Issue Query to Staff</h4>

    <form method="post" class="mt-3">
        <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 * FROM staffs ORDER BY name ASC");
                while ($staff = $staffs->fetch_assoc()) {
                    echo "<option value='{$staff['id']}'>{$staff['name']} - {$staff['email']}</option>";
                }
                ?>
            </select>
        </div>

        <div class="mb-3">
            <label>Query Text</label>
            <textarea name="query_text" class="form-control" rows="5" required></textarea>
        </div>

        <button type="submit" name="issue_query" class="btn btn-danger">Issue Query</button>
    </form>
</div>

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