// File: app/(dashboard)/correspondences/new/page.tsx "use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import * as z from "zod"; import { format } from "date-fns"; import { CalendarIcon, ChevronLeft, Save, Loader2, Send } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Calendar } from "@/components/ui/calendar"; import { FileUpload } from "@/components/forms/file-upload"; import { cn } from "@/lib/utils"; // --- Schema Definition --- const correspondenceSchema = z.object({ projectId: z.string().min(1, "กรุณาเลือกโครงการ"), originatorId: z.string().min(1, "กรุณาเลือกองค์กรผู้ส่ง"), type: z.string().min(1, "กรุณาเลือกประเภทเอกสาร"), discipline: z.string().optional(), // สำหรับ RFA/Letter ที่มีสาขา subType: z.string().optional(), // สำหรับแบ่งประเภทย่อย recipientId: z.string().min(1, "กรุณาเลือกผู้รับ (To)"), subject: z.string().min(5, "หัวข้อต้องยาวอย่างน้อย 5 ตัวอักษร"), message: z.string().optional(), replyRequiredBy: z.date().optional(), }); type FormValues = z.infer; // --- Mock Data for Dropdowns --- const projects = [ { id: "1", code: "LCBP3-C1", name: "งานก่อสร้างงานทางทะเล (ส่วนที่ 1)" }, { id: "2", code: "LCBP3-C2", name: "งานก่อสร้างอาคาร (ส่วนที่ 2)" }, ]; const organizations = [ { id: "1", code: "PAT", name: "การท่าเรือฯ (Owner)" }, { id: "2", code: "CSC", name: "ที่ปรึกษาคุมงาน (Consult)" }, { id: "3", code: "CNNC", name: "ผู้รับเหมา C1" }, ]; const docTypes = [ { id: "LET", name: "Letter (จดหมาย)" }, { id: "MEM", name: "Memo (บันทึกข้อความ)" }, { id: "RFA", name: "RFA (ขออนุมัติ)" }, { id: "RFI", name: "RFI (ขอข้อมูล)" }, ]; const disciplines = [ { id: "GEN", name: "General (ทั่วไป)" }, { id: "STR", name: "Structural (โครงสร้าง)" }, { id: "ARC", name: "Architectural (สถาปัตยกรรม)" }, { id: "MEP", name: "MEP (งานระบบ)" }, ]; export default function CreateCorrespondencePage() { const router = useRouter(); const [isLoading, setIsLoading] = useState(false); const [files, setFiles] = useState([]); // React Hook Form const { register, handleSubmit, setValue, watch, formState: { errors }, } = useForm({ resolver: zodResolver(correspondenceSchema), defaultValues: { originatorId: "3", // Default เป็น Org ของ User (Mock: CNNC) }, }); // Watch values to update dynamic parts const selectedProject = watch("projectId"); const selectedType = watch("type"); const selectedDiscipline = watch("discipline"); // Logic จำลองการ Preview เลขที่เอกสาร (Document Numbering) const getPreviewNumber = () => { if (!selectedProject || !selectedType) return "---"; const proj = projects.find(p => p.id === selectedProject)?.code || "PROJ"; const type = selectedType; const disc = selectedDiscipline ? `-${selectedDiscipline}` : ""; return `${proj}-${type}${disc}-0001 (Draft)`; }; const onSubmit = async (data: FormValues) => { setIsLoading(true); try { console.log("Form Data:", data); console.log("Files:", files); // Simulate API call await new Promise((resolve) => setTimeout(resolve, 1500)); alert("บันทึกเอกสารเรียบร้อยแล้ว"); router.push("/correspondences"); } catch (error) { console.error(error); alert("เกิดข้อผิดพลาดในการบันทึก"); } finally { setIsLoading(false); } }; return (
{/* Header */}

Create Correspondence

สร้างเอกสารใหม่เพื่อส่งออกไปยังหน่วยงานอื่น

{/* Section 1: Basic Info */} ข้อมูลเบื้องต้น (Basic Information) ระบุโครงการและประเภทของเอกสาร เลขที่เอกสารจะถูกสร้างอัตโนมัติ {/* Project Select */}
{errors.projectId &&

{errors.projectId.message}

}
{/* Document Type Select */}
{errors.type &&

{errors.type.message}

}
{/* Discipline (Conditional) */}

จำเป็นสำหรับ RFA/RFI เพื่อการรันเลขที่ถูกต้อง

{/* Originator (Sender) */}
{/* Document Number Preview */}
Preview Document No: {getPreviewNumber()}
{/* Section 2: Details & Recipients */} รายละเอียดและผู้รับ (Details & Recipients) {/* Recipient */}
{errors.recipientId &&

{errors.recipientId.message}

}
{/* Subject */}
{errors.subject &&

{errors.subject.message}

}
{/* Message/Body */}