
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 {
  getCommunityList,
  communityExamine,
} from '@/request/config'
import { 
  EXAMINE_NO_AUDIT_STATUS,
  EXAMINE_SHOW_STATUS_1 as EXAMINE_SHOW_STATUS, 
  UN_REQ_NUM,
  EXAMINE_NO_COM_AUDIT_STATUS,
  EXAMINE_ADOPT_STATUS,
  EXAMINE_NO_ADOPT_ALL_STATUS,
  EXAMINE_ALL_STATUS,
  EXANINE_SUCCESS_STATUS
  
} from '@/constant'


const getTimeMapSearch = (search, start, end) => {
  const time = ref(null)

  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 status = ref(user.value.role === 'region' 
  ? EXAMINE_NO_COM_AUDIT_STATUS : EXAMINE_NO_AUDIT_STATUS)


  const state = getTableState({
    getUrl: getCommunityList,
    updateUrl: communityExamine,
    pagAttr: {listMap: {vrGardenId: 'id'}},
    operAttr: { 
      vrGardenId: "",
      gardenId: "", 
      gardenName: '',
      creatorName: user.value.info.nickName,
      creatorId: user.value.info.id,
      companyId: user.value.info.departmentId,
      maker: '',
      auditStatus: null,
      operStatus: 1,
      companyName: ''
    },
    searchAttr: {
      gardenNumber: "",
      gardenName: '',
      cityId: '',
      readType: 'all',
      areaId: '',
      propertySort: '',
      updateByName: '',
      startCreateTime: '',
      endCreateTime: '',
      buildStartTime: '',
      buildEndTime: '',
      type: UN_REQ_NUM
    }
  });
  
  watch(status, () => {
    const map = {
      [EXAMINE_ALL_STATUS]: 'all',
      [EXAMINE_NO_COM_AUDIT_STATUS]: 'areaAuditing',
      [EXAMINE_NO_AUDIT_STATUS]: 'groupAuditing',
      [EXAMINE_ADOPT_STATUS]: 'online',
      [EXAMINE_NO_ADOPT_ALL_STATUS]: 'unPass',
      [EXANINE_SUCCESS_STATUS]: 'auditPass'
    }
    state.search.value.state.readType = map[status.value]
  }, {immediate: true})

  const yearsTime = getTimeMapSearch(state.search, 'buildStartTime', 'buildEndTime')
  const createTime = getTimeMapSearch(state.search, 'startCreateTime', 'endCreateTime')

  return { ...state, headList, auth, yearsTime, createTime, selectStatus: status };
}


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