"use client";

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

export default function SubcontractorOutboundPage() {
  const router = useRouter();
  const companyPath = usePopCompanyPath();
  const cart = useCartSync("outbound");
  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/outbound/cart")}?backUrl=${companyPath("/pop/outbound/subcontractor")}`);
  };

  return (
    <SubcontractorOutbound
      cart={cart}
      onCartClick={handleCartClick}
      saving={saving}
      outboundType="외주출고"
      sourceTable="outsource_outbound_detail"
    />
  );
}
