251127:1700 Frontend Start Build

This commit is contained in:
admin
2025-11-27 17:08:49 +07:00
parent 6abb746e08
commit 4f3aa87a93
1795 changed files with 893474 additions and 10 deletions

View File

@@ -0,0 +1,355 @@
// 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<typeof correspondenceSchema>;
// --- 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<File[]>([]);
// React Hook Form
const {
register,
handleSubmit,
setValue,
watch,
formState: { errors },
} = useForm<FormValues>({
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 (
<div className="max-w-4xl mx-auto space-y-6 pb-10">
{/* Header */}
<div className="flex items-center gap-4">
<Button variant="outline" size="icon" onClick={() => router.back()}>
<ChevronLeft className="h-5 w-5" />
</Button>
<div>
<h2 className="text-2xl font-bold tracking-tight">Create Correspondence</h2>
<p className="text-muted-foreground">
</p>
</div>
</div>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-8">
{/* Section 1: Basic Info */}
<Card>
<CardHeader>
<CardTitle> (Basic Information)</CardTitle>
<CardDescription>
</CardDescription>
</CardHeader>
<CardContent className="grid gap-6 md:grid-cols-2">
{/* Project Select */}
<div className="space-y-2">
<Label className="after:content-['*'] after:text-red-500"> (Project)</Label>
<Select onValueChange={(val) => setValue("projectId", val)}>
<SelectTrigger className={errors.projectId ? "border-destructive" : ""}>
<SelectValue placeholder="เลือกโครงการ..." />
</SelectTrigger>
<SelectContent>
{projects.map((p) => (
<SelectItem key={p.id} value={p.id}>{p.name}</SelectItem>
))}
</SelectContent>
</Select>
{errors.projectId && <p className="text-xs text-destructive">{errors.projectId.message}</p>}
</div>
{/* Document Type Select */}
<div className="space-y-2">
<Label className="after:content-['*'] after:text-red-500"> (Type)</Label>
<Select onValueChange={(val) => setValue("type", val)}>
<SelectTrigger className={errors.type ? "border-destructive" : ""}>
<SelectValue placeholder="เลือกประเภท..." />
</SelectTrigger>
<SelectContent>
{docTypes.map((t) => (
<SelectItem key={t.id} value={t.id}>{t.name}</SelectItem>
))}
</SelectContent>
</Select>
{errors.type && <p className="text-xs text-destructive">{errors.type.message}</p>}
</div>
{/* Discipline (Conditional) */}
<div className="space-y-2">
<Label> (Discipline)</Label>
<Select onValueChange={(val) => setValue("discipline", val)}>
<SelectTrigger>
<SelectValue placeholder="ระบุสาขา (ถ้ามี)..." />
</SelectTrigger>
<SelectContent>
{disciplines.map((d) => (
<SelectItem key={d.id} value={d.id}>{d.name}</SelectItem>
))}
</SelectContent>
</Select>
<p className="text-[10px] text-muted-foreground"> RFA/RFI </p>
</div>
{/* Originator (Sender) */}
<div className="space-y-2">
<Label className="after:content-['*'] after:text-red-500"> (From)</Label>
<Select defaultValue="3" onValueChange={(val) => setValue("originatorId", val)} disabled>
<SelectTrigger>
<SelectValue placeholder="Select Originator" />
</SelectTrigger>
<SelectContent>
{organizations.map((o) => (
<SelectItem key={o.id} value={o.id}>{o.name}</SelectItem>
))}
</SelectContent>
</Select>
</div>
{/* Document Number Preview */}
<div className="md:col-span-2 p-4 bg-muted/30 rounded-lg border border-dashed flex items-center justify-between">
<span className="text-sm font-medium text-muted-foreground">Preview Document No:</span>
<span className="font-mono text-lg font-bold text-primary">{getPreviewNumber()}</span>
</div>
</CardContent>
</Card>
{/* Section 2: Details & Recipients */}
<Card>
<CardHeader>
<CardTitle> (Details & Recipients)</CardTitle>
</CardHeader>
<CardContent className="space-y-6">
{/* Recipient */}
<div className="space-y-2">
<Label className="after:content-['*'] after:text-red-500"> (To)</Label>
<Select onValueChange={(val) => setValue("recipientId", val)}>
<SelectTrigger className={errors.recipientId ? "border-destructive" : ""}>
<SelectValue placeholder="เลือกหน่วยงานผู้รับ..." />
</SelectTrigger>
<SelectContent>
{organizations.filter(o => o.id !== "3").map((o) => (
<SelectItem key={o.id} value={o.id}>{o.name}</SelectItem>
))}
</SelectContent>
</Select>
{errors.recipientId && <p className="text-xs text-destructive">{errors.recipientId.message}</p>}
</div>
{/* Subject */}
<div className="space-y-2">
<Label className="after:content-['*'] after:text-red-500"> (Subject)</Label>
<Input
placeholder="ระบุหัวข้อเอกสาร..."
className={errors.subject ? "border-destructive" : ""}
{...register("subject")}
/>
{errors.subject && <p className="text-xs text-destructive">{errors.subject.message}</p>}
</div>
{/* Message/Body */}
<div className="space-y-2">
<Label> (Message)</Label>
<Textarea
placeholder="พิมพ์รายละเอียดเพิ่มเติม..."
className="min-h-[120px]"
{...register("message")}
/>
</div>
{/* Reply Required Date */}
<div className="flex flex-col space-y-2">
<Label> (Reply Required By)</Label>
<Popover>
<PopoverTrigger asChild>
<Button
variant={"outline"}
className={cn(
"w-[240px] pl-3 text-left font-normal",
!watch("replyRequiredBy") && "text-muted-foreground"
)}
>
{watch("replyRequiredBy") ? (
format(watch("replyRequiredBy")!, "PPP")
) : (
<span>Pick a date</span>
)}
<CalendarIcon className="ml-auto h-4 w-4 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-auto p-0" align="start">
<Calendar
mode="single"
selected={watch("replyRequiredBy")}
onSelect={(date) => setValue("replyRequiredBy", date)}
disabled={(date) => date < new Date()}
initialFocus
/>
</PopoverContent>
</Popover>
</div>
</CardContent>
</Card>
{/* Section 3: Attachments */}
<Card>
<CardHeader>
<CardTitle> (Attachments)</CardTitle>
<CardDescription>
PDF, DWG, Office ( 50MB )
</CardDescription>
</CardHeader>
<CardContent>
<FileUpload
onFilesChange={setFiles}
maxFiles={10}
accept=".pdf,.doc,.docx,.xls,.xlsx,.dwg,.zip,.jpg,.png"
/>
</CardContent>
</Card>
{/* Actions */}
<div className="flex justify-end gap-4">
<Button variant="outline" type="button" onClick={() => router.back()} disabled={isLoading}>
</Button>
<Button type="submit" disabled={isLoading} className="min-w-[120px]">
{isLoading ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" /> Saving...
</>
) : (
<>
<Save className="mr-2 h-4 w-4" /> Save as Draft
</>
)}
</Button>
<Button type="submit" disabled={isLoading} className="min-w-[120px] bg-green-600 hover:bg-green-700">
{isLoading ? (
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
) : (
<Send className="mr-2 h-4 w-4" />
)}
Submit
</Button>
</div>
</form>
</div>
);
}

