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()

export default {
  init() {
    useModel.formData.value = {
      ...useModel.formData.value,
      // worker_phone: auth?.data?.user?.phone || useModel.formData.value?.worker_phone || '',
      worker_name: auth?.data?.user?.name || useModel.formData.value?.worker_name || ''
    }
  },
  handleSubmit() {
    let data = useModel?.formData?.value
    if (!userLoginStatus?.onBindPhone()) {
      return false
    }
    if (!data?.worker_name) {
      return showToast('请设置工人姓名!')
    }
    if (!data?.worker_phone) {
      return showToast('请设置工人手机号!')
    }
    if (!data?.card_type) {
      return showToast('请选择工人证件类型!')
    }
    if (!data?.id_card) {
      return showToast('请设置工人证件号!')
    }
    if (!data?.card_front?.length || !data?.card_back?.length) {
      return showToast('请上传工人证件照片!')
    }
    if (!data?.worker_photo?.length) {
      return showToast('请上传工人照片!')
    }
    if (!data?.valid_from || !data?.valid_to) {
      return showToast('请设置通行有效期!')
    }
    getApiLoading(Apis.Renovation.RenovationWorkers.Store, useModel?.formData?.value).then(res => {
      if (useModel?.formData?.value?.type === 'share') {
        showToast('提交成功!', () => {
          uni.switchTab({
            url: '/pages/index/index'
          })
        })
      } else {
        showToastBack('提交成功!', 1, true)
      }
      console.log(res)
    })
  }
}
