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

export const PartCancelInfoSection = ({
  transactionCategory,
  partCancelInfo,
  serviceCode,
  isOpen,
  onClickToOpenInfo
}: InfoSectionProps) => {
  const { t } = useTranslation();
  
  const subItems: Record<string, Record<string, string>> = {
    originalTid: {name: t('transaction.fields.originalTid'), type: 'string'},
    originalAmount: {name: t('transaction.fields.originalAmount'), type: 'number'},
    partCancelTid: {name: (serviceCode === '05')? t('transaction.fields.reApprovalTid'): t('transaction.fields.partCancelTid'), type: 'string'},
    partCancelAmount: {name: t('transaction.fields.partCancelAmount'), type: 'number'},
    remainingAmount: {name: (serviceCode === '05')? t('transaction.fields.reApprovalAmount'): t('transaction.fields.remainingAmount'), type: 'number'},
  };
  
  const openSubItems: Record<string, Array<string>> = {
    // 신용카드
    '01': ['originalTid', 'originalAmount', 'partCancelTid', 
      'partCancelAmount', 'remainingAmount'],
    // 계좌이체
    '02': ['originalTid', 'originalAmount', 'partCancelTid', 
      'partCancelAmount', 'remainingAmount'],
    // 가상계좌
    '03': ['originalTid', 'originalAmount', 'partCancelTid', 
      'partCancelAmount', 'remainingAmount'],
    // 휴대폰
    '04': ['originalTid', 'originalAmount', 'partCancelTid', 
      'partCancelAmount', 'remainingAmount'],
    // 계좌간편결제
    '26': ['originalTid', 'originalAmount', 'partCancelTid', 
      'partCancelAmount', 'remainingAmount'],
    // SSG머니
    '21': ['originalTid', 'originalAmount', 'partCancelTid', 
      'partCancelAmount', 'remainingAmount'],
    // SSG은행계좌
    '24': ['originalTid', 'originalAmount', 'partCancelTid', 
      'partCancelAmount', 'remainingAmount'],
    // 문화상품권
    '14': ['originalTid', 'originalAmount', 'partCancelTid', 
      'partCancelAmount', 'remainingAmount'],
    // 티머니페이
    '31': ['originalTid', 'originalAmount', 'partCancelTid', 
      'partCancelAmount', 'remainingAmount'],
  };

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

    if(!!newPartCancelInfo && !!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(newPartCancelInfo[k]) && subItems[k]?.type === 'string') &&
                newPartCancelInfo[k] 
              }
              { (checkValue(newPartCancelInfo[k]) && subItems[k]?.type === 'number') &&
                <NumericFormat
                  value={ newPartCancelInfo[k] } 
                  thousandSeparator
                  displayType="text"
                  suffix='원'
                ></NumericFormat>
              }
              { (checkValue(newPartCancelInfo[k]) && subItems[k]?.type === 'date') &&
                moment(newPartCancelInfo[k]).format('YYYY.MM.DD')
              }
              </span>
            </li>
          );
        }
      }
    }
    return rs;
  };

  const onClickToSetOpenInfo = () => {
    if(!!onClickToOpenInfo){
      onClickToOpenInfo(InfoSectionKeys.PartCancel);
    }
  };
  
  return (
    <>
      <div className="txn-section">
        <div
          className="section-title with-toggle"
          onClick={ () => onClickToSetOpenInfo() }
        >
          { t('transaction.sections.partCancelInfo') } <SectionTitleArrow isOpen={ isOpen }></SectionTitleArrow>
        </div>
        <SlideDown className={'my-dropdown-slidedown'}>
        { !!isOpen &&
          <ul className="kv-list">
          { subLi() }
          </ul>
        }
        </SlideDown>

      </div>
    </>
  )
};