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 getList = () => {
  if (!getUserLogin?.getLoginStatus()) return
  console.log('getList', auth?.data)
  let OwnerArr: any = []
  let NoOwnerArr: any = []
  getApiLoading(Apis.Archive.HouseOccupants.GetHouseOccupants, {
    ...useModel?.formData?.value
  }).then(res => {
    useModel.showData.value = res?.data
    res?.data?.list?.map((i: any) => {
      if (i?.house_relation === 'Owner') {
        OwnerArr?.push(i)
      }
      if (i?.house_relation === 'NonOwner') {
        NoOwnerArr?.push(i)
      }
    })
    useModel.listData.value = [
      { label: '产权人', list: OwnerArr || [] },
      { label: '非产权人', list: NoOwnerArr || [] }
    ]
    console.log(res)
  })
}

const handleSearch = () => {
  useModel.showData.value = {}
  useModel.formData.value.page = 1
  if (!useModel?.formData?.value?.asset_houses_id) return
  getList()
}

export default {
  getList,
  handleSearch,
  handleLoadMore(page: number) {
    useModel.formData.value.page = page
    getList()
  },
  handleUnbind() {
    getApiLoading(Apis.Archive.HouseOccupants.Unbind, {
      asset_houses_id: useModel?.showData?.value?.house_relation?.asset_houses_id
    }).then(res => {
      showToast('解绑成功！')
      uni.switchTab({
        url: '/pages/index/index'
      })
      console.log(res)
    })
  },
  hadnleNewBindingHouse() {
    uni.navigateTo({
      url: getUserLogin?.getLoginStatus() ? '/INDEX/binding/index' : '/pages/login'
    })
  },
  handleRemove(e: { id: number }) {
    uni.showModal({
      title: '提示',
      content: '确定要移除当前人员!',
      confirmColor: '#2A7EFB',
      success: function (res) {
        if (res.confirm) {
          getApiLoading(Apis.Archive.HouseOccupants.DeleteOccupant, {
            asset_houses_id: useModel?.showData?.value?.house_relation?.asset_houses_id,
            house_occupants_id: e?.id
          }).then(res => {
            getList()
            showToast('移除成功！')
            console.log(res)
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
}
