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

$id = intval($_GET['id'] ?? 0);
if (!$id) {
    die("Invalid ID");
}

// Update logic
if (isset($_POST['update'])) {
    $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'];

    $update = "
        UPDATE escalation_master 
        SET department_id='$department_id',
            level_no='$level_no',
            user_id='$user_id',
            time_limit_hours='$time_limit_hours',
            next_level_user_id='$next_level_user_id',
            status='$status'
        WHERE id=$id
    ";

    if (mysqli_query($con, $update)) {
        echo "<script>alert('Escalation updated successfully!'); window.location='escalation_master_list.php';</script>";
        exit;
    } else {
        echo "Error: " . mysqli_error($con);
    }
}

// Fetch current data
$record = mysqli_fetch_assoc(mysqli_query($con, "SELECT * FROM escalation_master WHERE id=$id"));
if (!$record) {
    die("Record not found!");
}

// Fetch dropdown data
$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">Edit Escalation</h2>

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

        <label>Escalation Level</label>
        <input type="number" name="level_no" value="<?= $record['level_no'] ?>" required>

        <label>User</label>
        <select name="user_id" required>
            <?php 
            mysqli_data_seek($user_q, 0);
            while($u = mysqli_fetch_assoc($user_q)) { ?>
                <option value="<?= $u['user_id'] ?>" <?= ($u['user_id'] == $record['user_id']) ? 'selected' : '' ?>>
                    <?= $u['name'] ?>
                </option>
            <?php } ?>
        </select>

        <label>Time Limit (in hours)</label>
        <input type="number" name="time_limit_hours" value="<?= $record['time_limit_hours'] ?>" required>

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

        <label>Status</label>
        <select name="status">
            <option value="1" <?= $record['status'] == 1 ? 'selected' : '' ?>>Active</option>
            <option value="0" <?= $record['status'] == 0 ? 'selected' : '' ?>>Inactive</option>
        </select>

        <button type="submit" name="update" class="btn btn-success mt-3">Update Escalation</button>
        <a href="escalation_master_list.php" class="btn btn-secondary mt-3">Back</a>
    </form>
</div>

</body>
</html>
