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

if (isset($_POST['add_query'])) {
    $agency_id   = $_SESSION['agency_id'];
    $service     = $_POST['service_type']; 
    $description = $_POST['description'];
    $client_id   = !empty($_POST['client_id']) ? (int)$_POST['client_id'] : NULL;
    $manual_name = $_POST['manual_name'];
    $manual_phone= $_POST['manual_phone'];

    // SQL strictly matching the columns: agency_id(i), client_id(i), manual_name(s), manual_phone(s), service_type(s), description(s)
    $stmt = $conn->prepare("INSERT INTO queries (agency_id, client_id, manual_name, manual_phone, service_type, description, status) VALUES (?, ?, ?, ?, ?, ?, 'Pending')");
    
    // Total 6 placeholders: i, i, s, s, s, s
    $stmt->bind_param("iissss", $agency_id, $client_id, $manual_name, $manual_phone, $service, $description);

    if ($stmt->execute()) {
        header("Location: queries.php?msg=success");
    } else {
        echo "Error: " . $conn->error;
    }
}
?>