View File

@@ -0,0 +1,290 @@
// File: app/(dashboard)/correspondences/page.tsx
"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import {
Plus,
Search,
Filter,
MoreHorizontal,
FileText,
Calendar,
Eye
} from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Badge } from "@/components/ui/badge";
import { Checkbox } from "@/components/ui/checkbox";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Card, CardContent, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
// --- Type Definitions ---
type Correspondence = {
id: string;
documentNumber: string;
subject: string;
type: "Letter" | "Memo" | "RFI" | "Transmittal";
status: "Draft" | "Submitted" | "Approved" | "Rejected";
project: string;
date: string;
from: string;
to: string;
};
// --- Mock Data ---
const mockData: Correspondence[] = [
{
id: "1",
documentNumber: "LCBP3-LET-GEN-001",
subject: "Submission of Monthly Progress Report - January 2025",
type: "Letter",
status: "Submitted",
project: "LCBP3-C1",
date: "2025-01-05",
from: "CNNC",
to: "PAT",
},
{
id: "2",
documentNumber: "LCBP3-RFI-STR-024",
subject: "Clarification on Beam Reinforcement Detail at Zone A",
type: "RFI",
status: "Approved",
project: "LCBP3-C2",
date: "2025-01-10",
from: "ITD-NWR",
to: "TEAM",
},
{
id: "3",
documentNumber: "LCBP3-MEMO-HR-005",
subject: "Site Access Protocol Update",
type: "Memo",
status: "Draft",
project: "LCBP3",
date: "2025-01-12",
from: "PAT",
to: "All Contractors",
},
{
id: "4",
documentNumber: "LCBP3-TRN-STR-011",
subject: "Transmittal of Shop Drawings for Piling Works",
type: "Transmittal",
status: "Submitted",
project: "LCBP3-C1",
date: "2025-01-15",
from: "CNNC",
to: "CSC",
},
];
export default function CorrespondencesPage() {
const router = useRouter();
const [searchTerm, setSearchTerm] = useState("");
const [typeFilter, setTypeFilter] = useState("ALL");
// Filter Logic
const filteredData = mockData.filter((item) => {
const matchesSearch =
item.subject.toLowerCase().includes(searchTerm.toLowerCase()) ||
item.documentNumber.toLowerCase().includes(searchTerm.toLowerCase());
const matchesType = typeFilter === "ALL" || item.type === typeFilter;
return matchesSearch && matchesType;
});
const getStatusVariant = (status: string) => {
switch (status) {
case "Approved": return "success";
case "Rejected": return "destructive";
case "Draft": return "secondary";
default: return "default"; // Submitted
}
};
const handleCreate = () => {
router.push("/correspondences/new");
};
return (
<div className="space-y-6">
{/* Header */}
<div className="flex flex-col gap-4 md:flex-row md:items-center md:justify-between">
<div>
<h2 className="text-2xl font-bold tracking-tight">Correspondences</h2>
<p className="text-muted-foreground">
-
</p>
</div>
<Button onClick={handleCreate} className="w-full md:w-auto">
<Plus className="mr-2 h-4 w-4" />
</Button>
</div>
{/* Toolbar (Search & Filter) */}
<div className="flex flex-col gap-4 md:flex-row md:items-center">
<div className="relative flex-1">
<Search className="absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground" />
<Input
placeholder="ค้นหาจากเลขที่เอกสาร หรือ หัวข้อ..."
className="pl-8"
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
/>
</div>
<div className="flex gap-2">
<Select defaultValue="ALL" onValueChange={setTypeFilter}>
<SelectTrigger className="w-[150px]">
<SelectValue placeholder="ประเภทเอกสาร" />
</SelectTrigger>
<SelectContent>
<SelectItem value="ALL"></SelectItem>
<SelectItem value="Letter">Letter</SelectItem>
<SelectItem value="Memo">Memo</SelectItem>
<SelectItem value="RFI">RFI</SelectItem>
<SelectItem value="Transmittal">Transmittal</SelectItem>
</SelectContent>
</Select>
<Button variant="outline" size="icon">
<Filter className="h-4 w-4" />
</Button>
</div>
</div>
{/* Desktop View: Table */}
<div className="hidden rounded-md border bg-card md:block">
<Table>
<TableHeader>
<TableRow>
<TableHead className="w-[40px]">
<Checkbox />
</TableHead>
<TableHead>Document No.</TableHead>
<TableHead className="w-[400px]">Subject</TableHead>
<TableHead>Type</TableHead>
<TableHead>From/To</TableHead>
<TableHead>Date</TableHead>
<TableHead>Status</TableHead>
<TableHead className="text-right">Actions</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{filteredData.map((item) => (
<TableRow key={item.id} className="cursor-pointer hover:bg-muted/50" onClick={() => router.push(`/correspondences/${item.id}`)}>
<TableCell onClick={(e) => e.stopPropagation()}>
<Checkbox />
</TableCell>
<TableCell className="font-medium text-primary">
{item.documentNumber}
<div className="text-xs text-muted-foreground mt-0.5">{item.project}</div>
</TableCell>
<TableCell>
<span className="line-clamp-2">{item.subject}</span>
</TableCell>
<TableCell>{item.type}</TableCell>
<TableCell>
<div className="text-xs">
<div className="text-muted-foreground">From: <span className="text-foreground font-medium">{item.from}</span></div>
<div className="text-muted-foreground">To: <span className="text-foreground font-medium">{item.to}</span></div>
</div>
</TableCell>
<TableCell>{item.date}</TableCell>
<TableCell>
<Badge variant={getStatusVariant(item.status)}>{item.status}</Badge>
</TableCell>
<TableCell className="text-right">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="h-8 w-8 p-0">
<span className="sr-only">Open menu</span>
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuLabel>Actions</DropdownMenuLabel>
<DropdownMenuItem onClick={(e) => { e.stopPropagation(); router.push(`/correspondences/${item.id}`); }}>
View Details
</DropdownMenuItem>
<DropdownMenuItem>Download PDF</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem>Create Transmittal</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
{/* Mobile View: Cards */}
<div className="grid gap-4 md:hidden">
{filteredData.map((item) => (
<Card key={item.id} onClick={() => router.push(`/correspondences/${item.id}`)}>
<CardHeader className="pb-2">
<div className="flex justify-between items-start">
<div className="flex flex-col">
<Badge variant="outline" className="w-fit mb-2">{item.type}</Badge>
<CardTitle className="text-base font-bold">{item.documentNumber}</CardTitle>
</div>
<Badge variant={getStatusVariant(item.status)}>{item.status}</Badge>
</div>
</CardHeader>
<CardContent className="pb-2 text-sm">
<p className="line-clamp-2 mb-3">{item.subject}</p>
<div className="grid grid-cols-2 gap-2 text-muted-foreground">
<div className="flex items-center gap-1">
<FileText className="h-3 w-3" /> {item.project}
</div>
<div className="flex items-center gap-1">
<Calendar className="h-3 w-3" /> {item.date}
</div>
</div>
<div className="mt-3 pt-3 border-t grid grid-cols-2 gap-2 text-xs">
<div>
<span className="text-muted-foreground block">From</span>
<span className="font-medium">{item.from}</span>
</div>
<div>
<span className="text-muted-foreground block">To</span>
<span className="font-medium">{item.to}</span>
</div>
</div>
</CardContent>
<CardFooter className="pt-2">
<Button variant="ghost" size="sm" className="w-full text-primary">
<Eye className="mr-2 h-4 w-4" /> View Details
</Button>
</CardFooter>
</Card>
))}
</div>
</div>
);
}