import { Component, OnInit, Output, EventEmitter } from '@angular/core'; import { FormGroup, FormControl, Validators } from '@angular/forms'; import { ToastrService } from 'ngx-toastr'; @Component({ selector: 'app-main-project-add', standalone: false, templateUrl: './main-project-add.html', styleUrl: './main-project-add.css', }) export class MainProjectAdd implements OnInit { // ไม่ต้องใช้ @Input() แล้ว Parent จะเข้าถึงผ่าน ViewChild isLoading: boolean = false; @Output() save = new EventEmitter(); @Output() cancel = new EventEmitter(); currentStep: number = 1; projectForm!: FormGroup; attachedFiles: any[] = []; constructor(private toastr: ToastrService) {} ngOnInit(): void { this.setupFormControl(); } setupFormControl(): void { this.projectForm = new FormGroup({ projectName: new FormControl('', [Validators.required, Validators.maxLength(200)]), budgetAmount: new FormControl('', [Validators.required, Validators.min(1)]) }); } goToStep(step: number): void { if (step === 2 && this.currentStep === 1) { if (this.projectForm.invalid) { this.projectForm.markAllAsTouched(); this.toastr.warning('กรุณากรอกข้อมูลให้ครบถ้วน', 'แจ้งเตือน'); return; } } this.currentStep = step; } onFileSelected(event: any): void { const files = event.target.files; if (files && files.length > 0) { for (let i = 0; i < files.length; i++) { const file = files[i]; const reader = new FileReader(); reader.onload = (e: any) => { this.attachedFiles.push({ name: file.name, size: file.size, type: file.type, content: e.target.result }); }; reader.readAsDataURL(file); } } } removeFile(index: number): void { this.attachedFiles.splice(index, 1); } get f() { return this.projectForm.controls; } formatCurrency(amount: any): string { if (!amount) return '0.00 บาท'; return new Intl.NumberFormat('th-TH', { style: 'currency', currency: 'THB' }).format(Number(amount)); } onSubmit(): void { if (this.projectForm.invalid) return; const body = { ...this.projectForm.value, files: this.attachedFiles }; this.save.emit(body); } onCancel(): void { this.cancel.emit(); } }