<?php
// submit.php — REVERTED TO ORIGINAL POSTGRESQL BACKEND
ob_start();

header('Content-Type: application/json');

// Helper Function
function get_post_val($field, $is_array = false) {
    if (!isset($_POST[$field])) return null;
    if ($is_array) return implode(', ', (array)$_POST[$field]);
    return trim($_POST[$field]);
}

try {
    // --- 1. Database Connection (SQL Server - CONFIG.dbo.EMD_QUESTIONNAIRE) ---
    $host_sqlsrv = '192.168.10.61';
    $user_sqlsrv = 'zack';
    $pass_sqlsrv = 'zack123456';
    $db_sqlsrv   = 'EHONGDB';
    $dsn_sqlsrv  = "sqlsrv:Server=$host_sqlsrv;Database=$db_sqlsrv;TrustServerCertificate=true";

    $pdo = new PDO($dsn_sqlsrv, $user_sqlsrv, $pass_sqlsrv, [
        PDO::SQLSRV_ATTR_ENCODING => PDO::SQLSRV_ENCODING_UTF8,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);

    // --- 2. Prepare Data ---
    $barcode = isset($_COOKIE['kRlfYPpU_user_barcode']) ? trim($_COOKIE['kRlfYPpU_user_barcode']) : null;
    $branch  = isset($_COOKIE['kRlfYPpU_user_br_id']) ? trim($_COOKIE['kRlfYPpU_user_br_id']) : null;
    $app     = isset($_COOKIE['kRlfYPpU_activity_code']) ? trim($_COOKIE['kRlfYPpU_activity_code']) : null;

    $params = [
        date('Y-m-d H:i:s'),
        $barcode, $branch, $app,
        get_post_val('gender'),
        get_post_val('birthyear'),
        get_post_val('job'),
        get_post_val('job_other'),
        get_post_val('brand', true),
        get_post_val('brand_other'),
        get_post_val('used_service'),
        get_post_val('bought_products', true),
        get_post_val('bought_other'),
        get_post_val('ehong_satisfaction'),
        get_post_val('ehong_suggestion'),
        get_post_val('ehong_return'),
        get_post_val('ehong_return_other'),
        get_post_val('ehong_branch'),
        get_post_val('ehong_branch_other'),
        get_post_val('other_shop_name'),
        get_post_val('other_shop_satisfaction'),
        get_post_val('other_shop_return'),
        get_post_val('other_return_other'),
        get_post_val('future_interest_ehong'),
        get_post_val('future_interest_ehong_other'),
        get_post_val('fullname'),
        get_post_val('phone'),
        get_post_val('changwat'),
        get_post_val('amphoe'),
        get_post_val('tambon'),
        get_post_val('event_interest'),
        get_post_val('event_interest_other')
    ];

    // --- 3. SQL Insert ---
    $sql = "INSERT INTO CONFIG.dbo.EMD_QUESTIONNAIRE (
        submission_date, employee_barcode, branch, app_code,
        gender, birthyear, job, job_other, brand, brand_other, used_service,
        bought_products, bought_other, ehong_satisfaction, ehong_suggestion,
        ehong_return, ehong_return_other, ehong_branch, ehong_branch_other,
        other_shop_name, other_shop_satisfaction, other_shop_return, other_shop_return_other,
        future_interest_ehong, future_interest_ehong_other,
        fullname, phone,
        changwat, amphoe, tambon,
        event_interest, event_interest_other
    ) VALUES (" . implode(',', array_fill(0, count($params), '?')) . ")";

    $stmt = $pdo->prepare($sql);
    $stmt->execute($params);

    ob_end_clean();
    echo json_encode(['success' => true]);

} catch (PDOException $e) {
    ob_end_clean();
    error_log("Database Error: " . $e->getMessage());
    echo json_encode(['success' => false, 'message' => "Database Error: " . $e->getMessage()]);
}