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 getProjectList = () => {
  if (!getUserLogin?.getLoginStatus()) return
  getApiLoading(Apis.Asset.AssetHouses.SelectProject, useModel?.formData?.value).then(res => {
    useModel.dataList.value = [...useModel?.dataList?.value, ...res?.data]
    useModel.metaData.value = res?.meta
    console.log(res)
  })
}

const getPermission = (data: any) => {
  getApiLoading(Apis.Emergency.EmergencyEventTeams.GetPermission, {
    asset_projects_id: data?.id
  }).then(res => {
    if (res?.data?.permission) {
      useModel.selectedHouseEd.value = data
      work.selectWorkProject = data
      showToastBack('选择成功！', 1, true)
    } else {
      uni.navigateTo({
        url: `/pages/public/select_project/pages/permission_show?id=${data?.id}`
      })
    }
    console.log(res)
  })
}

export default {
  getProjectList,
  // getHouseRegistersList,
  handleLoadMore(params: { page: number }) {
    console.log(params)
    useModel.formData.value.page = params.page
    getProjectList()
  },
  handleSelectedProject(e: { id: string }) {
    if (useModel?.source?.value === 'emergency_add') {
      //从突发事件-过来选择
      getPermission(e)
    } else {
      useModel.selectedHouseEd.value = e
      work.selectWorkProject = e
      showToastBack('选择成功！', 1, true)
    }
  },
  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}`
    })
  }
}
