2026-03-03 22:00:52 +09:00
|
|
|
"use client";
|
|
|
|
|
|
2026-03-07 03:02:36 +09:00
|
|
|
import React, { useState, useEffect } from "react";
|
2026-03-04 11:19:57 +09:00
|
|
|
import {
|
|
|
|
|
Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle,
|
|
|
|
|
} from "@/components/ui/dialog";
|
2026-03-03 22:00:52 +09:00
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
|
import { Textarea } from "@/components/ui/textarea";
|
2026-03-04 11:19:57 +09:00
|
|
|
import { Badge } from "@/components/ui/badge";
|
2026-03-05 23:06:36 +09:00
|
|
|
import {
|
|
|
|
|
Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
|
|
|
|
|
} from "@/components/ui/select";
|
2026-03-07 03:02:36 +09:00
|
|
|
import { Plus, X, Loader2, GripVertical, Users, ArrowDown, Layers, FileText, ChevronsUpDown } from "lucide-react";
|
|
|
|
|
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command";
|
|
|
|
|
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
|
2026-03-04 11:19:57 +09:00
|
|
|
import { toast } from "sonner";
|
2026-03-05 23:06:36 +09:00
|
|
|
import {
|
|
|
|
|
createApprovalRequest,
|
|
|
|
|
getApprovalTemplates,
|
|
|
|
|
getTemplateSteps,
|
|
|
|
|
type ApprovalLineTemplate,
|
|
|
|
|
} from "@/lib/api/approval";
|
2026-03-04 11:19:57 +09:00
|
|
|
import { getUserList } from "@/lib/api/user";
|
|
|
|
|
|
|
|
|
|
// 결재 방식
|
|
|
|
|
type ApprovalMode = "sequential" | "parallel";
|
|
|
|
|
|
2026-03-05 23:06:36 +09:00
|
|
|
// 결재 유형
|
|
|
|
|
type ApprovalType = "self" | "escalation" | "consensus" | "post";
|
|
|
|
|
|
|
|
|
|
// step_type 라벨 매핑
|
|
|
|
|
const STEP_TYPE_LABEL: Record<string, { label: string; variant: "default" | "secondary" | "outline" }> = {
|
|
|
|
|
approval: { label: "결재", variant: "default" },
|
|
|
|
|
consensus: { label: "합의", variant: "secondary" },
|
|
|
|
|
notification: { label: "통보", variant: "outline" },
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-03 22:00:52 +09:00
|
|
|
interface ApproverRow {
|
2026-03-04 11:19:57 +09:00
|
|
|
id: string;
|
|
|
|
|
user_id: string;
|
|
|
|
|
user_name: string;
|
|
|
|
|
position_name: string;
|
|
|
|
|
dept_name: string;
|
2026-03-05 23:06:36 +09:00
|
|
|
step_type?: "approval" | "consensus" | "notification";
|
|
|
|
|
step_order?: number;
|
2026-03-03 22:00:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ApprovalModalEventDetail {
|
|
|
|
|
targetTable: string;
|
|
|
|
|
targetRecordId: string;
|
|
|
|
|
targetRecordData?: Record<string, any>;
|
|
|
|
|
definitionId?: number;
|
|
|
|
|
screenId?: number;
|
|
|
|
|
buttonComponentId?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ApprovalRequestModalProps {
|
|
|
|
|
open: boolean;
|
|
|
|
|
onOpenChange: (open: boolean) => void;
|
|
|
|
|
eventDetail?: ApprovalModalEventDetail | null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 11:19:57 +09:00
|
|
|
interface UserSearchResult {
|
2026-03-04 18:26:16 +09:00
|
|
|
userId: string;
|
|
|
|
|
userName: string;
|
|
|
|
|
positionName?: string;
|
|
|
|
|
deptName?: string;
|
|
|
|
|
deptCode?: string;
|
|
|
|
|
email?: string;
|
|
|
|
|
user_id?: string;
|
|
|
|
|
user_name?: string;
|
2026-03-04 11:19:57 +09:00
|
|
|
position_name?: string;
|
|
|
|
|
dept_name?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function genId(): string {
|
|
|
|
|
return `a_${Date.now()}_${Math.random().toString(36).slice(2, 7)}`;
|
2026-03-03 22:00:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const ApprovalRequestModal: React.FC<ApprovalRequestModalProps> = ({
|
|
|
|
|
open,
|
|
|
|
|
onOpenChange,
|
|
|
|
|
eventDetail,
|
|
|
|
|
}) => {
|
|
|
|
|
const [title, setTitle] = useState("");
|
|
|
|
|
const [description, setDescription] = useState("");
|
2026-03-04 11:19:57 +09:00
|
|
|
const [approvalMode, setApprovalMode] = useState<ApprovalMode>("sequential");
|
2026-03-03 22:00:52 +09:00
|
|
|
const [approvers, setApprovers] = useState<ApproverRow[]>([]);
|
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
|
|
2026-03-05 23:06:36 +09:00
|
|
|
// 결재 유형 상태
|
|
|
|
|
const [approvalType, setApprovalType] = useState<ApprovalType>("escalation");
|
|
|
|
|
|
|
|
|
|
// 템플릿 상태
|
|
|
|
|
const [templates, setTemplates] = useState<ApprovalLineTemplate[]>([]);
|
|
|
|
|
const [selectedTemplateId, setSelectedTemplateId] = useState<number | null>(null);
|
|
|
|
|
const [showTemplatePopover, setShowTemplatePopover] = useState(false);
|
|
|
|
|
const [isLoadingTemplates, setIsLoadingTemplates] = useState(false);
|
|
|
|
|
|
2026-03-07 03:02:36 +09:00
|
|
|
// 결재자 Combobox 상태
|
|
|
|
|
const [comboboxOpen, setComboboxOpen] = useState(false);
|
|
|
|
|
const [allUsers, setAllUsers] = useState<UserSearchResult[]>([]);
|
|
|
|
|
const [isLoadingUsers, setIsLoadingUsers] = useState(false);
|
2026-03-03 22:00:52 +09:00
|
|
|
|
|
|
|
|
// 모달 닫힐 때 초기화
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!open) {
|
|
|
|
|
setTitle("");
|
|
|
|
|
setDescription("");
|
2026-03-04 11:19:57 +09:00
|
|
|
setApprovalMode("sequential");
|
2026-03-05 23:06:36 +09:00
|
|
|
setApprovalType("escalation");
|
2026-03-03 22:00:52 +09:00
|
|
|
setApprovers([]);
|
|
|
|
|
setError(null);
|
2026-03-07 03:02:36 +09:00
|
|
|
setComboboxOpen(false);
|
|
|
|
|
setAllUsers([]);
|
2026-03-05 23:06:36 +09:00
|
|
|
setSelectedTemplateId(null);
|
|
|
|
|
setShowTemplatePopover(false);
|
|
|
|
|
}
|
|
|
|
|
}, [open]);
|
|
|
|
|
|
2026-03-07 03:02:36 +09:00
|
|
|
// 모달 열릴 때 템플릿 + 사용자 목록 로드
|
2026-03-05 23:06:36 +09:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (open) {
|
|
|
|
|
loadTemplates();
|
2026-03-07 03:02:36 +09:00
|
|
|
loadUsers();
|
2026-03-03 22:00:52 +09:00
|
|
|
}
|
|
|
|
|
}, [open]);
|
|
|
|
|
|
2026-03-07 03:02:36 +09:00
|
|
|
const loadUsers = async () => {
|
|
|
|
|
setIsLoadingUsers(true);
|
|
|
|
|
try {
|
|
|
|
|
const res = await getUserList({ limit: 100 });
|
|
|
|
|
const data = res?.data || res || [];
|
|
|
|
|
const rawUsers: any[] = Array.isArray(data) ? data : [];
|
|
|
|
|
const users: UserSearchResult[] = rawUsers.map((u: any) => ({
|
|
|
|
|
userId: u.userId || u.user_id || "",
|
|
|
|
|
userName: u.userName || u.user_name || "",
|
|
|
|
|
positionName: u.positionName || u.position_name || "",
|
|
|
|
|
deptName: u.deptName || u.dept_name || "",
|
|
|
|
|
deptCode: u.deptCode || u.dept_code || "",
|
|
|
|
|
email: u.email || "",
|
|
|
|
|
}));
|
|
|
|
|
setAllUsers(users.filter((u) => u.userId));
|
|
|
|
|
} catch {
|
|
|
|
|
setAllUsers([]);
|
|
|
|
|
} finally {
|
|
|
|
|
setIsLoadingUsers(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-05 23:06:36 +09:00
|
|
|
const loadTemplates = async () => {
|
|
|
|
|
setIsLoadingTemplates(true);
|
|
|
|
|
try {
|
|
|
|
|
const res = await getApprovalTemplates({ is_active: "Y" });
|
|
|
|
|
if (res.success && res.data) {
|
|
|
|
|
setTemplates(res.data);
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// 템플릿 로드 실패는 무시 (선택사항이므로)
|
|
|
|
|
} finally {
|
|
|
|
|
setIsLoadingTemplates(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 템플릿 선택 시 결재자 리스트에 반영
|
|
|
|
|
const applyTemplate = async (templateId: number) => {
|
|
|
|
|
try {
|
|
|
|
|
const res = await getTemplateSteps(templateId);
|
|
|
|
|
if (!res.success || !res.data || res.data.length === 0) {
|
|
|
|
|
toast.error("템플릿에 설정된 결재 단계가 없습니다.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const steps = res.data;
|
|
|
|
|
const hasMultipleTypes = new Set(steps.map((s) => s.step_type || "approval")).size > 1;
|
|
|
|
|
|
|
|
|
|
// step_type이 혼합이면 escalation으로 설정
|
|
|
|
|
if (hasMultipleTypes) {
|
|
|
|
|
setApprovalType("escalation");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newApprovers: ApproverRow[] = steps.map((step) => ({
|
|
|
|
|
id: genId(),
|
|
|
|
|
user_id: step.approver_user_id || "",
|
|
|
|
|
user_name: step.approver_label || step.approver_user_id || "",
|
|
|
|
|
position_name: step.approver_position || "",
|
|
|
|
|
dept_name: step.approver_dept_code || "",
|
|
|
|
|
step_type: step.step_type || "approval",
|
|
|
|
|
step_order: step.step_order,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
setApprovers(newApprovers);
|
|
|
|
|
setSelectedTemplateId(templateId);
|
|
|
|
|
setShowTemplatePopover(false);
|
|
|
|
|
toast.success("템플릿이 적용되었습니다.");
|
|
|
|
|
} catch {
|
|
|
|
|
toast.error("템플릿 적용 중 오류가 발생했습니다.");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-07 03:02:36 +09:00
|
|
|
// Combobox에서 이미 선택된 사용자 제외한 목록
|
|
|
|
|
const availableUsers = allUsers.filter(
|
|
|
|
|
(u) => !approvers.some((a) => a.user_id === u.userId)
|
|
|
|
|
);
|
2026-03-04 11:19:57 +09:00
|
|
|
|
|
|
|
|
const addApprover = (user: UserSearchResult) => {
|
2026-03-03 22:00:52 +09:00
|
|
|
setApprovers((prev) => [
|
|
|
|
|
...prev,
|
|
|
|
|
{
|
2026-03-04 11:19:57 +09:00
|
|
|
id: genId(),
|
2026-03-04 18:26:16 +09:00
|
|
|
user_id: user.userId,
|
|
|
|
|
user_name: user.userName,
|
|
|
|
|
position_name: user.positionName || "",
|
|
|
|
|
dept_name: user.deptName || "",
|
2026-03-03 22:00:52 +09:00
|
|
|
},
|
|
|
|
|
]);
|
2026-03-07 03:02:36 +09:00
|
|
|
setComboboxOpen(false);
|
2026-03-03 22:00:52 +09:00
|
|
|
};
|
|
|
|
|
|
2026-03-04 11:19:57 +09:00
|
|
|
const removeApprover = (id: string) => {
|
2026-03-03 22:00:52 +09:00
|
|
|
setApprovers((prev) => prev.filter((a) => a.id !== id));
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-04 11:19:57 +09:00
|
|
|
const moveApprover = (idx: number, direction: "up" | "down") => {
|
|
|
|
|
setApprovers((prev) => {
|
|
|
|
|
const next = [...prev];
|
|
|
|
|
const targetIdx = direction === "up" ? idx - 1 : idx + 1;
|
|
|
|
|
if (targetIdx < 0 || targetIdx >= next.length) return prev;
|
|
|
|
|
[next[idx], next[targetIdx]] = [next[targetIdx], next[idx]];
|
|
|
|
|
return next;
|
|
|
|
|
});
|
2026-03-03 22:00:52 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSubmit = async () => {
|
|
|
|
|
if (!title.trim()) {
|
|
|
|
|
setError("결재 제목을 입력해주세요.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-03-05 23:06:36 +09:00
|
|
|
// 자기결재가 아닌 경우 결재자 필수
|
|
|
|
|
if (approvalType !== "self" && approvers.length === 0) {
|
2026-03-03 22:00:52 +09:00
|
|
|
setError("결재자를 1명 이상 추가해주세요.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-03-04 18:26:16 +09:00
|
|
|
if (!eventDetail?.targetTable) {
|
|
|
|
|
setError("결재 대상 테이블 정보가 없습니다. 버튼 설정을 확인해주세요.");
|
2026-03-03 22:00:52 +09:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setIsSubmitting(true);
|
|
|
|
|
setError(null);
|
|
|
|
|
|
2026-03-05 23:06:36 +09:00
|
|
|
// 혼합형 여부: approvers에 step_type이 설정된 경우
|
|
|
|
|
const hasMixedStepTypes = approvers.some((a) => a.step_type);
|
|
|
|
|
|
2026-03-03 22:00:52 +09:00
|
|
|
const res = await createApprovalRequest({
|
|
|
|
|
title: title.trim(),
|
|
|
|
|
description: description.trim() || undefined,
|
2026-03-07 03:02:36 +09:00
|
|
|
template_id: selectedTemplateId || undefined,
|
2026-03-03 22:00:52 +09:00
|
|
|
target_table: eventDetail.targetTable,
|
2026-03-04 18:26:16 +09:00
|
|
|
target_record_id: eventDetail.targetRecordId || undefined,
|
|
|
|
|
target_record_data: eventDetail.targetRecordData,
|
2026-03-05 23:06:36 +09:00
|
|
|
approval_mode: approvalType === "consensus" ? "parallel" : approvalMode,
|
|
|
|
|
approval_type: approvalType,
|
2026-03-03 22:00:52 +09:00
|
|
|
screen_id: eventDetail.screenId,
|
|
|
|
|
button_component_id: eventDetail.buttonComponentId,
|
2026-03-05 23:06:36 +09:00
|
|
|
approvers: approvalType === "self"
|
|
|
|
|
? []
|
|
|
|
|
: approvers.map((a, idx) => ({
|
|
|
|
|
approver_id: a.user_id,
|
|
|
|
|
approver_name: a.user_name,
|
|
|
|
|
approver_position: a.position_name || undefined,
|
|
|
|
|
approver_dept: a.dept_name || undefined,
|
|
|
|
|
step_type: hasMixedStepTypes ? (a.step_type || "approval") : undefined,
|
|
|
|
|
step_order: hasMixedStepTypes ? (a.step_order ?? idx + 1) : undefined,
|
|
|
|
|
approver_label: hasMixedStepTypes
|
|
|
|
|
? STEP_TYPE_LABEL[a.step_type || "approval"]?.label
|
|
|
|
|
: approvalMode === "sequential"
|
|
|
|
|
? `${idx + 1}차 결재`
|
|
|
|
|
: "동시 결재",
|
|
|
|
|
})),
|
2026-03-03 22:00:52 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
|
|
|
|
|
if (res.success) {
|
2026-03-04 11:19:57 +09:00
|
|
|
toast.success("결재 요청이 완료되었습니다.");
|
2026-03-03 22:00:52 +09:00
|
|
|
onOpenChange(false);
|
|
|
|
|
} else {
|
|
|
|
|
setError(res.error || res.message || "결재 요청에 실패했습니다.");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
|
|
|
<DialogContent className="max-w-[95vw] sm:max-w-[560px]">
|
|
|
|
|
<DialogHeader>
|
2026-03-04 11:19:57 +09:00
|
|
|
<DialogTitle className="text-base sm:text-lg">결재 상신</DialogTitle>
|
2026-03-03 22:00:52 +09:00
|
|
|
<DialogDescription className="text-xs sm:text-sm">
|
2026-03-04 11:19:57 +09:00
|
|
|
결재 방식을 선택하고 결재자를 검색하여 추가합니다.
|
2026-03-03 22:00:52 +09:00
|
|
|
</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
|
2026-03-04 11:19:57 +09:00
|
|
|
<div className="max-h-[65vh] space-y-4 overflow-y-auto pr-1">
|
2026-03-03 22:00:52 +09:00
|
|
|
{/* 결재 제목 */}
|
|
|
|
|
<div>
|
|
|
|
|
<Label htmlFor="approval-title" className="text-xs sm:text-sm">
|
|
|
|
|
결재 제목 <span className="text-destructive">*</span>
|
|
|
|
|
</Label>
|
|
|
|
|
<Input
|
|
|
|
|
id="approval-title"
|
|
|
|
|
value={title}
|
|
|
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
|
|
|
placeholder="결재 제목을 입력하세요"
|
|
|
|
|
className="h-8 text-xs sm:h-10 sm:text-sm"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* 결재 사유 */}
|
|
|
|
|
<div>
|
2026-03-04 11:19:57 +09:00
|
|
|
<Label htmlFor="approval-desc" className="text-xs sm:text-sm">
|
2026-03-03 22:00:52 +09:00
|
|
|
결재 사유
|
|
|
|
|
</Label>
|
|
|
|
|
<Textarea
|
2026-03-04 11:19:57 +09:00
|
|
|
id="approval-desc"
|
2026-03-03 22:00:52 +09:00
|
|
|
value={description}
|
|
|
|
|
onChange={(e) => setDescription(e.target.value)}
|
|
|
|
|
placeholder="결재 사유를 입력하세요 (선택사항)"
|
|
|
|
|
className="min-h-[60px] text-xs sm:text-sm"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-03-05 23:06:36 +09:00
|
|
|
{/* 결재 유형 + 템플릿 불러오기 */}
|
2026-03-03 22:00:52 +09:00
|
|
|
<div>
|
2026-03-05 23:06:36 +09:00
|
|
|
<div className="flex items-end gap-2">
|
|
|
|
|
<div className="flex-1">
|
|
|
|
|
<Label className="text-xs sm:text-sm">결재 유형</Label>
|
|
|
|
|
<Select
|
|
|
|
|
value={approvalType}
|
|
|
|
|
onValueChange={(v) => setApprovalType(v as ApprovalType)}
|
|
|
|
|
>
|
|
|
|
|
<SelectTrigger size="default" className="mt-1 h-8 text-xs sm:h-10 sm:text-sm">
|
|
|
|
|
<SelectValue />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
<SelectItem value="escalation">상신결재</SelectItem>
|
|
|
|
|
<SelectItem value="self">자기결재 (전결)</SelectItem>
|
|
|
|
|
<SelectItem value="consensus">합의결재</SelectItem>
|
|
|
|
|
<SelectItem value="post">후결</SelectItem>
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
</div>
|
|
|
|
|
{templates.length > 0 && (
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="sm"
|
|
|
|
|
className="h-8 shrink-0 text-xs sm:h-10 sm:text-sm"
|
|
|
|
|
onClick={() => setShowTemplatePopover(!showTemplatePopover)}
|
|
|
|
|
>
|
|
|
|
|
<FileText className="mr-1 h-3.5 w-3.5" />
|
|
|
|
|
템플릿
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
2026-03-03 22:00:52 +09:00
|
|
|
</div>
|
|
|
|
|
|
2026-03-05 23:06:36 +09:00
|
|
|
{/* 템플릿 선택 드롭다운 */}
|
|
|
|
|
{showTemplatePopover && (
|
|
|
|
|
<>
|
|
|
|
|
<div
|
|
|
|
|
className="fixed inset-0 z-40"
|
|
|
|
|
onClick={() => setShowTemplatePopover(false)}
|
|
|
|
|
/>
|
|
|
|
|
<div className="relative z-50 mt-2 rounded-md border bg-popover p-2 shadow-lg">
|
|
|
|
|
<p className="text-muted-foreground mb-2 text-[10px] font-medium">결재선 템플릿 선택</p>
|
|
|
|
|
{isLoadingTemplates ? (
|
|
|
|
|
<div className="flex items-center justify-center p-3">
|
2026-03-04 11:19:57 +09:00
|
|
|
<Loader2 className="text-muted-foreground h-4 w-4 animate-spin" />
|
|
|
|
|
</div>
|
2026-03-05 23:06:36 +09:00
|
|
|
) : templates.length === 0 ? (
|
|
|
|
|
<p className="text-muted-foreground p-3 text-center text-xs">등록된 템플릿이 없습니다.</p>
|
2026-03-04 11:19:57 +09:00
|
|
|
) : (
|
2026-03-05 23:06:36 +09:00
|
|
|
<div className="max-h-40 space-y-1 overflow-y-auto">
|
|
|
|
|
{templates.map((tpl) => (
|
2026-03-04 11:19:57 +09:00
|
|
|
<button
|
2026-03-05 23:06:36 +09:00
|
|
|
key={tpl.template_id}
|
2026-03-04 11:19:57 +09:00
|
|
|
type="button"
|
2026-03-05 23:06:36 +09:00
|
|
|
onClick={() => applyTemplate(tpl.template_id)}
|
|
|
|
|
className={`flex w-full items-center gap-2 rounded-md px-3 py-2 text-left text-xs transition-colors hover:bg-accent ${
|
|
|
|
|
selectedTemplateId === tpl.template_id ? "bg-accent" : ""
|
|
|
|
|
}`}
|
2026-03-04 11:19:57 +09:00
|
|
|
>
|
2026-03-05 23:06:36 +09:00
|
|
|
<FileText className="text-muted-foreground h-3.5 w-3.5 shrink-0" />
|
2026-03-04 11:19:57 +09:00
|
|
|
<div className="min-w-0 flex-1">
|
2026-03-05 23:06:36 +09:00
|
|
|
<p className="truncate font-medium">{tpl.template_name}</p>
|
|
|
|
|
{tpl.description && (
|
|
|
|
|
<p className="text-muted-foreground truncate text-[10px]">{tpl.description}</p>
|
|
|
|
|
)}
|
2026-03-04 11:19:57 +09:00
|
|
|
</div>
|
|
|
|
|
</button>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2026-03-05 23:06:36 +09:00
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* 후결 안내 배너 */}
|
|
|
|
|
{approvalType === "post" && (
|
2026-03-06 01:24:50 +09:00
|
|
|
<div className="rounded-md border border-warning/30 bg-warning/10 p-3 text-sm text-warning">
|
2026-03-05 23:06:36 +09:00
|
|
|
먼저 처리 후 나중에 결재받습니다. 결재 반려 시 별도 조치가 필요할 수 있습니다.
|
2026-03-04 11:19:57 +09:00
|
|
|
</div>
|
2026-03-05 23:06:36 +09:00
|
|
|
)}
|
2026-03-04 11:19:57 +09:00
|
|
|
|
2026-03-05 23:06:36 +09:00
|
|
|
{/* 자기결재: 결재자 섹션 대신 안내 메시지 */}
|
|
|
|
|
{approvalType === "self" ? (
|
|
|
|
|
<div className="bg-muted text-muted-foreground rounded-md p-4 text-sm">
|
|
|
|
|
본인이 직접 승인합니다.
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
{/* 결재 방식 (escalation, post에서만 표시. consensus는 순서 무관) */}
|
|
|
|
|
{approvalType !== "consensus" && (
|
|
|
|
|
<div>
|
|
|
|
|
<Label className="text-xs sm:text-sm">결재 방식</Label>
|
|
|
|
|
<div className="mt-1.5 grid grid-cols-2 gap-2">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setApprovalMode("sequential")}
|
|
|
|
|
className={`flex items-center gap-2 rounded-md border p-3 text-left transition-colors ${
|
|
|
|
|
approvalMode === "sequential"
|
|
|
|
|
? "border-primary bg-primary/5 ring-1 ring-primary"
|
|
|
|
|
: "hover:bg-muted/50"
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
<ArrowDown className="h-4 w-4 shrink-0" />
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-xs font-medium sm:text-sm">다단 결재</p>
|
|
|
|
|
<p className="text-muted-foreground text-[10px]">순차적으로 결재</p>
|
|
|
|
|
</div>
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setApprovalMode("parallel")}
|
|
|
|
|
className={`flex items-center gap-2 rounded-md border p-3 text-left transition-colors ${
|
|
|
|
|
approvalMode === "parallel"
|
|
|
|
|
? "border-primary bg-primary/5 ring-1 ring-primary"
|
|
|
|
|
: "hover:bg-muted/50"
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
<Layers className="h-4 w-4 shrink-0" />
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-xs font-medium sm:text-sm">동시 결재</p>
|
|
|
|
|
<p className="text-muted-foreground text-[10px]">모든 결재자 동시 진행</p>
|
|
|
|
|
</div>
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2026-03-03 22:00:52 +09:00
|
|
|
|
2026-03-05 23:06:36 +09:00
|
|
|
{/* 결재자 추가 (사용자 검색) */}
|
|
|
|
|
<div>
|
|
|
|
|
<div className="mb-2 flex items-center justify-between">
|
|
|
|
|
<Label className="text-xs sm:text-sm">
|
|
|
|
|
{approvalType === "consensus" ? "합의 결재자" : "결재자"}{" "}
|
|
|
|
|
<span className="text-destructive">*</span>
|
|
|
|
|
</Label>
|
|
|
|
|
<span className="text-muted-foreground text-[10px]">
|
|
|
|
|
{approvers.length}명 선택됨
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-03-07 03:02:36 +09:00
|
|
|
{/* 결재자 Combobox (Select + 검색) */}
|
|
|
|
|
<Popover open={comboboxOpen} onOpenChange={setComboboxOpen}>
|
|
|
|
|
<PopoverTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
role="combobox"
|
|
|
|
|
aria-expanded={comboboxOpen}
|
|
|
|
|
disabled={isLoadingUsers}
|
|
|
|
|
className="h-8 w-full justify-between text-xs sm:h-10 sm:text-sm"
|
|
|
|
|
>
|
|
|
|
|
{isLoadingUsers ? (
|
|
|
|
|
<span className="flex items-center gap-2">
|
|
|
|
|
<Loader2 className="h-3 w-3 animate-spin" />
|
|
|
|
|
사용자 목록 로딩 중...
|
|
|
|
|
</span>
|
2026-03-05 23:06:36 +09:00
|
|
|
) : (
|
2026-03-07 03:02:36 +09:00
|
|
|
<span className="text-muted-foreground">결재자를 선택하세요...</span>
|
|
|
|
|
)}
|
|
|
|
|
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
|
|
|
|
|
</Button>
|
|
|
|
|
</PopoverTrigger>
|
|
|
|
|
<PopoverContent
|
|
|
|
|
className="p-0"
|
|
|
|
|
style={{ width: "var(--radix-popover-trigger-width)" }}
|
|
|
|
|
align="start"
|
|
|
|
|
>
|
|
|
|
|
<Command>
|
|
|
|
|
<CommandInput
|
|
|
|
|
placeholder="이름 또는 사번으로 검색..."
|
|
|
|
|
className="text-xs sm:text-sm"
|
|
|
|
|
/>
|
|
|
|
|
<CommandList>
|
|
|
|
|
<CommandEmpty className="py-4 text-center text-xs">
|
|
|
|
|
검색 결과가 없습니다.
|
|
|
|
|
</CommandEmpty>
|
|
|
|
|
<CommandGroup>
|
|
|
|
|
{availableUsers.map((user) => (
|
|
|
|
|
<CommandItem
|
2026-03-05 23:06:36 +09:00
|
|
|
key={user.userId}
|
2026-03-07 03:02:36 +09:00
|
|
|
value={`${user.userName} ${user.userId} ${user.deptName || ""} ${user.positionName || ""}`}
|
|
|
|
|
onSelect={() => addApprover(user)}
|
|
|
|
|
className="flex cursor-pointer items-center gap-3 px-3 py-2 text-xs sm:text-sm"
|
2026-03-05 23:06:36 +09:00
|
|
|
>
|
2026-03-07 03:02:36 +09:00
|
|
|
<div className="bg-muted flex h-7 w-7 shrink-0 items-center justify-center rounded-full">
|
|
|
|
|
<Users className="h-3.5 w-3.5" />
|
2026-03-05 23:06:36 +09:00
|
|
|
</div>
|
|
|
|
|
<div className="min-w-0 flex-1">
|
2026-03-07 03:02:36 +09:00
|
|
|
<p className="truncate font-medium">
|
2026-03-05 23:06:36 +09:00
|
|
|
{user.userName}
|
|
|
|
|
<span className="text-muted-foreground ml-1 text-[10px]">
|
|
|
|
|
({user.userId})
|
|
|
|
|
</span>
|
|
|
|
|
</p>
|
|
|
|
|
<p className="text-muted-foreground truncate text-[10px]">
|
|
|
|
|
{[user.deptName, user.positionName].filter(Boolean).join(" / ") || "-"}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<Plus className="text-muted-foreground h-4 w-4 shrink-0" />
|
2026-03-07 03:02:36 +09:00
|
|
|
</CommandItem>
|
2026-03-05 23:06:36 +09:00
|
|
|
))}
|
2026-03-07 03:02:36 +09:00
|
|
|
</CommandGroup>
|
|
|
|
|
</CommandList>
|
|
|
|
|
</Command>
|
|
|
|
|
</PopoverContent>
|
|
|
|
|
</Popover>
|
2026-03-05 23:06:36 +09:00
|
|
|
|
|
|
|
|
{/* 선택된 결재자 목록 */}
|
|
|
|
|
{approvers.length === 0 ? (
|
|
|
|
|
<p className="text-muted-foreground mt-3 rounded-md border border-dashed p-4 text-center text-xs">
|
2026-03-07 03:02:36 +09:00
|
|
|
위 선택창에서 결재자를 선택하여 추가하세요
|
2026-03-05 23:06:36 +09:00
|
|
|
</p>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="mt-3 space-y-2">
|
|
|
|
|
{approvers.map((approver, idx) => (
|
|
|
|
|
<div
|
|
|
|
|
key={approver.id}
|
|
|
|
|
className="bg-muted/30 flex items-center gap-2 rounded-md border p-2"
|
|
|
|
|
>
|
|
|
|
|
{/* 순서 표시: consensus에서는 drag 숨김 */}
|
|
|
|
|
{approvalType === "consensus" ? (
|
|
|
|
|
<Badge variant="secondary" className="h-5 shrink-0 px-1.5 text-[10px]">
|
|
|
|
|
합의
|
|
|
|
|
</Badge>
|
|
|
|
|
) : approvalMode === "sequential" ? (
|
|
|
|
|
<div className="flex shrink-0 flex-col items-center gap-0.5">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => moveApprover(idx, "up")}
|
|
|
|
|
disabled={idx === 0}
|
|
|
|
|
className="text-muted-foreground hover:text-foreground disabled:opacity-30"
|
|
|
|
|
>
|
|
|
|
|
<GripVertical className="h-3 w-3 rotate-90" />
|
|
|
|
|
</button>
|
|
|
|
|
<Badge variant="outline" className="h-5 min-w-[24px] justify-center px-1 text-[10px]">
|
|
|
|
|
{idx + 1}
|
|
|
|
|
</Badge>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => moveApprover(idx, "down")}
|
|
|
|
|
disabled={idx === approvers.length - 1}
|
|
|
|
|
className="text-muted-foreground hover:text-foreground disabled:opacity-30"
|
|
|
|
|
>
|
|
|
|
|
<GripVertical className="h-3 w-3 rotate-90" />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<Badge variant="secondary" className="h-5 shrink-0 px-1.5 text-[10px]">
|
|
|
|
|
동시
|
|
|
|
|
</Badge>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* step_type 뱃지 (템플릿에서 불러온 혼합형일 때) */}
|
|
|
|
|
{approver.step_type && (
|
|
|
|
|
<Badge
|
|
|
|
|
variant={STEP_TYPE_LABEL[approver.step_type]?.variant || "outline"}
|
|
|
|
|
className="h-5 shrink-0 px-1.5 text-[10px]"
|
|
|
|
|
>
|
|
|
|
|
{STEP_TYPE_LABEL[approver.step_type]?.label || approver.step_type}
|
|
|
|
|
</Badge>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* 사용자 정보 */}
|
|
|
|
|
<div className="min-w-0 flex-1">
|
|
|
|
|
<p className="truncate text-xs font-medium">
|
|
|
|
|
{approver.user_name}
|
|
|
|
|
<span className="text-muted-foreground ml-1 text-[10px]">
|
|
|
|
|
({approver.user_id})
|
|
|
|
|
</span>
|
|
|
|
|
</p>
|
|
|
|
|
<p className="text-muted-foreground truncate text-[10px]">
|
|
|
|
|
{[approver.dept_name, approver.position_name].filter(Boolean).join(" / ") || "-"}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* 제거 버튼 */}
|
|
|
|
|
<Button
|
2026-03-04 11:19:57 +09:00
|
|
|
type="button"
|
2026-03-05 23:06:36 +09:00
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
className="h-6 w-6 shrink-0"
|
|
|
|
|
onClick={() => removeApprover(approver.id)}
|
2026-03-04 11:19:57 +09:00
|
|
|
>
|
2026-03-05 23:06:36 +09:00
|
|
|
<X className="h-3 w-3" />
|
|
|
|
|
</Button>
|
2026-03-04 11:19:57 +09:00
|
|
|
</div>
|
2026-03-05 23:06:36 +09:00
|
|
|
))}
|
2026-03-04 11:19:57 +09:00
|
|
|
|
2026-03-05 23:06:36 +09:00
|
|
|
{/* 결재 흐름 시각화 (consensus가 아니고 sequential일 때만) */}
|
|
|
|
|
{approvalType !== "consensus" && approvalMode === "sequential" && approvers.length > 1 && (
|
|
|
|
|
<p className="text-muted-foreground text-center text-[10px]">
|
|
|
|
|
{approvers.map((a) => a.user_name).join(" → ")}
|
2026-03-04 11:19:57 +09:00
|
|
|
</p>
|
2026-03-05 23:06:36 +09:00
|
|
|
)}
|
2026-03-03 22:00:52 +09:00
|
|
|
</div>
|
2026-03-04 11:19:57 +09:00
|
|
|
)}
|
|
|
|
|
</div>
|
2026-03-05 23:06:36 +09:00
|
|
|
</>
|
|
|
|
|
)}
|
2026-03-03 22:00:52 +09:00
|
|
|
|
|
|
|
|
{/* 에러 메시지 */}
|
|
|
|
|
{error && (
|
2026-03-04 11:19:57 +09:00
|
|
|
<div className="bg-destructive/10 rounded-md p-2">
|
|
|
|
|
<p className="text-destructive text-xs">{error}</p>
|
|
|
|
|
</div>
|
2026-03-03 22:00:52 +09:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<DialogFooter className="gap-2 sm:gap-0">
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={() => onOpenChange(false)}
|
|
|
|
|
disabled={isSubmitting}
|
|
|
|
|
className="h-8 flex-1 text-xs sm:h-10 sm:flex-none sm:text-sm"
|
|
|
|
|
>
|
|
|
|
|
취소
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={handleSubmit}
|
2026-03-05 23:06:36 +09:00
|
|
|
disabled={isSubmitting || (approvalType !== "self" && approvers.length === 0)}
|
2026-03-03 22:00:52 +09:00
|
|
|
className="h-8 flex-1 text-xs sm:h-10 sm:flex-none sm:text-sm"
|
|
|
|
|
>
|
|
|
|
|
{isSubmitting ? (
|
|
|
|
|
<>
|
|
|
|
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
|
|
|
|
요청 중...
|
|
|
|
|
</>
|
2026-03-05 23:06:36 +09:00
|
|
|
) : approvalType === "self" ? (
|
|
|
|
|
"전결 처리"
|
2026-03-03 22:00:52 +09:00
|
|
|
) : (
|
2026-03-04 11:19:57 +09:00
|
|
|
`결재 상신 (${approvers.length}명)`
|
2026-03-03 22:00:52 +09:00
|
|
|
)}
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ApprovalRequestModal;
|