import { useEffect, useState } from 'react';
import { useLocation } from 'react-router';
import { PATHS } from '@/shared/constants/paths';
import { useNoticeDetailMutation } from '@/entities/support/api/use-notice-detail-mutaion';
import { useNavigate } from '@/shared/lib/hooks/use-navigate';
import { HeaderType } from '@/entities/common/model/types';
import { NoticeItem } from '@/entities/support/model/types';
import { 
  useSetHeaderTitle,
  useSetHeaderType,
  useSetFooterMode,
  useSetOnBack
} from '@/widgets/sub-layout/use-sub-layout';
import moment from 'moment';

export const NoticeDetailPage = () => {
  const { navigate } = useNavigate();
  const location = useLocation();

  const [result, setResult] = useState<NoticeItem>({});

  let from = location?.state.from;

  useSetHeaderTitle('공지사항');
  useSetHeaderType(HeaderType.RightClose);
  useSetFooterMode(false);
  useSetOnBack(() => {
    if(from){
      navigate(from);
    }
    else{
      navigate(PATHS.support.notice.list);
    }
    
  });

  const { mutateAsync: noticeDetail } = useNoticeDetailMutation();

  

  const callDetail = () => {
    let detailParams = {
      noticeId: location?.state.id,
    };
    
    noticeDetail(detailParams).then((rs) => {
      console.log(rs);
      setResult(rs);
    });
  };

  useEffect(() => {
    callDetail();
  }, []);

  return (
    <>
      <main>
        <div className="tab-content">
          <div className="tab-pane sub active">
            <div className="option-list pb-120">
              <div className="notice-detail">
                <div className="notice-detail__title">{ result.title }</div>
                <div className="notice-detail__meta">{ moment(result.regDate).format('YYYY.MM.DD') } | { result.category }</div>
                <div className="notice-detail__divider"></div>
                <div className="notice-detail__body">{ result.content }</div>
              </div>
            </div>
          </div>
        </div>
    </main>
    </>
  );
};