<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
  <div style="padding-left: 30px">
    <span style="font-weight: bold">通道：</span>
    <vxe-select v-model="tunnelId" placeholder="请选择使用通道" style="width: 12%" clearable>
      <vxe-option v-for="item in tunnelList" :key="item.id" :value="item.id" :label="item.tunnelName" />
    </vxe-select>
    <span style="font-weight: bold">设备：</span>
    <vxe-select v-model="eqId" placeholder="请选择查询设备" style="width: 12%" clearable>
      <vxe-option v-for="(item,index) in eqTableData" :key="index" :value="index" :label="item.name" />
    </vxe-select>
    <div style="display: inline-block">
      <vxe-toolbar>
        <template v-slot:buttons>
          <vxe-button status="primary" @click="searchPointData()">查询</vxe-button>
          <vxe-button status="primary" icon="fa fa-plus" @click="insertEvent()">新增</vxe-button>
          <vxe-button status="primary" icon="fa fa-files-o" @click="multiAdd=true">批量新增</vxe-button>
          <vxe-button status="primary" icon="fa fa-files-o" @click="batchDeleteSpoint">批量删除</vxe-button>
          <vxe-button status="primary" icon="vxe-icon--download" @click="exportDataEvent">导出</vxe-button>
          <vxe-button status="primary" icon="vxe-icon--refresh roll" @click="refreshPoint()">刷新数据点</vxe-button>
        </template>
      </vxe-toolbar>

    </div>
    <vxe-table
      ref="xTable"
      border
      stripe
      resizable
      show-overflow
      keep-source
      max-height="800"
      align="center"
      :loading="loading"
      :data="tableData.slice((currentPage-1)*pageSize,currentPage*pageSize)"
    >
      <vxe-table-column type="checkbox" />
      <vxe-table-column field="sender.id" title="Id" />
      <vxe-table-column field="sender.dataSource" title="数据源" :formatter="formatterDataSource" :filters="dataSourceList" :filter-multiple="false" />
      <vxe-table-column field="sender.magnification" title="倍率" />
      <vxe-table-column field="sender.tunnelId" title="通道名" :formatter="formateId" />
      <vxe-table-column field="sender.protocolPointNo" title="通道点位" sortable />
      <vxe-table-column field="sender.protocolPointType" title="通道数据类型" :formatter="formatterPointType" />
      <vxe-table-column field="equipmentId" title="设备名" :formatter="formateEqId" />
      <vxe-table-column field="equipmentAttribute.equipmentType.message" title="设备类型" />
      <vxe-table-column field="equipmentAttribute.explanation" title="设备属性" />
      <vxe-table-column field="sender.forecastForSendAttribute.forecastType" title="预测数据属性" :formatter="forForecastType" />
      <vxe-table-column field="sender.forecastForSendAttribute.forecastNode" title="预测天数(天/时)" :formatter="formatterForecastNode" sortable />
      <vxe-table-column field="sender.forecastForSendAttribute.pointNumber" title="预测数据的点位" :formatter="formatterPointNumber" />
      <vxe-table-column title="手动置数" width="80">
        <template v-slot="{ row }">
          <template>
            <vxe-input v-model="row.value1" clearable @blur="setValueByManual(row)" />
          </template>
        </template>
      </vxe-table-column>
      <!--<vxe-table-column field="sender.generationRegulation" title="属性规则" />-->
      <vxe-table-column title="操作" width="150">
        <template v-slot="{ row }">
          <template>
            <vxe-button status="primary" size="mini" @click="editRowEvent(row)">编辑</vxe-button>
            <vxe-button status="danger" size="mini" @click="removeEvent(row)">删除</vxe-button>
          </template>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      perfect
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :total="totalResult"
      :layouts="['PrevJump', 'PrevPage', 'JumpNumber', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
    />
    <!--添加编辑-->
    <vxe-modal ref="xModal" v-model="showEdit" :title="selectRow ? '编辑&保存' : '新增&保存'" width="800" destroy-on-close min-height="550">
      <vxe-form :data="formData" :rules="formRules" title-align="center" title-width="100" @submit="submitEvent">
        <vxe-form-item title="数据源" field="dataSource" span="20" title-width="100">
          <vxe-select v-model="formData.dataSource" placeholder="请选择点位生成规则" @change="datasChange">
            <vxe-option v-for="item in enums.senderDataSourceEnum" :key="item.value" :value="item.value" :label="item.label" />
          </vxe-select>
        </vxe-form-item>
        <vxe-form-item title="使用通道" field="tunnelId" span="12">
          <vxe-select v-model="formData.tunnelId" placeholder="请选择使用通道" @change="sendgetByTunnel">
            <vxe-option v-for="item in tunnelList" :key="item.id" :value="item.id" :label="item.tunnelName" />
          </vxe-select>
        </vxe-form-item>
        <vxe-form-item title="通道数据类型" field="protocolPointType" span="12" title-width="120">
          <vxe-select v-model="formData.protocolPointType" placeholder="请选择通道数据类型">
            <vxe-option v-for="item in protocolDataType" :key="item.value" :value="item.value" :label="item.label" />
          </vxe-select>
        </vxe-form-item>
        <vxe-form-item title="通道点位" field="protocolPointNo" span="12" title-width="100">
          <vxe-input
            v-model="formData.protocolPointNo"
            placeholder="请输入通道点位"
            clearable
            type="number"
            :disabled="noDisable"
            @change="checkprotocolPointNo"
          />
        </vxe-form-item>
        <vxe-form-item title="倍率" field="magnification" span="12" :item-render="{name: 'input', attrs: {type: 'float'}}" />
        <!--外部接入-->
        <vxe-form-item title="设备类型" field="eqType" span="12">
          <vxe-select v-model="formData.eqType" placeholder="请选择设备类型" :disabled="gatherDisable" @change="changeEqA">
            <vxe-option v-for="item in equipmentTypeEnum" :key="item.value" :value="item.value" :label="item.label" />
          </vxe-select>
        </vxe-form-item>
        <vxe-form-item title="设备名" field="equipmentId" span="12">
          <vxe-select v-model="formData.equipmentId" placeholder="请选择设备名" :disabled="gatherDisable" @change="selectEqAtt">
            <vxe-option v-for="(item,index) in eqData" :key="index" :value="item.id" :label="item.name" />
          </vxe-select>
        </vxe-form-item>
        <vxe-form-item title="设备属性" field="equipmentAttribute" span="12">
          <vxe-select v-model="formData.equipmentAttribute" placeholder="请选择设备属性" :disabled="gatherDisable" @change="getGatherPoint">
            <vxe-option v-for="(item,index) in eqaTableData" :key="item.equipmentAttribute.id" :value="item.equipmentAttribute.id" :label="item.equipmentAttribute.explanation" />
          </vxe-select>
        </vxe-form-item>
        <!--公式生成 暂不使用-->
        <!-- <vxe-form-item title="属性规则" field="generationRegulation" span="20">
          <vxe-input v-model="formData.generationRegulation" placeholder="请输入属性规则" type="textarea" clearable :disabled="generateDisable" />
        </vxe-form-item>-->
        <!--预测生成-->
        <vxe-form-item title="预测数据属性" field="forecastType" span="12" title-width="120">
          <vxe-select v-model="formData.forecastType" placeholder="请选择预测数据属性" :disabled="forecastDisable" placement="top" @change="forecastDayChange">
            <vxe-option v-for="item in enums.forecastType" :key="item.value" :value="item.value" :label="item.label" />
          </vxe-select>
        </vxe-form-item>
        <vxe-form-item title="预测天数" span="12" field="forecastNode">
          <vxe-select v-model="formData.forecastNode" placeholder="请选择预测天数" :disabled="forecastDisable" placement="top" @change="upForecastAttribute">
            <vxe-option v-for="item in forecastDay" :key="item.value" :value="item.value" :label="item.label" />
          </vxe-select>
        </vxe-form-item>
        <vxe-form-item title="预测点位" span="12" field="pointNumber">
          <vxe-select v-model="formData.pointNumber" placeholder="请选择预测点位" :disabled="forecastDisable" placement="top" @change="getAttributeBypoint">
            <vxe-option v-for="item in forecastAttributeList" :key="item.value" :value="item.value" :label="item.label" />
          </vxe-select>
        </vxe-form-item>
        <vxe-form-item align="center" span="24">
          <vxe-button type="submit" status="primary" :disabled="disButton">保存</vxe-button>
          <vxe-button v-show="resetShow" @click="resetForm">重置</vxe-button>
          <vxe-button @click="$refs.xModal.close()">取消</vxe-button>
        </vxe-form-item>
      </vxe-form>
    </vxe-modal>
    <multi-add :show="multiAdd" @close="changeMultiAdd" />
  </div>
