<template>
  <div style="height: 100%;width: 100%;background: #d6dde5">
    <el-col :span="20" style="height: 100%; background: #fff">
      <el-row style="background-color: black;height: 100%;position: relative;margin: 0 8px 0 5px">
        <div class="slogan">
          {{ slogan }}
        </div>
      </el-row>
      <!--      <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 content="" placement="bottom" title="" trigger="click" width="290">
          <div style="width: 290px;">
            <el-switch
                v-if="buttonShowList.indexOf('MES_OnlineStatus') > -1"
                v-model="value"
                active-color="#13ce66"
                active-text="MES在线"
                inactive-color="#ff4949"
                inactive-text="MES屏蔽"
                @change="changeStationStatus"
            />
            <el-switch
                v-if="buttonShowList.indexOf('MES_PartStatus') > -1"
                v-model="TurnLineWorkValue"
                active-color="#13ce66"
                active-text="正常工件"
                inactive-color="#ff4949"
                inactive-text="转线工件"
                @change="changeTurnLineWork"
            />
            <el-button v-if="buttonShowList.indexOf('MES_ConfirmOk') > -1" class="button" plain type="primary"
                       @click="qualifiedRelease">
              <span style="font-size: 28px">合 格 放 行</span>
            </el-button>
            <el-button v-if="buttonShowList.indexOf('MES_NgGO') > -1" class="button" plain type="primary"
                       @click="unqualifiedRelease">
              <span style="font-size: 28px">不 合 格 放 行</span>
            </el-button>
            <el-button v-if="buttonShowList.indexOf('MES_WritePallet') > -1" class="button" plain type="primary"
                       @click="mesWritePallet">
              <span style="font-size: 28px">修改托盘号</span>
            </el-button>
            <el-button v-if="buttonShowList.indexOf('MES_Reset') > -1" class="button" plain type="primary"
                       @click="mesReset">
              <span style="font-size: 28px">MES 复 位</span>
            </el-button>
            <el-button v-if="buttonShowList.indexOf('MES_NingJingShow') > -1" class="button" plain type="primary"
                       @click="mes_qc_operationShow">
              <span style="font-size: 28px">拧紧指导</span>
            </el-button>
            <el-button class="button" plain type="primary" @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 :append-to-body="true" :visible.sync="dialogFormVisible2" center style="text-align: center"
               title="报警列表"
               width="850px">
      <el-table
          :data="alarmList"
          :header-cell-style="{background:'#F5F5F5'}"
          :index="tableIndexValue"
          :row-class-name="tableRowClassName"
          max-height="500px"
          style="width: 100%; border: 1px solid #9e9b9b"
          tooltip-effect="dark"
      >
        <el-table-column align="center" label="#" type="index" width="50px"/>
        <el-table-column align="center" label="报警内容" show-overflow-tooltip>
          <template #default="scope">
            {{ scope.row.报警内容 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="报警等级" width="300px">
          <template #default="scope">
            {{ scope.row.报警等级 }}
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog :append-to-body="true" :visible.sync="dialogFormVisible1" center style="text-align: center"
               title="合格放行"
               width="400px">
      <div style="text-align: center">
        <el-form
            ref="form1"
            :model="form1"
            class="demo-ruleForm"
            label-position="left"
            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 :append-to-body="true" :visible.sync="mesWritePalletVisible" center class="" style="text-align: center"
               title="修改托盘编号"
               width="400px">
      <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>
    <el-dialog :append-to-body="true" :title="ningJinTitle" :visible.sync="mesOperationShowVisible" center
               style="text-align: center"
               top="3vh" width="1500px">
      <!--<div style="margin: 0 0 20px 0;text-align: right">-->
      <!--  <el-button plain class="el-icon-refresh" type="primary" @click="mes_qc_operationShow">刷新</el-button>-->
      <!--  <el-button plain class="el-icon-circle-close" type="info" @click="mesOperationShowVisible = false">关闭</el-button>-->
      <!--</div>-->

      <div style="display: flex;flex-wrap: wrap">
        <div v-for="(item, index) in mesOperationShowCount" :key="index" class="operation-show"
             @click="sendWordPosition(item)">
          <div v-if="item.position_Value === 0 && item.position_Value_return === 0" class="gary_bolt">
            {{ item.position }}
          </div>
          <div v-if="item.position_Value === 0 && item.position_Value_return === 1" class="green_bolt">
            {{ item.position }}
          </div>
          <div v-if="item.position_Value === 1 && item.position_Value_return === 1" class="green_bolt">
            {{ item.position }}
          </div>
          <div v-if="item.position_Value === 1 && item.position_Value_return === 0" class="yellow_bolt">
            {{ item.position }}
          </div>
        </div>
      </div>
    </el-dialog>
    <!--<el-dialog v-el-drag-dialog :append-to-body="true" :visible.sync="showImageDialog" center-->
    <!--           style="text-align: center" title="拧紧指导图片" width="600px">-->
    <!--  <div id="ww" style="width: 600px; height: 834px;overflow-y: auto;">-->
    <!--    <el-image v-for="url in pic" :key="url" :preview-src-list="pic" :src="url" lazy style="width: 100%">-->
    <!--      <div slot="error">-->
    <!--        无工艺图片内容-->
    <!--      </div>-->
    <!--      <div slot="placeholder">-->
    <!--        未加载是显示的内容-->
    <!--      </div>-->
    <!--    </el-image>-->
    <!--  </div>-->
    <!--</el-dialog>-->

    <el-drawer
        :append-to-body="true"
        :before-close="handleClose"
        :modal="false"
        :visible.sync="showImageDialog"
        :with-mask="false"
        class="custom-drawer-right"
        direction="rtl"
        size="900px"
        title="工艺指导图片">
      <div id="ww" style="width: 900px; height: 834px;overflow-y: auto;">
        <el-image v-for="url in pic" :key="url" :preview-src-list="pic" :src="url" lazy style="width: 100%">
          <div slot="error">
            无工艺图片内容
          </div>
          <div slot="placeholder">
            未加载是显示的内容
          </div>
        </el-image>
      </div>
    </el-drawer>

    <el-drawer
        :append-to-body="true"
        :before-close="handleClose"
        :modal="false"
        :title="ningJinTitle"
        :visible.sync="showImageDialog"
        :with-mask="false"
        class="custom-drawer-left"
        direction="ltr"
        size="1000px">
      <div style="display: flex;flex-wrap: wrap">
        <div v-for="(item, index) in mesOperationShowCount" :key="index" class="operation-show"
             @click="sendWordPosition(item)">
          <div v-if="item.position_Value === 0 && item.position_Value_return === 0" class="gary_bolt">
            {{ item.position }}
          </div>
          <div v-if="item.position_Value === 0 && item.position_Value_return === 1" class="green_bolt">
            {{ item.position }}
          </div>
          <div v-if="item.position_Value === 1 && item.position_Value_return === 1" class="green_bolt">
            {{ item.position }}
          </div>
          <div v-if="item.position_Value === 1 && item.position_Value_return === 0" class="yellow_bolt">
            {{ item.position }}
          </div>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>

import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      tableIndexValue: Math.random(),
      value: true,
      NullWorkValue: false,
      mesWritePalletVisible: false,
      mesOperationShowVisible: false,
      showImageDialog: false,
      mesOperationShowCount: [],
      ningJinTitle: '手动拧紧指导',
      TurnLineWorkValue: true,
      publication: {
        topic: '',
        qos: 0,
        payload: ''
      },
      options: [
        {
          value: 0,
          label: '在线'
        }, {
          value: 2,
          label: '屏蔽'
        }
      ],
      slogan: '管控系统正在调试！',
      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: []
    }
  },
  computed: {
    ...mapGetters([
      'pic'
    ])
  },
  mounted() {
    this.getStationStatus()
    this.getStationButton()
    // this.getOperationList()
    const scope = this
    this.$root.$on('P_C_Message', paramsInfo => {
      switch (paramsInfo.type) {
        case 'C_B_NingJin_Work':
          scope.solveWordPosition(paramsInfo.params)
          break
        case 'C_B_NingJin_Work_Update':
          scope.updateWordPosition(paramsInfo.params)
          break
        case 'C_B_NingJin_Work_Current':
          scope.updateWordPosition_current(paramsInfo.params)
          break
        case 'C_B_NingJin_Work_CutRecipe':
          scope.mesOperationShowVisible = false
          break
      }
    })
  },
  methods: {
    // mes拧紧指导
    mes_qc_operationShow() {
      this.ningJinTitle = '手动拧紧指导(正在初始化，请稍等......)'
      this.mesOperationShowCount = []
      // this.mesOperationShowVisible = true
      this.publication.topic = window.g.mqttPublishTopic
      this.publication.payload = 'WEB|IniSingalToWeb_NingJin|' + this.$store.state.station.stationName + '|1'
      window.g.mqttClient.doPublish(this.publication)

      // 显示指导图片
      this.showImageDialog = true
    },

    // 点击拧紧位置
    sendWordPosition(mesOperationData) {
      // if (mesOperationData.position_Value_return === 1) {
      //   this.$message.error('当前位置已经拧紧完成！')
      //   return
      // }
      const flag = 0
      for (const mesOperationShowCountValue of this.mesOperationShowCount) {
        if (mesOperationShowCountValue.id == mesOperationData.id) {
          mesOperationShowCountValue.position_Value = 1
          mesOperationShowCountValue.position_Value_return = 0
        } else {
          mesOperationShowCountValue.position_Value = 0
          mesOperationShowCountValue.position_Value_return = 0
        }
        // if (mesOperationShowCountValue.position_Value === 1 && mesOperationShowCountValue.id !== mesOperationData.id) {
        //   mesOperationShowCountValue.position_Value = 0
        // }
        // if (mesOperationShowCountValue.position_Value === 1 && mesOperationShowCountValue.id === mesOperationData.id) {
        //   this.$message.warning('当前位置正在拧紧！')
        //   flag = 1
        // }
      }
      // if (flag === 1) return
      this.publication.topic = window.g.mqttPublishTopic
      this.publication.payload = 'WEB|NingJin_Work|' + this.$store.state.station.stationName + '|' + mesOperationData.position
      mesOperationData.position_Value = 1
      window.g.mqttClient.doPublish(this.publication)
      this.$message.success('请进行手动拧紧！')
    },
    handleClose(done) {
      // this.$confirm('确认关闭？')
      //   .then(_ => {
      done()
      //   })
      //   .catch(_ => {
      //   })
    },
    solveWordPosition(params) {
      this.mesOperationShowCount = []
      const operationShow = JSON.parse(params)
      let key = 1
      for (const operationShowElement in operationShow) {
        this.mesOperationShowCount.push({
          id: operationShow[operationShowElement].TagID,
          position: key,
          position_Value: 0,
          position_Value_return: parseInt(operationShow[operationShowElement].TagValue)
        })
        key++
      }
      if (this.mesOperationShowCount.length === 0) {
        this.ningJinTitle = '手动拧紧指导(管控系统未读到配方号！)'
      } else {
        this.ningJinTitle = '手动拧紧指导(初始化完成，请进行手动拧紧！)'
      }
    },
    updateWordPosition(params) {
      const operationShow = JSON.parse(params)
      for (const mesOperationShowCountValue of this.mesOperationShowCount) {
        if (mesOperationShowCountValue.id === operationShow.TagID) {
          mesOperationShowCountValue.position_Value_return = parseInt(operationShow.TagValue)
        } else {
          mesOperationShowCountValue.position_Value = 0
          mesOperationShowCountValue.position_Value_return = 0
        }
      }
    },

    // 获取到点击后反馈的拧紧位置
    updateWordPosition_current(params) {
      for (const mesOperationShowCountValue of this.mesOperationShowCount) {
        if (mesOperationShowCountValue.position === parseInt(params)) {
          mesOperationShowCountValue.position_Value = 1
        } else {
          mesOperationShowCountValue.position_Value = 0
          mesOperationShowCountValue.position_Value_return = 0
        }
      }
    },
    tableRowClassName({ row }) {
      if (row.报警等级 === '错误') {
        return 'alarm_Alarm'
      } else if (row.报警等级 === '报警') {
        return 'alarm_Alarm'
      } else if (row.报警等级 === '警告') {
        return 'warning_Alarm'
      } else {
        return 'alarm_Default'
      }
    },
    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('/')
    },
    // 不合格放行
    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
      })
    },
    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'])
        }
      })
    },
    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>
