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 { useExtensionFundAccountTransferDetailMutation } from '@/entities/additional-service/api/fund-account/use-extension-fund-account-transfer-detail-mutation';
import { ExtensionFundAccountTransferDetailParams, ExtensionFundAccountTransferDetailResponse, ExtensionFundAccountTransferRequestParams, ExtensionFundAccountTransferRequestResponse, FundAccountStatus } from '@/entities/additional-service/model/fund-account/types';
import { getFundAccountStatusName } from '@/entities/additional-service/model/fund-account/constant';
import moment from 'moment';
import { useExtensionFundAccountTransferRequestMutation } from '@/entities/additional-service/api/fund-account/use-extension-fund-account-transfer-request-mutation';

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

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

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

  const { mutateAsync: extensionFundAccountTransferDetail } = useExtensionFundAccountTransferDetailMutation();
  const { mutateAsync: extensionFundAccountTransferRequest } = useExtensionFundAccountTransferRequestMutation();

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

    extensionFundAccountTransferDetail(params).then((rs: ExtensionFundAccountTransferDetailResponse) => {
      console.log("FundAccountTransferDetail : " + rs.registDate)
      setDetail(rs);
    });
  };

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

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

  const onClickToRequest = () => {
    if (!detail) {
      alert('상세 정보를 불러오는 중입니다.');
      return;
    }

    let params: ExtensionFundAccountTransferRequestParams = {
      mid: mid,
      bankCode: detail.bankCode || '',
      accountNo: detail.accountNo || '',
      accountName: detail.accountName || '',
      amount: detail.amount || 0,
      moid: detail.moid || ''
    };
    extensionFundAccountTransferRequest(params).then((rs: ExtensionFundAccountTransferRequestResponse) => {
      console.log(rs)
      alert(rs.status ? '이체 요청이 완료되었습니다.' : '이체 요청에 실패했습니다.');
      navigate(PATHS.additionalService.fundAccount.transferList);
    });
  };

  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?.amount}
                    thousandSeparator
                    displayType="text"
                    suffix='원'
                  ></NumericFormat>
                </span>
              </div>
              <div className="num-store">{detail?.amount}({detail?.accountName})</div>
              <div className="num-day">
                {detail?.registDate ? moment(detail.registDate, 'YYYYMMDDHHmmss').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">
                    {detail?.registDate ? moment(detail.registDate, 'YYYYMMDDHHmmss').format('YYYY.MM.DD HH:mm:ss') : '-'}
                  </span>
                </li>
                <li className="kv-row">
                  <span className="k">처리결과</span>
                  <span className="v">{getFundAccountStatusName(detail?.resultStatus) || '-'}</span>
                </li>
                <li className="kv-row">
                  <span className="k">요청일자</span>
                  <span className="v">
                    {detail?.requestDate ? moment(detail.requestDate, 'YYYYMMDDHHmmss').format('YYYY.MM.DD HH:mm:ss') : '-'}
                  </span>
                </li>
                <li className="kv-row">
                  <span className="k">이체결과</span>
                  <span className="v">{detail?.resultMessage || '-'}</span>
                </li>
                <li className="kv-row">
                  <span className="k">수취인명</span>
                  <span className="v">{detail?.accountName}</span>
                </li>
                <li className="kv-row">
                  <span className="k">은행</span>
                  <span className="v">{detail?.bankCode}</span>
                </li>
                <li className="kv-row">
                  <span className="k">계좌번호</span>
                  <span className="v">{detail?.accountNo}</span>
                </li>
                <li className="kv-row">
                  <span className="k">MID</span>
                  <span className="v">{detail?.mid}</span>
                </li>

              </ul>
            </div>
            <div className="apply-row">
              <button
                className="btn-50 btn-blue flex-1"
                onClick={() => onClickToRequest()}
                disabled={detail?.resultStatus !== FundAccountStatus.REGIST_COMPLETE}
              >이체 요청</button>
            </div>
          </div>
        </div>
      </main>
    </>
  );
};