"use client";

// 구매관리 > 프로젝트별 발주/입고 현황 — wace purchaseOrder/projectPurchaseDeliveryStatus.jsp 1:1
// 검색: 년도/고객사/프로젝트번호/제품구분/품번/품명
// 그리드: 프로젝트정보(5) + 전체(2) + 발주(2) + 미발주(2) + 입고(2) + 미입고(2) = 15컬럼
// 액션: 조회만
// 데이터 출처:
//   - 프로젝트정보 / 전체(BOM기준) — project_mgmt + contract_mgmt + mbom_header/detail ✓
//   - 발주/입고/미발주/미입고 — purchase_order_part / arrival_plan 누락 → 0 표시

import React, { useCallback, useEffect, useMemo, useState } from "react";
import { Input } from "@/components/ui/input";
import { toast } from "sonner";
import { DataGrid, DataGridColumn } from "@/components/common/DataGrid";
import { SmartSelect, SmartSelectOption } from "@/components/common/SmartSelect";
import { CustomerSelect } from "@/components/common/CustomerSelect";
import { CompactFilterBar, CompactFilterField } from "@/components/common/CompactFilterBar";
import { PageHeader } from "@/components/common/PageHeader";
import { apiClient } from "@/lib/api/client";
import { purchaseApi, PurchaseListFilter, getYearOptions } from "@/lib/api/purchase";
import { exportToExcel } from "@/lib/utils/excelExport";

const PARENT_PRODUCT = "0000001";

const EMPTY_FILTER: PurchaseListFilter = {
  year: String(new Date().getFullYear()),
  customer_objid: "", project_no: "", product_cd: "",
  part_no: "", part_name: "",
  page: 1, page_size: 50,
};

