'use client' import { useState } from 'react' import { Download } from 'lucide-react' import { KriDialog } from '@/components/common/KriDialog' import { Button } from '@/components/ui/button' import { Checkbox } from '@/components/ui/checkbox' import { Label } from '@/components/ui/label' import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group' import { useDialog } from '@/hooks/useDialog' import type { DialogId } from '@/store/jotai/dialog' import ApplyFilterOption, { validateApplyFilterTarget } from './ListDialogOption/ApplyFilterOption' import KriDateRangePicker from '@/components/common/KriDateRangePicker' import { type ContactHistoryDownloadType, ListEventHistoryOrder, type ListDownloadMode, type ListDownloadPayload, type ListFilterTarget, } from '@/types/list' export type ListDownloadDialogProps = { dialogId?: DialogId open?: boolean onOpenChange?: (open: boolean) => void mode: ListDownloadMode selectedSids: string[] filterSummary?: string[] onDownload: (payload: ListDownloadPayload) => void | Promise } const titleMap: Record = { list: '리스트 다운로드', 'contact-history': '컨택 이력 다운로드', 'list-change-history': '리스트 수정 이력 다운로드', 'upload-file': '업로드 파일 다운로드', } const ListDownloadDialog = ({ dialogId = 'listDownloadDialog', open, onOpenChange, mode, selectedSids, filterSummary, onDownload, }: ListDownloadDialogProps) => { const { isDialogOpen, showDialog, hideDialog } = useDialog() const [target, setTarget] = useState('filtered') const [contactHistoryDateFrom, setContactHistoryDateFrom] = useState('') const [contactHistoryDateTo, setContactHistoryDateTo] = useState('') const [contactHistoryType, setContactHistoryType] = useState('all') const [historyOrder, setHistoryOrder] = useState(ListEventHistoryOrder.DESC) const [includeResult, setIncludeResult] = useState(true) const [submitting, setSubmitting] = useState(false) const resolvedOpen = open ?? isDialogOpen(dialogId) const handleOpenChange = (nextOpen: boolean) => { onOpenChange?.(nextOpen) if (nextOpen) { showDialog(dialogId) return } hideDialog(dialogId) } const handleDownload = async () => { if (!validateApplyFilterTarget(target, { selectedCount: selectedSids.length, filterSummary })) return if (mode === 'contact-history') { const hasFrom = contactHistoryDateFrom.length > 0 const hasTo = contactHistoryDateTo.length > 0 if (hasFrom !== hasTo) { alert('조회 기간은 시작일과 종료일을 모두 입력해주세요.') return } if (hasFrom && hasTo && contactHistoryDateFrom > contactHistoryDateTo) { alert('조회 시작일은 종료일보다 이후일 수 없습니다.') return } } setSubmitting(true) try { await onDownload({ mode, target, onlyFiltered: target === 'filtered', includeResult, contactHistoryDateFrom: mode === 'contact-history' ? contactHistoryDateFrom || undefined : undefined, contactHistoryDateTo: mode === 'contact-history' ? contactHistoryDateTo || undefined : undefined, contactHistoryType: mode === 'contact-history' ? contactHistoryType : undefined, historyOrder: mode === 'contact-history' ? historyOrder : undefined, }) handleOpenChange(false) } finally { setSubmitting(false) } } return ( {titleMap[mode]}} contentClassName="sm:max-w-[560px]" bodyClassName="space-y-4" footer={ <> } > {mode === 'contact-history' ? (

조회 기간

{ setContactHistoryDateFrom(range.from) setContactHistoryDateTo(range.to) }} />

비워두면 전체 기간으로 다운로드합니다.

) : null}

다운로드 범위

{mode === 'list' || mode === 'contact-history' ? (
setIncludeResult(checked === true)} />
) : null} {mode === 'contact-history' ? (

이력 기준

setContactHistoryType(v as ContactHistoryDownloadType)} className="flex items-center gap-6 flex-wrap" >
) : null} {mode === 'contact-history' && contactHistoryType === 'all' ? (

이력 순서

setHistoryOrder(value as ListEventHistoryOrder)} className="flex items-center gap-6 flex-wrap" >
) : null}
) } export default ListDownloadDialog