import { useWorkStore } from '@/common'
import { getApiLoading } from '@/common/libraries/apiLoading'
import { showToastBack } from '@/common/libraries/naviHelper'
import getUserLogin from '@/common/libraries/userUserLogin'
import { Apis } from '@/gen/Apis'
import useModel from './model'
const work = useWorkStore()

const getHouseList = () => {
  if (!getUserLogin?.getLoginStatus()) return
  getApiLoading(Apis.Asset.AssetHouses.SelectHouse, useModel?.formData?.value).then(res => {
    useModel.dataList.value = res?.data
    console.log(res)
  })
}
const getProjectList = () => {
  if (!getUserLogin?.getLoginStatus()) return
  getApiLoading(Apis.Asset.AssetHouses.SelectPermissionProject, useModel?.formData?.value).then(res => {
    useModel.dataList.value = res?.data
    if(res?.data?.length === 1 && useModel.formData.value.page === 1) {
      uni.navigateTo({
        url: `/ME/instrument/list/index?id=${res?.data?.[0]?.id}`
      })
    }
    console.log(res)
  })
}

export default {
  getHouseList,
  getProjectList,
  // getHouseRegistersList,
  handleLoadMore(page: number) {
    useModel.formData.value.page = page
    if (useModel?.selectType?.value === 'work_add') {
      getHouseList()
    }
    if (useModel?.selectType?.value === 'meter') {
      getProjectList()
    }
    // if (useModel.currentTabs.value === 1) {
    //   getHouseRegistersList()
    // }
  },
  onSearch(){
    useModel.formData.value.page = 1
    if (useModel?.selectType?.value === 'work_add') {
      getHouseList()
    }
    if (useModel?.selectType?.value === 'meter') {
      getProjectList()
    }
  },
  handleSelectedHouse(e: { id: string }) {
    useModel.selectedHouseEd.value = { id: e?.id }
    if (useModel?.selectType?.value === 'work_add') {
      work.selectWorkHouse = e
      showToastBack('选择成功！', 1, true)
      return
    }
  },
  handleSelectedProject(e: { id: string }) {
    uni.navigateTo({
      url: `/ME/instrument/list/index?id=${e?.id}`
    })
  },
  hadnleNewBindingHouse() {
    uni.navigateTo({
      url: getUserLogin?.getLoginStatus() ? '/INDEX/binding/index' : '/pages/login'
    })
  },
  handleQueryHouseArchives(e: { asset_houses_id: string }) {
    uni.navigateTo({
      url: `/INDEX/archive_houses/index?id=${e?.asset_houses_id}`
    })
  }
}
