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 } from '@/entities/additional-service/model/fund-account/types';
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) => {
      setDetail(rs);
    });
  };

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

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

  const onClickToRequest = () =>{
    let params: ExtensionFundAccountTransferRequestParams = {
      mid: mid,
      transferAmount: detail?.transferAmount,
      receiveBankCode: detail?.receiveBankName,
      receiveAccountNo: detail?.receiveAccountNo,
      receiveAccountName: detail?.receiveAccountName,
      transferMemo: ''
    };
    extensionFundAccountTransferRequest(params).then((rs: ExtensionFundAccountTransferRequestResponse) => {
      console.log(rs)
      alert(rs.message);
      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?.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">{ detail?.status }</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?.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>
                
              </ul>
            </div>
            <div className="apply-row">
              <button 
                className="btn-50 btn-blue flex-1"
                onClick={ () => onClickToRequest() }
              >이체 요청</button>
            </div>
          </div>
        </div>
      </main>
    </>
  );
};