import { useEffect, useState } from 'react';
import { motion } from 'framer-motion';
import { IMAGE_ROOT } from '@/shared/constants/common';
import { FilterSelect } from '@/shared/ui/filter/select';
import { FilterCalendar } from '@/shared/ui/filter/calendar';
import { FilterButtonGroups } from '@/shared/ui/filter/button-groups';
import { FilterRangeAmount } from '@/shared/ui/filter/range-amount';
import { 
  FilterMotionDuration, 
  FilterMotionStyle, 
  FilterMotionVariants
} from '@/entities/common/model/constant';
import moment from 'moment';
import { FilterInput } from '@/shared/ui/filter/input';
import { OrderStatus, PaymentStatus } from '../../model/ars/types';
import { ArsOrderStatusBtnGroup, ArsPaymentStatusBtnGroup } from '../../model/ars/constant';

export interface AlimtalkFilterProps {
  filterOn: boolean;
  setFilterOn: (filterOn: boolean) => void;
  mid: string;
  moid: string;
  fromDate: string;
  toDate: string;
  paymentStatus: PaymentStatus;
  orderStatus: OrderStatus;
  minAmount?: number;
  maxAmount?: number;
  setMid: (mid: string) => void;
  setMoid: (moid: string) => void;
  setFromDate: (fromDate: string) => void;
  setToDate: (toDate: string) => void;
  setPaymentStatus: (paymentStatus: PaymentStatus) => void;
  setOrderStatus: (orderStatus: OrderStatus) => void;
  setMinAmount: (minAmount?: number) => void;
  setMaxAmount: (maxAmount?: number) => void;
};

export const AlimtalkFilter = ({
  filterOn,
  setFilterOn,
  mid,
  moid,
  fromDate,
  toDate,
  paymentStatus,
  orderStatus,
  minAmount,
  maxAmount,
  setMid,
  setMoid,
  setFromDate,
  setToDate,
  setPaymentStatus,
  setOrderStatus,
  setMinAmount,
  setMaxAmount
}: AlimtalkFilterProps) => {

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterMoid, setFilterMoid] = useState<string>(moid);
  const [filterFromDate, setFilterFromDate] = useState<string>(moment(fromDate).format('YYYY.MM.DD'));
  const [filterToDate, setFilterToDate] = useState<string>(moment(toDate).format('YYYY.MM.DD'));
  const [filterPaymentStatus, setFilterPaymentStatus] = useState<PaymentStatus>(paymentStatus);
  const [filterOrderStatus, setFilterOrderStatus] = useState<OrderStatus>(orderStatus);
  const [filterMinAmount, setFilterMinAmount] = useState<number | undefined>(minAmount);
  const [filterMaxAmount, setFilterMaxAmount] = useState<number | undefined>(maxAmount);
  
  const onClickToClose = () => {
    setFilterOn(false);
  };

  const onClickToSetFilter = () => {
    setMid(filterMid);
    setFilterMoid(filterMoid);
    setFromDate(filterFromDate);
    setToDate(filterToDate);
    setPaymentStatus(filterPaymentStatus);
    setOrderStatus(filterOrderStatus);
    setMinAmount(filterMinAmount);
    setMaxAmount(filterMaxAmount);
    onClickToClose();
  };

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

  useEffect(() => {
    setFilterPaymentStatus(paymentStatus);
  }, [paymentStatus]);

  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>
            <FilterInput
              title='주문번호'
              inputValue={ filterMoid }
              inputSetter={ setFilterMoid }
            ></FilterInput>
            <FilterCalendar
              title='조회기간'
              startDate={ filterFromDate }
              endDate={ filterToDate }
              setStartDate={ setFilterFromDate }
              setEndDate={ setFilterToDate }
            ></FilterCalendar>
            <FilterButtonGroups
              title='결제상태'
              activeValue={ filterPaymentStatus }
              btnGroups={ ArsPaymentStatusBtnGroup }
              setter={ setFilterPaymentStatus }
            ></FilterButtonGroups>
            <FilterButtonGroups
              title='주문상태'
              activeValue={ filterOrderStatus }
              btnGroups={ ArsOrderStatusBtnGroup }
              setter={ setFilterOrderStatus }
            ></FilterButtonGroups>
            <FilterRangeAmount
              title='거래금액'
              minAmount={ filterMinAmount }
              maxAmount={ filterMaxAmount }
              setMinAmount={ setFilterMinAmount }
              setMaxAmount={ setFilterMaxAmount }
            ></FilterRangeAmount>
          </div>
          <div className="apply-row">
            <button 
              className="btn-50 btn-blue flex-1"
              onClick={ () => onClickToSetFilter() }
            >적용</button>
          </div>
        </div>
      </motion.div>
    </>
  );
};