"use client";

/**
 * 수입검사 진행 — wace_plm 의 incomingInspectionProgressList.jsp 1:1 이식.
 * 필터 11종, 그리드 19컬럼 (불량률 자동산정, 검사현황 컬러표시).
 */
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, IncomingMgmtRow } from "@/lib/api/quality";
import { exportToExcel } from "@/lib/utils/excelExport";

// wace_plm incomingInspectionProgressList.jsp 그리드 1:1
const GRID_COLUMNS: DataGridColumn[] = [
  { key: "inspection_date",   label: "검사일",       width: "w-[110px]", align: "center", frozen: true },
  { key: "inspector_name",    label: "검사자",       width: "w-[90px]",  align: "center" },
  { key: "proposal_no",       label: "품의서 No",    width: "w-[120px]", align: "center" },
  { key: "purchase_order_no", label: "발주서 No",    width: "w-[120px]", align: "center" },
  { key: "project_no",        label: "프로젝트번호", width: "w-[140px]", align: "center" },
  { key: "product_name",      label: "제품구분",     width: "w-[100px]", align: "center" },
  { key: "model_name",        label: "품명(모델명)", width: "w-[150px]" },
  { key: "part_no",           label: "부품품번",     width: "w-[140px]" },
  { key: "part_name",         label: "부품명",       width: "w-[150px]" },
  { key: "partner_name",      label: "공급업체",     width: "w-[150px]" },
  { key: "delivery_date",     label: "입고일",       width: "w-[110px]", align: "center" },
  { key: "delivery_qty",      label: "입고수량",     width: "w-[100px]", align: "right", formatNumber: true },
  { key: "delivery_status",   label: "입고결과",     width: "w-[90px]",  align: "center" },
  { key: "inspection_qty",    label: "검사수량",     width: "w-[100px]", align: "right", formatNumber: true },
  { key: "defect_qty_sum",    label: "불량수량",     width: "w-[100px]", align: "right", formatNumber: true },
  { key: "defect_rate",       label: "불량률(%)",    width: "w-[100px]", align: "right" },
  { key: "inspection_result", label: "검사현황",     width: "w-[100px]", align: "center" },
  { key: "inspection_file_cnt", label: "검사성적서", width: "w-[100px]", align: "center", renderType: "clip" },
];

const INSPECTION_STATUS_OPTIONS = ["전체", "완료", "진행중"];
const DELIVERY_STATUS_OPTIONS   = ["전체", "입고중", "입고완료", "지연"];

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

  const [search, setSearch] = useState({
    search_proposal_no: "",
    search_purchase_order_no: "",
    project_no: "",
    search_part_no: "",
    search_part_name: "",
    search_partner: "",
    search_delivery_status: "",
    search_product_cd: "",
    inspector_id: "",
    inspection_start_date: "",
    inspection_end_date: "",
    search_inspection_status: "",
  });

  const fetchList = useCallback(async () => {
    if (!user) return;
    setLoading(true);
    try {
      const res = await qualityApi.incomingMgmt(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_proposal_no: "", search_purchase_order_no: "", project_no: "",
    search_part_no: "", search_part_name: "", search_partner: "",
    search_delivery_status: "", search_product_cd: "",
    inspector_id: "", inspection_start_date: "", inspection_end_date: "",
    search_inspection_status: "",
  });

  const summary = (() => {
    const fmt = (n: number) => n.toLocaleString();
    return [
      { label: "총 입고수량",   value: fmt(rows.reduce((a, r) => a + Number(r.delivery_qty || 0), 0)) },
      { label: "총 검사수량",   value: fmt(rows.reduce((a, r) => a + Number(r.inspection_qty || 0), 0)) },
      { label: "총 불량수량",   value: fmt(rows.reduce((a, r) => a + Number(r.defect_qty_sum || 0), 0)) },
    ];
  })();

  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="품의서 No" width={130}>
          <Input value={search.search_proposal_no} onChange={(e) => setSearch({ ...search, search_proposal_no: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="발주서 No" width={130}>
          <Input value={search.search_purchase_order_no} onChange={(e) => setSearch({ ...search, search_purchase_order_no: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="프로젝트번호" width={150}>
          <Input value={search.project_no} onChange={(e) => setSearch({ ...search, project_no: 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="공급업체 ID" width={140}>
          <Input value={search.search_partner} onChange={(e) => setSearch({ ...search, search_partner: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="입고결과" width={120}>
          <HardcodedSelect value={search.search_delivery_status} onChange={(v) => setSearch({ ...search, search_delivery_status: v })} options={DELIVERY_STATUS_OPTIONS} />
        </CompactFilterField>
        <CompactFilterField label="제품구분 ID" width={120}>
          <Input value={search.search_product_cd} onChange={(e) => setSearch({ ...search, search_product_cd: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="검사자 ID" width={120}>
          <Input value={search.inspector_id} onChange={(e) => setSearch({ ...search, inspector_id: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="검사일" width={280}>
          <CompactDateRange
            from={search.inspection_start_date} setFrom={(v) => setSearch({ ...search, inspection_start_date: v })}
            to={search.inspection_end_date}     setTo={(v) => setSearch({ ...search, inspection_end_date: v })}
          />
        </CompactFilterField>
        <CompactFilterField label="검사현황" width={110}>
          <HardcodedSelect value={search.search_inspection_status} onChange={(v) => setSearch({ ...search, search_inspection_status: v })} options={INSPECTION_STATUS_OPTIONS} />
        </CompactFilterField>
      </CompactFilterBar>
      <DataGrid
        gridId="quality-incoming-mgmt"
        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="조회된 수입검사 진행 내역이 없습니다."
        summaryStats={summary}
        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>
  );
}
