import { useWeAppAuthStore, useWorkStore } from '@/common'
import { getApiLoading } from '@/common/libraries/apiLoading'
import { showToast, showToastBack } from '@/common/libraries/naviHelper'
import { Apis } from '@/gen/Apis'
import useModel from './model'
const auth = useWeAppAuthStore()
const work = useWorkStore()
export default {
  unInit(){
    useModel.formData.value = { is_anonymous: false, is_self: '0' }
    work.selectWorkHouse = {}
  },
  init() {
    console.log('init', work?.selectWorkProject)
    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
      }
    }
    if (work?.selectWorkProject?.id) {
      useModel.formData.value = {
        ...useModel?.formData?.value,
        project_name: work?.selectWorkProject?.name,
        asset_projects_id: work?.selectWorkProject?.id
      }
    }
  },
  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() {
    console.log('handleSubmit', useModel.formData.value)
    if (!useModel?.formData?.value?.type) {
      return showToast('请选择工单类型！')
    }
    if (
      useModel?.formData?.value?.location !== 'MyHome' &&
      !useModel.formData.value.asset_projects_id
    ) {
      return showToast('请选择项目！')
    }
    if (
      useModel?.formData?.value?.location === 'MyHome' &&
      !useModel.formData.value.asset_houses_id
    ) {
      return showToast('请选择房屋！')
    }
    if (
      useModel?.formData?.value?.location === 'MyHome' &&
      !useModel?.formData.value?.reporter_phone
    ) {
      return showToast('请输入联系人电话！')
    }
    if (!useModel?.formData?.value?.content) {
      return showToast('请输入问题描述！')
    }
    // if (
    //   useModel?.formData?.value?.location === 'MyHome' &&
    //   !useModel.formData.value.expect_start_time
    // ) {
    //   return showToast('请选择上门时间！')
    // }
    // if (!useModel?.formData?.value?.is_self) {
    //   return showToast('请选择处理方！')
    // }
    getApiLoading(Apis.WorkOrder.HouseWorkOrders.Store, useModel.formData.value).then(res => {
      showToastBack('提交成功！', 1, true)
      console.log(res)
    })
  },

  handleSwitchChange(e: any) {
    useModel.formData.value.is_anonymous = e?.detail?.value
    console.log(e, 'e')
  }
}
