import { ref } from 'vue'
import useModel from './model'
import { getApiLoading } from '@/common/libraries/apiLoading'
import { Apis } from '@/gen/Apis'
import { useWeAppAuthStore } from '@/common'
import { showToastBack, showToast } from '@/common/libraries/naviHelper'
import userLoginStatus from '@/common/libraries/userUserLogin'
import { HouseOccupantsResidentialRelationEnum } from '@/gen/Enums'

const auth = useWeAppAuthStore()
const handleSubmitStore = () => {
  getApiLoading(Apis.Archive.HouseRegisters.Store, {
    ...useModel.formData.value,
    is_live_in: useModel?.formData?.value?.is_live_in ? 1 : 0,
    customer_info: [useModel.formData.value]
  })
    .then(res => {
      useModel.loading.value = false
      useModel.show.value = false
      uni.redirectTo({ url: '/INDEX/binding/success' })
      auth?.getIsHouseExist()
      // showToastBack('提交成功，等待审核！', 1, true)
      console.log(res)
    })
    .catch(e => {
      useModel.loading.value = false
      useModel.show.value = false
    })
}

export default {
  handleSubmitStore,
  handleSelectProject: (e: any) => {
    if (e[3]) {
      useModel.formData.value.asset_houses_id = e[3]?.id
    } else {
      useModel.formData.value.asset_houses_id = ''
    }
    console.log(e, 'e')
  },
  init(e: any) {
    let obj = HouseOccupantsResidentialRelationEnum as any
    delete obj['PropertyOwner']
    useModel.HouseOccupantsEnum.value = obj
    useModel.defaultHouseValueName.value = e?.houseName || ''
    useModel.formData.value = {
      asset_houses_id: e?.id || '',
      type: '',
      customer_info: [],
      name: '',
      phone: auth?.data?.user?.phone || '',
      card_front_image: [],
      card_back_image: [],
      house_relation: '',
      card_type: 'MainlandID',
      relation_with_owner: '',
      ownership_info: [],
      id_card: '',
      move_in_date: '',
      is_live_in: true
    }
  },
  handleIsLiveIn(e: any) {
    useModel.formData.value.is_live_in = e?.detail?.value
    console.log(e, 'e')
  },
  handleSubmit: (e: any) => {
    if (!userLoginStatus?.onBindPhone()) {
      return false
    }
    if (!useModel.formData.value.asset_houses_id) {
      return showToast('请先选择房屋！')
    }
    if (!useModel.formData.value.type) {
      return showToast('请先选择身份！')
    }
    if (!useModel.formData.value.name || !useModel.formData.value.phone) {
      return showToast('请设置姓名和手机号！')
    }
    if (!useModel.formData.value.id_card) {
      return showToast('请设置身份证号！')
    }
    if (
      !useModel.formData.value.card_front_image?.length ||
      !useModel.formData.value.card_back_image?.length
    ) {
      return showToast('请先上传身份证正反面！')
    }
    if (
      useModel.formData.value.type === 'AddOwner' &&
      !useModel.formData.value?.ownership_info?.length
    ) {
      return showToast('请先上传房产证！')
    }
    if (useModel.formData.value.type === 'AddOwner' && useModel?.ownerList?.value?.length) {
      useModel.show.value = true
      return
    }
    useModel.loading.value = true
    console.log(useModel?.formData.value, 'formData')
    uni.requestSubscribeMessage({
      tmplIds: ['0nIVHQNvIco7zeTWv-W3_MbZFR9ibIXejLxh2OFo5mg'],
      success(res) {
        console.log(res)
        handleSubmitStore()
      },
      fail(res: any) {
        handleSubmitStore()
        console.log(res, 'res定远失败')
      }
    })
  },
  handleClose() {
    useModel.show.value = false
  },
  handleSelectType(e: { value: string }) {
    useModel.formData.value.house_relation = e?.value === 'AddOwner' ? 'Owner' : 'NonOwner'
    console.log(e)
  },
  handleSelectResidentialRelation() {
    if (useModel?.formData?.value?.asset_houses_id) {
      getApiLoading(Apis.Archive.HouseOccupants.GetOwner, {
        asset_houses_id: useModel?.formData?.value?.asset_houses_id
      }).then(res => {
        useModel.ownerList.value = res?.data || []
        console.log(res)
      })
    }
  }
}
