<template>
  <div style="height: 100%;width: 100%;background: #d6dde5">
    <el-col :span="20" style="height: 100%; background: #fff">
      <el-row style="background-color: #ffffff;height: 100%;position: relative;margin: 0 6px 0 3px;cursor: pointer;border: 2px solid #9e9b9b" @click.native="showAlarmList()">
        <el-col :span="3" style="text-align: center;border-right: 2px solid #9e9b9b">
          <div style="font-size: 32px;background-color: #d7dee6;">
            报警信息：
          </div>
        </el-col>
        <el-col :span="21">
          <div v-if="alarmList.length > 0" class="slogan">
            {{ alarmList[0].报警内容 }}
          </div>
          <div v-else class="slogan1" />
        </el-col>
      </el-row>
    </el-col>
    <el-col :span="4" style="height: 100%">
      <el-row style="height: 100%">
        <el-popover placement="bottom" title="" width="290" trigger="click" content="">
          <div style="width: 290px;">
            <el-switch
              v-if="buttonShowList.indexOf('MES_OnlineStatus') > -1"
              v-model="value"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="MES在线"
              inactive-text="MES屏蔽"
              @change="changeStationStatus"
            />
            <el-switch
              v-if="buttonShowList.indexOf('MES_PartStatus') > -1"
              v-model="TurnLineWorkValue"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="正常工件"
              inactive-text="转线工件"
              @change="changeTurnLineWork"
            />
            <el-button v-if="buttonShowList.indexOf('MES_ConfirmOk') > -1" type="primary" plain class="button" @click="qualifiedRelease">
              <span style="font-size: 28px">合 格 放 行</span>
            </el-button>
            <el-button v-if="buttonShowList.indexOf('MES_NgGO') > -1" type="primary" plain class="button" @click="unqualifiedRelease">
              <span style="font-size: 28px">不 合 格 放 行</span>
            </el-button>
            <el-button v-if="buttonShowList.indexOf('MES_WritePallet') > -1" type="primary" plain class="button" @click="mesWritePallet">
              <span style="font-size: 28px">修改托盘号</span>
            </el-button>
            <el-button v-if="buttonShowList.indexOf('MES_Reset') > -1" type="primary" plain class="button" @click="mesReset">
              <span style="font-size: 28px">MES 复 位</span>
            </el-button>
            <el-button type="primary" plain class="button" @click="loginOut">
              <span style="font-size: 28px">退 出 系 统</span>
            </el-button>
          </div>
          <el-button slot="reference" style="font-size: 25px;background-color: #005bac;color: white; width: 99%; height: 100%;border: 3px solid black;padding: 0;">
            功能按钮
          </el-button>
        </el-popover>
      </el-row>
    </el-col>
    <el-dialog title="报警列表" :visible.sync="dialogFormVisible2" :append-to-body="true" center width="850px" style="text-align: center">
      <el-table
        :data="alarmList"
        :header-cell-style="{background:'#F5F5F5'}"
        :row-class-name="tableRowClassName"
        :index="tableIndexValue"
        style="width: 100%; border: 1px solid #9e9b9b"
        max-height="500px"
        tooltip-effect="dark"
      >
        <el-table-column align="center" label="#" type="index" width="50px" />
        <el-table-column align="center" show-overflow-tooltip label="报警内容">
          <template #default="scope">
            {{ scope.row.报警内容 }}
          </template>
        </el-table-column>
        <el-table-column align="center" width="300px" label="报警等级">
          <template #default="scope">
            {{ scope.row.报警等级 }}
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog title="合格放行" :visible.sync="dialogFormVisible1" :append-to-body="true" center width="400px" style="text-align: center">
      <div style="text-align: center">
        <el-form
          ref="form1"
          :model="form1"
          label-position="left"
          class="demo-ruleForm"
          label-width="100px"
          style="width: 90%;margin: auto"
        >
          <div>
            <span style="font-size: large; color: black; line-height: 90px; ">请输入用户名：</span>
            <el-input v-model="form1.userName" style="width:150px; margin-top: 20px; border: 2px solid black; border-radius:6px" />
          </div>
          <div>
            <span style="font-size: large; color: black; line-height: 90px; ">请输入密码：</span>
            <el-input v-model="form1.userPassword" style="width:150px; margin-top: 20px; margin-left: 15px; border: 2px solid black; border-radius:6px"/>
          </div>
          <div style="text-align: center; margin-top: 20px">
            <el-button class="el-icon-check" type="primary" @click="confirm11()">确定</el-button>
            <el-button class="el-icon-close" style="margin-left: 60px" @click="cancel()">取消</el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
    <el-dialog title="修改托盘编号" :visible.sync="mesWritePalletVisible" :append-to-body="true" center width="400px" style="text-align: center">
      <div style="text-align: center">
        <el-form
            ref="form2"
            :model="form2"
            label-position="left"
            label-width="100px"
            style="width: 100%;margin: auto"
        >
          <div>
            <span style="font-size: large; color: black; line-height: 90px; ">托盘编号：</span>
            <el-input-number v-model="form2.pallet" :min="0" :step="1" style="width:150px;" />
          </div>
          <div style="text-align: center; margin-top: 20px">
            <el-button class="el-icon-check" type="primary" @click="confirmWritePallet()">确定</el-button>
            <el-button class="el-icon-close" style="margin-left: 50px" @click="cancelWritePallet()">关闭</el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  data() {
    return {
      tableIndexValue: Math.random(),
      value: true,
      NullWorkValue: false,
      mesWritePalletVisible: false,
      TurnLineWorkValue: true,
      publication: {
        topic: '',
        qos: 0,
        payload: ''
      },
      options: [
        {
          value: 0,
          label: '在线'
        }, {
          value: 2,
          label: '屏蔽'
        }
      ],
      slogan: 'MES系统正在调试！！',
      visible: false,
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      alarmListTagId: [''],
      TagIdList: new Map(),
      alarmList: [],
      alarmList_Alarm: [],
      alarmList_Error: [],
      alarmList_Warning: [],
      title: '合格放行',
      form1: {
        userName: '',
        userPassword: ''
      },
      form2: {
        pallet: 0
      },
      buttonShowList: []
    }
  },
  mounted() {
    this.getStationStatus()
    this.getStationButton()
    this.getTagList()
  },
  methods: {
    tableRowClassName({ row }) {
      if (row.报警等级 === '错误') {
        return 'alarm_Alarm'
      } else if (row.报警等级 === '报警') {
        return 'alarm_Alarm'
      } else if (row.报警等级 === '警告') {
        return 'warning_Alarm'
      } else {
        return 'alarm_Default'
      }
    },
    getTagList() {
      const param = []
      param[0] = ['工位号', this.$store.state.station.stationName]
      const Data = this.CreateData('11', 'MES_基本变量_原始数据_根据工位号查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length > 0) {
          for (let i = 0; i < response.data.length; i++) {
            this.TagIdList.set(response.data[i].TagID, response.data[i])
          }
        }
      })
    },
    loginOut() {
      this.$store.commit('set_processList_table', [])
      this.$store.commit('set_materialList_table', [])
      this.$store.commit('set_pic', '')
      this.$store.commit('set_smile', '')
      this.$store.commit('set_tighteningPosition', [])
      this.$store.commit('set_greenBoltInformation', [])
      this.$store.commit('set_partAssemblyInformation', {})
      this.$store.commit('set_engineValue', {})
      this.$router.push('/')
    },
    solveAlarm(m_Value) {
      const m_ValueList = m_Value.split('&')
      const tagId = m_ValueList[0]
      const tagValue = m_ValueList[1]
      const alarmContent = m_ValueList[2]
      const alarmType = m_ValueList[3]
      if (tagValue === '1') {
        if (this.alarmListTagId.indexOf(tagId) === -1) {
          const tagInfo = this.TagIdList.get(tagId)
          this.alarmListTagId.push(tagId)
          switch (alarmType) {
            case '报警':
              this.alarmList_Alarm.unshift({
                TagId: tagId,
                报警内容: tagInfo['DbName'] + '.' + tagInfo['变量类型'] + tagInfo['变量地址'] + tagInfo['变量其它'] + '【' + alarmContent + '】',
                报警等级: alarmType
              })
              break
            case '错误':
              this.alarmList_Error.unshift({
                TagId: tagId,
                报警内容: tagInfo['DbName'] + '.' + tagInfo['变量类型'] + tagInfo['变量地址'] + tagInfo['变量其它'] + '【' + alarmContent + '】',
                报警等级: alarmType
              })
              break
            case '警告':
              this.alarmList_Warning.unshift({
                TagId: tagId,
                报警内容: tagInfo['DbName'] + '.' + tagInfo['变量类型'] + tagInfo['变量地址'] + tagInfo['变量其它'] + '【' + alarmContent + '】',
                报警等级: alarmType
              })
              break
          }
          this.alarmList = []
          this.alarmList = [...this.alarmList_Error, ...this.alarmList_Alarm, ...this.alarmList_Warning]
        }
      } else {
        this.alarmListTagId = this.alarmListTagId.filter(item => item !== tagId)
        this.alarmList_Alarm = this.alarmList_Alarm.filter(res => {
          return res.TagId !== tagId
        })
        this.alarmList_Error = this.alarmList_Error.filter(res => {
          return res.TagId !== tagId
        })
        this.alarmList_Warning = this.alarmList_Warning.filter(res => {
          return res.TagId !== tagId
        })
        this.alarmList = []
        this.alarmList = [...this.alarmList_Error, ...this.alarmList_Alarm, ...this.alarmList_Warning]
      }
      this.tableIndexValue = Math.random()
    },
    showAlarmList() {
      this.dialogFormVisible2 = true
      this.tableIndexValue = Math.random()
    },
    // 不合格放行
    unqualifiedRelease() {
      this.publication.topic = window.g.mqttPublishTopic
      this.publication.payload = 'WEB|NgGO|' + this.$store.state.station.stationName + '|' + '1'
      const result = window.g.mqttClient.doPublish(this.publication)
      if (result === true) {
        this.$notify.success({
          title: '提示',
          message: '不合格放行成功！',
          position: 'bottom-right',
          duration: 2000,
          offset: 170
        })
      } else {
        this.$notify.error({
          title: '提示',
          message: '不合格放行失败！',
          position: 'bottom-right',
          duration: 2000,
          offset: 170
        })
      }
    },
    // mes复位
    mesReset() {
      this.publication.topic = window.g.mqttPublishTopic
      this.publication.payload = 'WEB|Reset|' + this.$store.state.station.stationName + '|' + '1'
      const result = window.g.mqttClient.doPublish(this.publication)
      if (result === true) {
        this.$notify.success({
          title: '提示',
          message: 'MES复位成功！',
          position: 'bottom-right',
          duration: 2000,
          offset: 170
        })
      } else {
        this.$notify.error({
          title: '提示',
          message: 'MES复位失败！',
          position: 'bottom-right',
          duration: 2000,
          offset: 170
        })
      }
    },
    // mes写入托盘号
    mesWritePallet() {
      this.mesWritePalletVisible = true
    },
    // 是否屏蔽MES
    changeStationStatus(status) {
      const str = status ? '0' : '2'
      this.publication.topic = window.g.mqttPublishTopic
      this.publication.payload = 'WEB|OnLineState|' + this.$store.state.station.stationName + '|' + str
      window.g.mqttClient.doPublish(this.publication)
    },
    // 空托盘放行
    changeStationStatusNullWork(status) {
      const str = !status ? '0' : '1'
      this.publication.topic = window.g.mqttPublishTopic
      this.publication.payload = 'WEB|NullWorkGo|' + this.$store.state.station.stationName + '|' + str
      window.g.mqttClient.doPublish(this.publication)
    },
    // MA150独有 转线工件上线
    changeTurnLineWork(status) {
      const str = status ? '0' : '1'
      this.publication.topic = window.g.mqttPublishTopic
      this.publication.payload = 'WEB|TurnLineWork|' + this.$store.state.station.stationName + '|' + str
      window.g.mqttClient.doPublish(this.publication)
    },
    getStationStatus() {
      const param = []
      param[0] = ['工位号', this.$store.state.station.stationName]
      const Data = this.CreateData('11', 'MES_计划BOM_工位与名称_查询_在线状态', param)
      this.ExecDatabase(Data).then(response => {
        this.value = parseInt(response.data[0]['在线状态']) === 0
        console.log(this.value)
      })
    },
    getStationButton() {
      const param = []
      param[0] = ['工位号', this.$store.state.station.stationName]
      const Data = this.CreateData('11', 'MES界面显示_功能按钮_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.buttonShowList.push(response.data[i]['按钮ID'])
        }
        console.log(this.buttonShowList)
      })
    },
    qualifiedRelease() {
      // console.log('合格放型')
      this.dialogFormVisible1 = true
    },
    cancel() {
      this.dialogFormVisible1 = false
    },
    confirm11() {
      if (this.form1.userName && this.form1.userPassword) {
        if (this.$store.state.station.stationUserCount === this.form1.userName) {
          this.publication.topic = window.g.mqttPublishTopic
          this.publication.payload = 'WEB|ConfirmOkGo|' + this.$store.state.station.stationName + '|' + '1'
          const result = window.g.mqttClient.doPublish(this.publication)
          if (result === true) {
            this.$notify.success({
              title: '提示',
              message: '合格放行成功！',
              position: 'bottom-right',
              duration: 2000,
              offset: 170
            })
          } else {
            this.$notify.error({
              title: '提示',
              message: '合格放行失败！',
              position: 'bottom-right',
              duration: 2000,
              offset: 170
            })
          }
        } else {
          this.$notify.error({
            title: '提示',
            message: '用户名错误！',
            position: 'bottom-right',
            duration: 2000,
            offset: 170
          })
        }
        this.dialogFormVisible1 = false
      } else {
        this.$notify.error({
          title: '提示',
          message: '用户名或密码为空！',
          position: 'bottom-right',
          duration: 2000,
          offset: 170
        })
      }
    },
    confirmWritePallet() {
      this.publication.topic = window.g.mqttPublishTopic
      this.publication.payload = 'WEB|WritePartPalletCode|' + this.$store.state.station.stationName + '|' + this.form2.pallet
      const result = window.g.mqttClient.doPublish(this.publication)
      if (result === true) {
        this.$notify.success({
          title: '提示',
          message: '发送申请写入托盘号成功！',
          position: 'bottom-right',
          duration: 2000,
          offset: 170
        })
      } else {
        this.$notify.error({
          title: '提示',
          message: '发送申请写入托盘号失败！',
          position: 'bottom-right',
          duration: 2000,
          offset: 170
        })
      }
    },
    cancelWritePallet() {
      this.mesWritePalletVisible = false
    }
  }
}
</script>

