"use client";

// 자재관리 > 불출의뢰서 — wace inventoryMng/materialRequestList.jsp 1:1
// 그리드: inventory_mgmt_out_master + inventory_mgmt_out 집계
// 검색: 품번/품명 / 불출의뢰일 / 의뢰자 / 접수상태 / 접수자 / 접수일 / 불출상태
// 액션: 자재불출 / 접수 / 삭제 / 조회

import React, { useCallback, useEffect, useMemo, useState } from "react";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { CheckCircle2, Trash2, PackageMinus } from "lucide-react";
import { toast } from "sonner";
import { DataGrid, DataGridColumn } from "@/components/common/DataGrid";
import { SmartSelect } from "@/components/common/SmartSelect";
import { CompactFilterBar, CompactFilterField, CompactDateRange } from "@/components/common/CompactFilterBar";
import { PageHeader } from "@/components/common/PageHeader";
import { exportToExcel } from "@/lib/utils/excelExport";
import { inventoryMngApi, IssueRequestFilter, OptionItem } from "@/lib/api/inventoryMng";
import { IssueDispatchDialog } from "@/components/material/IssueDispatchDialog";

const EMPTY: IssueRequestFilter = {
  part_no: "", part_name: "",
  request_start_date: "", request_end_date: "",
  request_user: "", reception_status: "",
  reception_user: "", reception_start_date: "", reception_end_date: "",
  out_status: "",
  page: 1, page_size: 50,
};

const RECEPTION_OPTS = [
  { code: "reception", label: "접수" },
  { code: "AA",        label: "미접수" },
];

