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

export const QnaDetailPage = () => {
  const { navigate } = useNavigate();
  const { t } = useTranslation();
  const location = useLocation();

  const [answer, setAnswer] = useState<string>('');
  const [answerDate, setAnswerDate] = useState<string>('');
  const [contents, setContents] = useState<string>('');
  const [corpName, setCorpName] = useState<string | null>('');
  const [cursorId, setCursorId] = useState<number>(0);
  const [requestDate, setRequestDate] = useState<string>('');
  const [requestName, setRequestName] = useState<string>('');
  const [requestType, setRequestType] = useState<string>('');
  const [sendEmail, setSendEmail] = useState<string>('');
  const [seq, setSeq] = useState<string>('');
  const [statusCode, setStatusCode] = useState<string>('');
  const [title, setTitle] = useState<string>('');
  
  useSetHeaderTitle(t('support.qna.title'));
  useSetHeaderType(HeaderType.LeftArrow);
  useSetFooterMode(false);
  useSetOnBack(() => {
    navigate(PATHS.support.qna.list);
  });

  useEffect(() => {
    setAnswer(location?.state.answer);
    setAnswerDate(location?.state.answerDate);
    setContents(location?.state.contents);
    setCorpName(location?.state.corpName);
    setCursorId(location?.state.cursorId);
    setRequestDate(location?.state.requestDate);
    setRequestName(location?.state.requestName);
    setRequestType(location?.state.requestType);
    setSendEmail(location?.state.sendEmail);
    setSeq(location?.state.seq);
    setStatusCode(location?.state.statusCode);
    setTitle(location?.state.title);
  }, []);

  return (
    <>
      <main>
        { statusCode && 
          <div className="tab-content">
            <div className="tab-pane active">
              <div className="inq-detail">
                <div className="inq-detail__head">
                  <div className="inq-detail__row">
                    <span className="inq-badge">{t('support.qna.detailLabels.title')}</span>
                    <span className="inq-head-text bold">{ title }</span>
                  </div>
                  <div className="inq-detail__row">
                    <span className="inq-badge">{t('support.qna.detailLabels.type')}</span>
                    <span className="inq-head-text">{ t(`support.qna.categories.${requestType}`) }</span>
                  </div>
                  <div className="inq-detail__row">
                    <span className="inq-badge">{t('support.qna.detailLabels.registrationDate')}</span>
                    <span className="inq-head-text">{ !!requestDate? moment(requestDate).format('YYYY.MM.DD'): '' }</span>
                  </div>
                  <div className="inq-detail__row">
                    <span className="inq-badge">{t('support.qna.detailLabels.answerDate')}</span>
                    <span className="inq-head-text">{ !!answerDate? moment(answerDate).format('YYYY.MM.DD'): '' }</span>
                  </div>
                </div>
                <div className="inq-detail__divider"></div>
                <div className="inq-detail__section">
                  <div className="inq-detail__section-title">{t('support.qna.detailLabels.inquiryAnswer')}</div>
                  <div className="inq-detail__body" dangerouslySetInnerHTML={{ __html: answer || '' }}></div>
                </div>
                <div className="inq-detail__section">
                  <div className="inq-detail__section-title">{t('support.qna.detailLabels.inquiryContents')}</div>
                  <div className="inq-detail__body" dangerouslySetInnerHTML={{ __html: contents || '' }}></div>
                </div>
              </div>
            </div>
          </div>
        }
      </main>
    </>
  );
};