'use client';

import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import InventoryAdjustmentForm from '@/components/pages/inventory/adjustment/form/InventoryAdjustmentForm';
import type { InventoryAdjustment } from '@/types/api/inventory/adjustment';

const DetailInventoryAdjustment = () => {
  const router = useRouter();
  const [inventoryAdjustment, setInventoryAdjustment] =
    useState<InventoryAdjustment | null>(null);

  // Ambil data dari router state
  useEffect(() => {
    const state = window.history.state?.usr as
      | { inventoryAdjustment?: InventoryAdjustment }
      | undefined;

    if (state?.inventoryAdjustment) {
      // jika object dikirim via router.push(state)
      setInventoryAdjustment(state.inventoryAdjustment);
    }
  }, [router]);

  const finalData = inventoryAdjustment;

  if (!finalData) {
    return (
      <div className='w-full flex flex-row justify-center items-center p-4'>
        <span className='loading loading-spinner loading-xl' />
      </div>
    );
  }

  return (
    <section className='w-full p-4 flex flex-row justify-center'>
      <InventoryAdjustmentForm initialValues={finalData} />
    </section>
  );
};

export default DetailInventoryAdjustment;
