<?php
include("../../includes/auth_check.php");
include("../../includes/header.php");
include("../../config/config.php");

if(isset($_POST['submit'])){
    $department_id = $_POST['department_id'];
    $level_no = $_POST['level_no'];
    $user_id = $_POST['user_id'];
    $time_limit_hours = $_POST['time_limit_hours'];
    $next_level_user_id = $_POST['next_level_user_id'];
    $status = $_POST['status'];
    $created_by = $_SESSION['id'] ?? 1;

    $query = "INSERT INTO escalation_master (department_id, level_no, user_id, time_limit_hours, next_level_user_id, status, created_by)
              VALUES ('$department_id', '$level_no', '$user_id', '$time_limit_hours', '$next_level_user_id', '$status', '$created_by')";
    
    if (mysqli_query($con, $query)) {
        echo "<script>alert('Escalation level added successfully!');</script>";
    } else {
        echo "Error: " . mysqli_error($con);
    }
}

// Fetch departments and users
$dept_q = mysqli_query($con, "SELECT dept_id, dept_name FROM departments WHERE status=1");
$user_q = mysqli_query($con, "SELECT user_id, name FROM users WHERE status=1");
?>

<div class="container mt-5">
    <h2 class="mb-4">Manage Users</h2>

    <!-- Add Admin Form -->
    <div class="card mb-4 shadow-sm">
        <div class="card-body">
            <h5 class="card-title">Add New User</h5>

            <form method="post">
                <h3>Escalation Master</h3>
                
                <label>Department</label>
                <select name="department_id" required>
                    <option value="">Select Department</option>
                    <?php while($row = mysqli_fetch_assoc($dept_q)) { ?>
                        <option value="<?= $row['dept_id'] ?>"><?= $row['dept_name'] ?></option>
                    <?php } ?>
                </select>

                <label>Escalation Level</label>
                <input type="number" name="level_no" required>

                <label>User</label>
                <select name="user_id" required>
                    <option value="">Select User</option>
                    <?php while($u = mysqli_fetch_assoc($user_q)) { ?>
                        <option value="<?= $u['user_id'] ?>"><?= $u['name'] ?></option>
                    <?php } ?>
                </select>

                <label>Time Limit (in hours)</label>
                <input type="number" name="time_limit_hours" required>

                <label>Next Level User (Optional)</label>
                <select name="next_level_user_id">
                    <option value="">Select Next Level User</option>
                    <?php 
                    mysqli_data_seek($user_q, 0); // reset user result pointer
                    while($n = mysqli_fetch_assoc($user_q)) { ?>
                        <option value="<?= $n['user_id'] ?>"><?= $n['name'] ?></option>
                    <?php } ?>
                </select>

                <label>Status</label>
                <select name="status">
                    <option value="1">Active</option>
                    <option value="0">Inactive</option>
                </select>

                <button type="submit" name="submit">Save Escalation</button>
            </form>
        </div>
    </div>
</div>

</body>
</html>
