2025-11-28 19:44:32 +07:00
|
|
|
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
|
2025-11-28 19:28:41 +07:00
|
|
|
import { FormGroup, FormControl, Validators } from '@angular/forms';
|
|
|
|
|
import { ToastrService } from 'ngx-toastr';
|
2025-11-21 10:24:49 +07:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'app-main-project-add',
|
|
|
|
|
standalone: false,
|
|
|
|
|
templateUrl: './main-project-add.html',
|
|
|
|
|
styleUrl: './main-project-add.css',
|
|
|
|
|
})
|
2025-11-28 19:28:41 +07:00
|
|
|
export class MainProjectAdd implements OnInit {
|
2025-11-28 19:44:32 +07:00
|
|
|
// ไม่ต้องใช้ @Input() แล้ว Parent จะเข้าถึงผ่าน ViewChild
|
2025-11-28 19:28:41 +07:00
|
|
|
isLoading: boolean = false;
|
2025-11-21 10:24:49 +07:00
|
|
|
|
2025-11-28 19:44:32 +07:00
|
|
|
@Output() save = new EventEmitter<any>();
|
|
|
|
|
@Output() cancel = new EventEmitter<void>();
|
|
|
|
|
|
|
|
|
|
currentStep: number = 1;
|
2025-11-28 19:28:41 +07:00
|
|
|
projectForm!: FormGroup;
|
|
|
|
|
attachedFiles: any[] = [];
|
|
|
|
|
|
2025-11-28 19:44:32 +07:00
|
|
|
constructor(private toastr: ToastrService) {}
|
2025-11-28 19:28:41 +07:00
|
|
|
|
|
|
|
|
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)])
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-21 10:24:49 +07:00
|
|
|
goToStep(step: number): void {
|
2025-11-28 19:28:41 +07:00
|
|
|
if (step === 2 && this.currentStep === 1) {
|
|
|
|
|
if (this.projectForm.invalid) {
|
|
|
|
|
this.projectForm.markAllAsTouched();
|
|
|
|
|
this.toastr.warning('กรุณากรอกข้อมูลให้ครบถ้วน', 'แจ้งเตือน');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-21 10:24:49 +07:00
|
|
|
this.currentStep = step;
|
|
|
|
|
}
|
2025-11-28 19:28:41 +07:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-28 19:44:32 +07:00
|
|
|
get f() { return this.projectForm.controls; }
|
2025-11-28 19:28:41 +07:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2025-11-28 19:44:32 +07:00
|
|
|
const body = {
|
2025-11-28 19:28:41 +07:00
|
|
|
...this.projectForm.value,
|
|
|
|
|
files: this.attachedFiles
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-28 19:44:32 +07:00
|
|
|
this.save.emit(body);
|
|
|
|
|
}
|
2025-11-28 19:28:41 +07:00
|
|
|
|
2025-11-28 19:44:32 +07:00
|
|
|
onCancel(): void {
|
|
|
|
|
this.cancel.emit();
|
2025-11-28 19:28:41 +07:00
|
|
|
}
|
2025-11-21 10:24:49 +07:00
|
|
|
}
|