<template>
  <a-spin style="width: 100%" :tip="tip"> </a-spin>
</template>

<script>
import warnApi from '@/api/warnApi'
export default {
  name: 'DataNotReady',
  props: {
    jh: {
      type: String,
      default: ''
    },
    jhRecord: {
      type: Object,
      default: () => { }
    }
  },

  data () {
    return {
      tip: '正在准备历史数据',
      jhSnap: ''
    }
  },
  created () {
    this.jhSnap = this.jh.toString()
    warnApi.checkWarnReady({ jh: this.jhSnap })
      .then(res => {
        if (res.message === 'ready') {
          this.$emit('dataReady')
        } else {
          this.startTask()
        }
      }).catch(e => {
        this.$message.info(e.message)
      })
  },
  mounted () {
  },
  computed: {},
  watch: {},
  methods: {
    startTask () {
      warnApi.startWarnHistoryTask({ jh: this.jhSnap, ltd: this.jhRecord.wellRecord.ltd.toString() })
        .then(res => {
          this.$notification.warning({ message: '提示', description: `${this.jh}已开始计算历史报警信息` })
          this.reCheck()
        }).catch(e => {
          this.$message.info(e.message)
        })
    },
    reCheck () {
      if (this._isDestroyed) {
        return
      }
      warnApi.recheckWarnReady({ jh: this.jhSnap })
        .then(res => {
          if (res.message === 'ready') {
            this.$emit('dataReady')
          } else {
            this.tip = `正在准备历史数据，进度:${res.result}`
            setTimeout(() => {
              this.reCheck()
            }, 3000)
          }
        }).catch(e => {
          this.$message.info(e.message)
        })
    }
  }
}
</script>

<style lang="less" scoped>
</style>