</template>

<script>
import enumerations from '../../enumeration'
import MultiAdd from './multiAdd'

export default {
  components: {
    MultiAdd
  },
  data() {
    // const checkmagnification = ({ cellValue, rule, row, column }) => {
    //   if (this.formData.magnification < 0) {
    //     return Promise.reject(new Error('倍率不能小于0'))
    //   }
    // }
    return {
      loading: false,
      disButton: false,
      resetShow: true,
      multiAdd: false,
      noDisable: true,
      // 后台传输数据
      tableData: [],
      tableData1: [],
      elTableData: [],
      elId: '',
      eqTableData: [],
      eqId: '',
      eqData: [],
      tempId: '',
      eqaTableData: [],
      eqaId: '',
      tunnelId: '',
      tunnelTable: [],
      showEdit: false,
      showAllEdit: false,
      selectRow: null,
      saveFlag: 1,
      modelId: '',
      // 预测属性点位id
      pointId: '',
      // 分页
      currentPage: 1,
      pageSize: 10,
      totalResult: 0,
      page: 0,
      // 判断表示
      gatherDisable: true,
      generateDisable: true,
      forecastDisable: false,
      needdisable: false,
      equipmentList: [],
      form: {
        forecastNode: [],
        pointNumber: []
      },
      Rules: [],
      value1: '',
      formData: {
        equipmentId: '',
        equipmentAttribute: '',
        eqType: '',
        dataSource: 'FORECAST',
        tunnelId: '',
        protocolPointNo: null,
        protocolPointType: 'ABCD',
        magnification: 1,
        generationRegulation: '',
        forecastType: null,
        forecastNode: '',
        pointNumber: '',
        forecastForSendAttribute: {
          'id': '',
          'forecastType': null,
          'forecastNode': '',
          'pointNumber': ''
        }
      },
      gatherPointList: [],
      gpList: [], // 采集点表
      forecastDay: [],
      forecastAttributeList: [],
      // 数据类型
      code: '',
      message: '',
      forecastRules: {
        dataSource: [{ required: true, message: '请选择数据来源' }],
        tunnelId: [{ required: true, message: '请选择通道' }],
        protocolPointType: [{ required: true, message: '请选择通道数据类型' }],
        magnification: [{ required: true, message: '请输入倍率' }],
        forecastType: [{ required: true, message: '请选择预测数据类型' }],
        forecastNode: [{ required: true, message: '请选择预测天数' }],
        pointNumber: [{ required: true, message: '请选择预测点位' }]
      },
      gatherRules: {
        dataSource: [{ required: true, message: '请选择数据来源' }],
        tunnelId: [{ required: true, message: '请选择通道' }],
        protocolPointType: [{ required: true, message: '请选择通道数据类型' }],
        magnification: [{ required: true, message: '请输入倍率' }],
        equipmentId: [{ required: true, message: '请选择设备' }],
        equipmentAttribute: [{ required: true, message: '请选择设备类型' }],
        eqType: [{ required: true, message: '请选择设备属性' }]
      },
      formRules: {},
      enums: enumerations,
      protocolDataType: enumerations.ProtocolDataType,
      tunnelList: [],
      temp: [],
      dataSourceList: [{ label: '预测生成', value: 'FORECAST' }, { label: '外部接入', value: 'POWERSTATION' }],
      equipmentTypeEnum: [{ value: 'POWERSTATION', label: '发电站', key: 0 },
        { value: 'WEATHERSTATION', label: '气象站', key: 1 },
        { value: 'INVERTER', label: '逆变器', key: 2 },
        { value: 'WINDTOWER', label: '测风塔', key: 3 },
        { value: 'WINDTURBINE', label: '风机', key: 4 }
      ]
    }
  },
  mounted() {
    this.getAll()
    this.formRules = this.forecastRules
  },

  methods: {
    getAll() {
      Promise.all([this.getEq(), this.getTunnel(), this.getEl()]).then((res) => {
        this.eqTableData = res[0]
        this.tunnelList = res[1]
        this.elTableData.push(res[2])
        this.getSenderPoint()
      })
    },
    // 场站
    getEl() {
      const axios = this.$axios
      return new Promise(function(resolve, reject) {
        axios.get('/electricField/').then(res => {
          resolve(res.data)
        })
      })
    },
    // 场站设备
    getEq() {
      const axios = this.$axios
      return new Promise(function(resolve, reject) {
        axios.get('/dataExchange/equipmentInfo/').then(res => {
          resolve(res.data)
        })
      })
    },
    // 获取通道
    getTunnel() {
      const axios = this.$axios
      return new Promise(function(resolve, reject) {
        axios.get('/TunnelInfo/getTunnel/Slave').then(res => {
          resolve(res.data)
        })
      })
    },
    changeMultiAdd() {
      this.multiAdd = false
    },
    getSenderPoint() {
      this.page = this.currentPage
      this.loading = true
      this.$axios.get('/dataExchange/senderProtocolDataPoint/DataPoint').then((res) => {
        this.tableData = res.data
        this.totalResult = this.tableData.length
        this.currentPage = this.page
        this.loading = false
      })
    },
    // 查询采集数据点位
    getGatherPoint() {
      var equipmentId = ''
      equipmentId = this.formData.equipmentId
      if (this.formData.eqType === 'POWERSTATION') {
        equipmentId = 0
      }
      this.$axios.get('/dataExchange/protocolDataPoint/getProtocolGatherDataPoint/' + equipmentId + '/' + this.formData.equipmentAttribute).then(res => {
        this.formData.gatherPoint = res.data[0].id
      })
    },
    selectEqAtt() {
      this.eqaTableData = []
      // this.formData.equipmentAttribute = ''
      const params = {
        equipmentType: this.formData.eqType,
        equipmentId: this.formData.equipmentId
      }
      if (this.formData.eqType == 'POWERSTATION') {
        params.equipmentId = null
      }
      this.$axios.get('/dataExchange/protocolDataPoint/gatherDataPoint', { params: params }).then(res => {
        // console.log(res.data)
        this.eqaTableData = res.data
      })
    },
    // 获取预测点位
    upForecastAttribute() {
      if (this.saveFlag === 2) {
        this.formData.pointNumber = ''
      }
      this.forecastAttributeList = []
      this.$axios.get('/dataExchange/ForecastForSendAttribute/' + this.formData.forecastNode + '/' + this.formData.forecastType).then(res => {
        var list = []
        for (var i = 0; i < res.data.length; i++) {
          list.push({ label: '第' + res.data[i].pointNumber + '节点', value: res.data[i].pointNumber })
        }
        this.forecastAttributeList = list
      })
    },
    // 获取预测点位
    getforecastAttribute() {
      this.forecastAttributeList = []
      this.$axios.get('/dataExchange/ForecastForSendAttribute/' + this.formData.forecastNode + '/' + this.formData.forecastType).then(res => {
        var list = []
        for (var i = 0; i < res.data.length; i++) {
          list.push({ label: '第' + res.data[i].pointNumber + '节点', value: res.data[i].pointNumber })
        }
        this.forecastAttributeList = list
      })
    },
    // 获取预测属性id
    getAttributeBypoint() {
      var pointNumber = this.formData.pointNumber
      var forecastNode = this.formData.forecastNode
      var forcastType = this.formData.forecastType
      this.$axios.get('/dataExchange/ForecastForSendAttribute/getAttributeBypoint/' + pointNumber + '/' + forecastNode + '/' + forcastType).then((res) => {
        // console.log(res.data)
        this.pointId = res.data[0].id
        this.formData.forecastForSendAttribute.id = this.pointId
      })
    },
    // 查询
    searchPointData() {
      this.page = this.currentPage
      // console.log(this.tunnelId)
      if ((this.tunnelId === '' || this.tunnelId === null) && (this.eqId === '' || this.eqId === null)) {
        this.getSenderPoint()
      } else {
        this.loading = true
        var tunnelId = ''
        var equipmentId = ''
        var equipmentType = ''
        if (this.tunnelId === '' || this.tunnelId === undefined || this.tunnelId === null) {
          tunnelId = 'ALL'
        } else tunnelId = this.tunnelId

        if (this.eqTableData[this.eqId] === [] || this.eqTableData[this.eqId] === undefined) {
          equipmentId = 'ALL'
          equipmentType = 0
        } else {
          equipmentId = this.eqTableData[this.eqId].id
          if (equipmentId === this.elTableData[0].stationCode) {
            equipmentId = 0
            equipmentType = this.eqTableData[this.eqId].code
          } else {
            equipmentType = this.eqTableData[this.eqId].equipmentType.code
          }
        }
        this.$axios.get('/dataExchange/senderProtocolDataPoint/findByTunnelIdAndGatherPoint/' + tunnelId + '/' + equipmentId + '/' + equipmentType).then(res => {
          this.tableData = res.data
          this.totalResult = this.tableData.length
          this.currentPage = this.page
          // console.log(res.data)
          this.loading = false
        })
      }
    },
    // 保存
    submitEvent() {
      // console.log(this.formData)
      this.formData.stationCode = this.elTableData[0].stationCode
      this.formData.forecastForSendAttribute.forecastType = this.formData.forecastType
      this.formData.forecastForSendAttribute.forecastNode = this.formData.forecastNode
      this.formData.forecastForSendAttribute.pointNumber = this.formData.pointNumber
      this.$delete(this.formData, 'equipmentId')
      this.$delete(this.formData, 'equipmentAttribute')
      this.$delete(this.formData, 'eqType')
      this.$delete(this.formData, 'forecastType')
      this.$delete(this.formData, 'forecastNode')
      this.$delete(this.formData, 'pointNumber')
      if (this.saveFlag === 1) {
        if (this.formData.dataSource === 'FORECAST') {
          this.formData.gatherPoint = null
          // this.formData.forecastForSendAttribute.id = this.pointId
        } else {
          this.formData.forecastForSendAttribute = null
        }
        this.$axios.post('/dataExchange/senderProtocolDataPoint/saveDataPoint/', this.formData).then((res) => {
          // console.log(res.code)
          if (res.code === 0) {
            this.searchPointData()
            this.$message.success('保存转发点表成功')
            this.resetForm()
          } else {
            this.searchPointData()
            this.$message.error('保存转发点表失败')
            this.resetForm()
          }
        })
        this.$refs.xModal.close()
      } else {
        if (this.formData.dataSource !== 'FORECAST') {
          this.formData.forecastForSendAttribute = null
        }

        this.$axios.put('/dataExchange/senderProtocolDataPoint/updateDataPoint/', this.formData).then((res) => {
          // console.log(res.code)
          if (res.code === 0) {
            this.searchPointData()
            // this.test()
            this.$message.success('修改转发点表成功')
          } else {
            this.searchPointData()
            this.$message.error('修改转发点表失败')
          }
        })
        this.resetForm()
        this.$refs.xModal.close()
      }
    },
    // 编辑
    editRowEvent(row) {
      this.resetShow = false
      this.noDisable = false
      this.saveFlag = 2
      this.formData.id = row.sender.id
      this.formData.magnification = row.sender.magnification
      this.formData.gatherPoint = row.sender.gatherPoint
      this.formData.tunnelId = row.sender.tunnelId
      this.formData.protocolPointType = row.sender.protocolPointType
      this.formData.protocolPointNo = row.sender.protocolPointNo
      this.formData.dataSource = row.sender.dataSource
      this.datasChange()
      if (this.formData.dataSource === 'FORECAST') {
        this.formData.equipmentId = ''
        this.formData.equipmentAttribute = ''
        this.formData.eqType = null
      } else {
        // 外部
        if (this.formateEq(row.equipmentAttribute.equipmentType.code)) {
          if (this.changeEqA()) {
            this.formData.equipmentId = row.equipmentId
            if (this.formData.eqType === 'POWERSTATION') {
              this.formData.equipmentId = this.elTableData[0].stationCode
            }
            if (this.formatterEqType(row.equipmentAttribute.equipmentType.message)) {
              this.formData.equipmentAttribute = row.equipmentAttribute.id
            }
          }
        }
      }
      // 公式
      this.formData.generationRegulation = row.sender.generationRegulation
      // 预测
      if (this.formData.dataSource === 'FORECAST') {
        this.formData = {
          id: row.sender.id,
          equipmentId: '',
          equipmentAttribute: '',
          eqType: null,
          gatherPoint: row.sender.gatherPoint,
          dataSource: row.sender.dataSource,
          tunnelId: row.sender.tunnelId,
          protocolPointNo: row.sender.protocolPointNo,
          protocolPointType: row.sender.protocolPointType,
          magnification: row.sender.magnification,
          forecastForSendAttribute: {
            'id': row.sender.forecastForSendAttribute.id
          },
          pointNumber: row.sender.forecastForSendAttribute.pointNumber
        }
        // this.formData.forecastForSendAttribute.id = row.sender.forecastForSendAttribute.id
        this.formData.forecastType = row.sender.forecastForSendAttribute.forecastType
        this.forecastDayChange()
        this.formData.forecastNode = row.sender.forecastForSendAttribute.forecastNode
        this.getforecastAttribute()
        // this.formData.pointNumber = row.sender.forecastForSendAttribute.pointNumber
      } else {
        this.formData.forecastForSendAttribute.id = null
        this.formData.forecastType = null
        this.formData.forecastNode = null
        this.formData.pointNumber = null
      }
      this.selectRow = row
      this.showEdit = true
    },
    // 删除
    removeEvent(row) {
      // console.log(row)
      this.$axios.delete('/dataExchange/senderProtocolDataPoint/deleteDataPoint/' + row.sender.id).then((res) => {
        // console.log(res)
        if (res.code === 0) {
          this.getSenderPoint()
          this.$message.success('删除转发点表成功')
        } else {
          this.getSenderPoint()
          this.$message.error('删除转发点表失败')
        }
      })
    },
    sendgetByTunnel() {
      this.$axios.get('/dataExchange/senderProtocolDataPoint/findByTunnelId/' + this.formData.tunnelId).then((res) => {
        this.tunnelTable = res.data
        this.tunnelChange()
        var max = 0
        if (this.tunnelTable.length === 0) {
          this.formData.protocolPointNo = 1
        } else if (this.tunnelTable.length === 1) {
          this.formData.protocolPointNo = this.tunnelTable[0].sender.protocolPointNo + 1
        } else {
          for (var i = 0; i < this.tunnelTable.length; i++) {
            if (i !== this.tunnelTable.length - 1) {
              var num1 = this.tunnelTable[i].sender.protocolPointNo
              var num2 = this.tunnelTable[i + 1].sender.protocolPointNo
              max = Math.max(num1, num2)
            }
          }
          this.formData.protocolPointNo = max + 1
        }
      })
    },
    tunnelChange() {
      this.protocolDataType = []
      this.formData.protocolPointType = 'ABCD'
      for (let i = 0; i < this.tunnelList.length; i++) {
        if (this.formData.tunnelId === this.tunnelList[i].id) {
          if (this.tunnelList[i].tunnelType.substring(0, 3) === 'IEC' || this.tunnelList[i].tunnelType.substring(0, 3) === 'FIL') {
            this.protocolDataType = [{ label: '遥信开关量', value: 'A', key: 0 }, { label: 'ABCD', value: 'ABCD', key: 9 }]
          } else if (this.tunnelList[i].tunnelType.substring(0, 3) === 'CDT') {
            this.protocolDataType = [{ label: '遥信开关量', value: 'A', key: 0 }, { label: '+AB', value: 'P_AB', key: 1 }]
            this.formData.protocolPointType = 'P_AB'
          } else {
            this.protocolDataType = this.enums.ProtocolDataType
          }
        }
      }
    },
    // 设备名改变设备属性下拉框改变
    changeEqA() {
      this.formData.equipmentId = ''
      if (this.formateEqType(this.formData.eqType)) {
        this.code = this.tempId
        if (this.tempId === 0) {
          this.$axios.get('/electricField/').then(res => {
            this.eqData = []
            if (res.code == 0) {
              this.eqData.push({ id: res.data.stationCode, name: res.data.name })
              this.formData.equipmentId = res.data.stationCode
            }
            this.selectEqAtt()
          })
        } else {
          this.$axios.get('/dataExchange/equipmentInfo/' + this.tempId).then(res => {
            // console.log(res)
            this.eqData = res.data
            this.selectEqAtt()
          })
        }
      }
      return true
    },
    // 预测属性改变天数
    forecastDayChange() {
      this.formData.forecastNode = ''
      this.forecastAttributeList = []
      if (this.formData.forecastType === 'ULTRA_SHORT_AVAILABLE' || this.formData.forecastType === 'ULTRA_SHORT_THEORETICAL') {
        this.forecastDay = [{ label: '第一小时', value: 0 },
          { label: '第二小时', value: 1 },
          { label: '第三小时', value: 2 },
          { label: '第四小时', value: 3 }]
      } else {
        this.forecastDay = [{ label: '第1天', value: 0 }, { label: '第2天', value: 1 }, { label: '第3天', value: 2 }, { label: '第4天', value: 3 }, { label: '第5天', value: 4 },
          { label: '第6天', value: 5 }, { label: '第7天', value: 6 }, { label: '第8天', value: 7 }, { label: '第9天', value: 8 }, { label: '第10天', value: 9 },{ label: '第11天', value: 10}]
      }
    },
    // 判断点表生成规则
    datasChange() {
      this.formRules = {}
      if (this.formData.dataSource === 'GATHER') {
        this.gatherDisable = false
        this.forecastDisable = true
        this.generateDisable = true
        this.formRules = this.gatherRules
      } else if (this.formData.dataSource === 'FORECAST') {
        this.gatherDisable = true
        this.forecastDisable = false
        this.generateDisable = true
        this.formRules = this.forecastRules
      } else {
        this.gatherDisable = true
        this.forecastDisable = true
        this.generateDisable = false
      }
    },
    // 重置
    resetForm() {
      this.formData = {
        equipmentId: '',
        equipmentAttribute: '',
        eqType: '',
        dataSource: 'FORECAST',
        tunnelId: '',
        protocolPointNo: null,
        protocolPointType: 'ABCD',
        magnification: 1,
        generationRegulation: '',
        forecastForSendAttribute: {
          'id': '',
          'forecastType': null,
          'forecastNode': '',
          'pointNumber': ''
        }
      }
      this.formRules = {}
      this.formRules = this.forecastRules
      this.protocolDataType = this.enums.ProtocolDataType
      this.disButton = false
    },
    // 新增
    insertEvent() {
      this.saveFlag = 1
      this.disButton = true
      this.resetForm()
      this.selectRow = null
      this.showEdit = true
      this.noDisable = true
      this.gatherDisable = true
      this.forecastDisable = false
      this.formData.equipmentId = ''
      if (this.tunnelId !== '' && this.tunnelId !== null) {
        this.formData.tunnelId = this.tunnelId
        this.sendgetByTunnel()
      }
      this.protocolDataType = this.enums.ProtocolDataType
    },
    batchDeleteSpoint() {
      var selectRecords = this.$refs.xTable.getCheckboxRecords()
      console.log(selectRecords)
      if (selectRecords.length == 0) {
        this.$message.error('请选择要删除的数据')
      } else {
        this.$XModal.confirm('您确定要批量删除数据?').then(type => {
          if (type === 'confirm') {
            var ids = []
            for (var i = 0; i < selectRecords.length; i++) {
              ids.push(selectRecords[i].sender.id)
            }
            this.$axios.delete('/dataExchange/senderProtocolDataPoint/batchDeleteSenderDataPoint/' + ids).then(res => {
              if (res.data === 1) {
                this.$message.success('删除批量点表成功')
              } else {
                this.$message.error('删除批量点表失败')
              }
              this.searchPointData()
            })
          }
        })
      }
    },
    insertAllEvent() {
      this.showAllEdit = true
    },
    checkprotocolPointNo() {
      this.disButton = false
      if (this.formData.tunnelId === '' || this.formData.tunnelId === null) {
        this.$message.error('请先选择通道')
        this.formData.protocolPointNo = ''
      } else {
        if (this.formData.protocolPointNo < 0) {
          this.formData.protocolPointNo = 0
          this.$message.error('通道点位不能小于0')
        }
        this.$axios.get('/dataExchange/senderProtocolDataPoint/findByTunnelId/' + this.formData.tunnelId).then((res) => {
          var v6 = res.data
          var num = this.formData.protocolPointNo
          console.log(v6)
          for (let i = 0; i < v6.length; i++) {
            // 修改
            if (parseInt(num) === v6[i].sender.protocolPointNo) {
              this.$message.error('通道号不能重复')
              this.disButton = true
            }
          }
        })
      }
    },
    setValueByManual(row) {
      if (row.value1 !== undefined) {
        var flag = false
        if (row.sender.protocolPointType === 'A') {
          var re = /^[0-1]*$/
          if (!re.test(row.value1)) {
            this.$message.error('手动置数只能0或1')
          } else {
            flag = true
          }
        } else {
          var reg = /|^(\-|\+?)\d+(\.\d+)?$/
          if (reg.test(row.value1)) {
            flag = true
          } else {
            this.$message.error('手动置数只能数字')
          }
        }
        if (flag) {
          this.$axios.get('/dataExchange/senderProtocolDataPoint/setValueByManual/' + row.sender.id + '/' + row.value1).then((res) => {
            this.$message.success('手动置数成功')
          })
        }
      }
    },
    // 导出
    exportDataEvent() {
      // this.$refs.xTable.exportData({
      //   filename: `转发点表`,
      //   type: 'csv'
      // })
      this.$axios.post('/dataExchange/senderProtocolDataPoint/export', {}, {
        fileName: '',
        responseType: 'blob'// 用于解决中文乱码
      }).then((response) => {
      })
    },
    // 刷新全部转发通道数据点
    refreshPoint() {
      this.$axios.get('/refreshContainer/allSenderTunnelDataPoint').then((res) => {
        // console.log(res)
        this.$message.success('刷新成功')
      })
    },
    // 转换类型
    formateEq(code) {
      let returnValue = ''
      if (code === 0) returnValue = 'POWERSTATION'
      if (code === 1) returnValue = 'WEATHERSTATION'
      if (code === 2) returnValue = 'INVERTER'
      if (code === 3) returnValue = 'WINDTOWER'
      if (code === 4) returnValue = 'WINDTURBINE'
      if (code === 5) returnValue = 'FORECASTDATA'
      this.formData.eqType = returnValue
      return true
    },
    // 转换类型
    formateEqType(eqType) {
      this.tempId = ''
      let returnValue = ''
      if (eqType === 'POWERSTATION') returnValue = 0
      if (eqType === 'WEATHERSTATION') returnValue = 1
      if (eqType === 'INVERTER') returnValue = 2
      if (eqType === 'WINDTOWER') returnValue = 3
      if (eqType === 'WINDTURBINE') returnValue = 4
      if (eqType === 'FORECASTDATA') returnValue = 5
      this.tempId = returnValue
      return true
    },
    // 设备id转名称
    formateEqId(row, column) {
      // console.log(row.row)
      let belongTo = '未知设备名称'
      if (row.row.equipmentId === null) {
        belongTo = ''
      } else {
        if (row.row.equipmentAttribute.equipmentType.code === 0) {
          belongTo = this.elTableData[0].name
        } else {
          for (let i = 0; i < this.eqTableData.length; i++) {
            if (row.row.equipmentId === this.eqTableData[i].id && row.row.equipmentAttribute.equipmentType.code === this.eqTableData[i].equipmentType.code) {
              belongTo = this.eqTableData[i].name
            }
          }
        }
      }
      return belongTo
    },
    // 转换通道类型
    formatterPointType({ cellValue }) {
      const item = this.enums.ProtocolDataType.find(item => item.value === cellValue)
      return item ? item.label : ''
    },
    // 转设备类别
    formatterEqType(cellValue) {
      /* if(cellValue === )*/
      const item = this.enums.equipmentTypeEnum.find(item => item.label === cellValue)
      this.formData.eqType = item ? item.value : ''
      return true
    },
    // 预测属性
    forForecastType({ cellValue }) {
      const item = this.enums.forecastType.find(item => item.value === cellValue)
      return item ? item.label : ''
    },
    formatterForecastNode({ cellValue, row }) {
      var forecastNode = ''
      if (cellValue !== undefined) {
        if (row.sender.forecastForSendAttribute.forecastType === 'ULTRA_SHORT_AVAILABLE' || row.sender.forecastForSendAttribute.forecastType === 'ULTRA_SHORT_THEORETICAL') {
          switch (cellValue) {
            case 0: forecastNode = '第一小时'
              break
            case 1: forecastNode = '第二小时'
              break
            case 2: forecastNode = '第三小时'
              break
            case 3: forecastNode = '第四小时'
              break
          }
        } else {
          forecastNode = '第' + (cellValue + 1) + '天'
        }
        return forecastNode
      }
    },
    formatterPointNumber({ cellValue }) {
      if (cellValue !== undefined) {
        return '第' + cellValue + '节点'
      }
    },
    formatterDataSource({ cellValue }) {
      const item = this.enums.senderDataSourceEnum.find(item => item.value === cellValue)
      return item ? item.label : ''
    },
    // 通道id转名称
    formateId(row, column) {
      // console.log(row)
      let belongTo = '未知通道名称'
      if (row.row.sender.tunnelId === '') {
        belongTo = ''
      } else {
        for (let i = 0; i < this.tunnelList.length; i++) {
          if (row.row.sender.tunnelId === this.tunnelList[i].id) {
            belongTo = this.tunnelList[i].tunnelName
          }
        }
      }
      return belongTo
    }
  }

}

</script>

<style>
</style>
