import useModel from './model'
import { getApiLoading } from '@/common/libraries/apiLoading'
import { Apis } from '@/gen/Apis'

const getShow = () => {
  getApiLoading(Apis.Emergency.EmergencyEvents.Show, useModel?.formData?.value).then(res => {
    useModel.showData.value = res?.data
    useModel.formDataLogs.value = { emergency_events_id: res?.data?.id, page: 1 }
    getLogs()
  })
}

const getLogs = () => {
  getApiLoading(Apis.Emergency.EmergencyEventFollows.List, useModel?.formDataLogs?.value).then(
    res => {
      useModel.logsData.value = [...useModel?.logsData?.value, ...res?.data]
      useModel.metaData.value = res?.meta
    }
  )
}

export default {
  getShow,
  handlePreview(index?: number | string) {
    uni.previewImage({
      urls: useModel?.showData?.value?.emergency_images?.map((i: { url: string }) => i?.url || ''),
      current: index
    })
  },
  handleLoad(page: number) {
    console.log(page)
    useModel.formDataLogs.value.page = page
    getLogs()
  },
  handleClose() {
    uni.navigateTo({
      url: `/ME/emergency/update/index?id=${useModel?.showData?.value?.id}&type=申请关闭`
    })
  },
  handleUpdate() {
    uni.navigateTo({
      url: `/ME/emergency/update/index?id=${useModel?.showData?.value?.id}&type=更新进度`
    })
  },
  handleUpdateFollow() {
    uni.navigateTo({
      url: `/ME/emergency/change/index?id=${useModel?.showData?.value?.id}`
    })
  }
}
