import { Apis } from '@/gen/Apis'
import { getApiLoading } from '@/common/libraries/apiLoading'
import userLoginStatus from '@/common/libraries/userUserLogin'
import { showToastBack, showToast } from '@/common/libraries/naviHelper'
import { useWeAppAuthStore, useWorkStore } from '@/common'
import useModel from './model'
const work = useWorkStore()
const auth = useWeAppAuthStore()
const handleSave = (type?: number) => {
  // 暂存
  getApiLoading(Apis.Renovation.RenovationApplies.Update, {
    ...useModel?.formData?.value,
    is_deposit: useModel?.formData?.value?.is_deposit ? 1 : 0,
    status: 'Draft'
  }).then(res => {
    if (type) {
      showToast('提交成功!', () => {
        uni.switchTab({
          url: '/pages/index/index'
        })
      })
    } else {
      showToast('暂存成功!')
    }
    console.log(res)
  })
}

export default {
  init() {
    if (work?.selectWorkHouse?.asset_house) {
      useModel.formData.value = {
        ...useModel.formData.value,
        asset_houses_id: work?.selectWorkHouse?.asset_house?.id,
        full_name: work?.selectWorkHouse?.asset_house?.full_name
      }
    } else {
      useModel.formData.value = {
        ...useModel.formData.value,
        asset_houses_id: auth?.data?.selected_house?.id,
        full_name: auth?.data?.selected_house?.full_name
      }
    }
  },
  getShow(res: { id: number }) {
    getApiLoading(Apis.Renovation.RenovationApplies.Show, { id: res?.id }).then(res => {
      useModel.showData.value = res?.data
      useModel.formData.value = {
        ...res?.data,
        is_deposit: res?.data?.is_deposit ? true : false
      }
      if (res?.data?.status !== 'Draft') {
        showToast('当前装修申请已完成!', () => {
          uni.switchTab({
            url: '/pages/index/index'
          })
        })
      }
      console.log(res)
    })
  },
  handleToSelectHouse() {
    uni.navigateTo({
      url: '/INDEX/asset_houses/index?type=work_add'
    })
  },
  handleNextSubmit() {
    let data = useModel?.formData?.value
    if (!data?.construction_principal_name) {
      return showToast('请输入施工负责人名称!')
    }
    if (!data?.construction_principal_phone) {
      return showToast('请输入施工负责人手机号!')
    }
    if (!data?.construction_principal_card_type) {
      return showToast('请选择施工负责人证件类型!')
    }
    if (!data?.construction_principal_id_card) {
      return showToast('请输入施工负责人证件号!')
    }
    if (
      !data?.construction_principal_id_card_front?.length ||
      !data?.construction_principal_id_card_back?.length
    ) {
      return showToast('请上传施工负责人证件!')
    }
    if (useModel?.formData?.value?.type === 'RenovationCompany') {
      // if (!data?.company_principal_name || !data?.company_principal_phone) {
      //   return showToast('请输入装修公司负责人名称/手机号!')
      // }
      if (!data?.company_name) {
        return showToast('请设置装修公司全称!')
      }
      if (!data?.company_business_license_num) {
        return showToast('请输入社会统一信用代码!')
      }
      if (!data?.company_business_license?.length) {
        return showToast('请上传装修公司营业执照!')
      }
      if (!data?.company_asset_certificate?.length) {
        return showToast('请上传装修公司资质证明!')
      }
      if (!data?.company_power_attorney?.length) {
        return showToast('请上传装修授权书!')
      }
    }

    if (!data?.construction_start_date || !data?.construction_end_date) {
      return showToast('请选择施工时间!')
    }
    if (!data?.renovation_content) {
      return showToast('请选择装修内容!')
    }
    handleSave(1)
  },
  handleSave
}
