<?php
require_once 'includes/config.php';

try {
    $pdo->exec("ALTER TABLE yeu_cau_dat_lich ADD COLUMN IF NOT EXISTS email VARCHAR(255) DEFAULT NULL");
    $pdo->exec("ALTER TABLE yeu_cau_dat_lich ADD COLUMN IF NOT EXISTS so_luong INT NOT NULL DEFAULT 1");
    $pdo->exec("ALTER TABLE yeu_cau_dat_lich ADD COLUMN IF NOT EXISTS ma_gioi_thieu VARCHAR(50) DEFAULT NULL");
    $pdo->exec("ALTER TABLE yeu_cau_dat_lich ADD COLUMN IF NOT EXISTS nguoi_gioi_thieu_id INT DEFAULT NULL");
    $pdo->exec("ALTER TABLE yeu_cau_dat_lich ADD COLUMN IF NOT EXISTS linh_vuc VARCHAR(20) DEFAULT 'nhac'");
    $pdo->exec("ALTER TABLE yeu_cau_dat_lich ADD COLUMN IF NOT EXISTS trang_thai VARCHAR(30) NOT NULL DEFAULT 'cho_xu_ly'");
    $pdo->exec("ALTER TABLE yeu_cau_dat_lich ADD COLUMN IF NOT EXISTS tinh_thanh VARCHAR(100) DEFAULT NULL");
    $pdo->exec("ALTER TABLE yeu_cau_dat_lich ADD COLUMN IF NOT EXISTS dia_chi VARCHAR(255) DEFAULT NULL");
} catch (Exception $e) {
    // Bỏ qua lỗi nếu cấu trúc bảng đã có sẵn.
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $la_ajax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';

    $dich_vu = trim($_POST['dich_vu_quan_tam'] ?? '');
    $ten_kh = trim($_POST['ten_khach_hang'] ?? '');
    $sdt = trim($_POST['so_dien_thoai'] ?? '');
    $tinh_thanh = trim($_POST['tinh_thanh'] ?? '');
    $dia_chi = trim($_POST['dia_chi'] ?? '');
    $ngay = !empty($_POST['ngay_su_kien']) ? $_POST['ngay_su_kien'] : null;
    $ghi_chu = trim($_POST['ghi_chu'] ?? '');
    $so_luong = max(1, (int)($_POST['so_luong'] ?? 1));
    $ma_gioi_thieu = trim(strtoupper($_POST['ma_gioi_thieu'] ?? ''));

    $nguoi_gioi_thieu_id = null;
    if (!empty($ma_gioi_thieu)) {
        $stmt_ref = $pdo->prepare("SELECT id FROM thanh_vien WHERE ma_gioi_thieu = ? LIMIT 1");
        $stmt_ref->execute([$ma_gioi_thieu]);
        $ref_user = $stmt_ref->fetch();
        if ($ref_user) {
            $nguoi_gioi_thieu_id = (int)$ref_user['id'];
        }
    }

    $linh_vuc = trim($_POST['linh_vuc'] ?? '');
    if ($linh_vuc === '') {
        $linh_vuc = 'nhac';
    }

    $email = trim($_POST['email'] ?? '');
    
    $sql = "INSERT INTO yeu_cau_dat_lich (ten_khach_hang, email, so_dien_thoai, tinh_thanh, dia_chi, dich_vu_quan_tam, ngay_su_kien, ghi_chu, so_luong, ma_gioi_thieu, nguoi_gioi_thieu_id, linh_vuc, trang_thai) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    $stmt = $pdo->prepare($sql);
    $ok = $stmt->execute([
        $ten_kh,
        $email !== '' ? $email : null,
        $sdt,
        $tinh_thanh !== '' ? $tinh_thanh : null,
        $dia_chi !== '' ? $dia_chi : null,
        $dich_vu,
        $ngay,
        $ghi_chu,
        $so_luong,
        $ma_gioi_thieu !== '' ? $ma_gioi_thieu : null,
        $nguoi_gioi_thieu_id,
        $linh_vuc,
        'cho_xu_ly'
    ]);

    if ($la_ajax) {
        header('Content-Type: application/json');
        echo json_encode(['ok' => $ok]);
        exit;
    }

    if ($ok) {
        echo "<script>alert('Gửi yêu cầu thành công! Ban Quản trị sẽ liên hệ với bạn trong thời gian sớm nhất.'); window.location='am-nhac.php';</script>";
    } else {
        echo "<script>alert('Có lỗi xảy ra, vui lòng thử lại.'); window.location='am-nhac.php';</script>";
    }
}
?>