import { useEffect, useState } from 'react';
import { motion } from 'framer-motion';
import { IMAGE_ROOT } from '@/shared/constants/common';
import { 
  SettlementPaymentMethod,
  SettlementPeriodType
} from '../../model/types';
import { 
  FilterMotionDuration, 
  FilterMotionStyle, 
  FilterMotionVariants
} from '@/entities/common/model/constant';
import { FilterSelect } from '@/shared/ui/filter/select';
import { FilterCalendar } from '@/shared/ui/filter/calendar';
import { SettlementPaymentMethodOptionsGroup, SettlementPeriodTypeBtnGroup } from '../../model/constant';
import { FilterButtonGroups } from '@/shared/ui/filter/button-groups';

export interface ListFilterProps {
  filterOn: boolean;
  setFilterOn: (filterOn: boolean) => void;
  mid: string;
  periodType: SettlementPeriodType;
  startDate: string;
  endDate: string;
  paymentMethod: SettlementPaymentMethod;
  setMid: (mid: string) => void;
  setPeriodType: (periodType: SettlementPeriodType) => void;
  setStartDate: (startDate: string) => void;
  setEndDate: (endDate: string) => void;
  setPaymentMethod: (paymentMethod: SettlementPaymentMethod) => void;
};

export const ListFilter = ({
  filterOn,
  setFilterOn,
  mid,
  periodType,
  startDate,
  endDate,
  paymentMethod,
  setMid,
  setPeriodType,
  setStartDate,
  setEndDate,
  setPaymentMethod
}: ListFilterProps) => {

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterPeriodType, setFilterPeriodType] = useState<SettlementPeriodType>(periodType);
  const [filterStartDate, setFilterStartDate] = useState<string>(startDate);
  const [filterEndDate, setFilterEndDate] = useState<string>(endDate);
  const [filterPaymentMethod, setFilterPaymentMethod] = useState<SettlementPaymentMethod>(paymentMethod);
  
  const onClickToClose = () => {
    setFilterOn(false);
  };

  const onClickToSetFilter = () => {
    setMid(filterMid);
    setPeriodType(filterPeriodType);
    setStartDate(filterStartDate);
    setEndDate(filterEndDate);
    setPaymentMethod(filterPaymentMethod);
    onClickToClose();
  };

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

  useEffect(() => {
    setFilterPeriodType(periodType);
  }, [periodType]);

  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>
            <FilterButtonGroups
              title='조회기준'
              activeValue={ filterPeriodType }
              btnGroups={ SettlementPeriodTypeBtnGroup }
              setter={ setFilterPeriodType }
            ></FilterButtonGroups>
            <FilterCalendar
              title='조회기간'
              startDate={ filterStartDate }
              endDate={ filterEndDate }
              setStartDate={ setFilterStartDate }
              setEndDate={ setFilterEndDate }
            ></FilterCalendar>
            <FilterSelect
              title='결제수단'
              selectValue={ filterPaymentMethod }
              selectSetter={ setFilterPaymentMethod }
              selectOptions={ SettlementPaymentMethodOptionsGroup }
            ></FilterSelect>
          </div>
          <div className="apply-row">
            <button 
              className="btn-50 btn-blue flex-1"
							onClick={ () => onClickToSetFilter() }
            >적용</button>
          </div>
        </div>
      </motion.div>
    </>
  );
};