summaryrefslogtreecommitdiff
path: root/lib/vendors/table/request-pq-dialog.tsx
blob: 206846dfcd8cbc35285206e8e5b0d519a296153e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
"use client"

import * as React from "react"
import { type Row } from "@tanstack/react-table"
import { Loader, SendHorizonal, Search, X, Plus, Router } from "lucide-react"
import { toast } from "sonner"
import { useMediaQuery } from "@/hooks/use-media-query"
import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
import {
  Drawer,
  DrawerClose,
  DrawerContent,
  DrawerDescription,
  DrawerFooter,
  DrawerHeader,
  DrawerTitle,
  DrawerTrigger,
} from "@/components/ui/drawer"
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"
import { Checkbox } from "@/components/ui/checkbox"
import { Label } from "@/components/ui/label"
import { Input } from "@/components/ui/input"
import { Badge } from "@/components/ui/badge"
import { Progress } from "@/components/ui/progress"
import { Vendor } from "@/db/schema/vendors"
import { requestBasicContractInfo, requestPQVendors, sendBasicContractEmail } from "../service"
import { getProjectsWithPQList } from "@/lib/pq/service"
import type { Project } from "@/lib/pq/service"
import { useSession } from "next-auth/react"
import { DatePicker } from "@/components/ui/date-picker"
import { getALLBasicContractTemplates } from "@/lib/basic-contract/service"
import type { BasicContractTemplate } from "@/db/schema"
import { searchItemsForPQ } from "@/lib/items/service"
import { saveNdaAttachments } from "../service"
import { useRouter } from "next/navigation"
import { createGtcVendorDocuments, createProjectGtcVendorDocuments, getStandardGtcDocumentId, getProjectGtcDocumentId } from "@/lib/gtc-contract/service"
// import { PQContractViewer } from "../pq-contract-viewer" // 더 이상 사용하지 않음

interface RequestPQDialogProps extends React.ComponentPropsWithoutRef<typeof Dialog> {
  vendors: Row<Vendor>["original"][]
  showTrigger?: boolean
  onSuccess?: () => void
}

// const AGREEMENT_LIST = [
//   "준법서약",
//   "표준하도급계약",
//   "안전보건관리계약",
//   "윤리규범 준수 서약",
//   "동반성장협약",
//   "내국신용장 미개설 합의",
//   "기술자료 제출 기본 동의",
//   "GTC 합의",
// ]

// PQ 대상 품목 타입 정의
interface PQItem {
  itemCode: string
  itemName: string
}

