'use client'

import { useEffect, useRef, useState, useContext } from 'react'
import { useAxios } from '@/hooks/useAxios'
import { useRecoilState, useRecoilValue, useResetRecoilState, useSetRecoilState } from 'recoil'
import { appMessageStore, globalLocaleStore } from '@/store/localeAtom'
import Select from 'react-select'
import KO from '@/locales/ko.json'
import JA from '@/locales/ja.json'
import { stuffSearchState } from '@/store/stuffAtom'
import { isEmptyArray } from '@/util/common-utils'
import dayjs from 'dayjs'
import { useRouter } from 'next/navigation'
import SingleDatePicker from '../common/datepicker/SingleDatePicker'
import { useMessage } from '@/hooks/useMessage'
import { isObjectNotEmpty } from '@/util/common-utils'

import { SessionContext } from '@/app/SessionProvider'

import { QcastContext } from '@/app/QcastProvider'

export default function StuffSearchCondition() {
  const router = useRouter()
  const { session } = useContext(SessionContext)
  const setAppMessageState = useSetRecoilState(appMessageStore)
  const globalLocaleState = useRecoilValue(globalLocaleStore)
  const { getMessage } = useMessage()
  const ref = useRef()
  const ref2 = useRef()
  const { get } = useAxios(globalLocaleState)

  const objectNoRef = useRef(null)
  const saleStoreNameRef = useRef(null)
  const addressRef = useRef(null)
  const objectNameRef = useRef(null)
  const dispCompanyNameRef = useRef(null)
  const receiveUserRef = useRef(null)

  //달력 props 관련 날짜 셋팅
  const [startDate, setStartDate] = useState(dayjs(new Date()).add(-1, 'year').format('YYYY-MM-DD'))
  const [endDate, setEndDate] = useState(dayjs(new Date()).format('YYYY-MM-DD'))

  const rangeDatePickerProps1 = {
    startDate, //시작일
    setStartDate,
  }

  const rangeDatePickerProps2 = {
    startDate: endDate, //종료일
    setStartDate: setEndDate,
  }

  //여기서 선택한 검색조건들을 recoil로 관리
  const resetStuffRecoil = useResetRecoilState(stuffSearchState)
  const [stuffSearch, setStuffSearch] = useRecoilState(stuffSearchState)
  const [objectNo, setObjectNo] = useState('') //물건번호
  const [saleStoreName, setSaleStoreName] = useState('') //판매대리점명
  const [address, setAddress] = useState('') //물건주소
  const [objectName, setObjectName] = useState('') //물건명
  const [dispCompanyName, setDispCompanyName] = useState('') //견적처
  const [schSelSaleStoreId, setSchSelSaleStoreId] = useState('') //판매대리점 선택
  const [receiveUser, setReceiveUser] = useState('') //담당자
  const [dateType, setDateType] = useState('U') //갱신일(U)/등록일(R)
  const [tempFlg, setTempFlg] = useState('') //임시저장여부
  //내물건보기 체크박스
  const [myDataCheck, setMyDataCheck] = useState(false)

  const [schSelSaleStoreList, setSchSelSaleStoreList] = useState([]) //판매대리점 자동완성 SELECT 전체
  const [favoriteStoreList, setFavoriteStoreList] = useState([]) //즐겨찾기한 판매점목록
  const [showSaleStoreList, setShowSaleStoreList] = useState([]) //보여줄 판매점목록

  const [otherSaleStoreList, setOtherSaleStoreList] = useState([]) //1차점 이외 판매점목록
  const [otherSaleStoreId, setOtherSaleStoreId] = useState('')

  const { setIsGlobalLoading } = useContext(QcastContext)

  // 조회
  const onSubmit = () => {
    let diff = dayjs(endDate).diff(startDate, 'day')

    if (diff > 366) {
      return alert(getMessage('stuff.message.periodError'))
    }

    if (isNaN(diff)) {
      return alert(getMessage('stuff.message.periodError'))
    }

    setIsGlobalLoading(true)

    if (stuffSearch.code === 'S') {
      if (stuffSearch.pageNo !== 1) {
        setStuffSearch({
          schObjectNo: objectNo ? objectNo.trim() : stuffSearch.schObjectNo.trim(),
          schSaleStoreName: saleStoreName ? saleStoreName.trim() : '',
          schAddress: address ? address.trim() : '',
          schObjectName: objectName ? objectName.trim() : '',
          schDispCompanyName: dispCompanyName ? dispCompanyName.trim() : '',
          schSaleStoreId: stuffSearch?.schMyDataCheck ? stuffSearch?.schSaleStoreId : '',
          schSelSaleStoreId: stuffSearch?.schMyDataCheck ? '' : stuffSearch.schSelSaleStoreId,
          schOtherSelSaleStoreId: stuffSearch?.schMyDataCheck ? '' : stuffSearch.schOtherSelSaleStoreId,
          schReceiveUser: receiveUser ? receiveUser.trim() : '',
          schDateType: dateType,
          schTempFlg: tempFlg, //임시저장물건
          schMyDataCheck: myDataCheck,
          schFromDt: startDate ? dayjs(startDate).format('YYYY-MM-DD') : '',
          schToDt: endDate ? dayjs(endDate).format('YYYY-MM-DD') : '',
          code: 'E',
          startRow: 1,
          endRow: 1 * stuffSearch?.pageSize,
          schSortType: stuffSearch?.schSortType ? stuffSearch.schSortType : 'U',
          pageNo: 1,
          pageSize: stuffSearch?.pageSize,
        })
      } else {
        if (session.storeId !== 'T01') {
          setStuffSearch({
            schObjectNo: objectNo ? objectNo.trim() : stuffSearch.schObjectNo.trim(),
            schSaleStoreName: saleStoreName ? saleStoreName.trim() : '',
            schAddress: address ? address.trim() : '',
            schObjectName: objectName ? objectName.trim() : '',
            schDispCompanyName: dispCompanyName ? dispCompanyName.trim() : '',
            schSelSaleStoreId: '',
            schOtherSelSaleStoreId: otherSaleStoreId ? otherSaleStoreId : stuffSearch.schOtherSelSaleStoreId,
            schReceiveUser: receiveUser ? receiveUser.trim() : '',
            schDateType: dateType,
            schTempFlg: tempFlg, //임시저장물건
            schMyDataCheck: myDataCheck,
            schFromDt: startDate ? dayjs(startDate).format('YYYY-MM-DD') : '',
            schToDt: endDate ? dayjs(endDate).format('YYYY-MM-DD') : '',
            code: 'E',
            startRow: stuffSearch?.startRow ? stuffSearch.startRow : 1,
            endRow: stuffSearch?.endRow ? stuffSearch.endRow : 100,
            schSortType: stuffSearch?.schSortType ? stuffSearch.schSortType : 'U',
            pageNo: stuffSearch?.pageNo,
            pageSize: stuffSearch?.pageSize,
          })
        } else {
          setStuffSearch({
            schObjectNo: objectNo ? objectNo.trim() : stuffSearch.schObjectNo.trim(),
            schSaleStoreName: saleStoreName ? saleStoreName.trim() : '',
            schAddress: address ? address.trim() : '',
            schObjectName: objectName ? objectName.trim() : '',
            schDispCompanyName: dispCompanyName ? dispCompanyName.trim() : '',
            schSelSaleStoreId: schSelSaleStoreId ? schSelSaleStoreId : stuffSearch.schSelSaleStoreId,
            schOtherSelSaleStoreId: otherSaleStoreId ? otherSaleStoreId : stuffSearch.schOtherSelSaleStoreId,
            schReceiveUser: receiveUser ? receiveUser.trim() : '',
            schDateType: dateType,
            schTempFlg: tempFlg, //임시저장물건
            schMyDataCheck: myDataCheck,
            schFromDt: startDate ? dayjs(startDate).format('YYYY-MM-DD') : '',
            schToDt: endDate ? dayjs(endDate).format('YYYY-MM-DD') : '',
            code: 'E',
            startRow: stuffSearch?.startRow ? stuffSearch.startRow : 1,
            endRow: stuffSearch?.endRow ? stuffSearch.endRow : 100,
            schSortType: stuffSearch?.schSortType ? stuffSearch.schSortType : 'U',
            pageNo: stuffSearch?.pageNo,
            pageSize: stuffSearch?.pageSize,
          })
        }
      }
    } else if (stuffSearch.code === 'FINISH') {
      setStuffSearch({
        schObjectNo: objectNo.trim(),
        schSaleStoreName: saleStoreName.trim(),
        schAddress: address.trim(),
        schObjectName: objectName.trim(),
        schDispCompanyName: dispCompanyName.trim(),
        schSelSaleStoreId: schSelSaleStoreId,
        schOtherSelSaleStoreId: otherSaleStoreId,
        schReceiveUser: receiveUser.trim(),
        schDateType: dateType,
        schTempFlg: tempFlg, //임시저장물건
        schMyDataCheck: myDataCheck,
        schFromDt: startDate ? dayjs(startDate).format('YYYY-MM-DD') : '',
        schToDt: endDate ? dayjs(endDate).format('YYYY-MM-DD') : '',
        code: 'E',
        startRow: 1,
        endRow: 100,
        schSortType: stuffSearch?.schSortType ? stuffSearch.schSortType : 'U',
      })
    } else if (stuffSearch.code === 'E') {
      if (session.storeId !== 'T01' && session.storeLvl === '1') {
        if (stuffSearch.pageNo !== 1) {
          setStuffSearch({
            schObjectNo: objectNo ? objectNo.trim() : stuffSearch.schObjectNo.trim(),
            schSaleStoreName: saleStoreName ? saleStoreName.trim() : '',
            schAddress: address ? address.trim() : '',
            schObjectName: objectName ? objectName.trim() : '',
            schDispCompanyName: dispCompanyName ? dispCompanyName.trim() : '',
            schSelSaleStoreId: stuffSearch?.schSelSaleStoreId ? stuffSearch.schSelSaleStoreId : '',
            schOtherSelSaleStoreId: otherSaleStoreId ? otherSaleStoreId : stuffSearch.schOtherSelSaleStoreId,
            schReceiveUser: receiveUser ? receiveUser.trim() : '',
            schDateType: dateType,
            schTempFlg: tempFlg, //임시저장물건
            schMyDataCheck: myDataCheck,
            schFromDt: startDate ? dayjs(startDate).format('YYYY-MM-DD') : '',
            schToDt: endDate ? dayjs(endDate).format('YYYY-MM-DD') : '',
            code: 'E',
            startRow: 1,
            endRow: 1 * stuffSearch?.pageSize,
            schSortType: 'U',
            pageNo: 1,
            pageSize: stuffSearch?.pageSize,
          })
        } else {
          setStuffSearch({
            schObjectNo: objectNo ? objectNo.trim() : '',
            schSaleStoreName: saleStoreName ? saleStoreName.trim() : '',
            schAddress: address ? address.trim() : '',
            schObjectName: objectName ? objectName.trim() : '',
            schDispCompanyName: dispCompanyName ? dispCompanyName.trim() : '',
            schSelSaleStoreId: myDataCheck ? '' : otherSaleStoreId ? '' : '',
            schOtherSelSaleStoreId: myDataCheck ? '' : otherSaleStoreId,
            schReceiveUser: receiveUser ? receiveUser.trim() : '',
            schDateType: dateType,
            schTempFlg: tempFlg, //임시저장물건
            schMyDataCheck: myDataCheck,
            schFromDt: startDate ? dayjs(startDate).format('YYYY-MM-DD') : '',
            schToDt: endDate ? dayjs(endDate).format('YYYY-MM-DD') : '',
            code: 'E',
            startRow: stuffSearch?.startRow ? stuffSearch.startRow : 1,
            endRow: stuffSearch?.endRow ? stuffSearch.endRow : 100,
            schSortType: stuffSearch?.schSortType ? stuffSearch.schSortType : 'U',
            pageNo: stuffSearch?.pageNo,
            pageSize: stuffSearch?.pageSize,
          })
        }
      } else if (session.storeId === 'T01') {
        if (stuffSearch.pageNo !== 1) {
          setStuffSearch({
            schObjectNo: objectNo ? objectNo.trim() : stuffSearch.schObjectNo.trim(),
            schSaleStoreName: saleStoreName ? saleStoreName.trim() : '',
            schAddress: address ? address.trim() : '',
            schObjectName: objectName ? objectName.trim() : '',
            schDispCompanyName: dispCompanyName ? dispCompanyName.trim() : '',
            schSelSaleStoreId: schSelSaleStoreId ? schSelSaleStoreId : stuffSearch.schSelSaleStoreId,
            schOtherSelSaleStoreId: otherSaleStoreId ? otherSaleStoreId : stuffSearch.schOtherSelSaleStoreId,
            schReceiveUser: receiveUser ? receiveUser.trim() : '',
            schDateType: dateType,
            schTempFlg: tempFlg, //임시저장물건
            schMyDataCheck: myDataCheck,
            schFromDt: startDate ? dayjs(startDate).format('YYYY-MM-DD') : '',
            schToDt: endDate ? dayjs(endDate).format('YYYY-MM-DD') : '',
            code: 'E',
            startRow: 1,
            endRow: 1 * stuffSearch?.pageSize,
            schSortType: 'U',
            pageNo: 1,
            pageSize: stuffSearch?.pageSize,
          })
        } else {
          setStuffSearch({
            schObjectNo: objectNo ? objectNo.trim() : '',
            schSaleStoreName: saleStoreName ? saleStoreName.trim() : '',
            schAddress: address ? address.trim() : '',
            schObjectName: objectName ? objectName.trim() : '',
            schDispCompanyName: dispCompanyName ? dispCompanyName.trim() : '',
            schSelSaleStoreId: schSelSaleStoreId,
            schOtherSelSaleStoreId: otherSaleStoreId,
            schReceiveUser: receiveUser ? receiveUser.trim() : '',
            schDateType: dateType,
            schTempFlg: tempFlg, //임시저장물건
            schMyDataCheck: myDataCheck,
            schFromDt: startDate ? dayjs(startDate).format('YYYY-MM-DD') : '',
            schToDt: endDate ? dayjs(endDate).format('YYYY-MM-DD') : '',
            code: 'E',
            startRow: stuffSearch?.startRow ? stuffSearch.startRow : 1,
            endRow: stuffSearch?.endRow ? stuffSearch.endRow : 100,
            schSortType: stuffSearch?.schSortType ? stuffSearch.schSortType : 'U',
            pageNo: stuffSearch?.pageNo,
            pageSize: stuffSearch?.pageSize,
          })
        }
      } else {
        setStuffSearch({
          schObjectNo: objectNo ? objectNo.trim() : '',
          schSaleStoreName: saleStoreName ? saleStoreName.trim() : '',
          schAddress: address ? address.trim() : '',
          schObjectName: objectName ? objectName.trim() : '',
          schDispCompanyName: dispCompanyName ? dispCompanyName.trim() : '',
          schSelSaleStoreId: schSelSaleStoreId,
          schOtherSelSaleStoreId: otherSaleStoreId,
          schReceiveUser: receiveUser ? receiveUser.trim() : '',

          schDateType: dateType,
          schTempFlg: tempFlg, //임시저장물건
          schMyDataCheck: myDataCheck,
          schFromDt: startDate ? dayjs(startDate).format('YYYY-MM-DD') : '',
          schToDt: endDate ? dayjs(endDate).format('YYYY-MM-DD') : '',
          code: 'E',
          startRow: stuffSearch?.startRow ? stuffSearch.startRow : 1,
          endRow: stuffSearch?.endRow ? stuffSearch.endRow : 100,
          schSortType: stuffSearch?.schSortType ? stuffSearch.schSortType : 'U',
          pageNo: stuffSearch?.pageNo,
          pageSize: stuffSearch?.pageSize,
        })
      }
    } else {
      setStuffSearch({
        schObjectNo: objectNo.trim(),
        schSaleStoreName: saleStoreName.trim(),
        schAddress: address.trim(),
        schObjectName: objectName.trim(),
        schDispCompanyName: dispCompanyName.trim(),
        schSelSaleStoreId: schSelSaleStoreId,
        schOtherSelSaleStoreId: otherSaleStoreId,
        schReceiveUser: receiveUser.trim(),
        schDateType: dateType,
        schTempFlg: tempFlg, //임시저장물건
        schMyDataCheck: stuffSearch.schMyDataCheck,
        schFromDt: startDate ? dayjs(startDate).format('YYYY-MM-DD') : '',
        schToDt: endDate ? dayjs(endDate).format('YYYY-MM-DD') : '',
        code: 'E',
        startRow: stuffSearch?.startRow ? stuffSearch.startRow : 1,
        endRow: stuffSearch?.endRow ? stuffSearch.endRow : 100,
        schSortType: stuffSearch?.schSortType ? stuffSearch.schSortType : 'U',
        pageNo: stuffSearch?.pageNo,
        pageSize: stuffSearch?.pageSize,
      })
    }
  }

  //초기화
  const resetRecoil = () => {
    setIsGlobalLoading(false)
    //T01아니면 자동완성 초기화 막기
    objectNoRef.current.value = ''
    saleStoreNameRef.current.value = ''
    addressRef.current.value = ''
    objectNameRef.current.value = ''
    dispCompanyNameRef.current.value = ''
    receiveUserRef.current.value = ''
    stuffSearch.schDateType = 'U'
    stuffSearch.schTempFlg = ''
    stuffSearch.schMyDataCheck = false
    stuffSearch.schSaleStoreId = ''
    setObjectNo('')
    setAddress('')
    setObjectName('')
    setSaleStoreName('')
    setReceiveUser('')
    setDispCompanyName('')
    setDateType('U')
    setTempFlg('')
    setMyDataCheck(false)
    setStartDate(dayjs(new Date()).add(-1, 'year').format('YYYY-MM-DD'))
    setEndDate(dayjs(new Date()).format('YYYY-MM-DD'))
    if (session?.storeId === 'T01') {
      setSchSelSaleStoreId('')
      setOtherSaleStoreId('')
      handleClear1() //판매대리점선택 자동완성 클리어
      resetStuffRecoil()
      setStuffSearch({
        schObjectNo: '',
        schAddress: '',
        schObjectName: '',
        schSaleStoreName: '',
        schReceiveUser: '',
        schDispCompanyName: '',
        schSelSaleStoreId: '',
        schOtherSelSaleStoreId: '',
        schSaleStoreId: '',
        schDateType: 'U',
        schTempFlg: '',
        schMyDataCheck: false,
        startRow: 1,
        endRow: 100,
        schSortType: 'U',
        pageNo: 1,
        pageSize: 100,
      })
    } else {
      if (session?.storeLvl === '2') {
        if (otherSaleStoreList.length === 1) {
          setOtherSaleStoreId(session.storeId)
          stuffSearch.schOtherSelSaleStoreId = session.storeId
          stuffSearch.schSaleStoreId = ''
          stuffSearch.schObjectNo = ''
          stuffSearch.schAddress = ''
          stuffSearch.schObjectName = ''
          stuffSearch.schSaleStoreName = ''
          stuffSearch.schReceiveUser = ''
          stuffSearch.schDispCompanyName = ''
          stuffSearch.schDateType = 'U'
          stuffSearch.schTempFlg = ''
          stuffSearch.schMyDataCheck = false

          stuffSearch.startRow = 1
          stuffSearch.endRow = 100
          stuffSearch.schSortType = 'U'
          stuffSearch.pageNo = 1
          stuffSearch.pageSize = 100
        } else if (otherSaleStoreList.length > 1) {
          setOtherSaleStoreId('')
          stuffSearch.schOtherSelSaleStoreId = session.storeId
          stuffSearch.schSaleStoreId = ''
          stuffSearch.schObjectNo = ''
          stuffSearch.schAddress = ''
          stuffSearch.schObjectName = ''
          stuffSearch.schSaleStoreName = ''
          stuffSearch.schReceiveUser = ''
          stuffSearch.schDispCompanyName = ''
          stuffSearch.schDateType = 'U'
          stuffSearch.schTempFlg = ''
          stuffSearch.schMyDataCheck = false

          stuffSearch.startRow = 1
          stuffSearch.endRow = 100
          stuffSearch.schSortType = 'U'
          stuffSearch.pageNo = 1
          stuffSearch.pageSize = 100
        } else {
          stuffSearch.schObjectNo = ''
          stuffSearch.schAddress = ''
          stuffSearch.schObjectName = ''
          stuffSearch.schSaleStoreName = ''
          stuffSearch.schReceiveUser = ''
          stuffSearch.schDispCompanyName = ''
          stuffSearch.schDateType = 'U'
          stuffSearch.schTempFlg = ''
          stuffSearch.schMyDataCheck = false
          stuffSearch.startRow = 1
          stuffSearch.endRow = 100
          stuffSearch.schSortType = 'U'
          stuffSearch.pageNo = 1
          stuffSearch.pageSize = 100
          stuffSearch.schSaleStoreId = ''
        }
      } else if (session?.storeLvl === '1') {
        if (otherSaleStoreList.length > 0) {
          handleClear2()
          setOtherSaleStoreId('')
          stuffSearch.schSaleStoreId = ''
          stuffSearch.schObjectNo = ''
          stuffSearch.schAddress = ''
          stuffSearch.schObjectName = ''
          stuffSearch.schSaleStoreName = ''
          stuffSearch.schReceiveUser = ''
          stuffSearch.schDispCompanyName = ''
          stuffSearch.schDateType = 'U'
          stuffSearch.schTempFlg = ''
          stuffSearch.schMyDataCheck = false
          stuffSearch.startRow = 1
          stuffSearch.endRow = 100
          stuffSearch.schSortType = 'U'
          stuffSearch.pageNo = 1
          stuffSearch.pageSize = 100
        } else {
          stuffSearch.schSaleStoreId = ''
          stuffSearch.schObjectNo = ''
          stuffSearch.schAddress = ''
          stuffSearch.schObjectName = ''
          stuffSearch.schSaleStoreName = ''
          stuffSearch.schReceiveUser = ''
          stuffSearch.schDispCompanyName = ''
          stuffSearch.schDateType = 'U'
          stuffSearch.schTempFlg = ''
          stuffSearch.schMyDataCheck = false
          stuffSearch.startRow = 1
          stuffSearch.endRow = 100
          stuffSearch.schSortType = 'U'
          stuffSearch.pageNo = 1
          stuffSearch.pageSize = 100
        }
      }
    }
  }

  useEffect(() => {
    if (isObjectNotEmpty(session)) {
      // storeId가 T01 이거나 storeLvl이 1차점일때만 판매대리점 선택 활성화
      let url
      if (session?.storeId === 'T01') {
        //T01일떄
        url = `/api/object/saleStore/${session?.storeId}/firstList?userId=${session?.userId}`
      } else {
        url = `/api/object/saleStore/${session?.storeId}/list?firstFlg=1&userId=${session?.userId}`
      }

      get({ url: url }).then((res) => {
        if (!isEmptyArray(res)) {
          res.map((row) => {
            //#399
            row.saleStoreName = row.saleStoreName + ' - ' + row.saleStoreId
            row.value = row.saleStoreId
            row.label = row.saleStoreName
          })

          let allList
          let favList
          let otherList
          if (session?.storeId === 'T01') {
            allList = res
            allList.sort((a, b) => (a.saleStoreId !== 'T01') - (b.saleStoreId !== 'T01') || a.saleStoreId - b.saleStoreId)
            favList = res.filter((row) => row.saleStoreId === 'T01' || row.priority !== 'B')

            setSchSelSaleStoreList(allList)
            setFavoriteStoreList(favList)
            setShowSaleStoreList(favList)
            if (stuffSearch.schSelSaleStoreId != '') {
              setSchSelSaleStoreId(stuffSearch.schSelSaleStoreId)
              url = `/api/object/saleStore/${stuffSearch.schSelSaleStoreId}/list?firstFlg=1&userId=${session?.userId}`
              get({ url: url }).then((res) => {
                if (!isEmptyArray(res)) {
                  res.map((row) => {
                    //#399
                    row.saleStoreName = row.saleStoreName + ' - ' + row.saleStoreId
                    row.value = row.saleStoreId
                    row.label = row.saleStoreName
                  })

                  otherList = res.filter((row) => row.saleStoreLevel !== '1')
                  setOtherSaleStoreList(otherList)
                  setOtherSaleStoreId(stuffSearch.schOtherSelSaleStoreId)
                } else {
                  setOtherSaleStoreList([])
                }
              })
            }
          } else {
            if (session?.storeLvl === '1') {
              allList = res
              favList = res.filter((row) => row.priority !== 'B')
              otherList = res.filter((row) => row.firstAgentYn === 'N')

              setSchSelSaleStoreList(allList)
              setFavoriteStoreList(allList)
              setShowSaleStoreList(allList)
              setSchSelSaleStoreId(allList[0].saleStoreId)

              setOtherSaleStoreList(otherList)
              if (stuffSearch.schOtherSelSaleStoreId != '') {
                setOtherSaleStoreId(stuffSearch.schOtherSelSaleStoreId)
              }
            } else {
              setSchSelSaleStoreList(res)
              setFavoriteStoreList(res)
              setShowSaleStoreList(res)
              setSchSelSaleStoreId(res[0].saleStoreId)
              otherList = res.filter((row) => row.firstAgentYn === 'N')
              setOtherSaleStoreList(otherList)

              //선택한 2차점 세션으로 자동셋팅
              if (otherList.length === 1) {
                setOtherSaleStoreId(session?.storeId)
                setStuffSearch({
                  ...stuffSearch,
                  code: 'S',
                  schSelSaleStoreId: res[0].saleStoreId,
                  schOtherSelSaleStoreId: otherList[0].saleStoreId,
                })
              } else {
                if (stuffSearch.code === 'S') {
                  if (otherList.length === 1) {
                    setOtherSaleStoreId(session?.storeId)
                    setStuffSearch({
                      ...stuffSearch,
                      code: 'S',
                      schSelSaleStoreId: res[0].saleStoreId,
                      schOtherSelSaleStoreId: otherList[0].saleStoreId,
                    })
                  } else {
                    setOtherSaleStoreId('')
                    setStuffSearch({
                      ...stuffSearch,
                      code: 'S',
                      schSelSaleStoreId: res[0].saleStoreId,
                      schOtherSelSaleStoreId: '',
                    })
                  }
                } else {
                  setOtherSaleStoreId(stuffSearch?.schOtherSelSaleStoreId)
                  setStuffSearch({
                    ...stuffSearch,
                    code: 'S',
                  })
                }
              }
            }
          }
        }
      })
    }
  }, [session])

  //초기화 눌렀을 때 1차판매점 자동완성도..
  const handleClear1 = () => {
    if (ref.current) {
      ref.current.clearValue()
    }
  }

  const handleClear2 = () => {
    if (ref2.current) {
      ref2.current.clearValue()
    }
  }

  //자동완성 인풋
  const onInputChange = (key) => {
    //내 물건보기 체크 풀어주기
    setMyDataCheck(false)
    stuffSearch.schMyDataCheck = false
    if (key !== '') {
      setShowSaleStoreList(schSelSaleStoreList)
    } else {
      setShowSaleStoreList(favoriteStoreList)
    }
  }

  //판매대리점 자동완성 변경
  const onSelectionChange = (key) => {
    if (isObjectNotEmpty(key)) {
      setOtherSaleStoreId('')
      setSchSelSaleStoreId(key.saleStoreId)
      //고른 1차점의 saleStoreId로 2차점 API호출하기
      let url = `/api/object/saleStore/${key.saleStoreId}/list?firstFlg=0&userId=${session?.userId}`
      let otherList
      get({ url: url }).then((res) => {
        if (!isEmptyArray(res)) {
          res.map((row) => {
            //#399
            row.saleStoreName = row.saleStoreName + ' - ' + row.saleStoreId
            row.value = row.saleStoreId
            row.label = row.saleStoreName
          })

          otherList = res
          setOtherSaleStoreList(otherList)
        } else {
          setOtherSaleStoreList([])
        }
      })
    } else {
      //X누름
      //화면에선 지우는데 리코일은 조회누르지 않으면 보존
      setSchSelSaleStoreId('')
      setOtherSaleStoreId('')
      if (stuffSearch.code === 'S') {
        stuffSearch.schSelSaleStoreId = ''
        stuffSearch.schOtherSelSaleStoreId = ''
      } else if (stuffSearch.code === 'E') {
        //#401
        stuffSearch.schSelSaleStoreId = ''
        stuffSearch.schOtherSelSaleStoreId = ''
      }

      //2차점 판매점목록비우기
      setOtherSaleStoreList([])
    }
  }

  //2차점 자동완성 변경
  const onSelectionChange2 = (key) => {
    //내 물건보기 체크 풀어주기
    setMyDataCheck(false)
    stuffSearch.schMyDataCheck = false
    if (isObjectNotEmpty(key)) {
      setOtherSaleStoreId(key.saleStoreId)

      //2차점 골랐을때 1차점 값
      if (session.storeId === 'T01') {
        // stuffSearch.schSelSaleStoreId = schSelSaleStoreId
      } else {
        stuffSearch.schSelSaleStoreId = ''
      }
    } else {
      //X누르면 검색조건에 1차점으로 셋팅
      if (session.storeLvl === '1') {
        if (stuffSearch.schOtherSelSaleStoreId === '') {
          // 화면에선 지우는데 조회누르기 전이면 리코일은 남김
          setSchSelSaleStoreId(session.storeId)
          setOtherSaleStoreId('')
        } else {
          // 화면에선 지우는데 조회누르기 전이면 리코일은 남김
          setOtherSaleStoreId('')
          if (stuffSearch.code === 'S') {
            stuffSearch.schOtherSelSaleStoreId = ''
          }
        }
      } else {
        setOtherSaleStoreId('')
        setSchSelSaleStoreId(schSelSaleStoreId)
        stuffSearch.schOtherSelSaleStoreId = ''
        stuffSearch.schSelSaleStoreId = schSelSaleStoreId
      }
    }
  }

  useEffect(() => {
    if (session?.storeId === 'T01') {
      if (stuffSearch.code === 'DELETE') {
        setObjectNo('')
        setSaleStoreName('')
        setAddress('')
        setObjectName('')
        setDispCompanyName('')
        setReceiveUser('')
        objectNoRef.current.value = ''
        saleStoreNameRef.current.value = ''
        addressRef.current.value = ''
        objectNameRef.current.value = ''
        dispCompanyNameRef.current.value = ''
        receiveUserRef.current.value = ''
        stuffSearch.schObjectNo = ''
        stuffSearch.schAddress = ''
        stuffSearch.schObjectName = ''
        stuffSearch.schSaleStoreName = ''
        stuffSearch.schReceiveUser = ''
        stuffSearch.schDispCompanyName = ''
        stuffSearch.schDateType = 'U'
        stuffSearch.schTempFlg = ''
        stuffSearch.schMyDataCheck = false
        stuffSearch.schFromDt = dayjs(new Date()).add(-1, 'year').format('YYYY-MM-DD')
        stuffSearch.schToDt = dayjs(new Date()).format('YYYY-MM-DD')
        stuffSearch.startRow = 1
        stuffSearch.endRow = 100
        stuffSearch.schSelSaleStoreId = ''
        stuffSearch.schOtherSelSaleStoreId = ''
        stuffSearch.schSaleStoreId = ''
        stuffSearch.schSortType = 'U'
        stuffSearch.pageNo = 1
        stuffSearch.pageSize = 100

        setSchSelSaleStoreId('')
        setOtherSaleStoreId('')
        stuffSearch.code = 'S'
      } else if (stuffSearch.code === 'S') {
        setObjectNo('')
        setSaleStoreName('')
        setAddress('')
        setObjectName('')
        setDispCompanyName('')
        setReceiveUser('')
        objectNoRef.current.value = ''
        saleStoreNameRef.current.value = ''
        addressRef.current.value = ''
        objectNameRef.current.value = ''
        dispCompanyNameRef.current.value = ''
        receiveUserRef.current.value = ''
        stuffSearch.schObjectNo = ''
        stuffSearch.schAddress = ''
        stuffSearch.schObjectName = ''
        stuffSearch.schSaleStoreName = ''
        stuffSearch.schReceiveUser = ''
        stuffSearch.schDispCompanyName = ''
        stuffSearch.schDateType = 'U'
        stuffSearch.schTempFlg = ''
        stuffSearch.schMyDataCheck = false
        stuffSearch.schFromDt = dayjs(new Date()).add(-1, 'year').format('YYYY-MM-DD')
        stuffSearch.schToDt = dayjs(new Date()).format('YYYY-MM-DD')
        stuffSearch.startRow = 1
        stuffSearch.endRow = 100
        stuffSearch.schSelSaleStoreId = ''
        stuffSearch.schOtherSelSaleStoreId = ''
        stuffSearch.schSaleStoreId = ''
        stuffSearch.schSortType = 'U'
        stuffSearch.pageNo = 1
        stuffSearch.pageSize = 100
        setSchSelSaleStoreId('')
        setOtherSaleStoreId('')
      } else {
        setObjectNo(stuffSearch.schObjectNo ? stuffSearch.schObjectNo : objectNo)
        setSaleStoreName(stuffSearch.schSaleStoreName ? stuffSearch.schSaleStoreName : saleStoreName)
        setDispCompanyName(stuffSearch.schDispCompanyName ? stuffSearch.schDispCompanyName : dispCompanyName)
        setObjectName(stuffSearch.schObjectName ? stuffSearch.schObjectName : objectName)
        setReceiveUser(stuffSearch.schReceiveUser ? stuffSearch.schReceiveUser : receiveUser)
        setAddress(stuffSearch.schAddress ? stuffSearch.schAddress : address)
        setDateType(stuffSearch.schDateType ? stuffSearch.schDateType : dateType)
        setStartDate(stuffSearch?.schFromDt ? stuffSearch.schFromDt : dayjs(new Date()).add(-1, 'year').format('YYYY-MM-DD'))
        setEndDate(stuffSearch?.schToDt ? stuffSearch.schToDt : dayjs(new Date()).format('YYYY-MM-DD'))
        setTempFlg(stuffSearch.schTempFlg ? stuffSearch.schTempFlg : tempFlg)
      }
    } else {
      if (stuffSearch.code === 'DELETE') {
        //1차점인경우
        if (session.storeLvl === '1') {
          stuffSearch.schOtherSelSaleStoreId = ''
          setOtherSaleStoreId('')
        } else {
          //2차점 본인하나인경우
          //34있는경우
          stuffSearch.schOtherSelSaleStoreId = ''
          setOtherSaleStoreId('')
        }
        setObjectNo('')
        setSaleStoreName('')
        setAddress('')
        setObjectName('')
        setDispCompanyName('')
        setReceiveUser('')
        objectNoRef.current.value = ''
        saleStoreNameRef.current.value = ''
        addressRef.current.value = ''
        objectNameRef.current.value = ''
        dispCompanyNameRef.current.value = ''
        receiveUserRef.current.value = ''
        stuffSearch.schObjectNo = ''
        stuffSearch.schAddress = ''
        stuffSearch.schObjectName = ''
        stuffSearch.schSaleStoreName = ''
        stuffSearch.schReceiveUser = ''
        stuffSearch.schDispCompanyName = ''
        stuffSearch.schDateType = 'U'
        stuffSearch.schTempFlg = ''
        stuffSearch.schMyDataCheck = false
        stuffSearch.schFromDt = dayjs(new Date()).add(-1, 'year').format('YYYY-MM-DD')
        stuffSearch.schToDt = dayjs(new Date()).format('YYYY-MM-DD')
        stuffSearch.startRow = 1
        stuffSearch.endRow = 100
        stuffSearch.schSortType = 'U'
        stuffSearch.pageNo = 1
        stuffSearch.pageSize = 100
        stuffSearch.code = 'S'
      } else {
        setStartDate(stuffSearch?.schFromDt ? stuffSearch.schFromDt : dayjs(new Date()).add(-1, 'year').format('YYYY-MM-DD'))
        setEndDate(stuffSearch?.schToDt ? stuffSearch.schToDt : dayjs(new Date()).format('YYYY-MM-DD'))
        setObjectNo(stuffSearch.schObjectNo ? stuffSearch.schObjectNo : objectNo)
        setSaleStoreName(stuffSearch.schSaleStoreName ? stuffSearch.schSaleStoreName : saleStoreName)
        setAddress(stuffSearch.schAddress ? stuffSearch.schAddress : address)
        setObjectName(stuffSearch.schObjectName ? stuffSearch.schObjectName : objectName)
        setDispCompanyName(stuffSearch.schDispCompanyName ? stuffSearch.schDispCompanyName : dispCompanyName)
        setReceiveUser(stuffSearch.schReceiveUser ? stuffSearch.schReceiveUser : receiveUser)
        setDateType(stuffSearch.schDateType ? stuffSearch.schDateType : dateType)
        setTempFlg(stuffSearch.schTempFlg ? stuffSearch.schTempFlg : tempFlg)

        if (session.storeLvl !== '1') {
          stuffSearch.schSelSaleStoreId = ''
        }
      }
    }

    if (stuffSearch.schDateType === 'R') {
      setDateType('R')
    }

    if (stuffSearch.schTempFlg === '0') {
      setTempFlg('0')
    } else if (stuffSearch.schTempFlg === '1') {
      setTempFlg('1')
    }
  }, [stuffSearch])

  useEffect(() => {
    if (globalLocaleState === 'ko') {
      setAppMessageState(KO)
    } else {
      setAppMessageState(JA)
    }
  }, [globalLocaleState])

  // 엔터 이벤트
  const handleByOnKeyUp = (e) => {
    if (e.key === 'Enter') {
      stuffSearch.code = 'E'
      onSubmit()
    }
  }

  // 내 물건 보기
  const checkMyData = (e) => {
    //0203 내물건보기 체크시 공통 schSaleStoreId에 storeId담아 전송 schSelSaleStoreId, schOtherSelSaleStoreId 비우기
    if (session?.storeId === 'T01') {
      if (e.target.checked) {
        setMyDataCheck(true)
        setOtherSaleStoreId('') //2차점 비우기
        setSchSelSaleStoreId('T01')

        stuffSearch.schSelSaleStoreId = ''
        stuffSearch.schOtherSelSaleStoreId = ''
        const key = { saleStoreId: session.storeId }
        onSelectionChange(key)
      } else {
        stuffSearch.schSaleStoreId = ''
        setSchSelSaleStoreId('')
        setMyDataCheck(false)
        //아래꺼 확인필요
        onSelectionChange(null)
      }
    } else if (session?.storeLvl === '1') {
      if (e.target.checked) {
        setMyDataCheck(true)
        setOtherSaleStoreId('') //2차점 비우기
        setSchSelSaleStoreId(schSelSaleStoreId)
        stuffSearch.schSelSaleStoreId = ''
        stuffSearch.schOtherSelSaleStoreId = ''
      } else {
        setMyDataCheck(false)
      }
    } else {
      //2차점인제 3,4가 없으면 상관없음
      //3,4등등이 있는경우 처리필요
      if (e.target.checked) {
        setMyDataCheck(true)
        if (otherSaleStoreList.length > 1) {
          stuffSearch.schSaleStoreId = session.storeId
          stuffSearch.schOtherSelSaleStoreId = ''
          setOtherSaleStoreId(session.storeId)
        } else {
          stuffSearch.schSaleStoreId = session.storeId
          stuffSearch.schSelSaleStoreId = ''
          stuffSearch.schOtherSelSaleStoreId = ''
        }
      } else {
        setMyDataCheck(false)
        stuffSearch.schSaleStoreId = ''
      }
    }
  }

  const CustomOption = (props) => {
    const { data, innerRef, innerProps, isSelected, isFocused, isDisabled } = props
    const customClass = data.saleStoreId === 'T01' || data.priority !== 'B' ? 'special-option' : ''
    // 기본 선택/호버 상태 적용
    const optionClass = `${customClass} ${isSelected ? 'custom__option--is-selected' : ''} ${isFocused ? 'custom__option--is-focused' : ''} ${isDisabled ? 'custom__option--is-disabled' : ''}`
    return (
      <div ref={innerRef} {...innerProps} className={`custom__option ${optionClass}`}>
        {data.label}
      </div>
    )
  }

  const CustomOption2 = (props) => {
    const { data, innerRef, innerProps, isSelected, isFocused, isDisabled } = props
    const customClass = data.priority !== 'B' ? 'special-option' : ''
    // 기본 선택/호버 상태 적용
    const optionClass = `${customClass} ${isSelected ? 'custom__option--is-selected' : ''} ${isFocused ? 'custom__option--is-focused' : ''} ${isDisabled ? 'custom__option--is-disabled' : ''}`
    return (
      <div ref={innerRef} {...innerProps} className={`custom__option ${optionClass}`}>
        {data.label}
      </div>
    )
  }

  return (
    <>
      {/* 퍼블적용시작 */}
      <div className="sub-table-box">
        <div className="table-box-title-wrap">
          <div className="title-wrap">
            <h3>{getMessage('stuff.search.title')}</h3>
          </div>
          <div className="left-unit-box">
            {/* <Link href="/management/stuff/tempReg" scroll={false}>
              <button type="button" className="btn-origin navy mr5">
                {getMessage('stuff.search.btn.register')}
              </button>
            </Link> */}
            <button
              type="button"
              className="btn-origin navy mr5"
              onClick={() => {
                setIsGlobalLoading(true)
                router.push(`/management/stuff/tempReg`, { scroll: false })
              }}
            >
              {getMessage('stuff.search.btn.register')}
            </button>
            <button type="button" className="btn-origin navy mr5" onClick={onSubmit}>
              {getMessage('stuff.search.btn.search')}
            </button>
            <button type="button" className="btn-origin grey" onClick={resetRecoil}>
              {getMessage('stuff.search.btn.reset')}
            </button>
          </div>
        </div>
        <div className="common-table">
          <table>
            <colgroup>
              <col style={{ width: '160px' }} />
              <col />
              <col style={{ width: '160px' }} />
              <col />
              <col style={{ width: '160px' }} />
              <col />
            </colgroup>
            <tbody>
              <tr>
                <th>{getMessage('stuff.search.schObjectNo')}</th>
                <td>
                  <div className="input-wrap">
                    <input
                      type="text"
                      ref={objectNoRef}
                      className="input-light"
                      defaultValue={stuffSearch?.schObjectNo ? stuffSearch.schObjectNo : objectNo}
                      onChange={() => {
                        setObjectNo(objectNoRef.current.value)
                      }}
                      onKeyUp={handleByOnKeyUp}
                    />
                  </div>
                </td>
                <th>{getMessage('stuff.search.schSaleStoreName')}</th>
                <td>
                  <div className="input-wrap">
                    <input
                      type="text"
                      ref={saleStoreNameRef}
                      className="input-light"
                      defaultValue={stuffSearch?.schSaleStoreName ? stuffSearch.schSaleStoreName : saleStoreName}
                      onChange={() => {
                        setSaleStoreName(saleStoreNameRef.current.value)
                      }}
                      onKeyUp={handleByOnKeyUp}
                    />
                  </div>
                </td>

                <th>{getMessage('stuff.search.schDispCompanyName')}</th>
                <td>
                  <div className="input-wrap">
                    <input
                      type="text"
                      ref={dispCompanyNameRef}
                      className="input-light"
                      defaultValue={stuffSearch?.schDispCompanyName ? stuffSearch.schDispCompanyName : dispCompanyName}
                      onChange={() => {
                        setDispCompanyName(dispCompanyNameRef.current.value)
                      }}
                      onKeyUp={handleByOnKeyUp}
                    />
                  </div>
                </td>
              </tr>
              <tr>
                <th>{getMessage('stuff.search.schObjectName')}</th>
                <td>
                  <div className="input-wrap">
                    <input
                      type="text"
                      ref={objectNameRef}
                      className="input-light"
                      defaultValue={stuffSearch?.schObjectName ? stuffSearch.schObjectName : objectName}
                      onChange={() => {
                        setObjectName(objectNameRef.current.value)
                      }}
                      onKeyUp={handleByOnKeyUp}
                    />
                  </div>
                </td>
                <th>{getMessage('stuff.search.schReceiveUser')}</th>
                <td>
                  <div className="input-wrap">
                    <input
                      type="text"
                      className="input-light"
                      ref={receiveUserRef}
                      defaultValue={stuffSearch?.schReceiveUser ? stuffSearch.schReceiveUser : receiveUser}
                      onChange={() => {
                        setReceiveUser(receiveUserRef.current.value)
                      }}
                      onKeyUp={handleByOnKeyUp}
                    />
                  </div>
                </td>
                <th>{getMessage('stuff.search.schAddress')}</th>
                <td>
                  <div className="input-wrap">
                    <input
                      type="text"
                      ref={addressRef}
                      className="input-light"
                      defaultValue={stuffSearch?.schAddress ? stuffSearch.schAddress : address}
                      onChange={() => {
                        setAddress(addressRef.current.value)
                      }}
                      onKeyUp={handleByOnKeyUp}
                    />
                  </div>
                </td>
              </tr>
              <tr>
                <th>{getMessage('stuff.search.period')}</th>
                <td colSpan={3}>
                  <div className="form-flex-wrap">
                    <div className="radio-wrap mr10">
                      <div className="d-check-radio light mr10">
                        <input
                          type="radio"
                          name="radio_ptype"
                          id="radio_u"
                          checked={dateType === 'U' ? true : false}
                          value={'U'}
                          onChange={(e) => {
                            setDateType(e.target.value)
                          }}
                        />
                        <label htmlFor="radio_u">{getMessage('stuff.search.schDateTypeU')}</label>
                      </div>
                      <div className="d-check-radio light">
                        <input
                          type="radio"
                          name="radio_ptype"
                          id="radio_r"
                          checked={dateType === 'R' ? true : false}
                          value={'R'}
                          onChange={(e) => {
                            setDateType(e.target.value)
                          }}
                        />
                        <label htmlFor="radio_r">{getMessage('stuff.search.schDateTypeR')}</label>
                      </div>
                    </div>
                    <div className="date-picker-wrap">
                      <div className="date-picker" style={{ flex: 1 }}>
                        <SingleDatePicker {...rangeDatePickerProps1} />
                      </div>
                      <span> ~ </span>
                      <div className="date-picker" style={{ flex: 1 }}>
                        <SingleDatePicker {...rangeDatePickerProps2} />
                      </div>
                    </div>
                  </div>
                </td>
                <th>{getMessage('stuff.search.schTempFlgT')}</th>
                <td>
                  <div className="radio-wrap mr10">
                    <div className="d-check-radio light mr10">
                      <input
                        type="radio"
                        name="schTempFlg"
                        id="schTempFlg"
                        checked={tempFlg === '' ? true : false}
                        value={''}
                        onChange={(e) => {
                          setTempFlg(e.target.value)
                        }}
                      />
                      <label htmlFor="schTempFlg">{getMessage('stuff.search.schTempFlg')}</label>
                    </div>
                    <div className="d-check-radio light mr10">
                      <input
                        type="radio"
                        name="schTempFlg"
                        id="schTempFlg0"
                        checked={tempFlg === '0' ? true : false}
                        value={'0'}
                        onChange={(e) => {
                          setTempFlg(e.target.value)
                        }}
                      />
                      <label htmlFor="schTempFlg0">{getMessage('stuff.search.schTempFlg0')}</label>
                    </div>
                    <div className="d-check-radio light">
                      <input
                        type="radio"
                        name="schTempFlg"
                        id="schTempFlg1"
                        checked={tempFlg === '1' ? true : false}
                        value={'1'}
                        onChange={(e) => {
                          setTempFlg(e.target.value)
                        }}
                      />
                      <label htmlFor="schTempFlg1">{getMessage('stuff.search.schTempFlg1')}</label>
                    </div>
                  </div>
                </td>
              </tr>
              <tr>
                <th>{getMessage('stuff.search.schSelSaleStoreId')}</th>
                <td colSpan={5}>
                  <div className="form-flex-wrap">
                    <div className="select-wrap mr5" style={{ flex: 1 }}>
                      {session?.storeId === 'T01' && (
                        <Select
                          id="long-value-select1"
                          instanceId="long-value-select1"
                          className="react-select-custom"
                          classNamePrefix="custom"
                          placeholder="Select"
                          ref={ref}
                          options={showSaleStoreList}
                          onInputChange={onInputChange}
                          onChange={onSelectionChange}
                          getOptionLabel={(x) => x.saleStoreName}
                          getOptionValue={(x) => x.saleStoreId}
                          value={schSelSaleStoreList.filter(function (option) {
                            if (stuffSearch?.code === 'S' && schSelSaleStoreId === '') {
                              return false
                            } else if (stuffSearch?.code === 'S' && schSelSaleStoreId !== '') {
                              return option.saleStoreId === schSelSaleStoreId
                            } else if (stuffSearch?.code === 'E' && schSelSaleStoreId !== '') {
                              return option.saleStoreId === schSelSaleStoreId
                            } else {
                              if (stuffSearch?.code === 'FINISH') {
                                return option.saleStoreId === schSelSaleStoreId
                              } else if (stuffSearch?.schSelSaleStoreId !== '') {
                                return option.saleStoreId === stuffSearch.schSelSaleStoreId
                              } else {
                                return false
                              }
                            }
                          })}
                          isDisabled={session?.storeLvl !== '1' ? true : session?.storeId !== 'T01' ? true : false}
                          isClearable={true}
                          components={{ Option: CustomOption }}
                        />
                      )}
                      {session?.storeId !== 'T01' && session?.storeLvl === '1' && (
                        <Select
                          id="long-value-select1"
                          instanceId="long-value-select1"
                          className="react-select-custom"
                          classNamePrefix="custom"
                          placeholder="Select"
                          ref={ref}
                          options={showSaleStoreList[0]}
                          onChange={onSelectionChange}
                          getOptionLabel={(x) => x.saleStoreName}
                          getOptionValue={(x) => x.saleStoreId}
                          value={showSaleStoreList.filter(function (option) {
                            if (stuffSearch?.code === 'S' && schSelSaleStoreId === '') {
                              return false
                            } else if (stuffSearch?.code === 'S' && schSelSaleStoreId !== '') {
                              return option.saleStoreId === schSelSaleStoreId
                            } else if (stuffSearch?.code === 'E' && schSelSaleStoreId !== '') {
                              return option.saleStoreId === schSelSaleStoreId
                            } else {
                              if (stuffSearch?.code === 'FINISH') {
                                return option.saleStoreId === schSelSaleStoreId
                              } else if (stuffSearch?.schSelSaleStoreId !== '') {
                                return option.saleStoreId === stuffSearch.schSelSaleStoreId
                              } else {
                                return false
                              }
                            }
                          })}
                          isDisabled={session?.storeLvl !== '1' ? true : session?.storeId !== 'T01' ? true : false}
                          isClearable={false}
                        />
                      )}
                      {session?.storeId !== 'T01' && session?.storeLvl !== '1' && (
                        <Select
                          id="long-value-select1"
                          instanceId="long-value-select1"
                          className="react-select-custom"
                          classNamePrefix="custom"
                          placeholder="Select"
                          ref={ref}
                          options={showSaleStoreList[0]}
                          onChange={onSelectionChange}
                          getOptionLabel={(x) => x.saleStoreName}
                          getOptionValue={(x) => x.saleStoreId}
                          value={showSaleStoreList.filter(function (option) {
                            if (stuffSearch?.code === 'S' && schSelSaleStoreId === '') {
                              return false
                            } else if (stuffSearch?.code === 'S' && schSelSaleStoreId !== '') {
                              return option.saleStoreId === schSelSaleStoreId
                            } else if (stuffSearch?.code === 'E' && schSelSaleStoreId !== '') {
                              return option.saleStoreId === schSelSaleStoreId
                            } else {
                              if (stuffSearch?.schSelSaleStoreId !== '') {
                                return option.saleStoreId === stuffSearch.schSelSaleStoreId
                              } else {
                                return false
                              }
                            }
                          })}
                          isDisabled={true}
                          isClearable={false}
                        />
                      )}
                    </div>
                    <div className="select-wrap mr10" style={{ flex: 1 }}>
                      <Select
                        id="long-value-select2"
                        instanceId="long-value-select2"
                        className="react-select-custom"
                        classNamePrefix="custom"
                        placeholder="Select"
                        ref={ref2}
                        options={otherSaleStoreList}
                        onChange={onSelectionChange2}
                        getOptionLabel={(x) => x.saleStoreName}
                        getOptionValue={(x) => x.saleStoreId}
                        isDisabled={
                          session?.storeLvl === '1' ? (otherSaleStoreList.length > 0 ? false : true) : otherSaleStoreList.length === 1 ? true : false
                        }
                        isClearable={true}
                        value={otherSaleStoreList.filter(function (option) {
                          return option.saleStoreId === otherSaleStoreId
                        })}
                        components={{ Option: CustomOption2 }}
                      />
                    </div>
                    <div className="d-check-box light">
                      <input
                        type="checkbox"
                        id="schMine"
                        checked={myDataCheck ? true : false}
                        onChange={(e) => {
                          checkMyData(e)
                        }}
                      />
                      <label htmlFor="schMine">{getMessage('stuff.search.schMine')}</label>
                    </div>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
      {/* 퍼블적용끝 */}
    </>
  )
}
