import useModel from './model'
import { useWeAppAuthStore, useWorkStore } from '@/common'
import { getApiLoading } from '@/common/libraries/apiLoading'
import { Apis } from '@/gen/Apis'
import { showToastBack, showToast } from '@/common/libraries/naviHelper'
const work = useWorkStore()
const getList = () => {
  getApiLoading(Apis.WorkOrder.HouseWorkOrders.List, useModel?.formData?.value).then(res => {
    useModel.listData.value = [...useModel.listData.value, ...res?.data]
    useModel.metaData.value = res?.meta
  })
}

export default {
  getList,
  init() {
    console.log('init', work?.selectWorkProject)
    if (work?.selectWorkProject?.id) {
      useModel.formData.value = {
        ...useModel?.formData?.value,
        project_name: work?.selectWorkProject?.name,
        asset_projects_id: work?.selectWorkProject?.id
      }
    }
  },
  handleSubmit() {
    let data = useModel?.formData?.value
    if (useModel?.formData?.value?.type === '申请关闭') {
      if (!data?.handled_result) {
        return showToast('请输入处理结果！')
      }
      uni.showModal({
        title: '提示',
        content: '确定要申请关闭？',
        success: function (res) {
          if (res.confirm) {
            getApiLoading(
              Apis.Emergency.EmergencyEvents.ApplyClose,
              useModel?.formData?.value
            ).then(res => {
              showToastBack('提交成功！', 1, true)
              console.log(res)
            })
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    if (useModel?.formData?.value?.type === '更新进度') {
      if (!data?.content) {
        return showToast('请输入跟进内容！')
      }
      getApiLoading(Apis.Emergency.EmergencyEventFollows.Store, useModel?.formData?.value).then(
        res => {
          showToastBack('提交成功！', 1, true)
          console.log(res)
        }
      )
    }
  }
}
