<template>
  <div>
    <!--筛选条件-->
    <div class="map-search-div">
      <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
        <el-row>
          <el-form-item label="搜索：">
            <el-input v-model.trim="listQuery.keywords" placeholder="窨井编号/地址" clearable/>
          </el-form-item>
          <el-form-item v-if="wellTypeList.length>1">
            <el-select v-model="listQuery.wellType" placeholder="选择窨井类型" clearable>
              <el-option
                v-for="item in wellTypeList"
                :key="item.value"
                :label="item.name"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <dept-select v-model="listQuery.deptid" :need-top="deptShowTop" dept-type="03" placeholder="选择权属单位"/>
          </el-form-item>
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
          <el-button v-show="showClearBtn" class="filter-item" @click="clearSearch">清除查询</el-button>
          <el-form-item style="margin-left:10px">
            <el-checkbox v-model="showAll">显示全部窨井</el-checkbox>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <!--倒计时刷新-->
    <div v-if="refreshType=='clock'" class="refresh-div">
      <span class="font-red">{{ count }}</span>  s后刷新<i class="el-icon-refresh" @click="refreshAlarm"/>
      <el-button :type="showAlarm?'danger':'info'" icon="el-icon-bell" circle @click="showAlarm=!showAlarm"/>
    </div>
    <div v-if="refreshType=='websocket'" class="function-div">
      <el-button :type="showAlarm?'danger':'info'" icon="el-icon-bell" circle @click="showAlarm=!showAlarm"/>
      <el-button type="primary" icon="el-icon-refresh-right" circle @click="refreshAlarm"/>
    </div>
    <div v-if="showAlarm && alarmWells.length>0" class="map-alarm-div">
      <div class="map-alarm-div-header">
        告警列表
        <span class="icon-right" @click="tableShow=!tableShow">
          <i v-if="tableShow" class="el-icon-arrow-up"/>
          <i v-else class="el-icon-arrow-down"/>
        </span>
      </div>
      <transition name="el-collapse-transition">
        <el-scrollbar
          v-show="tableShow"
          :style="{visibility: tableShow?'visible':'hidden'}"
          :class="{moredatascollor:alarmList.length>6}"
          :native="false">
          <el-table
            :data="alarmList"
            class="alarm-list-table"
            border
            @row-click="alarmRowClick">
            <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row[column.value] }}
              </template>
            </el-table-column>
          </el-table>
        </el-scrollbar>
      </transition>
    </div>
    <div v-loading="loading" class="overview-map-container">
      <!--地图-->
      <cmap ref="map" :center="center" :zoom="zoom" :alarm-markers="alarmWells" class="map-demo" @ready="readyMap"/>
    </div>
  </div>
</template>

