import { useWorkStore } from '@/common'
import { getApiLoading } from '@/common/libraries/apiLoading'
import { showToast } from '@/common/libraries/naviHelper'
import { Apis } from '@/gen/Apis'
import useModel from './model'
const work = useWorkStore()
export default {
  init() {
    console.log('init', work?.selectWorkHouse)
    useModel.formData.value = {
      ...useModel.formData.value
    }
    if (work?.selectWorkHouse?.id) {
      useModel.formData.value = {
        ...useModel.formData.value,
        asset_houses_id: work?.selectWorkHouse?.id,
        full_name: work?.selectWorkHouse?.full_name
      }
    }
  },
   handleSelectProject(data:any){
      let obj = data[3]
       useModel.formData.value = {
          ...useModel?.formData?.value,
          asset_houses_id: obj?.id,
          full_name: obj?.full_name
        }
      console.log(data, 'data')
    },
  handleSubmit() {
    let data = useModel?.formData?.value
    if (!data?.asset_houses_id) {
      return showToast('请选择房屋')
    }
    if (!data?.type) {
      return showToast('请选择类型')
    }
    if (data.type === 'AddOwner') {
      data.house_relation = 'Owner'
      data.residential_relation = 'PropertyOwner'
    } else {
      data.house_relation = 'Owner'
    }
    if (!data?.residential_relation) {
      return showToast('请选择居住关系')
    }
    if (!data?.name) {
      return showToast('请填写姓名')
    }
    if (!data?.phone) {
      return showToast('请填写手机号')
    }
    if (!data?.id_card) {
      return showToast('请填写身份证号')
    }
    if (!data?.card_front_image?.length || !data?.card_back_image?.length) {
      return showToast('请上传身份证正反面')
    }
    if (data.type === 'AddOwner' && !data?.ownership_info?.length) {
      return showToast('请上传房产证')
    }

    getApiLoading(Apis.Archive.HouseRegisters.RegisterHouse, {
      ...data,
      customer_info: [data]
    }).then(res => {
      uni.redirectTo({
        url: '/CUSTOMER/bind_house_form/success'
      })
      console.log(res)
    })
  },
  handleToSelectHouse() {
    uni.navigateTo({
      url: '/pages/public/asset_houses/index?type=work_add'
    })
  }
}
