<?php 
include 'config.php'; 
if(!isset($_SESSION['user_id'])) exit();

$id = (int)$_GET['id'];
$agency_id = $_SESSION['agency_id'];

// FIXED: Uses LEFT JOIN so enquiries for manual names can be opened
$sql = "SELECT q.*, c.name as db_name 
        FROM queries q 
        LEFT JOIN clients c ON q.client_id = c.id 
        WHERE q.id = $id AND q.agency_id = $agency_id";

$res = $conn->query($sql);
if($res->num_rows == 0) die("Enquiry not found.");
$query = $res->fetch_assoc();

$display_name = !empty($query['db_name']) ? $query['db_name'] : $query['manual_name'];

if(isset($_POST['update_query'])){
    $status = $_POST['status'];
    $desc = $_POST['description'];
    $conn->query("UPDATE queries SET status='$status', description='$desc' WHERE id=$id");
    header("Location: queries.php?msg=updated");
    exit();
}

if(isset($_GET['delete'])){
    $conn->query("DELETE FROM queries WHERE id=$id AND agency_id=$agency_id");
    header("Location: queries.php?msg=deleted");
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Update Enquiry</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
    <style>
        body { font-family: 'Poppins', sans-serif; background: #f8f9fc; padding: 50px; }
        .card { background: #fff; max-width: 500px; margin: auto; padding: 30px; border-radius: 15px; box-shadow: 0 10px 30px rgba(0,0,0,0.05); }
        select, textarea { width: 100%; padding: 12px; margin: 10px 0; border: 1px solid #ddd; border-radius: 10px; display:block; box-sizing: border-box; }
        .btn { width: 100%; padding: 12px; border: none; border-radius: 10px; cursor: pointer; font-weight: 600; margin-top: 10px; color: white; display: block; text-align: center; text-decoration: none; }
    </style>
</head>
<body>
    <div class="card">
        <h2>Update Status</h2>
        <p>Client: <b style="color:#4e73df;"><?php echo htmlspecialchars($display_name); ?></b></p>
        <form method="POST">
            <select name="status">
                <option <?php if($query['status']=='Pending') echo 'selected';?>>Pending</option>
                <option <?php if($query['status']=='In Process') echo 'selected';?>>In Process</option>
                <option <?php if($query['status']=='Done') echo 'selected';?>>Done</option>
                <option <?php if($query['status']=='Cancelled') echo 'selected';?>>Cancelled</option>
            </select>
            <textarea name="description" rows="4"><?php echo $query['description'];?></textarea>
            <button type="submit" name="update_query" class="btn" style="background:#4e73df;">Save Changes</button>
            <a href="update_query.php?id=<?php echo $id;?>&delete=1" class="btn" style="background:#ef4444;" onclick="return confirm('Delete permanently?')">Delete Enquiry</a>
            <a href="queries.php" style="display:block; text-align:center; margin-top:15px; color:#64748b; text-decoration:none; font-size:13px;">Cancel</a>
        </form>
    </div>
</body>
</html>