
import { ref, watch } from "vue";
import getTableState from "@/state/tableRef";
import auth from "@/state/viewAuth";
import { dateFormat } from '@/util'
import user from '@/state/user'
import {
  getSecondHouseList1 as getSecondHouseList,
  communityExamine,
} from '@/request/config'
import { 
  EXAMINE_ADOPT_STATUS,
  EXAMINE_SHOW_STATUS_1 as EXAMINE_SHOW_STATUS, 
  EXAMINE_NO_AUDIT_STATUS,
  EXAMINE_UP_ONLINE_STATUS,
  UN_REQ_NUM,
  EXAMINE_NO_ADOPT_ALL_STATUS,
  EXAMINE_NO_COM_ADOPT_STATUS,
  EXANINE_SUCCESS_STATUS,
  EXAMINE_NO_COM_AUDIT_STATUS,
} from '@/constant'

const getTimeMapSearch = (search, searchKey) => {
  const time = ref(null)
  const start = `${searchKey}Start`
  const end = `${searchKey}End`

  watch(time, () => {
    if (time.value) {
      search.value.state[start] = dateFormat(new Date(time.value[0]), 'yyyy-MM-dd hh:mm:ss').toString()
      search.value.state[end] = dateFormat(new Date(time.value[1]), 'yyyy-MM-dd 23:59:59').toString()
    } else {
      search.value.state[start] = search.value.state[end] = ''
    }
  })

  watch(
    () => search.value.state[start], 
    () => {
      if (!search.value.state[start]) {
        time.value = null
      }
    }
  )
  watch(
    () => search.value.state[end], 
    () => {
      if (!search.value.state[end]) {
        time.value = null
      }
    }
  )

  return time
}

const getState = () => {
  const headList = ref(EXAMINE_SHOW_STATUS);
  const statusa = ref(user.value.role === 'region' 
  ? EXAMINE_NO_COM_AUDIT_STATUS : EXAMINE_NO_AUDIT_STATUS)

  const state = getTableState({
    getUrl: getSecondHouseList,
    updateUrl: communityExamine,
    operAttr: { 
      auditStatus: null, 
      maker: '', 
      cityId: '',
      vRUrl: '',
      dataSource: '',
      vrCover: '',
      operStatus: 1,
      gardenId: "", 
      auditId: '' 
    },
    searchAttr: {
      roomlistingId: "", 
      createName: '',
      gardenName: '', 
      maintenanceman: '',
      reviewer: '',
      reviewTimeStart: '',
      reviewTimeEnd: '',
      arraignmentTimeStart: '',
      arraignmentTimeEnd: '',
      type: UN_REQ_NUM,
      auditStatus: statusa.value
    }
  });
  
  watch(statusa, () => {
    console.log(statusa.value)
    if (statusa.value !== -2) {
      state.search.value.state.auditStatus = statusa.value === EXANINE_SUCCESS_STATUS 
        ? EXAMINE_ADOPT_STATUS
        : statusa.value === EXAMINE_ADOPT_STATUS 
          ? EXAMINE_UP_ONLINE_STATUS
          : statusa.value === EXAMINE_NO_ADOPT_ALL_STATUS 
            ? user.value.role === 'admin' || user.value.role === 'group'
              ? `${EXAMINE_NO_ADOPT_ALL_STATUS},${EXAMINE_NO_COM_ADOPT_STATUS}`
              : EXAMINE_NO_COM_ADOPT_STATUS
            : statusa.value
    } else {
      delete state.search.value.state.auditStatus
    }
  }, {immediate: true})


  const arraignmentTime = getTimeMapSearch(state.search, 'arraignmentTime')
  const reviewTime = getTimeMapSearch(state.search, 'reviewTime')

  
  return { ...state, headList, auth, arraignmentTime, reviewTime, statusa };
}


const map = new WeakMap()
export default (parentVm) => {
  console.log(map.has(parentVm))
  if (map.has(parentVm)) {
    return map.get(parentVm)
  } else {
    const state = getState()
    map.set(parentVm, state)
    return state
  }
}