"use client";

/**
 * 공정검사 관리 — wace_plm 의 processInspectionList.jsp 1:1 이식.
 * 필터 10종 (2행), 그리드 9컬럼 (검사결과 OK/NG 자동산정).
 */
import React, { useCallback, useEffect, useState } from "react";
import { Plus } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { toast } from "sonner";
import { useAuth } from "@/hooks/useAuth";
import { DataGrid, DataGridColumn } from "@/components/common/DataGrid";
import { PageHeader } from "@/components/common/PageHeader";
import { CompactFilterBar, CompactFilterField, CompactDateRange } from "@/components/common/CompactFilterBar";
import { qualityApi, ProcessInspectionRow } from "@/lib/api/quality";
import { exportToExcel } from "@/lib/utils/excelExport";

// wace_plm processInspectionList.jsp 그리드 1:1
const GRID_COLUMNS: DataGridColumn[] = [
  { key: "inspection_date",   label: "검사일",       width: "w-[110px]", align: "center", frozen: true },
  { key: "inspector_name",    label: "검사자",       width: "w-[100px]", align: "center" },
  { key: "project_no",        label: "프로젝트번호", width: "w-[150px]", align: "center" },
  { key: "product_name",      label: "제품구분",     width: "w-[110px]", align: "center" },
  { key: "part_no",           label: "품번",         width: "w-[180px]" },
  { key: "part_name",         label: "품명",         width: "w-[200px]" },
  { key: "inspection_qty",    label: "검사수량 합계", width: "w-[120px]", align: "right", formatNumber: true },
  { key: "inspection_result", label: "검사결과",      width: "w-[100px]", align: "center" },
  { key: "process_inspection_file_cnt", label: "첨부파일", width: "w-[100px]", align: "center", renderType: "clip" },
];

const OK_NG_OPTIONS = ["전체", "OK", "NG"];

export default function ProcessInspectionPage() {
  const { user } = useAuth();
  const [rows, setRows] = useState<ProcessInspectionRow[]>([]);
  const [loading, setLoading] = useState(false);
  const [selectedId, setSelectedId] = useState<string | null>(null);

  const [search, setSearch] = useState({
    search_project_no: "",
    productType: "",
    search_part_no: "",
    search_part_name: "",
    search_work_env_status: "",
    search_measuring_device: "",
    search_inspection_date_from: "",
    search_inspection_date_to: "",
    search_inspector: "",
    search_inspection_result: "",
    search_process_cd: "",
  });

  const fetchList = useCallback(async () => {
    if (!user) return;
    setLoading(true);
    try {
      const res = await qualityApi.processInspection(search);
      setRows(res.list.map((r) => ({ ...r, id: r.objid } as any)));
      setSelectedId(null);
    } catch { toast.error("공정검사 목록 조회 실패"); }
    finally { setLoading(false); }
  }, [user, search]);

  useEffect(() => { fetchList(); }, [fetchList]);

  const handleReset = () => setSearch({
    search_project_no: "", productType: "",
    search_part_no: "", search_part_name: "",
    search_work_env_status: "", search_measuring_device: "",
    search_inspection_date_from: "", search_inspection_date_to: "",
    search_inspector: "", search_inspection_result: "", search_process_cd: "",
  });

  const HardcodedSelect = ({ value, onChange, options }: { value: string; onChange: (v: string) => void; options: string[] }) => (
    <Select value={value || "all"} onValueChange={(v) => onChange(v === "all" || v === "전체" ? "" : v)}>
      <SelectTrigger className="h-7 text-xs"><SelectValue placeholder="전체" /></SelectTrigger>
      <SelectContent>
        {options.map((o) => <SelectItem key={o} value={o === "전체" ? "all" : o}>{o}</SelectItem>)}
      </SelectContent>
    </Select>
  );

  return (
    <div className="flex h-full flex-col overflow-hidden p-2 gap-2">
      <PageHeader
        loading={loading}
        onSearch={fetchList}
        onReset={handleReset}
        actions={
          <Button size="sm" className="h-8 gap-1 text-xs" disabled>
            <Plus className="h-3.5 w-3.5" />공정검사 등록
          </Button>
        }
      />
      <CompactFilterBar totalText={<>총 {rows.length.toLocaleString()}건</>}>
        <CompactFilterField label="프로젝트번호" width={150}>
          <Input value={search.search_project_no} onChange={(e) => setSearch({ ...search, search_project_no: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="제품구분 ID" width={130}>
          <Input value={search.productType} onChange={(e) => setSearch({ ...search, productType: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="품번" width={140}>
          <Input value={search.search_part_no} onChange={(e) => setSearch({ ...search, search_part_no: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="품명" width={160}>
          <Input value={search.search_part_name} onChange={(e) => setSearch({ ...search, search_part_name: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="작업환경상태" width={120}>
          <HardcodedSelect value={search.search_work_env_status} onChange={(v) => setSearch({ ...search, search_work_env_status: v })} options={OK_NG_OPTIONS} />
        </CompactFilterField>
        <CompactFilterField label="측정기" width={120}>
          <HardcodedSelect value={search.search_measuring_device} onChange={(v) => setSearch({ ...search, search_measuring_device: v })} options={OK_NG_OPTIONS} />
        </CompactFilterField>
        <CompactFilterField label="검사일" width={280}>
          <CompactDateRange
            from={search.search_inspection_date_from} setFrom={(v) => setSearch({ ...search, search_inspection_date_from: v })}
            to={search.search_inspection_date_to}     setTo={(v) => setSearch({ ...search, search_inspection_date_to: v })}
          />
        </CompactFilterField>
        <CompactFilterField label="검사자 ID" width={130}>
          <Input value={search.search_inspector} onChange={(e) => setSearch({ ...search, search_inspector: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="검사결과" width={110}>
          <HardcodedSelect value={search.search_inspection_result} onChange={(v) => setSearch({ ...search, search_inspection_result: v })} options={OK_NG_OPTIONS} />
        </CompactFilterField>
        <CompactFilterField label="진행공정 ID" width={130}>
          <Input value={search.search_process_cd} onChange={(e) => setSearch({ ...search, search_process_cd: e.target.value })} />
        </CompactFilterField>
      </CompactFilterBar>
      <DataGrid
        gridId="quality-process-inspection"
        columns={GRID_COLUMNS}
        data={rows}
        loading={loading}
        showCheckbox
        checkedIds={selectedId ? [selectedId] : []}
        onCheckedChange={(ids) => setSelectedId(ids.length ? ids[ids.length - 1] : null)}
        selectedId={selectedId}
        onSelect={setSelectedId}
        emptyMessage="조회된 공정검사 내역이 없습니다."
        showColumnSettings
        paginationStyle="range"
        pageSizeOptions={[10, 15, 20, 50, 100]}
        showChart
        onRefresh={fetchList}
        onDownload={() => {
          if (rows.length === 0) { toast.info("내보낼 데이터가 없습니다."); return; }
          const exportRows = rows.map((r) => {
            const out: Record<string, any> = {};
            GRID_COLUMNS.forEach((c) => { out[c.label] = (r as any)[c.key] ?? ""; });
            return out;
          });
          exportToExcel(exportRows, "공정검사관리.xlsx", "공정검사관리");
        }}
      />
    </div>
  );
}