export function RequestPQDialog({ vendors, showTrigger = true, onSuccess, ...props }: RequestPQDialogProps) {
  const [isApprovePending, startApproveTransition] = React.useTransition()
  const isDesktop = useMediaQuery("(min-width: 640px)")
  const { data: session } = useSession()
  const router = useRouter()
  const [type, setType] = React.useState<"GENERAL" | "PROJECT" | "NON_INSPECTION" | null>(null)
  const [dueDate, setDueDate] = React.useState<string | null>(null)
  const [projects, setProjects] = React.useState<Project[]>([])
  const [selectedProjectId, setSelectedProjectId] = React.useState<number | null>(null)
  const [agreements, setAgreements] = React.useState<Record<string, boolean>>({})
  const [extraNote, setExtraNote] = React.useState<string>("")
  const [pqItems, setPqItems] = React.useState<PQItem[]>([])
  
  // 아이템 검색 관련 상태
  const [itemSearchQuery, setItemSearchQuery] = React.useState<string>("")
  const [filteredItems, setFilteredItems] = React.useState<PQItem[]>([])
  const [showItemDropdown, setShowItemDropdown] = React.useState<boolean>(false)
  const [isLoadingProjects, setIsLoadingProjects] = React.useState(false)
  const [basicContractTemplates, setBasicContractTemplates] = React.useState<BasicContractTemplate[]>([])
  const [selectedTemplateIds, setSelectedTemplateIds] = React.useState<number[]>([])
  const [isLoadingTemplates, setIsLoadingTemplates] = React.useState(false)
  
  // 비밀유지 계약서 첨부파일 관련 상태
  const [ndaAttachments, setNdaAttachments] = React.useState<File[]>([])
  const [isUploadingNdaFiles, setIsUploadingNdaFiles] = React.useState(false)
  
  // 프로그레스 관련 상태
  const [progressValue, setProgressValue] = React.useState(0)
  const [currentStep, setCurrentStep] = React.useState("")
  const [showProgress, setShowProgress] = React.useState(false)

  // 아이템 검색 필터링
  React.useEffect(() => {
    if (itemSearchQuery.trim() === "") {
      setFilteredItems([])
      setShowItemDropdown(false)
      return
    }

    const searchItems = async () => {
      try {
        const results = await searchItemsForPQ(itemSearchQuery)
        setFilteredItems(results)
        setShowItemDropdown(true)
      } catch (error) {
        console.error("아이템 검색 오류:", error)
        toast.error("아이템 검색 중 오류가 발생했습니다.")
        setFilteredItems([])
        setShowItemDropdown(false)
      }
    }

    // 디바운싱: 300ms 후에 검색 실행
    const timeoutId = setTimeout(searchItems, 300)
    return () => clearTimeout(timeoutId)
  }, [itemSearchQuery])

  React.useEffect(() => {
    if (type === "PROJECT") {
      setIsLoadingProjects(true)
      getProjectsWithPQList().then(setProjects).catch(() => toast.error("프로젝트 로딩 실패"))
        .finally(() => setIsLoadingProjects(false))
    }
  }, [type])

  // 기본계약서 템플릿 로딩 및 자동 선택
  React.useEffect(() => {
    setIsLoadingTemplates(true)
    getALLBasicContractTemplates()
      .then((templates) => {
        setBasicContractTemplates(templates)

        // 벤더 국가별 자동 선택 로직
        if (vendors.length > 0) {
          const isAllForeign = vendors.every(vendor => vendor.country !== 'KR')
          const isAllDomestic = vendors.every(vendor => vendor.country === 'KR')

          if (isAllForeign) {
            // 외자: 준법서약 (영문), GTC 선택 (GTC는 1개만 선택하도록)
            // 비밀유지 계약서, 기술자료 요구서 제외
            const foreignTemplates = templates.filter(template => {
              const name = template.templateName?.toLowerCase() || ''
              return (
                (template.templateName?.includes('준법서약') && template.templateName?.includes('영문')) ||
                template.templateName?.includes('gtc')
              ) && !name.includes('비밀유지') && !name.includes('기술자료')
            })
            // GTC 템플릿 중 최신 리비전의 것만 선택
            const gtcTemplates = foreignTemplates.filter(t => t.templateName?.includes('gtc'))
            const nonGtcTemplates = foreignTemplates.filter(t => !t.templateName?.includes('gtc'))

            if (gtcTemplates.length > 0) {
              // GTC 템플릿 중 이름이 가장 긴 것 (프로젝트 GTC 우선) 선택
              const selectedGtcTemplate = gtcTemplates.reduce((prev, current) =>
                (prev.templateName?.length || 0) > (current.templateName?.length || 0) ? prev : current
              )
              setSelectedTemplateIds([...nonGtcTemplates.map(t => t.id), selectedGtcTemplate.id])
            } else {
              setSelectedTemplateIds(nonGtcTemplates.map(t => t.id))
            }
          } else if (isAllDomestic) {
            // 내자: 준법서약 (영문), GTC 제외한 모든 템플릿 선택
            // 비밀유지 계약서, 기술자료 요구서 제외
            const domesticTemplates = templates.filter(template => {
              const name = template.templateName?.toLowerCase() || ''
              return !(name.includes('준법서약') && name.includes('영문')) && 
                     !name.includes('gtc') && 
                     !name.includes('비밀유지') && 
                     !name.includes('기술자료')
            })
            setSelectedTemplateIds(domesticTemplates.map(t => t.id))
          }
        }
      })
      .catch(() => toast.error("기본계약서 템플릿 로딩 실패"))
      .finally(() => setIsLoadingTemplates(false))
  }, [vendors])

  React.useEffect(() => {
    if (!props.open) {
      setType(null)
      setSelectedProjectId(null)
      setAgreements({})
      setDueDate(null)
      setPqItems([])
      setExtraNote("")
      setSelectedTemplateIds([])
      setItemSearchQuery("")
      setFilteredItems([])
      setShowItemDropdown(false)
      setNdaAttachments([])
      setIsUploadingNdaFiles(false)
      setProgressValue(0)
      setCurrentStep("")
      setShowProgress(false)
    }
  }, [props.open])

  // 아이템 선택 함수
  const handleSelectItem = (item: PQItem) => {
    // 이미 선택된 아이템인지 확인
    const isAlreadySelected = pqItems.some(selectedItem => 
      selectedItem.itemCode === item.itemCode
    )
    
    if (!isAlreadySelected) {
      setPqItems(prev => [...prev, item])
    }
    
    // 검색 초기화
    setItemSearchQuery("")
    setFilteredItems([])
    setShowItemDropdown(false)
  }

  // 아이템 제거 함수
  const handleRemoveItem = (itemCode: string) => {
    setPqItems(prev => prev.filter(item => item.itemCode !== itemCode))
  }

  // 비밀유지 계약서 첨부파일 추가 함수
  const handleAddNdaAttachment = (event: React.ChangeEvent<HTMLInputElement>) => {
    const files = event.target.files
    if (files) {
      const newFiles = Array.from(files)
      setNdaAttachments(prev => [...prev, ...newFiles])
    }
    // input 초기화
    event.target.value = ''
  }

  // 비밀유지 계약서 첨부파일 제거 함수
  const handleRemoveNdaAttachment = (fileIndex: number) => {
    setNdaAttachments(prev => prev.filter((_, index) => index !== fileIndex))
  }

  // 비밀유지 계약서가 선택되었는지 확인하는 함수
  const isNdaTemplateSelected = () => {
    return basicContractTemplates.some(template => 
      selectedTemplateIds.includes(template.id) && 
      template.templateName?.includes("비밀유지")
    )
  }

  const onApprove = () => {
    if (!type) return toast.error("PQ 유형을 선택하세요.")
    if (type === "PROJECT" && !selectedProjectId) return toast.error("프로젝트를 선택하세요.")
    if (!dueDate) return toast.error("마감일을 선택하세요.")
    if (!session?.user?.id) return toast.error("인증 실패")

    // GTC 템플릿 선택 검증
    const selectedGtcTemplates = basicContractTemplates.filter(template =>
      selectedTemplateIds.includes(template.id) &&
      template.templateName?.toLowerCase().includes('gtc')
    )

    if (selectedGtcTemplates.length > 1) {
      return toast.error("GTC 템플릿은 하나만 선택할 수 있습니다.")
    }

    // 프로그레스 바를 즉시 표시
    setShowProgress(true)
    setProgressValue(0)
    setCurrentStep("시작 중...")

    startApproveTransition(async () => {
      try {
        
        // 전체 단계 수 계산
        const gtcTemplates = basicContractTemplates.filter(template =>
          selectedTemplateIds.includes(template.id) &&
          template.templateName?.toLowerCase().includes('gtc')
        )

        const totalSteps = 1 +
          (selectedTemplateIds.length > 0 ? 1 : 0) +
          (isNdaTemplateSelected() && ndaAttachments.length > 0 ? 1 : 0) +
          (gtcTemplates.length > 0 ? 1 : 0)
        let completedSteps = 0
        
        // 1단계: PQ 생성
        setCurrentStep("PQ 생성 중...")
        console.log("🚀 PQ 생성 시작")
        const { error: pqError } = await requestPQVendors({
          ids: vendors.map((v) => v.id),
          userId: Number(session.user.id),
          agreements,
          dueDate,
          projectId: type === "PROJECT" ? selectedProjectId : null,
          type: type || "GENERAL",
          extraNote,
          pqItems: JSON.stringify(pqItems),
          templateId: selectedTemplateIds.length > 0 ? selectedTemplateIds[0] : null,
        })

        if (pqError) {
          setShowProgress(false)
          toast.error(`PQ 생성 실패: ${pqError}`)
          return
        }
        
        completedSteps++
        setProgressValue((completedSteps / totalSteps) * 100)
        console.log("✅ PQ 생성 완료")
        toast.success("PQ가 성공적으로 요청되었습니다")

        // 2단계: 기본계약서 템플릿이 선택된 경우 백그라운드에서 처리
        if (selectedTemplateIds.length > 0) {
          const templates = basicContractTemplates.filter(t => 
            selectedTemplateIds.includes(t.id)
          )
          
          setCurrentStep(`기본계약서 생성 중... (${templates.length}개 템플릿)`)
          console.log("📋 기본계약서 백그라운드 처리 시작", templates.length, "개 템플릿")
          await processBasicContractsInBackground(templates, vendors)
          
          completedSteps++
          setProgressValue((completedSteps / totalSteps) * 100)
        }

        // 3단계: 비밀유지 계약서 첨부파일이 있는 경우 저장
        if (isNdaTemplateSelected() && ndaAttachments.length > 0) {
          setCurrentStep(`비밀유지 계약서 첨부파일 저장 중... (${ndaAttachments.length}개 파일)`)
          console.log("📎 비밀유지 계약서 첨부파일 처리 시작", ndaAttachments.length, "개 파일")
          
          const ndaResult = await saveNdaAttachments({
            vendorIds: vendors.map((v) => v.id),
            files: ndaAttachments,
            userId: session.user.id.toString()
          })

          if (ndaResult.success) {
            toast.success(`비밀유지 계약서 첨부파일이 모두 저장되었습니다 (${ndaResult.summary?.success}/${ndaResult.summary?.total})`)
          } else {
            toast.error(`첨부파일 처리 중 일부 오류가 발생했습니다: ${ndaResult.error}`)
          }
          
          completedSteps++
          setProgressValue((completedSteps / totalSteps) * 100)
        }
        //4단계: GTC 템플릿 처리
        if (selectedGtcTemplates.length > 0) {
          setCurrentStep(`GTC 문서 생성 중... (${selectedGtcTemplates.length}개 템플릿)`)
          console.log("📋 GTC 문서 생성 시작", selectedGtcTemplates.length, "개 템플릿")

          try {
            await processGtcTemplates(selectedGtcTemplates, vendors)
            completedSteps++
            setProgressValue((completedSteps / totalSteps) * 100)
          } catch (error) {
            console.error("GTC 템플릿 처리 중 오류:", error)
            toast.error(`GTC 템플릿 처리 중 오류가 발생했습니다`)
            // GTC 처리 실패해도 PQ 생성은 성공으로 간주
            completedSteps++
            setProgressValue((completedSteps / totalSteps) * 100)
          }
        }
        //5단계: 각 협력업체들에게 기본계약서 이메일 발송
        if (selectedTemplateIds.length > 0) {
          setCurrentStep(`기본계약서 이메일 발송 중... (${selectedTemplateIds.length}개 템플릿)`)
          console.log("📋 기본계약서 이메일 발송 시작", selectedTemplateIds.length, "개 템플릿")
          await processBasicContractsEmail(selectedTemplateIds, vendors)
          completedSteps++
          setProgressValue((completedSteps / totalSteps) * 100)
        }

        setCurrentStep("완료!")
        setProgressValue(100)
        
        // 잠시 완료 상태를 보여준 후 다이얼로그 닫기
        setTimeout(() => {
          setShowProgress(false)
          props.onOpenChange?.(false)
          onSuccess?.()
        }, 1000)

      } catch (error) {
        console.error('PQ 생성 오류:', error)
        setShowProgress(false)
        toast.error(`처리 중 오류가 발생했습니다: ${error instanceof Error ? error.message : '알 수 없는 오류'}`)
      }
    })
  }

  // 백그라운드에서 기본계약서 처리
  const processBasicContractsInBackground = async (templates: BasicContractTemplate[], vendors: any[]) => {
    if (!session?.user?.id) {
      toast.error("인증 정보가 없습니다")
      return
    }

    try {
      const totalContracts = templates.length * vendors.length
      let processedCount = 0

      // 각 벤더별로, 각 템플릿을 처리
      for (let vendorIndex = 0; vendorIndex < vendors.length; vendorIndex++) {
        const vendor = vendors[vendorIndex]
        
        // 벤더별 템플릿 데이터 생성 (한글 변수명 사용)
        const templateData = {
          company_name: vendor.vendorName || '협력업체명',
          company_address: vendor.address || '주소',
          representative_name: vendor.representativeName || '대표자명',
          signature_date: new Date().toLocaleDateString('ko-KR'),
          tax_id: vendor.taxId || '사업자번호',
          phone_number: vendor.phone || '전화번호',
        }

        console.log(`🔄 벤더 ${vendorIndex + 1}/${vendors.length} 템플릿 데이터:`, templateData)

        // 해당 벤더에 대해 각 템플릿을 순차적으로 처리
        for (let templateIndex = 0; templateIndex < templates.length; templateIndex++) {
          const template = templates[templateIndex]
          processedCount++
          
          // 진행률 업데이트 (2단계 범위 내에서)
          const baseProgress = 33.33 // 1단계 완료 후
          const contractProgress = (processedCount / totalContracts) * 33.33 // 2단계는 33.33% 차지
          const newProgress = baseProgress + contractProgress
          setProgressValue(newProgress)
          setCurrentStep(`기본계약서 생성 중... (${processedCount}/${totalContracts})`)
          
          console.log(`📄 처리 중: ${vendor.vendorName} - ${template.templateName} (${processedCount}/${totalContracts})`)
          
          // 개별 벤더에 대한 기본계약 생성
          await processTemplate(template, templateData, [vendor])
          
          console.log(`✅ 완료: ${vendor.vendorName} - ${template.templateName}`)
        }
      }

      toast.success(`총 ${totalContracts}개 기본계약이 모두 생성되었습니다`)
      router.refresh();

    } catch (error) {
      console.error('기본계약 처리 중 오류:', error)
      toast.error(`기본계약 처리 중 오류가 발생했습니다: ${error instanceof Error ? error.message : '알 수 없는 오류'}`)
    }
  }

  const processTemplate = async (template: BasicContractTemplate, templateData: any, vendors: any[]) => {
    try {
      // 1. 템플릿 파일 가져오기
      const templateResponse = await fetch('/api/basic-contract/get-template', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ templateId: template.id })
      })

      if (!templateResponse.ok) {
        throw new Error(`템플릿 파일을 가져올 수 없습니다: ${template.templateName}`)
      }

      const templateBlob = await templateResponse.blob()

      // 2. PDFTron을 사용해서 변수 치환 및 PDF 변환
      // @ts-ignore
      const WebViewer = await import("@pdftron/webviewer").then(({ default: WebViewer }) => WebViewer)
      
      // 임시 WebViewer 인스턴스 생성 (DOM에 추가하지 않음)
      const tempDiv = document.createElement('div')
      tempDiv.style.display = 'none'
      document.body.appendChild(tempDiv)

      const instance = await WebViewer(
        {
          path: "/pdftronWeb",
          licenseKey: process.env.NEXT_PUBLIC_PDFTRON_WEBVIEW_KEY,
          fullAPI: true,
        },
        tempDiv
      )

      try {
        const { Core } = instance
        const { createDocument } = Core

        // 3. 템플릿 문서 생성 및 변수 치환
        const templateDoc = await createDocument(templateBlob, {
          filename: template.fileName || 'template.docx',
          extension: 'docx',
        })

        console.log("🔄 변수 치환 시작:", templateData)
        await templateDoc.applyTemplateValues(templateData)
        console.log("✅ 변수 치환 완료")

        // 4. PDF 변환
        const fileData = await templateDoc.getFileData()
        const pdfBuffer = await Core.officeToPDFBuffer(fileData, { extension: 'docx' })

        console.log(`✅ PDF 변환 완료: ${template.templateName}`, `크기: ${pdfBuffer.byteLength} bytes`)

        // 5. 기본계약 생성 요청
        const { error: contractError } = await requestBasicContractInfo({
          vendorIds: vendors.map((v) => v.id),
          requestedBy: Number(session!.user.id),
          templateId: template.id,
          pdfBuffer: new Uint8Array(pdfBuffer),
        })

        if (contractError) {
          throw new Error(contractError)
        }

        console.log(`✅ 기본계약 생성 완료: ${template.templateName}`)
        

      } finally {
        // 임시 WebViewer 정리
        instance.UI.dispose()
        document.body.removeChild(tempDiv)
      }

    } catch (error) {
      console.error(`❌ 템플릿 처리 실패: ${template.templateName}`, error)
      throw error
    }
  }

  // GTC 템플릿 처리 함수
  const processGtcTemplates = async (gtcTemplates: BasicContractTemplate[], vendors: any[]) => {
    if (!session?.user?.id) {
      toast.error("인증 정보가 없습니다")
      return
    }

    try {
      const vendorIds = vendors.map(v => v.id)
      const userId = Number(session.user.id)

      for (const template of gtcTemplates) {
        const templateName = template.templateName?.toLowerCase() || ''

        if (templateName.includes('general gtc') || (templateName.includes('gtc') && !templateName.includes(' '))) {
          // General GTC 처리
          console.log(`📄 General GTC 템플릿 처리: ${template.templateName}`)

          const gtcDocument = await getStandardGtcDocumentId()
          if (!gtcDocument) {
            toast.error(`표준 GTC 문서를 찾을 수 없습니다.`)
            continue
          }

          const result = await createGtcVendorDocuments({
            baseDocumentId: gtcDocument.id,
            vendorIds,
            createdById: userId,
            documentTitle: gtcDocument.title
          })

          if (result.success) {
            console.log(`✅ General GTC 문서 생성 완료: ${result.count}개`)
          } else {
            toast.error(`General GTC 문서 생성 실패: ${result.error}`)
          }

        } else if (templateName.includes('gtc') && templateName.includes(' ')) {
          // 프로젝트 GTC 처리 (프로젝트 코드 추출)
          const projectCodeMatch = template.templateName?.match(/^([A-Z0-9]+)\s+GTC/)
          console.log("🔄 프로젝트 GTC 템플릿 처리: ", template.templateName, projectCodeMatch)
          console.log(`   - 템플릿 이름 분석: "${template.templateName}"`)
          console.log(`   - 소문자 변환: "${templateName}"`)
          if (projectCodeMatch) {
            const projectCode = projectCodeMatch[1]
            console.log(`📄 프로젝트 GTC 템플릿 처리: ${template.templateName} (프로젝트: ${projectCode})`)

            // const gtcDocument = await getProjectGtcDocumentId(projectCode)
            // if (!gtcDocument) {
            //   toast.error(`프로젝트 "${projectCode}"의 GTC 문서를 찾을 수 없습니다.`)
            //   continue
            // }
            // console.log("🔄 getProjectGtcDocumentId", gtcDocument)

            const result = await createProjectGtcVendorDocuments({
              projectCode,
              vendorIds,
              createdById: userId,
              documentTitle: template.templateName
            })

            if (result.success) {
              console.log(`✅ 프로젝트 GTC 문서 생성 완료: ${result.count}개`)
            } else {
              toast.error(`프로젝트 GTC 문서 생성 실패: ${result.error}`)
            }
          } else {
            toast.error(`프로젝트 GTC 템플릿 이름 형식이 올바르지 않습니다: ${template.templateName}`)
          }
        } else {
          console.log(`⚠️ GTC 템플릿이지만 처리할 수 없는 형식: ${template.templateName}`)
        }
      }

      toast.success(`GTC 문서가 ${gtcTemplates.length}개 템플릿에 대해 생성되었습니다`)

    } catch (error) {
      console.error('GTC 템플릿 처리 중 오류:', error)
      toast.error(`GTC 템플릿 처리 중 오류가 발생했습니다: ${error instanceof Error ? error.message : '알 수 없는 오류'}`)
    }
  }

  const processBasicContractsEmail = async (templateIds: number[], vendors: any[]) => {
    if (!session?.user?.id) {
      toast.error("인증 정보가 없습니다")
      return
    }
    try {
      const vendorIds = vendors.map(v => v.id)
      const userId = Number(session.user.id)

    // 2. 성공한 템플릿이 있으면 이메일 발송
    if (templateIds.length > 0) {
      const emailResult = await sendBasicContractEmail({
        vendorIds,
        templateIds,
        requestedBy: userId
      })

      if (emailResult.success) {
        toast.success(`${templateIds.length}개 템플릿에 대한 기본계약서가 생성되었고, ${vendorIds.length}개 협력업체에 이메일이 발송되었습니다`)
      } else {
        toast.warning(`계약서는 생성되었으나 일부 이메일 발송 실패: ${emailResult.error}`)
      }
    } else {
      toast.error("기본계약서 생성에 실패했습니다")
    }

  } catch (error) {
    console.error('기본계약서 이메일 발송 중 오류:', error)
    toast.error(`기본계약서 이메일 발송 중 오류가 발생했습니다: ${error instanceof Error ? error.message : '알 수 없는 오류'}`)
  }
}
    

    

  const dialogContent = (
    <div className="space-y-4 py-2">
      {/* 선택된 협력업체 정보 */}
      <div className="space-y-2">
        <Label>선택된 협력업체 ({vendors.length}개)</Label>
        <div className="max-h-40 overflow-y-auto border rounded-md p-3 space-y-2">
          {vendors.map((vendor) => (
            <div key={vendor.id} className="flex items-center justify-between text-sm">
              <div className="flex-1">
                <div className="font-medium">{vendor.vendorName}</div>
                <div className="text-muted-foreground">
                  {vendor.vendorCode} • {vendor.email || "이메일 없음"}
                </div>
              </div>
            </div>
          ))}
        </div>
      </div>

      <div className="space-y-2">
        <Label htmlFor="type">PQ 종류 선택</Label>
        <Select onValueChange={(val: "GENERAL" | "PROJECT" | "NON_INSPECTION") => setType(val)} value={type ?? undefined}>
          <SelectTrigger id="type"><SelectValue placeholder="PQ 종류를 선택하세요" /></SelectTrigger>
          <SelectContent>
            <SelectItem value="GENERAL">일반 PQ</SelectItem>
            <SelectItem value="PROJECT">프로젝트 PQ</SelectItem>
            <SelectItem value="NON_INSPECTION">미실사 PQ</SelectItem>
          </SelectContent>
        </Select>
      </div>

      {type === "PROJECT" && (
        <div className="space-y-2">
          <Label htmlFor="project">프로젝트 선택</Label>
          <Select onValueChange={(val) => setSelectedProjectId(Number(val))}>
            <SelectTrigger id="project">
              <SelectValue placeholder="프로젝트 선택" />
            </SelectTrigger>
            <SelectContent>
              {isLoadingProjects ? (
                <SelectItem value="loading" disabled>로딩 중...</SelectItem>
              ) : projects.map((p) => (
                <SelectItem key={p.id} value={p.id.toString()}>{p.projectCode} - {p.projectName}</SelectItem>
              ))}
            </SelectContent>
          </Select>
        </div>
      )}

      {/* 마감일 입력 */}
      <div className="space-y-2">
        <Label htmlFor="dueDate">PQ 제출 마감일</Label>
        <DatePicker
          date={dueDate ? new Date(dueDate) : undefined}
          onSelect={(date?: Date) => {
            if (date) {
              // 한국 시간대로 날짜 변환 (UTC 변환으로 인한 날짜 변경 방지)
              const kstDate = new Date(date.getTime() - date.getTimezoneOffset() * 60000)
              setDueDate(kstDate.toISOString().slice(0, 10))
            } else {
              setDueDate("")
            }
          }}
          placeholder="마감일 선택"
        />
      </div>

      {/* PQ 대상품목 */}
      <div className="space-y-2">
        <Label>PQ 대상품목</Label>
        
        {/* 선택된 아이템들 표시 */}
        {pqItems.length > 0 && (
          <div className="flex flex-wrap gap-2 mb-2">
            {pqItems.map((item) => (
              <Badge key={item.itemCode} variant="secondary" className="flex items-center gap-1">
                <span className="text-xs">
                  {item.itemCode} - {item.itemName}
                </span>
                <Button
                  type="button"
                  variant="ghost"
                  size="sm"
                  className="h-4 w-4 p-0 hover:bg-destructive hover:text-destructive-foreground"
                  onClick={() => handleRemoveItem(item.itemCode)}
                >
                  <X className="h-3 w-3" />
                </Button>
              </Badge>
            ))}
          </div>
        )}
        
        {/* 검색 입력 */}
        <div className="relative">
          <div className="relative">
            <Input
              placeholder="아이템 코드 또는 이름으로 검색하세요"
              value={itemSearchQuery}
              onChange={(e) => setItemSearchQuery(e.target.value)}
              className="pl-9"
            />
          </div>
          
          {/* 검색 결과 드롭다운 */}
          {showItemDropdown && (
            <div className="absolute top-full left-0 right-0 z-50 mt-1 max-h-48 overflow-y-auto bg-background border rounded-md shadow-lg">
              {filteredItems.length > 0 ? (
                filteredItems.map((item) => (
                  <button
                    key={item.itemCode}
                    type="button"
                    className="w-full px-3 py-2 text-left text-sm hover:bg-muted focus:bg-muted focus:outline-none"
                    onClick={() => handleSelectItem(item)}
                  >
                    <div className="font-medium">{item.itemCode}</div>
                    <div className="text-muted-foreground text-xs">{item.itemName}</div>
                  </button>
                ))
              ) : (
                <div className="px-3 py-2 text-sm text-muted-foreground">
                  검색 결과가 없습니다.
                </div>
              )}
            </div>
          )}
        </div>
        
        <div className="text-xs text-muted-foreground">
          아이템 코드나 이름을 입력하여 검색하고 선택하세요. (선택사항)
        </div>
      </div>

      {/* 추가 안내사항 */}
      <div className="space-y-2">
        <Label htmlFor="extraNote">추가 안내사항</Label>
        <textarea
          id="extraNote"
          value={extraNote}
          onChange={(e) => setExtraNote(e.target.value)}
          placeholder="추가 안내사항을 입력하세요 (선택사항)"
          className="w-full rounded-md border px-3 py-2 text-sm min-h-20 resize-none"
        />
      </div>

      {/* 기본계약서 템플릿 선택 (다중 선택) */}
      <div className="space-y-2">
        <Label>기본계약서 템플릿 (선택사항, 복수 선택 가능)</Label>
        {isLoadingTemplates ? (
          <div className="text-sm text-muted-foreground">템플릿 로딩 중...</div>
        ) : (
          <div className="space-y-2 max-h-40 overflow-y-auto border rounded-md p-3">
            {basicContractTemplates.map((template) => (
              <div key={template.id} className="flex items-center gap-2">
                <Checkbox
                  id={`template-${template.id}`}
                  checked={selectedTemplateIds.includes(template.id)}
                  onCheckedChange={(checked) => {
                    if (checked) {
                      setSelectedTemplateIds(prev => [...prev, template.id])
                    } else {
                      setSelectedTemplateIds(prev => prev.filter(id => id !== template.id))
                    }
                  }}
                />
                <Label htmlFor={`template-${template.id}`} className="text-sm">
                  {template.templateName}
                </Label>
              </div>
            ))}
            {basicContractTemplates.length === 0 && (
              <div className="text-sm text-muted-foreground">사용 가능한 템플릿이 없습니다.</div>
            )}
          </div>
        )}
        {selectedTemplateIds.length > 0 && (
          <div className="text-xs text-muted-foreground">
            {selectedTemplateIds.length}개 템플릿이 선택되었습니다.
            {vendors.length > 0 && vendors.every(v => v.country !== 'KR') && 
              " (외자 벤더 - 자동 선택됨)"}
            {vendors.length > 0 && vendors.every(v => v.country === 'KR') && 
              " (내자 벤더 - 자동 선택됨)"}
          </div>
        )}
      </div>

      {/* 비밀유지 계약서 첨부파일 */}
      {isNdaTemplateSelected() && (
        <div className="space-y-2">
          <Label>비밀유지 계약서 첨부파일</Label>
          
          {/* 선택된 파일들 표시 */}
          {ndaAttachments.length > 0 && (
            <div className="space-y-2">
              <div className="text-sm text-muted-foreground">
                선택된 파일 ({ndaAttachments.length}개)
              </div>
              <div className="space-y-1 max-h-32 overflow-y-auto border rounded-md p-2">
                {ndaAttachments.map((file, index) => (
                  <div key={index} className="flex items-center justify-between text-sm bg-muted/50 rounded px-2 py-1">
                    <div className="flex-1 truncate">
                      <span className="font-medium">{file.name}</span>
                      <span className="text-muted-foreground ml-2">
                        ({(file.size / 1024 / 1024).toFixed(2)} MB)
                      </span>
                    </div>
                    <Button
                      type="button"
                      variant="ghost"
                      size="sm"
                      className="h-6 w-6 p-0 hover:bg-destructive hover:text-destructive-foreground"
                      onClick={() => handleRemoveNdaAttachment(index)}
                    >
                      <X className="h-3 w-3" />
                    </Button>
                  </div>
                ))}
              </div>
            </div>
          )}
          
          {/* 파일 선택 버튼 */}
          <div className="flex items-center gap-2">
            <input
              type="file"
              multiple
              accept=".pdf,.doc,.docx,.xlsx,.xls,.png,.jpg,.jpeg"
              onChange={handleAddNdaAttachment}
              className="hidden"
              id="nda-file-input"
            />
            <Button
              type="button"
              variant="outline"
              size="sm"
              className="gap-2"
              onClick={() => document.getElementById('nda-file-input')?.click()}
              disabled={isUploadingNdaFiles}
            >
              <Plus className="h-4 w-4" />
              파일 추가
            </Button>
            {isUploadingNdaFiles && (
              <div className="text-sm text-muted-foreground">
                파일 업로드 중...
              </div>
            )}
          </div>
          
          <div className="text-xs text-muted-foreground">
            비밀유지 계약서와 관련된 첨부파일을 업로드하세요. 
            각 벤더별로 동일한 파일이 저장됩니다.
          </div>
        </div>
      )}

      {/* <div className="space-y-2">
        <Label>계약 항목 선택</Label>
        {AGREEMENT_LIST.map((label) => (
          <div key={label} className="flex items-center gap-2">
            <Checkbox
              id={label}
              checked={agreements[label] || false}
              onCheckedChange={(val) =>
                setAgreements((prev) => ({ ...prev, [label]: Boolean(val) }))
              }
            />
            <Label htmlFor={label}>{label}</Label>
          </div>
        ))}
      </div> */}
    </div>
  )

  if (isDesktop) {
    return (
      <Dialog {...props}>
        {showTrigger && (
          <DialogTrigger asChild>
            <Button variant="outline" size="sm" className="gap-2">
              <SendHorizonal className="size-4" /> PQ 요청 ({vendors.length})
            </Button>
          </DialogTrigger>
        )}
        <DialogContent className="max-h-[80vh] flex flex-col">
          <DialogHeader>
            <DialogTitle>PQ 요청</DialogTitle>
            <DialogDescription>
              <span className="font-medium">{vendors.length}</span>
              {vendors.length === 1 ? "개 협력업체" : "개 협력업체들"}에게 PQ를 요청합니다.
            </DialogDescription>
          </DialogHeader>
          <div className="flex-1 overflow-y-auto">
            {dialogContent}
          </div>
          <DialogFooter className="flex-col gap-4">
            {/* 프로그레스 바 */}
            {(showProgress || isApprovePending) && (
              <div className="w-full space-y-2">
                <div className="flex items-center justify-between text-sm">
                  <span className="text-muted-foreground">{currentStep || "처리 중..."}</span>
                  <span className="font-medium">{Math.round(progressValue)}%</span>
                </div>
                <Progress value={progressValue} className="w-full" />
              </div>
            )}
            
            {/* 버튼들 */}
            <div className="flex justify-end gap-2">
              <DialogClose asChild>
                <Button variant="outline" disabled={isApprovePending}>취소</Button>
              </DialogClose>
              <Button 
                onClick={onApprove} 
                disabled={isApprovePending || !type || (type === "PROJECT" && !selectedProjectId)}
              >
                {isApprovePending && <Loader className="mr-2 size-4 animate-spin" />}
                요청하기
              </Button>
            </div>
          </DialogFooter>
        </DialogContent>


      </Dialog>
    )
  }

  return (
    <Drawer {...props}>
      {showTrigger && (
        <DrawerTrigger asChild>
          <Button variant="outline" size="sm" className="gap-2">
            <SendHorizonal className="size-4" /> PQ 요청 ({vendors.length})
          </Button>
        </DrawerTrigger>
      )}
      <DrawerContent className="max-h-[80vh] flex flex-col">
        <DrawerHeader>
          <DrawerTitle>PQ 요청</DrawerTitle>
          <DrawerDescription>
            <span className="font-medium">{vendors.length}</span>
            {vendors.length === 1 ? "개 협력업체" : "개 협력업체들"}에게 PQ를 요청합니다.
          </DrawerDescription>
        </DrawerHeader>
        <div className="flex-1 overflow-y-auto px-4">
          {dialogContent}
        </div>
        <DrawerFooter className="gap-4">
          {/* 프로그레스 바 */}
          {(showProgress || isApprovePending) && (
            <div className="w-full space-y-2">
              <div className="flex items-center justify-between text-sm">
                <span className="text-muted-foreground">{currentStep || "처리 중..."}</span>
                <span className="font-medium">{Math.round(progressValue)}%</span>
              </div>
              <Progress value={progressValue} className="w-full" />
            </div>
          )}
          
          {/* 버튼들 */}
          <div className="flex gap-2">
            <DrawerClose asChild>
              <Button variant="outline" disabled={isApprovePending} className="flex-1">취소</Button>
            </DrawerClose>
            <Button 
              onClick={onApprove} 
              disabled={isApprovePending || !type || (type === "PROJECT" && !selectedProjectId)}
              className="flex-1"
            >
              {isApprovePending && <Loader className="mr-2 size-4 animate-spin" />}
              요청하기
            </Button>
          </div>
        </DrawerFooter>
      </DrawerContent>


    </Drawer>
  )
}