import { PATHS } from '@/shared/constants/paths';
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 { useLocation } from 'react-router';
import { useEffect, useState } from 'react';
import { ExtensionAlimtalkDetailParams, ExtensionAlimtalkDetailResponse } from '@/entities/additional-service/model/alimtalk/types';
import { useExtensionAlimtalkDetailMutation } from '@/entities/additional-service/api/alimtalk/use-extansion-alimtalk-detail-mutation';
import moment from 'moment';

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

  const tid = location.state.tid;
  const mid = location.state.mid;

  const [detail, setDetail] = useState<ExtensionAlimtalkDetailResponse>();
  
  const { mutateAsync: extensionAlimtalkDetail } = useExtensionAlimtalkDetailMutation();

  const callDetail = () => {
    let params: ExtensionAlimtalkDetailParams = {
      tid: tid,
      mid: mid,
    };

    extensionAlimtalkDetail(params).then((rs: ExtensionAlimtalkDetailResponse) => {
      setDetail(rs);
    });
  };

  useSetHeaderTitle('알림톡 발송 상세');
  useSetHeaderType(HeaderType.LeftArrow);
  useSetFooterMode(false);
  useSetOnBack(() => {
    navigate(PATHS.additionalService.alimtalk.list);
  });

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

  const getDate = (date?: string) => {
    return (date)? moment(date.substr(0, 8)).format('YYYY.MM.DD'): '';
  };

  return (
    <>
      <main className="full-height">
        <div className="tab-content">
          <div className="tab-pane sub active">
            <div className="pay-top">
              <div className="num-amount">
                <span className="amount">{ detail?.receiverName }</span>
              </div>
              <div className="num-store">{ detail?.merchantName }</div>
              <div className="num-day">{ getDate(detail?.sendDate) }</div>
            </div>
            <div className="detail-divider"></div>
            <div className="pay-detail">
              <div className="detail-title">거래 정보</div>
              <ul className="kv-list">
                <li className="kv-row">
                  <span className="k">MID</span>
                  <span className="v">{ detail?.mid }</span>
                </li>
                <li className="kv-row">
                  <span className="k">TID</span>
                  <span className="v">{ detail?.tid }</span>
                </li>
                <li className="kv-row">
                  <span className="k">서비스명</span>
                  <span className="v">{ detail?.serviceName }</span>
                </li>
                <li className="kv-row">
                  <span className="k">발송 종류</span>
                  <span className="v">{ detail?.sendType }</span>
                </li>
                <li className="kv-row">
                  <span className="k">주문자명</span>
                  <span className="v">{detail?.senderName }</span>
                </li>
                <li className="kv-row">
                  <span className="k">결제서비스</span>
                  <span className="v">{ detail?.paymentMethod }</span>
                </li>
                <li className="kv-row">
                  <span className="k">알림구분</span>
                  <span className="v">{ detail?.alimCl }</span>
                </li>
                <li className="kv-row">
                  <span className="k">발송구분</span>
                  <span className="v">{ detail?.sendCl }</span>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </main>
    </>
  );
};