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 { useExtensionPayoutDetailMutation } from '@/entities/additional-service/api/payout/use-extension-payout-detail-mutation';
import { useLocation } from 'react-router';
import { ExtensionPayoutDetailDownloadCertificateParams, ExtensionPayoutDetailDownloadCertificateResponse, ExtensionPayoutDetailParams, ExtensionPayoutDetailResponse } from '@/entities/additional-service/model/payout/types';
import { useEffect, useState } from 'react';
import { NumericFormat } from 'react-number-format';
import { useExtensionPayoutDetailDownloadCertificateMutation } from '@/entities/additional-service/api/payout/use-extension-payout-detail-download-cetificate-mutation';

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

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

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

  const { mutateAsync: extensionPayoutDetail } = useExtensionPayoutDetailMutation();
  const { mutateAsync: extensionPayoutDetailDownloadCertification } = useExtensionPayoutDetailDownloadCertificateMutation();

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

    extensionPayoutDetail(params).then((rs: ExtensionPayoutDetailResponse) => {
      setDetail(rs);
    });
  };

  useSetHeaderTitle('지급대행 상세');
  useSetHeaderType(HeaderType.LeftArrow);
  useSetFooterMode(false);
  useSetOnBack(() => {
    navigate(PATHS.additionalService.payout.list);
  });

  const onClickToDownload = () => {
    let params: ExtensionPayoutDetailDownloadCertificateParams = {
      tid: tid,
      mid: mid,
    };
    extensionPayoutDetailDownloadCertification(params).then((rs: ExtensionPayoutDetailDownloadCertificateResponse) => {
      console.log(rs);
    });
  };

  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?.disbursementAmount } 
                    thousandSeparator
                    displayType="text"
                    suffix='원'
                  ></NumericFormat>
                </span>
              </div>
              <div className="num-store">나이스테스트가맹점</div>
              <div className="num-day">2025.08.19</div>
              <div className="receipt-row">
                <button
                  className="receipt-btn"
                  type="button"
                  onClick={ onClickToDownload }
                >
                  <span className="icon-24 download"></span>
                  <span>입출금 확인증</span>
                </button>
              </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?.disbursementStatus }</span>
                </li>
                <li className="kv-row">
                  <span className="k">거래유형</span>
                  <span className="v">{ detail?.transTypeName }</span>
                </li>
                <li className="kv-row">
                  <span className="k">요청일</span>
                  <span className="v">{ detail?.requestDate }</span>
                </li>
                <li className="kv-row">
                  <span className="k">지급일시</span>
                  <span className="v">{ detail?.settlementDate }</span>
                </li>
                <li className="kv-row">
                  <span className="k">사업자번호</span>
                  <span className="v">{ detail?.companyNo }</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?.bankName }</span>
                </li>
                <li className="kv-row">
                  <span className="k">계좌번호</span>
                  <span className="v">{ detail?.accountNo }</span>
                </li>
                <li className="kv-row">
                  <span className="k">입금인자</span>
                  <span className="v">{ detail?.depositName }</span>
                </li>
                <li className="kv-row">
                  <span className="k">실패사유</span>
                  <span className="v">{ detail?.failReason }</span>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </main>
    </>
  );
};