import moment from 'moment';
import { NumericFormat } from 'react-number-format';
import { motion } from 'framer-motion';
import { DetailArrow } from '../detail-arrow';
import { InfoWrapKeys, DetailInfoProps } from '../../model/types';

export const SettlementInfoWrap = ({
  transactionCategory,
  settlementInfo,
  serviceCode,
  show,
  onClickToShowInfo
}: DetailInfoProps) => {
  
  const subItems: Record<string, Record<string, string>> = {
    approvalSettleDate: {name: '승인정산일', type: 'date'},
    approvalSettleAmount: {name: '승인정산금액', type: 'number'},
    cancelSettleDate: {name: '취소정산일', type: 'date'},
    cancelSettleAmount: {name: '취소정산금액', type: 'number'},
  };

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

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

    if(!!newSettlementInfo && !!serviceCode && !!showSubItems[serviceCode]){
      for(let i=0;i<showSubItems[serviceCode].length;i++){
        let k = showSubItems[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 variants = {
    hidden: { height: 0, display: 'none' },
    visible: { height: 'auto', display: 'block' },
  };

  const onClickToSetShowInfo = () => {
    if(!!onClickToShowInfo){
      onClickToShowInfo(InfoWrapKeys.Settlement);
    }
  };
  
  return (
    <>
      <div className="txn-section">
        <div 
          className="section-title with-toggle"
          onClick={ () => onClickToSetShowInfo() }
        >
          정산 정보 <DetailArrow show={ show }></DetailArrow>
        </div>
        <motion.ul 
          className="kv-list"
          initial="hidden"
          animate={ (show)? 'visible': 'hidden' }
          variants={ variants }
          transition={{ duration: 0.3 }}
        >
          { subLi() }
        </motion.ul>
      </div>
    </>
  )
};