const OUT_STATUS_OPTS = [
  { code: "complete", label: "완료" },
  { code: "NG",       label: "미완료" },
];

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

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

  const [dispatchOpen, setDispatchOpen] = useState(false);
  const [dispatchTarget, setDispatchTarget] = useState<{ objid: string; readOnly: boolean } | null>(null);

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

  useEffect(() => {
    (async () => {
      try { setUserOpts(await inventoryMngApi.users()); } catch { /* skip */ }
    })();
    fetchList(EMPTY);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

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

  const checkedRows = useMemo(
    () => gridRows.filter((r: any) => checkedIds.includes(r.id)),
    [checkedIds, gridRows],
  );

  const GRID_COLUMNS: DataGridColumn[] = useMemo(() => ([
    { key: "inventory_out_no",      label: "자재불출번호",   width: "w-[140px]", align: "center" },
    { key: "part_no_arr",           label: "품번",          minWidth: "min-w-[200px]" },
    { key: "part_name_arr",         label: "품명",          minWidth: "min-w-[260px]" },
    { key: "request_date",          label: "불출의뢰일",    width: "w-[110px]", align: "center" },
    { key: "request_user_name",     label: "의뢰자",        width: "w-[100px]", align: "center" },
    { key: "reception_status_title",label: "상태",         width: "w-[80px]",  align: "center" },
    { key: "reception_user_name",   label: "접수자",        width: "w-[100px]", align: "center" },
    { key: "reception_date",        label: "접수일",        width: "w-[110px]", align: "center" },
    { key: "outstatus_title",       label: "불출상태",      width: "w-[90px]",  align: "center" },
    { key: "request_qty_total",     label: "의뢰수량합",    width: "w-[100px]", align: "right",
      render: (v: any) => Number(v ?? 0).toLocaleString() },
    { key: "out_qty_total",         label: "불출수량합",    width: "w-[100px]", align: "right",
      render: (v: any) => Number(v ?? 0).toLocaleString() },
    { key: "remark",                label: "특이사항",      minWidth: "min-w-[160px]" },
  ]), []);

  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); setCheckedIds([]); fetchList(EMPTY); };

  const handleReceive = async () => {
    const targets = checkedRows.filter((r: any) => r.reception_status !== "reception");
    if (!targets.length) return toast.info("미접수 상태인 항목을 선택해주세요.");
    try {
      const r = await inventoryMngApi.receiveIssue(targets.map((t: any) => t.objid));
      toast.success(`${r.updated}건 접수 처리되었습니다.`);
      setCheckedIds([]);
      fetchList();
    } catch (e: any) {
      toast.error(e?.response?.data?.message ?? e?.message ?? "접수 실패");
    }
  };

  const handleDispatch = () => {
    if (checkedRows.length !== 1) return toast.info("자재불출은 1건씩 처리합니다.");
    const r = checkedRows[0] as any;
    if (r.reception_status !== "reception") return toast.warning("접수된 항목만 불출 가능합니다.");
    if (r.outstatus === "complete")          return toast.warning("이미 불출완료된 항목입니다.");
    setDispatchTarget({ objid: r.objid, readOnly: false });
    setDispatchOpen(true);
  };

  const handleDelete = async () => {
    if (checkedRows.length !== 1) return toast.info("1건씩 삭제하세요.");
    const r = checkedRows[0] as any;
    if (r.outstatus === "complete") return toast.warning("이미 불출완료된 항목은 삭제할 수 없습니다.");
    if (!confirm("선택한 불출의뢰서를 삭제하시겠습니까?")) return;
    try {
      await inventoryMngApi.deleteIssue(r.objid);
      toast.success("삭제되었습니다.");
      setCheckedIds([]);
      fetchList();
    } catch (e: any) {
      toast.error(e?.response?.data?.message ?? e?.message ?? "삭제 실패");
    }
  };

  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="default" className="h-8 gap-1 px-2 text-xs"
                  onClick={handleDispatch} disabled={checkedRows.length !== 1}>
            <PackageMinus className="h-3.5 w-3.5" /> 자재불출
          </Button>
          <Button size="sm" variant="outline" className="h-8 gap-1 px-2 text-xs"
                  onClick={handleReceive} disabled={!checkedRows.length}>
            <CheckCircle2 className="h-3.5 w-3.5" /> 접수
          </Button>
          <Button size="sm" variant="ghost" className="h-8 gap-1 px-2 text-xs text-red-600"
                  onClick={handleDelete} disabled={checkedRows.length !== 1}>
            <Trash2 className="h-3.5 w-3.5" /> 삭제
          </Button>
        </>}
      />

      <CompactFilterBar totalText={<>총 {total.toLocaleString()}건</>}>
        <CompactFilterField label="품번" width={160}>
          <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>
        <CompactFilterField label="불출의뢰일" width={280}>
          <CompactDateRange
            from={filter.request_start_date ?? ""} setFrom={(v) => setFilter({ ...filter, request_start_date: v })}
            to={filter.request_end_date ?? ""}     setTo={(v) => setFilter({ ...filter, request_end_date: v })}
          />
        </CompactFilterField>
        <CompactFilterField label="의뢰자" width={170}>
          <SmartSelect options={userOpts} value={filter.request_user ?? ""}
            onValueChange={(v) => setFilter({ ...filter, request_user: v })} />
        </CompactFilterField>
        <CompactFilterField label="접수상태" width={120}>
          <SmartSelect options={RECEPTION_OPTS} value={filter.reception_status ?? ""}
            onValueChange={(v) => setFilter({ ...filter, reception_status: v })} />
        </CompactFilterField>
        <CompactFilterField label="접수자" width={170}>
          <SmartSelect options={userOpts} value={filter.reception_user ?? ""}
            onValueChange={(v) => setFilter({ ...filter, reception_user: v })} />
        </CompactFilterField>
        <CompactFilterField label="접수일" width={280}>
          <CompactDateRange
            from={filter.reception_start_date ?? ""} setFrom={(v) => setFilter({ ...filter, reception_start_date: v })}
            to={filter.reception_end_date ?? ""}     setTo={(v) => setFilter({ ...filter, reception_end_date: v })}
          />
        </CompactFilterField>
        <CompactFilterField label="불출상태" width={120}>
          <SmartSelect options={OUT_STATUS_OPTS} value={filter.out_status ?? ""}
            onValueChange={(v) => setFilter({ ...filter, out_status: v })} />
        </CompactFilterField>
      </CompactFilterBar>

      <DataGrid
        columns={GRID_COLUMNS}
        data={gridRows}
        loading={loading}
        showCheckbox
        checkedIds={checkedIds}
        onCheckedChange={setCheckedIds}
        emptyMessage={loading ? "조회 중..." : "데이터가 없습니다"}
        gridId="material-issue-request"
        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", "불출의뢰서");
        }}
        onRowDoubleClick={(row: any) => {
          setDispatchTarget({ objid: row.objid, readOnly: true });
          setDispatchOpen(true);
        }}
        showChart
      />

      {dispatchTarget && (
        <IssueDispatchDialog
          open={dispatchOpen}
          onClose={() => setDispatchOpen(false)}
          onSaved={() => { setCheckedIds([]); fetchList(); }}
          masterObjid={dispatchTarget.objid}
          readOnly={dispatchTarget.readOnly}
        />
      )}
    </div>
  );
}
