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 { FilterRangeAmount } from '@/shared/ui/filter/range-amount';
import { 
  EscrowDeliveryStatusBtnGroup,
  EscrowSettlementStatusBtnGroup
} from '@/entities/transaction/model/contant';
import { 
  EscrowFilterProps,
  EscrowSearchType,
  EscrowDeliveryStatus,
  EscrowSettlementStatus
} from '../../model/types';

export const EscrowFilter = ({
  filterOn,
  setFilterOn,
  mid,
  searchType,
  searchKeyword,
  startDate,
  endDate,
  deliveryStatus,
  settlementStatus,
  minAmount,
  maxAmount,
  setMid,
  setSearchType,
  setSearchKeyword,
  setStartDate,
  setEndDate,
  setDeliveryStatus,
  setSettlementStatus,
  setMinAmount,
  setMaxAmount
}: EscrowFilterProps) => {

  const [filterMid, setFilterMid] = useState<string>(mid);
  const [filterSearchType, setFilterSearchType] = useState<EscrowSearchType>(searchType);
  const [filterSearchKeyword, setFilterSearchKeyword] = useState<string>(searchKeyword);
  const [filterStartDate, setFilterStartDate] = useState<string>(startDate);
  const [filterEndDate, setFilterEndDate] = useState<string>(endDate);
  const [filterDeliveryStatus, setFilterDeliveryStatus] = useState<EscrowDeliveryStatus>(deliveryStatus);
  const [filterSettlementStatus, setFilterSettlementStatus] = useState<EscrowSettlementStatus>(settlementStatus);
  const [filterMinAmount, setFilterMinAmount] = useState<number | string>(minAmount || '');
  const [filterMaxAmount, setFilterMaxAmount] = useState<number | string>(maxAmount || '');
  
  const variants = {
    hidden: { x: '100%' },
    visible: { x: '0%' },
  };

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

  const onClickToSetFilter = () => {
    setMid(filterMid);
    setSearchType(filterSearchType);
    setSearchKeyword(filterSearchKeyword);
    setStartDate(filterStartDate);
    setEndDate(filterEndDate);
    setDeliveryStatus(filterDeliveryStatus);
    setSettlementStatus(filterSettlementStatus);
    setMinAmount(filterMinAmount);
    setMaxAmount(filterMaxAmount);
    onClickToClose();
  };
  let MidOptions = [
    {name: 'nictest001m', value: 'nictest001m'}
  ];
  let SearchTypeOptions = [
    {name: '주문번호', value: EscrowSearchType.ORDER_NUMBER },
    {name: 'TID', value: EscrowSearchType.TID }
  ];

  useEffect(() => {
    setFilterDeliveryStatus(deliveryStatus);
  }, [deliveryStatus]);

  return (
    <>
      <motion.div 
        id="fullMenuModal" 
        className="full-menu-modal" 
        initial="hidden"
        animate={ (filterOn)? 'visible': 'hidden' }
        variants={ variants }
        transition={{ duration: 0.3 }}
        style={{
          width: '100%',
          height: '100%',
        }}
      >
        <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>
            <FilterSelectInput
              title='주문번호/ID'
              selectValue={ filterSearchType }
              selectSetter={ setSearchType }
              selectOptions={ SearchTypeOptions }
              inputValue={ searchKeyword }
              inputSetter={ setSearchKeyword }
            ></FilterSelectInput>
            <FilterCalendar
              title='조회기간'
              startDate={ filterStartDate }
              endDate={ filterEndDate }
              setStartDate={ setFilterStartDate }
              setEndDate={ setFilterEndDate }
            ></FilterCalendar>
            
            <FilterButtonGroups
              title='배송상태'
              activeValue={ filterDeliveryStatus }
              btnGroups={ EscrowDeliveryStatusBtnGroup }
              setter={ setFilterDeliveryStatus }
            ></FilterButtonGroups>
            <FilterButtonGroups
              title='결제수단'
              activeValue={ filterSettlementStatus }
              btnGroups={ EscrowSettlementStatusBtnGroup }
              setter={ setFilterSettlementStatus }
            ></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>
    </>
  );
};