"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import { usePopCompanyPath } from "@/hooks/usePopCompanyPath";
import { SubcontractorInbound } from "../../_components/inbound/SubcontractorInbound";
import { useCartSync } from "../../_components/common/useCartSync";

export default function SubcontractorInboundPage() {
  const router = useRouter();
  const companyPath = usePopCompanyPath();
  const cart = useCartSync("inbound");
  const [saving, setSaving] = useState(false);

  const handleCartClick = async () => {
    if (cart.isDirty) {
      setSaving(true);
      const ok = await cart.saveToDb();
      setSaving(false);
      if (!ok) return;
    }
    router.push(`${companyPath("/pop/inbound/cart")}?backUrl=${companyPath("/pop/inbound/subcontractor")}`);
  };

  return (
    <SubcontractorInbound
      cart={cart}
      onCartClick={handleCartClick}
      saving={saving}
      inboundType="외주입고"
      sourceTable="subcontractor_detail"
    />
  );
}
