<!--
 * @Descripttion: 地球-浙江
 * @Author: sujunling
 * @Date: 2025-05-19 09:10:25
-->
<template>
  <div class="widget-constructionProgressSimulation">
    <LeftBox ref="LeftBox" />
    <RightBox ref="RightBox" />
    <BottomBox ref="BottomBox" />
    <div class="modelListDiv">
      <i>大公桥调蓄池施工进度模拟</i>
      <b @click="start"><span class="el-icon-loading" v-if="startType"></span>{{ title }}</b>
    </div>
  </div>
</template>

<script>
import LeftBox from './LeftBox.vue'
import RightBox from './RightBox.vue'
import BottomBox from './BottomBox.vue'
import { getResultList } from 'staticPub/config'
import CesiumDraw from '@/views/pipelineDefect/common/cesiumDraw'

export default {
  name: 'constructionProgressSimulation',
  components: { LeftBox, RightBox, BottomBox },
  data() {
    return {
      title: '开始模拟',
      startType: false,
      data: {
        c_puct: Math.sqrt(2),
        n_playout: 2000,
        start_date: '2025-05-21T05:41:33.376Z',
        conversion_date: '2025-05-21T05:41:33.376Z',
        rain_intervals: []
      }
    }
  },
  mounted() {
    this.changeDom()
    this.addFile()
    window.init = () => {
      var data = {
        c_puct: 1.4142135623730951,
        n_playout: 2000,
        start_date: '2025-05-01 00:00:00',
        conversion_date: '2025-05-05 00:00:00',
        rain_intervals: [
          { start: '2025-05-01 00:00:00', end: '2025-05-02 00:00:00' },
          { start: '2025-05-07 00:00:00', end: '2025-05-08 00:00:00' }
        ]
      }
      this.getData(data)
    }
  },

  methods: {
    start() {
      var l = this.$refs.LeftBox.stopDateList
      if (l && l.length) {
        l.map((i) => {
          if (i && i.value) {
            this.data.rain_intervals.push({
              start: i.value[0],
              end: i.value[1]
            })
          }
        })
      } else {
        return this.$message.error('请填写完整的计划时间！')
      }
      var l1 = this.$refs.RightBox.planDateList
      if (l1 && l1.length && l1.length == 2) {
        l1.map((i, k) => {
          if (i && i.value && !k) {
            if (i.file) {
              this.data.start_date = i.value
            } else {
              return this.$message.error('请上传施工计划表格！')
            }
          } else if (i && i.value && k) {
            if (i.file) {
              this.data.conversion_date = i.value
            } else {
              return this.$message.error('请上传施工调整计划表格！')
            }
          }
        })
      } else {
        return this.$message.error('请填写完整的开始计划时间和调整计划时间！')
      }
      this.title = '分析中请等待'
      this.startType = true
      this.getData(this.data)
    },

    getData(data) {
      fetch(getResultList.sgjdmn_analysis, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
      })
        .then((e) => {
          return e.json()
        })
        .then((r) => {
          console.log('分析结果:', r)
          this.startType = false
          this.title = '开始模拟'
          if (r && r.all_results && r.all_results.length) {
            this.$refs.BottomBox.changeTable(r.all_results, r.best_schedule)
          }
        })
    },

    /**
     *添加时间段
     */
    addPlanDate() {
      const item = {
        value: ''
      }
      this.planDateList.push(item)
    },

    changeDom() {
      var dom = document.querySelector('.widget-SectorToolbar')
      if (dom) {
        dom.style.display = 'block'
      }
      var dom2 = document.querySelector('.widget-Legend')
      if (dom2) {
        dom2.style.display = 'block'
      }
    },

    addFile() {
      fetch(getResultList.sgjdmn_init)
        .then((e) => {
          return e.json()
        })
        .then((r) => {
          console.log('分析结果:', r)
          if (r.code == 1) {
            if (r.result.outcome && r.result.outcome.all_results && r.result.outcome.all_results.length) {
              this.$refs.BottomBox.changeTable(r.result.outcome.all_results, r.result.outcome.best_schedule)
            }
            if (r.result.request) {
              this.$refs.RightBox.changeTable(r.result.request)
            }
            if (r.result.request && r.result.request.rain_intervals && r.result.request.rain_intervals.length) {
              this.$refs.LeftBox.changeTable(r.result.request.rain_intervals)
            }
          }
        })
    },

    changeTable(r) {},

  }
}
</script>
</script>

<style scoped>
.modelListDiv b {
  font-weight: bold;
  font-size: 16px;
  color: #feffff;
  width: 114px;
  height: 34px;
  background: #2187d3;
  border-radius: 4px;
  opacity: 0.9;
  line-height: 34px;
  position: absolute;
  left: 280px;
  top: 62px;
  text-align: center;
  display: inline-block;
  cursor: pointer;
}
.modelListDiv {
  position: fixed;
  top: 70px;
  left: 42%;
  z-index: 9;
}
.modelListDiv i {
  font-weight: bold;
  font-size: 20px;
  color: #feffff;
  width: 395px;
  height: 38px;
  line-height: 38px;
  background: url('~@/assets/images/jczc/title.png') no-repeat center;
  background-size: 395px 38px;
  position: absolute;
  left: -72px;
  top: 60px;
  text-align: center;
  display: inline-block;
}
</style>