import { useWeAppAuthStore } from '@/common'
import { getApiLoading } from '@/common/libraries/apiLoading'
import { showToast, showToastBack } from '@/common/libraries/naviHelper'
import { navigateToMiniProgram, sendChatMessage } from '@/common/libraries/sendWxWork'
import getUserLogin from '@/common/libraries/userUserLogin'
import { Apis } from '@/gen/Apis'
import useModel from './model'
const auth = useWeAppAuthStore()
export default {
  nextStep(type: string) {
    let data = useModel?.formData?.value
    if (getUserLogin?.toPageLogin()) return
    if (!data?.phone || !data?.name) {
      return showToast('请填写姓名/手机号')
    }
    getApiLoading(Apis.Archive.HouseOccupants.GetCustomerHouse, { phone: data?.phone }).then(
      res => {
        if (!res?.data?.length) {
          //有无登记房源
          if (type === 'phone') {
            uni.redirectTo({
              url: `/CUSTOMER/house_select/index?phone=${useModel?.formData?.value?.phone}&name=${useModel?.formData?.value?.name}`
            })
          } else {
            uni.navigateTo({
              url: `/CUSTOMER/house_select/index?phone=${useModel?.formData?.value?.phone}&name=${useModel?.formData?.value?.name}`
            })
          }
        } else {
          useModel.bindHouseSelectPage.value = true
          useModel.bindHouseFormPage.value = false
          useModel.listData.value = res?.data
        }
        console.log(res)
      }
    )
  },
  selectHouse(e: any, index: number) {
    //选择房屋
    let isSelect = useModel?.listData?.value[index]?.isSelect
    useModel.listData.value[index].isSelect = !isSelect
  },
  handleBack() {
    if (useModel?.bindPhone?.value) {
      //绑定其他无登记房源
      uni.navigateTo({
        url: `/CUSTOMER/house_select/index?phone=${useModel?.formData?.value?.phone}&name=${useModel?.formData?.value?.name}`
      })
    } else {
      useModel.bindHouseSelectPage.value = false
      useModel.bindHouseFormPage.value = true
    }
  },
  handleBindHouse() {
    let data = useModel?.formData?.value
    let house_info: any = []
    useModel?.listData?.value.map((res: any) => {
      if (res?.isSelect) {
        house_info.push({
          asset_houses_id: res?.asset_houses_id,
          residential_relation: res?.residential_relation
        })
      }
    })
    if (!house_info?.length) {
      return showToast('请至少选择一套房屋！')
    }
    console.log(house_info)
    getApiLoading(Apis.Archive.HouseRegisters.Store, {
      ...data,
      house_info: house_info
    }).then(res => {
      showToastBack('绑定成功', 1, false, () => {
        uni.reLaunch({
          url: `/CUSTOMER/house_list/index`
        })
      })
      console.log(res)
    })
  },
  handleInviteCustomer() {
    if (auth?.data?.environment === 'wxwork') {
      //企业微信
      sendChatMessage({ page: '/INDEX/binding/index.html', title: '邀请你绑定房屋' })
    } else {
      //绿微环境
      navigateToMiniProgram({
        path: '/pages/forward/index?source=HouseholdRegistration',
        envVersion: 'develop',
        extraData: { source: 'HouseholdRegistration' }
      })
    }
  }
}
