<template>
  <m-grid
    :template="[
      'title title title',
      'left . right',
    ]"
    columns="33.3vw auto 33.3vw"
    rows="0.4rem auto"
    gap="0.05rem"
    :complete="complete"
  >
    <m-map ref="map" @complete="mapComplete" @event="handleMapClick"/>
    <m-title area="title" :bgImg="require('@/assets/images/title-bg.png')">{{currentStreetInfo.name}}城市运行平台</m-title>
    <m-animate enter="fadeInLeft" leave="fadeOutLeft">
      <LeftComponent v-show="!fullView" area="left"/>
    </m-animate>
    <m-animate enter="fadeInRight" leave="fadeOutRight">
      <RightComponent
        v-show="!fullView"
        area="right"
        @patrol="handleMapClick"
        @handle="handleListClick"
        @video="handleViewVideo"
      />
    </m-animate>
    <MapBtns v-model="fullView" @zoomin="zoom('in')" @zoomout="zoom('out')" />
    <m-modal v-model="detailModal" title="智能报警">
      <m-form :label-width=".6" :model="detailData" :layout="detailLayout" />
    </m-modal>
    <m-modal v-model="doneModal" title="已完成处置">
      <DoneComponent v-if="doneModal" :data="doneData" />
    </m-modal>
    <m-modal v-model="patrolModal" title="主动巡检">
      <m-form :label-width=".6" :model="patrolData" :layout="patrolLayout" />
    </m-modal>
    <m-modal title="指挥体系" :value="showCommandModal" @close="$store.commit('SET_COMMAND_MODAL', false)">
      <m-form :label-width=".7" :model="streetInfo[currentStreetInfo.id]" :layout="streetInfo[currentStreetInfo.id].layout" />
    </m-modal>
    <m-modal title="鹰眼监控" v-model="videoModal">
      <VideoComponent v-if="videoModal" :videoSrc="videoSrc" />
    </m-modal>
  </m-grid>
</template>

<script>
import LeftComponent from './components/left-component'
import RightComponent from './components/right-component'
import MapBtns from './components/map-btns'
import VideoComponent from './components/video-component'
import DoneComponent from './components/done-component'
import {mapState, mapActions} from 'vuex'
export default {
  name: 'Main',
  components: {
    LeftComponent,
    RightComponent,
    MapBtns,
    VideoComponent,
    DoneComponent,
  },
  data() {
    return {
      timer: null,
      complete: false,
      fullView: false,
      videoModal: false,
      videoSrc: null,
      detailModal: false,
      patrolModal: false,
      smartPoints: null,
      patrolPoints: null,
      detailData: {},
      patrolData: {},
      detailLayout: [
        {
          communityName: {
            label: '小区名称',
            width: 12,
          },
          check_type: {
            label: '发现方式',
            width: 12,
            format: val => val || '智能发现'
          },
        },
        {
          types: {
            label: '协同事项',
            width: 12,
            format: val => val && this.$com.transType(val)
          },
          receive_property_name: {
            label: '响应部门',
            width: 12,
          },
        },
        {
          device_name: {
            label: '设备地点',
            width: 24,
          },
        },
      ],
      patrolLayout: [
        {
          communityName: {
            label: '小区名称',
            width: 12,
          },
          orderTypeName: {
            label: '整改事项',
            width: 12,
          },
        },
        {
          checkTime: {
            label: '发现时间',
            width: 12,
          },
          checkStateName: {
            label: '整改状态',
            width: 12,
          },
        },
        {
          checkPersonName: {
            label: '上报人',
            width: 12,
          },
          hoName: {
            label: '房办名称',
            width: 12,
          },
        },
        {
          tcName: {
            label: '检查主题',
            width: 24,
          },
        },
      ],
      doneModal: false,
      doneData: {},
    }
  },
  computed: {
    ...mapState([
      'currentStreetInfo',
      'streetInfo',
      'curSmartType',
      'showCommandModal',
      'patrolList',
      'handleList',
      'handleCurTab',
    ])
  },
  beforeCreate() {
    const {name} = this.$route.query
    this.$store.commit('SET_CURRENT_STREET', name)
  },
  beforeDestroy() {
    clearInterval(this.timer)
    this.timer = null
  },
  methods: {
    ...mapActions([
      'initData', // 初始化获取首次加载所需的所有数据
      'getDiscoverTrend',
      'getRepairRate',
      'getDiscoverInfo',
      'getRepairInfo',
      'getHandleList',
      'getDoneList',
      'getCheckSum',
      'getPatrolList',
    ]),
    mapComplete() {
      this.init()
      this.initData()
      this.complete = true
      this.timer = setInterval(() => {
        this.getDiscoverTrend()
        this.getRepairRate()
        this.getDiscoverInfo()
        this.getRepairInfo()
        this.getCheckSum()
        this.getPatrolList()
        this.getHandleList()
        this.getDoneList()
      }, 1000 * 60 * 1)
    },
    init() {
      const {name, id} = this.currentStreetInfo
      document.querySelector('title').innerText = `${name}城市运行平台`
      const {map} = this.$refs
      this.initMapFocus()
      map.addPolygon({paths: this.$store.state.streetPaths[id], fillColor: 'rgba(51,145,255,.3)'})
      map.addBoundary({name: name})
    },
    initMapFocus() {
      const {center} = this.currentStreetInfo
      this.$refs.map.focus(...center, 5.5)
    },
    handleMapClick(data) {
      if (data.key === 'patrol') {
        this.patrolData = data
        this.patrolModal = true
      } else {
        this.detailData = data
        this.detailModal = true
      }
      // this.$refs.map.focus(data.x || data.gpsx, data.y || data.gpsy, 8)
    },
    handleListClick(data) {
      if (this.handleCurTab === '处置中') {
        this.detailData = data
        this.detailModal = true
      } else {
        this.doneData = data
        this.doneModal = true
      }
    },
    drawSmartPoints(data) {
      const {addPoint, removePoint} = this.$refs.map
      removePoint(this.smartPoints)
      this.smartPoints = addPoint({
        key: 'smart',
        size: [14, 16],
        data: data.map(item => {
          return {
            ...item,
            icon: item.done_time ? 'blue4.png' : 'red4.png',
          }
        }),
      })
    },
    handleViewVideo(src) {
      this.videoSrc = src
      this.videoModal = true
    },
    zoom(type) {
      if (type === 'in') this.$refs.map.zoomIn()
      else this.$refs.map.zoomOut()
    },
  },
  watch: {
    patrolList(cur) {
      const {addPoint, removePoint} = this.$refs.map
      removePoint(this.patrolPoints)
      this.patrolPoints = addPoint({
        key: 'patrol',
        size: 14,
        icon: 'red.png',
        data: cur,
      })
    },
    curSmartType(cur) {
      let data = this.handleList
      if (cur) {
        data = this.handleList.filter(item => item.types === cur)
      }
      this.drawSmartPoints(data)
    },
    handleList(cur) {
      let data = cur
      if (this.curSmartType) {
        data = cur.filter(item => item.types === this.curSmartType)
      }
      this.drawSmartPoints(data)
    },
  }
}
</script>
