diff --git a/src/app/(pages)/(main)/safety-inspection/page.tsx b/src/app/(pages)/(main)/safety-inspection/page.tsx index 674e92b..e22ea26 100644 --- a/src/app/(pages)/(main)/safety-inspection/page.tsx +++ b/src/app/(pages)/(main)/safety-inspection/page.tsx @@ -1,12 +1,14 @@ 'use client' import {useLayoutFilter} from "@/components/layout/LayoutCategoryContext"; +import SafetyInspectionTotal from "./sub/SafetyInspectionTotal"; +import SafetyInspectionDivDetail from "./sub/SafetyInspectionDivDetail"; const SafetyInspectionPage = () => { const { selectedDiv } = useLayoutFilter(); return ( -
SafetyInspectionPage {selectedDiv}
+ selectedDiv === "-1" ? : ) } diff --git a/src/app/(pages)/(main)/safety-inspection/sub/SafetyInspectionDivDetail.tsx b/src/app/(pages)/(main)/safety-inspection/sub/SafetyInspectionDivDetail.tsx new file mode 100644 index 0000000..bac458c --- /dev/null +++ b/src/app/(pages)/(main)/safety-inspection/sub/SafetyInspectionDivDetail.tsx @@ -0,0 +1,302 @@ +"use client"; + +import { useEffect, useMemo, useState } from "react"; +import { useQueryClient } from "@tanstack/react-query"; +import { toast } from "sonner"; +import DataTable from "@/components/table/DataTable"; +import type { DataColumn } from "@/components/table/types"; +import { Button } from "@/components/ui/button"; +import { DIV_KEY_MAP } from "@/config/kfpaConfig"; +import { useAuth } from "@/hooks/useAuth"; +import { formatKoreanBaseDate } from "@/lib/utils"; +import { useWriteDivCommentMutation } from "@/query/admin"; +import { statisticsQueryKeys } from "@/query/key/keys"; +import { useDivDetailDataQuery } from "@/query/statistics"; +import type { CommentValue, DivDetailData } from "@/types/kfpa"; +import { Pencil, Save, X } from 'lucide-react'; +import SectionTitle from "@/components/common/SectionTitle"; + +export type SafetyInspectionDivDetailProps = { + selectedDiv: string; +}; + +type RankRow = { + type: string; + csi: number | null; + rank: string; +}; + +type CommentField = keyof CommentValue; + +type CommentRow = { + label: string; + importance: string; + field: CommentField; +}; + +type CommentTableRow = CommentRow & Record; + +const DEFAULT_COMMENT_COLUMNS = [ + { key: "performance25", label: "25년 성과" }, + { key: "plan26", label: "26년 업무계획" }, + { key: "performance26", label: "26년 업무계획 진행률 및 성과" }, +]; + +const COMMENT_ROWS: CommentRow[] = [ + { label: "고객 직접 체감", importance: "★★★", field: "directExperience" }, + { label: "고객 간접 체감", importance: "★★", field: "indirectExperience" }, + { label: "내부역량 강화 등", importance: "★", field: "capability" }, +]; + +const RANK_ROWS = [ + { key: "90", label: "일반" }, + { key: "4", label: "공장" }, + { key: "12", label: "아파트" }, +]; + +const getSelectedDetail = (data: Record | undefined, selectedDiv: string) => { + if (!data) return undefined; + return data[selectedDiv] ?? Object.values(data)[0]; +}; + +const getCommentColumns = (commentValues?: Record) => { + const keys = Object.keys(commentValues ?? {}); + const selectedKeys = keys.length > 0 ? keys : DEFAULT_COMMENT_COLUMNS.map((column) => column.key); + + return selectedKeys.map((key, index) => ({ + key, + label: DEFAULT_COMMENT_COLUMNS[index]?.label ?? key, + })); +}; + +const createEmptyCommentValue = (): CommentValue => ({ + directExperience: "", + indirectExperience: "", + capability: "", +}); + +const createDraft = (columns: Array<{ key: string }>, source?: Record) => + columns.reduce>((acc, column) => { + acc[column.key] = { + ...createEmptyCommentValue(), + ...(source?.[column.key] ?? {}), + }; + return acc; + }, {}); + +const SafetyInspectionDivDetail = ({ selectedDiv }: SafetyInspectionDivDetailProps) => { + const { auth } = useAuth(); + const queryClient = useQueryClient(); + const { data, isLoading, isError } = useDivDetailDataQuery(selectedDiv, selectedDiv !== "-1"); + const writeMutation = useWriteDivCommentMutation(); + + const detail = useMemo(() => getSelectedDetail(data, selectedDiv), [data, selectedDiv]); + const commentColumns = useMemo(() => getCommentColumns(detail?.commentValues), [detail?.commentValues]); + const [isEditing, setIsEditing] = useState(false); + const [draft, setDraft] = useState>(() => + createDraft(commentColumns, detail?.commentValues) + ); + + useEffect(() => { + setIsEditing(false); + setDraft(createDraft(commentColumns, detail?.commentValues)); + }, [commentColumns, detail?.commentValues, selectedDiv]); + + const baseDateLabel = useMemo(() => formatKoreanBaseDate(), []); + const isAuthenticated = auth.user != null; + + const rankRows: RankRow[] = useMemo(() => { + const ranks = detail?.csiRanks ?? {}; + return RANK_ROWS.map(({ key, label }) => ({ + type: label, + csi: ranks[key]?.csi ?? 0, + rank: `${ranks[key]?.divRank ?? 0}/6`, + })); + }, [detail?.csiRanks]); + + const rankColumns: DataColumn[] = useMemo( + () => [ + { key: "type", header: "물건 유형", align: "center" }, + { + key: "csi", + header: "고객만족지수", + align: "center", + render: (row) => (typeof row.csi === "number" ? row.csi.toFixed(2) : "-"), + }, + { key: "rank", header: "지역본부순위", align: "center" }, + ], + [] + ); + + const updateDraft = (columnKey: string, field: CommentField, value: string) => { + setDraft((prev) => ({ + ...prev, + [columnKey]: { + ...createEmptyCommentValue(), + ...(prev[columnKey] ?? {}), + [field]: value, + }, + })); + }; + + const commentRows: CommentTableRow[] = useMemo( + () => + COMMENT_ROWS.map((row) => ({ + ...row, + ...Object.fromEntries(commentColumns.map((column) => [column.key, draft[column.key]?.[row.field] ?? ""])), + })), + [commentColumns, draft] + ); + + const commentTableColumns: DataColumn[] = useMemo( + () => [ + { + key: "label", + header: "", + rowHead: true, + width: 170, + align: "center", + cellClassName: "bg-red-50 text-center font-bold", + }, + { + key: "importance", + header: "중요도(난이도)", + width: 110, + align: "center", + cellClassName: "font-bold", + }, + ...commentColumns.map>((column) => ({ + key: column.key, + header: column.label, + align: "left", + cellClassName: "align-top", + render: (row) => { + const value = String(draft[column.key]?.[row.field] ?? ""); + + if (!isEditing) { + return
{value || "-"}
; + } + + return ( +