diff --git a/ng-ttc-frontend/src/app/component/chat-widget-component/chat-widget-component.ts b/ng-ttc-frontend/src/app/component/chat-widget-component/chat-widget-component.ts index 453ed78..bb7c12a 100644 --- a/ng-ttc-frontend/src/app/component/chat-widget-component/chat-widget-component.ts +++ b/ng-ttc-frontend/src/app/component/chat-widget-component/chat-widget-component.ts @@ -1,4 +1,6 @@ +import { GeneralService } from './../../services/generalservice'; import { ReactiveFormsModule, FormGroup, FormControl, Validators } from '@angular/forms'; +import { IChat } from '../../interfaces/main.interface' import { Component, HostListener } from '@angular/core'; @Component({ @@ -11,6 +13,7 @@ export class ChatWidgetComponent { isOpen = false; // newMessage = ''; chatForm!: FormGroup; + messages:IChat[]=[]; // State สำหรับจัดการขนาด isMaximized = false; @@ -27,7 +30,9 @@ export class ChatWidgetComponent { private startHeight = 0; - constructor() { + constructor( + private generalService: GeneralService + ) { this.setupFormControl(); // เรียกใช้ตอนเริ่ม Component } @@ -38,14 +43,42 @@ export class ChatWidgetComponent { } - messages = [ - { text: 'สวัสดีครับ AI พร้อมช่วยเหลือครับ 👋', isUser: false }, - { text: 'ลองกดปุ่มขยายที่หัวมุม หรือลากมุมซ้ายบนของกล่องเพื่อปรับขนาดได้เลยครับ', isUser: false }, - ]; toggleChat() { this.isOpen = !this.isOpen; - this.isMaximized = false; // Reset ขนาดเมื่อปิด + let body = {methods: 'ind'} + this.OnAiChat(body); + const isAlreadyHave = this.messages.some(sub => sub.text == 'รอAi ประมวณผลสักครู่'); + if(isAlreadyHave === false){ + this.messages.push({ + text: 'รอAi ประมวณผลสักครู่', + isUser: false + }) + } + this.isMaximized = false; + } + + OnAiChat(value: any){ + let url = 'https://n8n.nuttakit.work/webhook/Ai' + let request = { + methods: value.methods || 'cht', + message: value.message ?? '' + } + this.generalService.postUrl(url, request).subscribe({ + next: (result: any) => { + if (result.code === 200) { + this.messages.push(result.data) + }else{ + this.generalService.trowApi(result); + } + }, + error: (error: any) => { + + }, + complete: () => { + + } + }) } toggleMaximize() { @@ -57,12 +90,12 @@ export class ChatWidgetComponent { if (newMessage.trim()) { this.messages.push({ text: newMessage, isUser: true }); newMessage = ''; - setTimeout(() => { - this.messages.push({ - text: 'รับทราบครับ ระบบกำลังประมวลผล...', - isUser: false - }); - }, 1000); + // setTimeout(() => { + // this.messages.push({ + // text: 'รับทราบครับ ระบบกำลังประมวลผล...', + // isUser: false + // }); + // }, 1000); } } diff --git a/ng-ttc-frontend/src/app/interfaces/main.interface.ts b/ng-ttc-frontend/src/app/interfaces/main.interface.ts index 5390030..91ba377 100644 --- a/ng-ttc-frontend/src/app/interfaces/main.interface.ts +++ b/ng-ttc-frontend/src/app/interfaces/main.interface.ts @@ -25,3 +25,9 @@ export interface IDropBdg { bdgnam?: string; bdgcod?: number; } + + +export interface IChat { + text?: string; + isUser?: boolean +} diff --git a/ng-ttc-frontend/src/app/services/generalservice.ts b/ng-ttc-frontend/src/app/services/generalservice.ts index cd3d1c9..b36fa49 100644 --- a/ng-ttc-frontend/src/app/services/generalservice.ts +++ b/ng-ttc-frontend/src/app/services/generalservice.ts @@ -73,6 +73,35 @@ export class GeneralService { ); } + postUrl(uri: string, body: any): Observable { + let payload: any; + let isJson = true; + + // เช็คว่าเป็น FormData หรือไม่? + if (body instanceof FormData) { + payload = body; // ส่งไปตรงๆ ไม่ต้องครอบ request + isJson = false; // บอก getHttpOptions ว่าไม่ต้องใส่ JSON Header + } else { + payload = this.wrapRequestBody(body); // ทำงานแบบเดิม + isJson = true; + } + + const fullUrl = `${uri}`; + + return this.http.post(fullUrl, payload, this.getHttpOptions(isJson)).pipe( + map((res: any) => res), + catchError((error: any) => { + const response = error?.error; + // console.error('❌ [POST Request Error]:', error); + return throwError(() => ({ + status: error.status, + code: response?.code ?? '500', + message: response?.message ?? 'Internal Server Error', + message_th: response?.message_th ?? 'เกิดข้อผิดพลาดภายในเซิร์ฟเวอร์' + })); + }) + ); + } // ตัวอย่างใน Component ที่จะ Upload // onSave() {