false, 'message' => 'No UID provided']); exit; } if (!$line_pdo) { echo json_encode(['success' => false, 'message' => 'Cannot connect to LINE database']); exit; } try { // 1. ตรวจสอบ lineUID จากฐานข้อมูล line_bot (ตาราง line_staff_register) $stmt = $line_pdo->prepare("SELECT cid, fname, lname FROM line_staff_register WHERE user_id = ? LIMIT 1"); $stmt->execute([$uid]); $line_user = $stmt->fetch(); if (!$line_user || empty($line_user['cid'])) { echo json_encode(['success' => false, 'message' => 'ไม่พบข้อมูลลงทะเบียน LINE']); exit; } $cid = $line_user['cid']; // ชื่อ-สกุลที่ลงทะเบียนในระบบ LINE $line_name = trim(($line_user['fname'] ?? '') . ' ' . ($line_user['lname'] ?? '')); // 2. นำ cid มา mapping กับ hr_cid ในระบบหลัก (ตาราง drivers) $stmt2 = $pdo->prepare("SELECT id, first_name, last_name FROM drivers WHERE hr_cid = ? LIMIT 1"); $stmt2->execute([$cid]); $driver = $stmt2->fetch(); if ($driver) { echo json_encode([ 'success' => true, 'cid' => $cid, 'driver_id' => $driver['id'], 'driver_name' => trim($driver['first_name'] . ' ' . $driver['last_name']), 'is_driver' => true ]); } else { // แม้จะไม่พบในระบบหลัก แต่มี cid ให้ส่งกลับไปให้กรอกในฟอร์มได้ // และใช้ชื่อ-สกุลจากฐานข้อมูล line_bot แทน echo json_encode([ 'success' => true, 'cid' => $cid, 'driver_id' => null, 'driver_name' => $line_name ?: null, 'is_driver' => false ]); } } catch (Exception $e) { echo json_encode(['success' => false, 'message' => 'Error: ' . $e->getMessage()]); }