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

const getHouseList = () => {
  if (!getUserLogin?.getLoginStatus()) return
  getApiLoading(Apis.Archive.HouseOccupants.List, useModel?.formData?.value).then(res => {
    useModel.dataList.value = [...useModel.dataList.value, ...res?.data]
    useModel.meta.value = res?.meta
    console.log(res)
  })
}

const getHouseRegistersList = () => {
  if (!getUserLogin?.getLoginStatus()) return
  getApiLoading(Apis.Archive.HouseRegisters.List, {
    ...useModel?.formData?.value,
    status: 'Pending'
  }).then(res => {
    useModel.dataList.value = [...useModel.dataList.value, ...res?.data]
    useModel.meta.value = res?.meta
    console.log(res)
  })
}

export default {
  getHouseList,
  getHouseRegistersList,
  handleLoadMore(page: number) {
    useModel.formData.value.page = page
    if (useModel.currentTabs.value === 0) {
      getHouseList()
    }
    if (useModel.currentTabs.value === 1) {
      getHouseRegistersList()
    }
  },
  handleSelectedHouse(e: { asset_houses_id: string }) {
    if (useModel?.selectType?.value === 'work_add') {
      work.selectWorkHouse = e
      showToastBack('选择成功！', 1, true)
      return
    }
    if (e?.asset_houses_id === useModel.selectedHouseEd.value?.id) {
      showToast('已是该房屋！')
      return
    }
    useModel.selectedHouseEd.value = { id: e?.asset_houses_id }
    getApiLoading(Apis.Archive.HouseOccupants.UpdateSelectedHouse, {
      asset_houses_id: e?.asset_houses_id
    }).then(res => {
      auth?.me(() => {
        showToastBack('选择成功！', 1, true)
      })
      console.log(res)
    })
  },
  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}`
    })
  }
}