<script>
import SelectTree from '@/components/SelectTree/singleSelect'
import { getWellType } from '@/api/well'
import { getAlarmsNow } from '@/api/overview'
import DeptSelect from '../../components/DeptSelect/index'
import Cmap from '../../components/CMap/components/cmap'
import CmapMarker from '../../components/CMap/components/cmapMarker'
export default {
  name: 'Overview',
  components: { CmapMarker, Cmap, DeptSelect, SelectTree },
  data() {
    return {
      refreshType: 'clock', // 刷新数据方式：clock或websocket
      listQuery: {
        keywords: '', // 关键字
        wellType: '', // 井类型
        deptid: '', // 组织机构
        isAlarm: '1' // 是否报警
      }, // 筛选条件
      columns: [
        {
          text: '窨井编号',
          value: 'wellCode',
          width: 100,
          align: 'center'
        },
        {
          text: '设备编号',
          value: 'devcode',
          width: 120,
          align: 'center'
        },
        {
          text: '告警原因',
          value: 'alarmContent',
          align: 'center',
          width: 120
        },
        {
          text: '时间',
          value: 'alarmTime',
          width: 180,
          align: 'center'
        }
      ], // 告警列表显示列
      tableShow: true, // 是否显示告警列表
      tableIcon: 'el-icon-arrow-up',
      showWellType: this.showWellType(), // 是否显示井类型下拉
      wellTypeList: [], // 井类型列表
      deptProps: {
        parent: 'pid',
        value: 'id',
        label: 'name',
        children: 'children'
      }, // 权属单位树形下拉菜单
      deptTreeList: null, // 组织树列表数据
      showDeptTree: 0, // 是否显示权属单位下拉,0不显示，1显示树，2显示平面
      commonIcon: 'well-common-green', // 通用图标 绿
      commonIconAlarm: 'well-common-red', // 通用图标 红
      center: { lng: this.$store.getters.lng, lat: this.$store.getters.lat }, // 地图中心
      zoom: 12, // 地图缩放比例
      markers: [], // 井列表标注marker
      alarmList: [], // 报警列表
      alarmListOri: [], // 报警列表
      alarmWells: [], // 显示报警井列表
      alarmWellsOri: [], // 原始报警井列表
      offset: [-10, -10], // 偏移量
      alarmOffset: [-15, -30], // 偏移量
      wellInfo: {
        wellCode: '',
        position: '',
        wellTypeName: '',
        deptName: '',
        bfztName: '',
        deep: ''
      }, // 显示井详细信息气泡内容
      alarmInfo: {
        wellCode: '',
        deptName: '',
        position: '',
        deep: '',
        alarms: []
      }, // 显示报警详情气泡内容
      currentWindow: {
        visible: false, // 窗体显示与否
        position: { lng: this.$store.getters.lng, lat: this.$store.getters.lat },
        windowType: 'info' // 窗体类型：详情info或报警alarm
      }, // 当前窗体属性
      infoWindowClass: 'nomal-info-window',
      deptShowTop: false, // 是否显示顶级
      clock: null, // 计时器
      count: 30, // 倒计时
      refreshTimes: 30, // 多少s数据
      showAlarm: true, // 是否显示报警
      hasAlarm: false, // 是否有报警,
      showAll: false, // 是否显示全部井（包含报警和不报警，为false时只显示报警）
      firstAmount: true, // 是否第一次加载井数据
      loading: false, // 加载图标是否显示
      massMarks: null, // 海量点
      showClearBtn: false // 是否显示清除查询按钮
    }
  },
  watch: {
    showAll(val) {
      if (val) { // 显示全部
        this.listQuery.isAlarm = '0'
        // 先清空查询条件
        this.listQuery.deptid = ''
        this.listQuery.keywords = ''
        this.listQuery.wellType = ''
        // 加载海量点
        this.$refs.map.renderPoi()
        // }
      } else { // 仅显示报警
        this.listQuery.isAlarm = '1'
        // 清除海量点
        this.$refs.map.cleanPoi()
      }
    },
    showAlarm(val) {
      if (val) {
        this.alarmWells = this.alarmWellsOri
        this.alarmList = this.alarmListOri
      } else {
        this.alarmWells = []
        this.alarmList = []
      }
    }
  },
  mounted() {
    const that = this
    that.fetchWellType()
  },
  activated() {
    this.refreshAlarm()
  },
  methods: {
    // 地图准备好后的操作
    readyMap() {
      console.log('readyMap')
      const that = this
      // that.getWellList() // 获取报警的井信息
      that.refreshAlarm() // 刷新报警内容
      if (this.showAll) {
        this.$refs.map.renderPoi()
      }
      if (this.refreshType === 'clock') { // 如果需要倒计时刷新的
        setTimeout(function() {
          that.countDown()
        }, 1000)
      }
    },
    // 获取井类型，显示井类型下拉
    fetchWellType() {
      getWellType().then(response => {
        this.wellTypeList = []
        // 如果该用户支持的井类型只有一个，则不显示该筛选框
        const wellTypes = this.$store.getters.wellTypes
        for (const wellType of response.data) {
          if (wellTypes.indexOf(wellType.value) !== -1) {
            this.wellTypeList.push(wellType)
          }
        }
        if (this.wellTypeList.length <= 1) {
          this.showWellType = false
        }
      })
    },
    // 数据查询
    search(showMessage = true) {
      const keywords = this.listQuery.keywords
      const deptid = this.listQuery.deptid
      const wellType = this.listQuery.wellType
      if (keywords === '' && deptid === '' && wellType === '') {
        this.$message.warning('查询条件不能为空')
      } else {
        this.showClearBtn = true
        this.filterIcon(showMessage)
      }
    },
    // 清除查询
    clearSearch() {
      this.listQuery.wellType = ''
      this.listQuery.deptid = ''
      this.listQuery.keywords = ''
      this.filterIcon()
      this.showClearBtn = false
    },
    // 过滤条件
    filterIcon(showMessage = false) {
      this.currentWindow.visible = false // 弹窗隐藏
      const keywords = this.listQuery.keywords
      const deptid = this.listQuery.deptid
      const wellType = this.listQuery.wellType
      console.log('markers length:', this.markers.length)
      const showWellCodes = []
      let center = null
      if (this.showAll) {
        this.resetMassMarker()
      }
      // 对报警标记进行过滤：
      // 如果地图上需要显示报警
      if (this.showAlarm) {
        this.alarmWells = []
        // 查询报警的井，是否匹配
        for (const marker of this.alarmWellsOri) {
          let show = true
          // 关键字不为空，且没有匹配成功，不显示
          if (keywords && keywords !== '' && !(marker.wellCode.indexOf(keywords) !== -1 || marker.positionInfo.indexOf(keywords) !== -1)) {
            show = false
          }
          // 部门不为空， 且没有匹配成功
          if (deptid && deptid !== '' && marker.deptid !== deptid) {
            show = false
          }
          // 井类型不为空，且没有匹配成功
          if (wellType && wellType !== '' && marker.wellType !== wellType) {
            show = false
          }
          // 需要显示，加到alarmWells数组中
          if (show) {
            this.alarmWells.push(marker)
            center = marker.position
            showWellCodes.push(marker.wellCode)
          }
        }
        // 过滤alarmList列表
        this.alarmList = []
        if (showWellCodes.length > 0) {
          for (const alarm of this.alarmListOri) {
            const findFlag = showWellCodes.findIndex(code => code === alarm.wellCode)
            if (findFlag !== -1) {
              this.alarmList.push(alarm)
            }
          }
        }
      } else { // 如果地图上不显示报警
        this.alarmWells = []
        this.alarmList = []
      }
      // 如果没有找到符合要求的井，将全部查询结果隐藏
      if (center !== null) {
        this.center = center
      }
    },
    // 过滤海量点
    resetMassMarker() {
      // 调用子组件，过滤海量点
      this.$refs.map.filterPoi(this.listQuery)
    },
    // 刷新报警列表
    refreshAlarm() {
      console.log('refresh Alarm')
      this.count = this.refreshTimes
      getAlarmsNow().then(response => {
        if (response.code === 200) {
          // 获取当前报警列表
          this.alarmListOri = response.data
          if (this.alarmList.length > 0) {
            this.hasAlarm = true
          }
          if (this.showAlarm) {
            this.alarmList = this.alarmListOri
          }
          const alarmWellSets = []
          for (const alarm of response.data) {
            const index = alarmWellSets.findIndex(item => {
              return item.wellCode === alarm.wellCode
            })
            if (index === -1) {
              alarmWellSets.push({
                wellCode: alarm.wellCode,
                wellId: alarm.wellId,
                position: { lng: parseFloat(alarm.coordinateX), lat: parseFloat(alarm.coordinateY) },
                wellType: alarm.wellType,
                deptid: alarm.deptid,
                positionInfo: alarm.position,
                visible: true
              })
            }
          }
          this.alarmWellsOri = alarmWellSets
          // 如果当前页面关闭了显示报警，则不更新alarmWells
          if (this.showAlarm) {
            this.alarmWells = alarmWellSets
          }
        }
      })
    },
    // 点击报警列表项
    alarmRowClick(row, column, event) {
      const wellId = row.wellId
      this.$refs.map.openAlarmInfoWindow(wellId)
    },
    // 倒计时函数
    countDown() {
      if (this.clock == null) {
        this.clock = window.setInterval(() => {
          this.count--
          if (this.count < 0) { // 当倒计时小于0时清除定时器
            this.refreshAlarm()
            this.count = this.refreshTimes
          }
        }, 1000)
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
// 查询框
.map-search-div{
  position: absolute;
  z-index: 500;
  padding: 5px 20px;
  background-color: rgba(244, 244, 244, 0.9);
  /*left: 90px;*/
  .el-form-item{
    margin-bottom: 0px;
  }
}
// 报警列表
.map-alarm-div{
  position: absolute;
  z-index: 500;
  background-color: rgba(255, 234, 241,0.8);
  top: 60px;
  left: 10px;
  .map-alarm-div-header{
    line-height: 40px;
    width: 504px;
    padding-left: 10px;
    .icon-right{
      position: absolute;
      right: 15px;
    }
    .icon-right:hover{
      color: #409EFF;
      cursor: pointer;
    }
  }
  .el-scrollbar {
    /*height: 200px;*/
    width: 100%;
  }
  .moredatascollor{
    height: 200px;
  }
  .el-scrollbar__wrap {
    overflow-y: auto;
    overflow-x: hidden;
    margin-bottom: 0px !important;
}

.el-table th{
/*background-color: rgba(255, 229, 230, 0.8);*/
    padding: 7px 0px;
  }
  .el-table td{
    /*background-color: rgba(255, 234, 241, 0.8);*/
    padding: 5px 0px;
    /*line-height: 1;*/
  }
  .el-table td:hover{
    /*background-color: rgba(255, 234, 241, 0.8);*/
  }
  .transition-box {
    margin-bottom: 10px;
    width: 200px;
    height: 100px;
    border-radius: 4px;
    background-color: #409EFF;
    text-align: center;
    color: #fff;
    padding: 40px 20px;
    box-sizing: border-box;
    margin-right: 20px;
  }
}
// 刷新框
.function-div{
  position: absolute;
  right: 10px;
  top: 7px;
  z-index: 1100;
  padding: 10px;
  color: #ce8b74;
  font-size: 14px;
  /*background-color: rgba(244, 233, 230, 1.0);*/
  .font-red{
    color: red;
    font-weight: bold;
  }
  .el-icon-refresh:hover{
    color: red;
    font-weight: bold;
    cursor: pointer;
  }
}
// 刷新框
.refresh-div{
  position: absolute;
  right: 10px;
  top: 7px;
  z-index: 1100;
  padding: 10px;
  color: #ce8b74;
  font-size: 14px;
  background-color: rgba(244, 233, 230, 1.0);
  .font-red{
    color: red;
    font-weight: bold;
  }
  .el-icon-refresh:hover{
    color: red;
    font-weight: bold;
    cursor: pointer;
  }
}
// 地图
.overview-map-container{
  width: 100%;
  padding: 5px;
  .map-demo{
    width: 100%;
    height: calc(100vh - 144px);
    .svg-icon{
      width: 20px;
      height: 20px;
    }
    .alarm-icon{
      width: 29px;
      height: 30px;
    }
    .nomal-info-window{
      background-color: pink;
    }
    .info-window{
      max-width: 250px;
      /*background-color: lightcyan;*/
      .info-header{
        line-height: 30px;
        font-weight: bold;
        font-size: 16px;
        /*background-color: #eaf4ff;*/
      }
      .info-body{
        padding: 0px;
        line-height: 23px;
        font-size: 14px;
      }
    }
    .alarm-window{
      max-width: 250px;
      /*background-color: #ffeaf1;*/
      .alarm-header {
        padding: 0px;
        line-height: 30px;
        color: red;
        font-weight: bold;
        font-size: 16px;
        /*background-color: #ffecec;*/
      }
      .alarm-body{
        padding: 0px;
        line-height: 23px;
        font-size: 14px;
        .alarm-red{
          color: #ff0000;
        }
        .divider{
          width:100%;
          height:5px;
          margin-bottom: 5px;
          border-bottom:solid rgb(184, 184, 184) 1px
        }
      }
    }
  }
}
  .el-divider--horizontal{
    margin: 5px 0;
  }
</style>
