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 { NumericFormat } from 'react-number-format';
import { 
  ExtensionFundAccountResultDetailParams, 
  ExtensionFundAccountResultDetailResponse, 
} from '@/entities/additional-service/model/fund-account/types';
import moment from 'moment';
import { useExtensionFundAccountResultDetailMutation } from '@/entities/additional-service/api/fund-account/use-extension-fund-account-result-detail-mutation';

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

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

  const [detail, setDetail] = useState<ExtensionFundAccountResultDetailResponse>();

  const { mutateAsync: extensionFundAccountResultDetail } = useExtensionFundAccountResultDetailMutation();
  
  const callDetail = () => {
    let params: ExtensionFundAccountResultDetailParams = {
      tid: tid,
      mid: mid,
    };

    extensionFundAccountResultDetail(params).then((rs: ExtensionFundAccountResultDetailResponse) => {
      setDetail(rs);
    });
  };

  useSetHeaderTitle('자금이체 상세');
  useSetHeaderType(HeaderType.LeftArrow);
  useSetFooterMode(false);
  useSetOnBack(() => {
    navigate(PATHS.additionalService.fundAccount.resultList);
  });

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

  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">
                  <NumericFormat
                    value={ detail?.transferAmount } 
                    thousandSeparator
                    displayType="text"
                    suffix='원'
                  ></NumericFormat>
                </span>
              </div>
              <div className="num-store">{ detail?.receiveAccountName }({ detail?.receiveAccountNo })</div>
              <div className="num-day">{ moment(detail?.processDate).format('YYYY.MM.DD') }</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">요청일시</span>
                  <span className="v">{ moment(detail?.processDate).format('YYYY.MM.DD') }</span>
                </li>
                <li className="kv-row">
                  <span className="k">이체일시</span>
                  <span className="v">{ moment(detail?.requestDate).format('YYYY.MM.DD') }</span>
                </li>
                <li className="kv-row">
                  <span className="k">이체결과</span>
                  <span className="v">{ detail?.status }</span>
                </li>
                <li className="kv-row">
                  <span className="k">실패사유</span>
                  <span className="v">{ detail?.failReason }</span>
                </li>
                <li className="kv-row">
                  <span className="k">수취인명</span>
                  <span className="v">{ detail?.receiveAccountName }</span>
                </li>
                <li className="kv-row">
                  <span className="k">은행</span>
                  <span className="v">{ detail?.receiveBankName }</span>
                </li>
                <li className="kv-row">
                  <span className="k">계좌번호</span>
                  <span className="v">{ detail?.receiveAccountNo }</span>
                </li>
                <li className="kv-row">
                  <span className="k">MID</span>
                  <span className="v">{ mid }</span>
                </li>
                <li className="kv-row">
                  <span className="k">주문번호</span>
                  <span className="v">{ '주문번호' }</span>
                </li>
                <li className="kv-row">
                  <span className="k">TID</span>
                  <span className="v">{ detail?.bankTid }</span>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </main>
    </>
  );
};