export default function ProjectStatusPage() {
  const [rows, setRows] = useState<any[]>([]);
  const [total, setTotal] = useState(0);
  const [loading, setLoading] = useState(false);
  const [filter, setFilter] = useState<PurchaseListFilter>(EMPTY_FILTER);

  const [productOpts, setProductOpts] = useState<SmartSelectOption[]>([]);
  const yearOpts = useMemo(() => getYearOptions(), []);

  const fetchList = useCallback(async (override?: Partial<PurchaseListFilter>) => {
    setLoading(true);
    try {
      const f = { ...filter, ...override };
      const res = await purchaseApi.listProjectStatus(f);
      setRows(res.rows ?? []);
      setTotal(res.totalCount ?? 0);
    } catch (e: any) {
      toast.error(e?.response?.data?.message ?? e?.message ?? "조회 실패");
    } finally {
      setLoading(false);
    }
  }, [filter]);

  useEffect(() => {
    let dead = false;
    (async () => {
      try {
        const r = await apiClient.get(`/sales/codes/${PARENT_PRODUCT}`);
        if (dead) return;
        setProductOpts(r.data?.data ?? []);
      } catch { /* skip */ }
    })();
    fetchList(EMPTY_FILTER);
    return () => { dead = true; };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  const gridRows = useMemo(() => rows.map((r, i) => ({ ...r, id: r.objid ?? `s_${i}` })), [rows]);

  const GRID_COLUMNS: DataGridColumn[] = useMemo(() => ([
    // 프로젝트정보
    { key: "project_no",       label: "프로젝트번호",  width: "w-[140px]", align: "center", frozen: true },
    { key: "product_name",     label: "제품구분",      width: "w-[115px]", align: "center" },
    { key: "part_no",          label: "품번",          width: "w-[150px]" },
    { key: "part_name",        label: "품명",          minWidth: "min-w-[280px]" },
    { key: "customer_name",    label: "고객사",        minWidth: "min-w-[180px]" },
    // 전체 (BOM기준)
    { key: "total_item_cnt",   label: "전체품목수",    width: "w-[115px]", align: "right", formatNumber: true },
    { key: "total_qty",        label: "전체수량",      width: "w-[115px]", align: "right", formatNumber: true },
    // 발주현황
    { key: "po_item_cnt",      label: "발주품목수",    width: "w-[115px]", align: "right", formatNumber: true },
    { key: "po_qty",           label: "발주수량",      width: "w-[115px]", align: "right", formatNumber: true },
    // 미발주현황
    { key: "non_po_item_cnt",  label: "미발주품목수",  width: "w-[125px]", align: "right", formatNumber: true },
    { key: "non_po_qty",       label: "미발주수량",    width: "w-[115px]", align: "right", formatNumber: true },
    // 입고현황
    { key: "dlv_item_cnt",     label: "입고품목수",    width: "w-[115px]", align: "right", formatNumber: true },
    { key: "dlv_qty",          label: "입고수량",      width: "w-[115px]", align: "right", formatNumber: true },
    // 미입고현황
    { key: "non_dlv_item_cnt", label: "미입고품목수",  width: "w-[125px]", align: "right", formatNumber: true },
    { key: "non_dlv_qty",      label: "미입고수량",    width: "w-[115px]", align: "right", formatNumber: true },
  ]), []);

  const summary = useMemo(() => {
    const pageQty = gridRows.reduce((acc, r: any) => acc + Number(r.total_qty || 0), 0);
    return [
      { label: "전체 건수", value: total.toLocaleString(), suffix: "건" },
      { label: "페이지 전체수량 합계", value: pageQty.toLocaleString() },
    ];
  }, [gridRows, total]);

  const handleSearch = () => { setFilter(f => ({ ...f, page: 1 })); fetchList({ page: 1 }); };
  const handleReset  = () => { setFilter(EMPTY_FILTER); fetchList(EMPTY_FILTER); };

  return (
    <div className="flex h-full flex-col overflow-hidden p-2 gap-2">
      <PageHeader loading={loading} onSearch={handleSearch} onReset={handleReset} />
      <CompactFilterBar totalText={<>총 {total.toLocaleString()}건</>}>
        <CompactFilterField label="년도" width={100}>
          <SmartSelect options={yearOpts} value={filter.year ?? ""}
                       onValueChange={(v) => setFilter({ ...filter, year: v })} />
        </CompactFilterField>
        <CompactFilterField label="고객사" width={170}>
          <CustomerSelect value={filter.customer_objid ?? ""}
                          onValueChange={(v) => setFilter({ ...filter, customer_objid: v })} />
        </CompactFilterField>
        <CompactFilterField label="프로젝트번호" width={170}>
          <Input value={filter.project_no ?? ""}
                 onChange={(e) => setFilter({ ...filter, project_no: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="제품구분" width={130}>
          <SmartSelect options={productOpts} value={filter.product_cd ?? ""}
                       onValueChange={(v) => setFilter({ ...filter, product_cd: v })} />
        </CompactFilterField>
        <CompactFilterField label="품번" width={150}>
          <Input value={filter.part_no ?? ""}
                 onChange={(e) => setFilter({ ...filter, part_no: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="품명" width={170}>
          <Input value={filter.part_name ?? ""}
                 onChange={(e) => setFilter({ ...filter, part_name: e.target.value })} />
        </CompactFilterField>
      </CompactFilterBar>

      <DataGrid
        columns={GRID_COLUMNS}
        data={gridRows}
        loading={loading}
        emptyMessage={loading ? "조회 중..." : "데이터가 없습니다"}
        gridId="purchase-project-status"
        pageSizeOptions={[10, 15, 20, 50, 100]}
        paginationStyle="range"
        serverPaging
        serverPage={filter.page ?? 1}
        serverPageSize={filter.page_size ?? 50}
        serverTotalItems={total}
        onPageChange={(p) => { setFilter(f => ({ ...f, page: p })); fetchList({ page: p }); }}
        onPageSizeChange={(n) => { setFilter(f => ({ ...f, page: 1, page_size: n })); fetchList({ page: 1, page_size: n }); }}
        showColumnSettings
        summaryStats={summary}
        onRefresh={() => fetchList()}
        onDownload={() => {
          if (gridRows.length === 0) { toast.info("내보낼 데이터가 없습니다."); return; }
          const exportRows = gridRows.map((r: any) => {
            const out: Record<string, any> = {};
            GRID_COLUMNS.forEach((c) => { out[c.label] = r[c.key] ?? ""; });
            return out;
          });
          exportToExcel(exportRows, "프로젝트별_발주_입고현황.xlsx", "프로젝트현황");
        }}
        showChart
      />
    </div>
  );
}
