"use client";

// 구매관리 > 입고일별 입고관리 — wace purchaseOrder/purchaseCloseList.jsp 1:1
// 검색: 년도/고객사/프로젝트/발주No/규격/품명/공급업체/구매담당자/입고일/매입마감/품번
// 그리드 26컬럼: 품의서·발주서·프로젝트·부품품번·품번·품명·공급업체·환종·입고일·담당자·등록자·
//              입고수량·입고금액·검사현황·폐기수량·확정입고수량·계정과목·국내해외·환율·과세구분·
//              세금계산서일·수출신고번호·선적일·관세·수입부가세·매입마감
// 액션: 조회 / 마감정보입력 / 매입마감
// ⚠️ arrival_plan / purchase_order_part 미존재 → 빈 그리드

import React, { useCallback, useEffect, useMemo, useState } from "react";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { FileEdit, Lock } from "lucide-react";
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, CompactDateRange } from "@/components/common/CompactFilterBar";
import { PageHeader } from "@/components/common/PageHeader";
import { purchaseApi, PurchaseListFilter, OptionItem, getYearOptions } from "@/lib/api/purchase";
import { exportToExcel } from "@/lib/utils/excelExport";
import { DeadlineInfoDialog, DeadlinePrefill } from "@/components/purchase/DeadlineInfoDialog";
import { useConfirmDialog } from "@/components/common/ConfirmDialog";

const CLOSE_OPTS: SmartSelectOption[] = [
  { code: "N", label: "미마감" },
  { code: "Y", label: "마감" },
];

