import useModel from './model'
import { useWeAppAuthStore, useWorkStore } from '@/common'
import { showDay } from '@/common/libraries/day'
import { getApiLoading } from '@/common/libraries/apiLoading'
import { Apis } from '@/gen/Apis'
import { showToastBack, showToast } from '@/common/libraries/naviHelper'
const work = useWorkStore()
const getShow = () => {
  getApiLoading(Apis.Emergency.EmergencyEvents.Show, useModel?.formData?.value).then(res => {
    useModel.formData.value = {
      ...res?.data,
      project_name: res?.data?.asset_project?.name,
      emergency_time: showDay(res?.data?.emergency_time)
    }
  })
}

export default {
  getShow,
  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 (!data?.asset_projects_id) {
      return showToast('请选择项目！')
    }
    if (!data?.name) {
      return showToast('请输入事件名称！')
    }
    if (!data?.two_emergency_categories_id) {
      return showToast('请选择分类！')
    }
    // let type = typeof data?.two_emergency_categories_id
    // console.log(type, 'type')
    if (!data?.two_emergency_categories_id || data?.two_emergency_categories_id?.length === 1) {
      return showToast('请选择2级分类！')
    }
    if (!data?.emergency_event_levels_id) {
      return showToast('请选择等级！')
    }
    if (!data?.emergency_event_levels_id) {
      return showToast('请选择等级！')
    }
    if (!data?.emergency_time) {
      return showToast('请选择发生时间！')
    }
    if (!data?.emergency_location) {
      return showToast('请输入事发地点！')
    }
    if (!data?.emergency_description) {
      return showToast('请输入事件描述！')
    }
    if (!data?.emergency_cause) {
      return showToast('请输入原因！')
    }
    if (!data?.cause_result) {
      return showToast('请设置结果！')
    }
    data.two_emergency_categories_id = data?.two_emergency_categories_id?.length
      ? data?.two_emergency_categories_id?.[1]
      : data?.two_emergency_categories_id
    getApiLoading(Apis.Emergency.EmergencyEvents.Update, data).then(res => {
      showToastBack('提交成功！', 1, true)
      console.log(res)
    })
  }
}
