'use client'

import React, { useState, useEffect } from 'react'
import { useAxios } from '@/hooks/useAxios'
import { useRouter, useSearchParams } from 'next/navigation'
import { globalLocaleStore } from '@/store/localeAtom'
import { useRecoilValue } from 'recoil'
export default function StuffHeader() {
  const router = useRouter()
  const searchParams = useSearchParams()
  const objectNo = searchParams.get('objectNo') //url에서 물건번호 꺼내서 바로 set
  const globalLocaleState = useRecoilValue(globalLocaleStore)
  const { get } = useAxios(globalLocaleState)
  const [headerData, setHeaderData] = useState({})

  useEffect(() => {
    get({ url: `/api/object/${objectNo}/detail` }).then((res) => {
      //console.log('res::', res)
      if (res != null && res != '') {
        console.log('헤더상세::::::::::', res)
        setHeaderData(res)
      } else {
        alert('삭제된 물건입니다')
        router.push('/management/stuff')
      }
    })
  }, [objectNo])

  return (
    <div className="infomation-box-wrap">
      <div className="sub-table-box">
        <div className="info-title">물건번호</div>
        <div className="info-inner">
          {headerData.objectNo} <button className="copy-ico"></button>
        </div>
      </div>
      <div className="sub-table-box">
        <div className="info-title">사양확정일</div>
        <div className="info-inner">{headerData.specificationConfirmDate}</div>
      </div>
      <div className="sub-table-box">
        <div className="info-title">갱신일시</div>
        <div className="info-inner">{headerData.lastEditDatetime}</div>
      </div>
      <div className="sub-table-box">
        <div className="info-title">등록일</div>
        <div className="info-inner">{headerData.createDatetime}</div>
      </div>
    </div>
  )
}
