import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useLocation } from 'react-router';
import { PATHS } from '@/shared/constants/paths';
import { useNavigate } from '@/shared/lib/hooks/use-navigate';
import { useBillingDetailMutation } from '@/entities/transaction/api/use-billing-detail-mutation';
import { BillingInfoSection } from '@/entities/transaction/ui/section/billing-info-section';
import { HeaderType } from '@/entities/common/model/types';
import { 
  TransactionCategory,
  BillingDetailParams,
  BillingDetailResponse,
  BillingInfo,
  AmountInfo
} from '@/entities/transaction/model/types';
import {
  useSetOnBack, 
  useSetHeaderTitle,
  useSetHeaderType,
  useSetFooterMode
} from '@/widgets/sub-layout/use-sub-layout';
import { NumericFormat } from 'react-number-format';
import { C } from 'node_modules/react-router/dist/development/index-react-server-client-DRhjXpk2.mjs';
import { AmountInfoSection } from '@/entities/transaction/ui/section/amount-info-section';

export const BillingDetailPage = () => {
  const { navigate } = useNavigate();
  const { t, i18n } = useTranslation();
  const location = useLocation();
  const tid = location?.state.tid;
  const serviceCode = location?.state.serviceCode;

    
  const [billingInfo, setBillingInfo] = useState<BillingInfo>();
  const [amountInfo, setAmountInfo] = useState<AmountInfo>();

  useSetHeaderTitle(t('billing.detailTitle'));
  useSetHeaderType(HeaderType.RightClose);
  useSetOnBack(() => {
    navigate(PATHS.transaction.billing.list);
  });
  useSetFooterMode(false);

  const { mutateAsync: billingDetail } = useBillingDetailMutation();
    
  const callDetail = () => {
    let billingDetailParams: BillingDetailParams = {
      tid: tid
    };
    billingDetail(billingDetailParams).then((rs: BillingDetailResponse) => {
      setBillingInfo(rs);
      setAmountInfo({
        transactionAmount: rs.transactionAmount,
        buyerName: rs.buyerName
      })
    });
  };
  useEffect(() => {
    callDetail();
  }, []);
  
  return (
    <>
      <main>
        <div className="tab-content">
          <div className="tab-pane sub active">
            <div className="option-list">
              <div className="txn-detail">
                <div className="txn-num-group">
                  <div className="txn-amount">
                    <div className="value">
                      { i18n.language === 'en' && <span className="unit">{ t('home.currencySymbol') }</span> }
                      <NumericFormat
                        value={ amountInfo?.transactionAmount }
                        thousandSeparator
                        displayType="text"
                      ></NumericFormat>
                      { i18n.language !== 'en' && <span className="unit">{ t('home.currencyWon') }</span> }
                    </div>
                  </div>
                  <div className="txn-mid">
                    <span className="value">{ amountInfo?.buyerName }</span>
                  </div>
                  <div className="txn-doc"></div>
                </div>
                <div className="txn-divider"></div>
                <BillingInfoSection
                  billingInfo={ billingInfo }
                ></BillingInfoSection>
              </div>
            </div>
          </div>
        </div>
      </main>
    </>
  );
};