import moment from 'moment';
import { NumericFormat } from 'react-number-format';
import { SectionTitleArrow } from '@/entities/common/ui/section-title-arrow';
import { InfoSectionKeys, InfoSectionProps, TransactionCategory } from '../../model/types';
import { SlideDown } from 'react-slidedown';
import 'react-slidedown/lib/slidedown.css';

export const SettlementInfoSection = ({
  transactionCategory,
  settlementInfo,
  serviceCode,
  isOpen,
  onClickToOpenInfo
}: InfoSectionProps) => {
  
  const subItems: Record<string, Record<string, string>> = {
    approvalSettlementDate: {name: '승인정산일', type: 'date'},
    approvalSettlementAmount: {name: '승인정산금액', type: 'number'},
    cancelSettlementDate: {name: '취소정산일', type: 'date'},
    cancelSettlementAmount: {name: '취소정산금액', type: 'number'},
  };

  const openSubItems: Record<string, Array<string>> = {
    // 신용카드
    '01': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // 계좌이체
    '02': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // 가상계좌
    '03': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // 휴대폰
    '05': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // 문화상품권
    '14': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // SSG머니
    '21': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // SSG은행계좌
    '24': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // 계좌간편결제
    '26': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // 티머니페이
    '31': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
  };

  const checkValue = (val: any) => {
    return (!!val || val === 0);
  };
  let newSettlementInfo: Record<string, any> | undefined = settlementInfo;
  const subLi = () => {
    let rs = [];

    if(!!newSettlementInfo && !!serviceCode && !!openSubItems[serviceCode]){
      for(let i=0;i<openSubItems[serviceCode].length;i++){
        let k = openSubItems[serviceCode][i];
        if(!!k){
          rs.push(
            <li 
              key={`key-important-item-${i}`}
              className="kv-row"
            >
              <span className="k">·&nbsp;&nbsp;{ subItems[k]?.name }</span>
              <span className="v">
              { (checkValue(newSettlementInfo[k]) && subItems[k]?.type === 'string') &&
                newSettlementInfo[k] 
              }
              { (checkValue(newSettlementInfo[k]) && subItems[k]?.type === 'number') &&
                <NumericFormat
                  value={ newSettlementInfo[k] } 
                  thousandSeparator
                  displayType="text"
                  suffix='원'
                ></NumericFormat>
              }
              { (checkValue(newSettlementInfo[k]) && subItems[k]?.type === 'date') &&
                moment(newSettlementInfo[k]).format('YYYY.MM.DD')
              }
              </span>
            </li>
          );
        }
      }
    }
    return rs;
  };

  const onClickToSetOpenInfo = () => {
    if(!!onClickToOpenInfo){
      onClickToOpenInfo(InfoSectionKeys.Settlement);
    }
  };
  
  return (
    <>
      <div className="txn-section">
        <div 
          className="section-title with-toggle"
          onClick={ () => onClickToSetOpenInfo() }
        >
          정산 정보 <SectionTitleArrow isOpen={ isOpen }></SectionTitleArrow>
        </div>
        
        <SlideDown className={'my-dropdown-slidedown'}>
        { !!isOpen &&
          <ul className="kv-list">
          { (transactionCategory === TransactionCategory.AllTransaction) && 
            subLi()
          }
          { (transactionCategory === TransactionCategory.Escrow) && 
            <>
              <li className="kv-row">
                <span className="k">·&nbsp;&nbsp;승인정산일</span>
                <span className="v">{ settlementInfo?.approvalSettlementDate? moment(settlementInfo?.approvalSettlementDate).format('YYYY.MM.DD'): '' }</span>
              </li>
              <li className="kv-row">
                <span className="k">·&nbsp;&nbsp;승인정산금액</span>
                <span className="v">
                  <NumericFormat
                    value={ settlementInfo?.approvalSettlementAmount } 
                    thousandSeparator
                    displayType="text"
                    suffix='원'
                  ></NumericFormat>
                </span>
              </li>
              <li className="kv-row">
                <span className="k">·&nbsp;&nbsp;취소정산일</span>
                <span className="v">{ settlementInfo?.cancelSettlementDate? moment(settlementInfo?.cancelSettlementDate).format('YYYY.MM.DD'): '' }</span>
              </li>
              <li className="kv-row">
                <span className="k">·&nbsp;&nbsp;취소정산금액</span>
                <span className="v">
                  <NumericFormat
                    value={ settlementInfo?.cancelSettlementAmount } 
                    thousandSeparator
                    displayType="text"
                    suffix='원'
                  ></NumericFormat>
                </span>
              </li>
            </>
          }
          </ul>
        }
        </SlideDown>
      </div>
    </>
  )
};