<style scoped>
::v-deep.el-table .alarm_Alarm {
  background: #ff0000 !important;
  color: #000 !important;
}
::v-deep.el-table .alarm_Default {
  background: #ffffff !important;
  color: #000 !important;
}
::v-deep.el-table .warning_Alarm {
  background: #ffad1b !important;
  color: #000 !important;
}
::v-deep.el-table th.el-table__cell>.cell{
  font-size: 22px;
  color: #2f2f2f;
}
.button {
  background-color: white;
  color: black;
  width: 290px;
  margin: 10px 0 0 0;
  border: 2px solid black;
  font-size: 18px
}

.slogan {
  color: #000;
  background-color: red;
  font-size: 32px;
  font-weight: bolder;
  text-align: center;
  animation: alarm_bg 1.5s cubic-bezier(0.4, 0, 0.2, 1) infinite;
}
.slogan1 {
  color: #000;
  background-color: #ffffff;
  font-size: 32px;
  font-weight: bolder;
  text-align: center;
}
.el-switch {
  margin: 10px 0;
}
@keyframes alarm_bg {
  0% {
    color: #000;
    background-color: #ff0000;
  }
  100%  {
    color: #000;
    background-color: #ffffff;
  }
}
@keyframes example {
  0% {
    left: 0px;
    top: 0px;
  }

  50% {
    left: 1250px;
    top: 0px;
  }

  100% {
    left: 0px;
    top: 0px;
  }
}
</style>
