import { useEffect, useState } from 'react';
import { FundAccountReceiveAccountNameNo, FundAccountStatus } from '../../model/fund-account/types';
import moment from 'moment';
import { motion } from 'framer-motion';
import { FilterMotionDuration, FilterMotionStyle, FilterMotionVariants } from '@/entities/common/model/constant';
import { IMAGE_ROOT } from '@/shared/constants/common';
import { FilterSelect } from '@/shared/ui/filter/select';
import { FilterCalendar } from '@/shared/ui/filter/calendar';
import { FundAccountReceiveAccountNameNoOptionsGroup, FundAccountReceiveBankCodeOptionsGroup, FundAccountStatusBtnGroup } from '../../model/fund-account/constant';
import { FilterButtonGroups } from '@/shared/ui/filter/button-groups';
import { FilterSelectInput } from '@/shared/ui/filter/select-input';

export interface FundAccountTransactionFilterProps {
  filterOn: boolean;
  setFilterOn: (filterOn: boolean) => void;
  mid: string;
  receiveAccountName: string;
  receiveAccountNo: string;
  fromDate: string;
  toDate: string;
  receiveBankCode: string;
  status: FundAccountStatus;
  setMid: (mid: string) => void;
  setReceiveAccountName: (receiveAccountName: string) => void;
  setReceiveAccountNo: (receiveAccountNo: string) => void;
  setFromDate: (fromDate: string) => void;
  setToDate: (toDate: string) => void;
  setReceiveBankCode: (receiveBankCode: string) => void;
  setStatus: (status: FundAccountStatus) => void;
};
export const FundAccountTransactionFilter = ({
  filterOn,
  setFilterOn,
  mid,
  receiveAccountName,
  receiveAccountNo,
  fromDate,
  toDate,
  receiveBankCode,
  status,
  setMid,
  setReceiveAccountName,
  setReceiveAccountNo,
  setFromDate,
  setToDate,
  setReceiveBankCode,
  setStatus
}: FundAccountTransactionFilterProps) => {

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterReceiveAccountName, setFilterReceiveAccountName] = useState<string>(receiveAccountName);
  const [filterReceiveAccountNo, setFilterReceiveAccountNo] = useState<string>(receiveAccountNo);
  const [filterFromDate, setFilterFromDate] = useState<string>(moment(fromDate).format('YYYY.MM.DD'));
  const [filterToDate, setFilterToDate] = useState<string>(moment(toDate).format('YYYY.MM.DD'));
  const [filterReceiveBankCode, setFilterReceiveBankCode] = useState<string>(receiveBankCode);
  const [filterStatus, setFilterStatus] = useState<FundAccountStatus>(status);
  const [
    filterReceiveAccountNameNoSelectValue,
    setFilterReceiveAccountNameNoSelectValue
  ] = useState<FundAccountReceiveAccountNameNo>(FundAccountReceiveAccountNameNo.ReceiveAccountName);
  const [
    filterReceiveAccountNameNoInputValue, 
    setFilterReceiveAccountNameNoInputValue
  ] = useState<string>('');

  const onClickToClose = () => {
    setFilterOn(false);
  };

  const onClickToSetFilter = () => {
    setMid(filterMid);
    if(filterReceiveAccountNameNoSelectValue === FundAccountReceiveAccountNameNo.ReceiveAccountName){
      setReceiveAccountName(filterReceiveAccountNameNoInputValue);
      setReceiveAccountNo('');
    }
    else if(filterReceiveAccountNameNoSelectValue === FundAccountReceiveAccountNameNo.ReceiveAccountNo){
      setReceiveAccountName('');
      setReceiveAccountNo(filterReceiveAccountNameNoInputValue);  
    }
    
    setFromDate(filterFromDate);
    setToDate(filterToDate);
    setReceiveBankCode(filterReceiveBankCode);
    setStatus(filterStatus);
    onClickToClose();
  };

  let MidOptions = [
    {name: 'nictest001m', value: 'nictest001m'}
  ];

  useEffect(() => {
    setFilterStatus(status);
  }, [status]);

  return (
    <>
      <motion.div 
        className="full-menu-modal" 
        initial="hidden"
        animate={ (filterOn)? 'visible': 'hidden' }
        variants={ FilterMotionVariants }
        transition={ FilterMotionDuration }
        style={ FilterMotionStyle }
      >
        <div className="full-menu-container">
          <div className="full-menu-header">
            <div className="full-menu-title center">필터</div>
            <div className="full-menu-actions">
              <button 
                id="closeFullMenu"
                className="full-menu-close"
              >
                <img 
                  src={ IMAGE_ROOT + '/ico_close.svg' }
                  alt="닫기"
                  onClick={ () => onClickToClose() }
                />
              </button>
            </div>
          </div>
          <div className="option-list pt-16">
            <FilterSelect
              title='가맹점'
              selectValue={ filterMid }
              selectSetter={ setFilterMid }
              selectOptions={ MidOptions }
            ></FilterSelect>
            <FilterSelectInput
              title='수취인/계좌번호'
              selectValue={ filterReceiveAccountNameNoSelectValue }
              selectSetter={ setFilterReceiveAccountNameNoSelectValue }
              selectOptions={ FundAccountReceiveAccountNameNoOptionsGroup }
              inputValue={ filterReceiveAccountNameNoInputValue }
              inputSetter={ setFilterReceiveAccountNameNoInputValue }
            ></FilterSelectInput>
            <FilterCalendar
              title='조회기간'
              startDate={ filterFromDate }
              endDate={ filterToDate }
              setStartDate={ setFilterFromDate }
              setEndDate={ setFilterToDate }
            ></FilterCalendar>
            <FilterSelect
              title='은행'
              selectValue={ filterReceiveBankCode }
              selectSetter={ setFilterReceiveBankCode }
              selectOptions={ FundAccountReceiveBankCodeOptionsGroup }
            ></FilterSelect>
            <FilterButtonGroups
              title='처리 결과'
              activeValue={ filterStatus }
              btnGroups={ FundAccountStatusBtnGroup }
              setter={ setFilterStatus }
            ></FilterButtonGroups>
          </div>
          <div className="apply-row">
            <button 
              className="btn-50 btn-blue flex-1"
              onClick={ () => onClickToSetFilter() }
            >적용</button>
          </div>
        </div>
      </motion.div>
    </>
  );
};