const EMPTY_FILTER: PurchaseListFilter = {
  year: String(new Date().getFullYear()),
  customer_cd: "", project_no: "", purchase_order_no: "",
  part_spec: "", part_name: "", partner_objid: "",
  sales_mng_user_id: "",
  receipt_date_start: "", receipt_date_end: "",
  close_status: "", part_no: "",
  page: 1, page_size: 50,
};

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

  const [supplierOpts, setSupplierOpts] = useState<OptionItem[]>([]);
  const [userOpts, setUserOpts] = useState<OptionItem[]>([]);

  // 마감정보입력 / 매입마감
  const [deadlineOpen, setDeadlineOpen] = useState(false);
  const [deadlineObjIds, setDeadlineObjIds] = useState<string[]>([]);
  const [deadlinePrefill, setDeadlinePrefill] = useState<DeadlinePrefill | undefined>(undefined);
  const { confirm, ConfirmDialogComponent } = useConfirmDialog();
  const yearOpts = useMemo(() => getYearOptions(), []);

  const fetchList = useCallback(async (override?: Partial<PurchaseListFilter>) => {
    setLoading(true);
    try {
      const f = { ...filter, ...override };
      const res = await purchaseApi.listInboundByDate(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 [s, u] = await Promise.all([purchaseApi.listSuppliers(), purchaseApi.listUsers()]);
        if (dead) return;
        setSupplierOpts(s); setUserOpts(u);
      } 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 ?? `id_${i}` })), [rows]);

  const GRID_COLUMNS: DataGridColumn[] = useMemo(() => ([
    { key: "proposal_no",           label: "품의서 No",         width: "w-[125px]", align: "center" },
    { key: "purchase_order_no",     label: "발주서 No",         width: "w-[125px]", align: "center" },
    { key: "project_no",            label: "프로젝트번호",       width: "w-[135px]", align: "center" },
    { key: "component_part_no",     label: "부품품번",           width: "w-[135px]" },
    { key: "part_no",               label: "품번",               width: "w-[135px]" },
    { key: "part_name",             label: "품명",               width: "w-[280px]" },
    { key: "partner_name",          label: "공급업체",           minWidth: "min-w-[150px]" },
    { key: "currency_name",         label: "환종",               width: "w-[80px]",  align: "center" },
    { key: "receipt_date",          label: "입고일",             width: "w-[110px]", align: "center" },
    { key: "writer_name",           label: "구매담당자",         width: "w-[110px]", align: "center" },
    { key: "delivery_writer_name",  label: "입고등록자",         width: "w-[110px]", align: "center" },
    { key: "receipt_qty",           label: "입고수량",           width: "w-[110px]", align: "right", formatNumber: true },
    { key: "total_delivery_price",  label: "입고금액",           width: "w-[120px]", align: "right", formatMoney: true },
    { key: "inspection_status",     label: "검사현황",           width: "w-[110px]", align: "center" },
    { key: "defect_qty",            label: "폐기수량",           width: "w-[110px]", align: "right", formatNumber: true },
    { key: "confirmed_qty",         label: "확정입고수량",       width: "w-[120px]", align: "right", formatNumber: true },
    { key: "sub_location_name",     label: "계정과목",           width: "w-[120px]", align: "center" },
    { key: "foreign_type_name",     label: "국내/해외",          width: "w-[110px]", align: "center" },
    { key: "exchange_rate",         label: "환율",               width: "w-[110px]", align: "right", formatNumber: true },
    { key: "tax_type_name",         label: "과세구분",           width: "w-[110px]", align: "center" },
    { key: "tax_invoice_date",      label: "세금계산서발행일",   width: "w-[140px]", align: "center" },
    { key: "export_decl_no",        label: "수출신고필증신고번호", width: "w-[150px]", align: "center" },
    { key: "loading_date",          label: "선적일자",           width: "w-[110px]", align: "center" },
    { key: "duty",                  label: "관세",               width: "w-[110px]", align: "right", formatMoney: true },
    { key: "import_vat",            label: "수입부가세",         width: "w-[110px]", align: "right", formatMoney: true },
    { key: "purchase_close_date",   label: "매입마감",           width: "w-[110px]", align: "center" },
  ]), []);

  const summary = useMemo(() => [
    { label: "전체 건수", value: total.toLocaleString(), suffix: "건" },
    { label: "선택", value: checkedIds.length.toLocaleString(), suffix: "건" },
  ], [total, checkedIds]);

  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}
        actions={<>
          <Button size="sm" variant="outline" className="h-8 gap-1 px-2 text-xs"
                  disabled={checkedIds.length === 0}
                  onClick={() => {
                    const objIds = rows
                      .filter((r: any) => checkedIds.includes(String(r.objid)))
                      .map((r: any) => String(r.arrival_plan_objid || r.objid));
                    if (objIds.length === 0) return;
                    setDeadlineObjIds(objIds);
                    // 단건 선택 시 기존 값 prefill
                    if (objIds.length === 1) {
                      const row = rows.find((r: any) => String(r.arrival_plan_objid || r.objid) === objIds[0]);
                      setDeadlinePrefill({
                        taxType: row?.tax_type,
                        taxInvoiceDate: row?.tax_invoice_date,
                        exportDeclNo: row?.export_decl_no,
                        loadingDate: row?.loading_date,
                        foreignType: row?.foreign_type,
                        duty: row?.duty ? String(row.duty) : "",
                        exchangeRate: row?.exchange_rate ? String(row.exchange_rate) : "",
                        importVat: row?.import_vat ? String(row.import_vat) : "",
                      });
                    } else {
                      setDeadlinePrefill(undefined);
                    }
                    setDeadlineOpen(true);
                  }}>
            <FileEdit className="h-3.5 w-3.5" /> 마감정보입력
          </Button>
          <Button size="sm" variant="default" className="h-8 gap-1 px-2 text-xs"
                  disabled={checkedIds.length === 0}
                  onClick={async () => {
                    const selected = rows.filter((r: any) => checkedIds.includes(String(r.objid)));
                    const alreadyClosed = selected.filter((r: any) => (r.purchase_close_date ?? "") !== "");
                    if (alreadyClosed.length > 0) {
                      toast.error("이미 매입마감된 건이 포함돼 있습니다");
                      return;
                    }
                    const objIds = selected.map((r: any) => String(r.arrival_plan_objid || r.objid));
                    if (objIds.length === 0) return;
                    const ok = await confirm(`선택한 ${objIds.length}건을 매입마감 처리하시겠어요?`, {
                      confirmText: "매입마감",
                    });
                    if (!ok) return;
                    try {
                      const r = await purchaseApi.closeArrival(objIds);
                      toast.success(`매입마감 완료 (${r.updated}건)`);
                      fetchList();
                    } catch (e: any) {
                      toast.error(e?.response?.data?.message ?? e?.message ?? "매입마감 실패");
                    }
                  }}>
            <Lock className="h-3.5 w-3.5" /> 매입마감
          </Button>
        </>}
      />
      <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_cd ?? ""}
                          onValueChange={(v) => setFilter({ ...filter, customer_cd: v })} />
        </CompactFilterField>
        <CompactFilterField label="프로젝트번호" width={160}>
          <Input value={filter.project_no ?? ""}
                 onChange={(e) => setFilter({ ...filter, project_no: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="발주No" width={140}>
          <Input value={filter.purchase_order_no ?? ""}
                 onChange={(e) => setFilter({ ...filter, purchase_order_no: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="규격" width={130}>
          <Input value={filter.part_spec ?? ""}
                 onChange={(e) => setFilter({ ...filter, part_spec: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="품명" width={150}>
          <Input value={filter.part_name ?? ""}
                 onChange={(e) => setFilter({ ...filter, part_name: e.target.value })} />
        </CompactFilterField>
        <CompactFilterField label="공급업체" width={170}>
          <SmartSelect options={supplierOpts} value={filter.partner_objid ?? ""}
                       onValueChange={(v) => setFilter({ ...filter, partner_objid: v })} />
        </CompactFilterField>
        <CompactFilterField label="구매담당자" width={150}>
          <SmartSelect options={userOpts} value={filter.sales_mng_user_id ?? ""}
                       onValueChange={(v) => setFilter({ ...filter, sales_mng_user_id: v })} />
        </CompactFilterField>
        <CompactFilterField label="입고일" width={280}>
          <CompactDateRange
            from={filter.receipt_date_start ?? ""} setFrom={(v) => setFilter({ ...filter, receipt_date_start: v })}
            to={filter.receipt_date_end ?? ""}     setTo={(v) => setFilter({ ...filter, receipt_date_end: v })}
          />
        </CompactFilterField>
        <CompactFilterField label="매입마감" width={120}>
          <SmartSelect options={CLOSE_OPTS} value={filter.close_status ?? ""}
                       onValueChange={(v) => setFilter({ ...filter, close_status: v })} />
        </CompactFilterField>
        <CompactFilterField label="품번" width={140}>
          <Input value={filter.part_no ?? ""}
                 onChange={(e) => setFilter({ ...filter, part_no: e.target.value })} />
        </CompactFilterField>
      </CompactFilterBar>

      <DataGrid
        columns={GRID_COLUMNS}
        data={gridRows}
        loading={loading}
        showCheckbox
        checkedIds={checkedIds}
        onCheckedChange={setCheckedIds}
        emptyMessage={loading ? "조회 중..." : "데이터가 없습니다"}
        gridId="purchase-inbound-by-date"
        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}
        systemColumnKeys={["delivery_writer_name", "purchase_close_date"]}
        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
      />

      <DeadlineInfoDialog
        open={deadlineOpen}
        objIds={deadlineObjIds}
        prefill={deadlinePrefill}
        onClose={() => setDeadlineOpen(false)}
        onSaved={() => { setDeadlineOpen(false); fetchList(); }}
      />
      {ConfirmDialogComponent}
    </div>
  );
}
