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

export const PartCancelInfoWrap = ({
  transactionCategory,
  partCancelInfo,
  serviceCode,
  isOpen,
  onClickToOpenInfo
}: DetailInfoProps) => {
  
  const subItems: Record<string, Record<string, string>> = {
    originalTid: {name: '원거래 TID', type: 'string'},
    originalAmount: {name: '원거래 금액', type: 'number'},
    partCancelTid: {name: (serviceCode === '05')? '재승인 TID': '부분취소 TID', type: 'string'},
    partCancelAmount: {name: '부분취소 금액', type: 'number'},
    remainingAmount: {name: (serviceCode === '05')? '재승인 금액': '부분취소 후 잔액', 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(InfoWrapKeys.PartCancel);
    }
  };
  
  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">
          { subLi() }
          </ul>
        }
        </SlideDown>
       
      </div>
    </>
  )
};