.custom-drawer-left, .custom-drawer-right {
  /* 设置一个较高的 z-index 值，确保它们位于其他元素之上 */
  z-index: 4001;
}

.custom-drawer-left {
  width: 1000px;
}

.operation-show {
  text-align: center;
  border: 1px solid #737373;
  border-radius: 5px;
  margin: 5px 5px 0 0;
  cursor: pointer;
  font-size: 34px;
}

.green_bolt {
  width: 95px;
  height: 95px;
  line-height: 95px;
  color: #000000;
  font-weight: bolder;
  background-size: 100% 100%;
  background-position: center;
  background-repeat: no-repeat;
  background-image: url("../../../assets/image/green.png")
}

.yellow_bolt {
  width: 95px;
  height: 95px;
  line-height: 95px;
  color: #000000;
  font-weight: bolder;
  background-position: center;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-image: url("../../../assets/image/StartWorkCircle.gif")
}

.gary_bolt {
  width: 95px;
  height: 95px;
  line-height: 95px;
  color: #000000;
  font-weight: bolder;
  background-position: center;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-image: url("../../../assets/image/grey.png")
}

::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 {
  position: absolute;
  color: red;
  font-size: 32px;
  font-weight: bolder;
  margin-top: 2px;
  animation-name: example;
  animation-duration: 18s;
  animation-iteration-count: infinite;
}

/*.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>
