import { useEffect } from 'react';
import { useState } from 'react';
import { motion } from 'framer-motion';
import { IMAGE_ROOT } from '@/shared/constants/common';
import { FilterSelect } from '@/shared/ui/filter/select';
import { FilterSelectInput } from '@/shared/ui/filter/select-input';
import { FilterCalendar } from '@/shared/ui/filter/calendar';
import { FilterButtonGroups } from '@/shared/ui/filter/button-groups';
import {
  CashReceiptPurposeTypeBtnGroup,
  CashReceiptIssueStatusBtnGroup,
  CashReceiptProcessResultBtnGroup
} from '@/entities/transaction/model/contant';
import { 
  CashReceiptFilterProps,
  CashReceiptPurposeType,
  CashReceiptIssueStatus,
  CashReceiptProcessResult
} from '../../model/types';
import { 
  FilterMotionDuration,
  FilterMotionStyle,
  FilterMotionVariants
} from '@/entities/common/model/constant';

export const CashReceiptFilter = ({
  filterOn,
  setFilterOn,
  mid,
  startDate,
  endDate,
  purposeType,
  issueStatus,
  processResult,
  setMid,
  setStartDate,
  setEndDate,
  setPurposeType,
  setIssueStatus,
  setProcessResult
}: CashReceiptFilterProps) => {

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterStartDate, setFilterStartDate] = useState<string>(startDate);
  const [filterEndDate, setFilterEndDate] = useState<string>(endDate);
  const [filterPurposeType, setFilterPurposeType] = useState<CashReceiptPurposeType>(purposeType);
  const [filterIssueStatus, setFilterIssueStatus] = useState<CashReceiptIssueStatus>(issueStatus);
  const [filterProcessResult, setFilterProcessResult] = useState<CashReceiptProcessResult>(processResult);

  const [filterIssueType, setFilterIssueType] = useState<string>('');
  const [filterIssueNumber, setFilterIssueNumber] = useState<string>('');
  const issueTypeOptionsGroup = [
    {name: '승인번호', value: 'issue'},
    {name: '발행번호', value: 'issue2'},
  ];

  const onClickToClose = () => {
    setFilterOn(false);
  };
  
  const setNewDate = (newDate: any) => {
    console.log(newDate)
  };

  const onClickToSetFilter = () => {
    setMid(filterMid);
    setStartDate(filterStartDate);
    setEndDate(filterEndDate);
    setPurposeType(filterPurposeType);
    setIssueStatus(filterIssueStatus);
    setProcessResult(filterProcessResult);
    onClickToClose();
  };
  let MidOptions = [
    {name: 'nictest001m', value: 'nictest001m'}
  ];
  
  useEffect(() => {
    setFilterIssueStatus(issueStatus);
  }, [issueStatus]);

  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={ setMid }
              selectOptions={ MidOptions }
            ></FilterSelect>
            <FilterCalendar
              title='발행일자'
              startDate={ filterStartDate }
              endDate={ filterEndDate }
              setStartDate={ setFilterStartDate }
              setEndDate={ setFilterEndDate }
            ></FilterCalendar>
            
            <FilterButtonGroups
              title='용도'
              activeValue={ filterPurposeType }
              btnGroups={ CashReceiptPurposeTypeBtnGroup }
              setter={ setFilterPurposeType }
            ></FilterButtonGroups>
            <FilterButtonGroups
              title='거래구분'
              activeValue={ filterIssueStatus }
              btnGroups={ CashReceiptIssueStatusBtnGroup }
              setter={ setFilterIssueStatus }
            ></FilterButtonGroups>
            <FilterButtonGroups
              title='진행상태'
              activeValue={ filterProcessResult }
              btnGroups={ CashReceiptProcessResultBtnGroup }
              setter={ setFilterProcessResult }
            ></FilterButtonGroups>
            <FilterSelectInput
              title='승인번호/발행번호'
              selectValue={ filterIssueType }
              selectSetter={ setFilterIssueType }
              selectOptions={ issueTypeOptionsGroup }
              inputValue={ filterIssueNumber }
              inputSetter={ setFilterIssueNumber }
            ></FilterSelectInput>
          </div>
          <div className="apply-row">
            <button 
              className="btn-50 btn-blue flex-1"
              onClick={ () => onClickToSetFilter() }
            >적용</button>
          </div>
        </div>
      </motion.div>
    </>
  );
};