<template>
  <div>
    <el-card>
      <el-badge :value="value" class="item" style="margin-bottom: 10px; float: right">
        <el-button size="small" @click="openWindow">Andon信息</el-button>
      </el-badge>
      <el-table
        :data="tableData"
        style="width: 100%;"
        size="mini"
        height="800"
        highlight-current-row
        border>
        <el-table-column align="center" label="照片" width="50">
          <template slot-scope="scope">
            <el-popover
              v-if="scope.row.文件标识 && scope.row.文件标识 !== 'null'"
              placement="top-start"
              width="200"
              trigger="hover">
              <el-button type="text" style="float:right" @click="deletePhoto(scope.row)">删除</el-button>
              <el-image :src="scope.row.文件标识 && scope.row.文件标识 !== 'null' ? (readFile + scope.row.文件标识 + '.' + scope.row.文件后缀) : readFile + '123.jpg'">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"/>
                </div>
              </el-image>
              <el-image slot="reference" :src="scope.row.文件标识 && scope.row.文件标识 !== 'null' ? (readFile + scope.row.文件标识 + '.' + scope.row.文件后缀) : readFile + '123.jpg'" style="width:30px;">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"/>
                </div>
              </el-image>
            </el-popover>
            <span v-else>—</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="工位号">
          <template slot-scope="scope">
            <el-button type="text" @click="openNew(scope.row.工位号)">{{ scope.row.工位号 }}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="登陆人">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.姓名===''||scope.row.姓名===null" slot="reference" size="small" style="width: 80px" type="warning">未登录</el-tag>
            <el-tag v-else slot="reference" type="success" size="small" style="width: 80px"> {{ scope.row.姓名 }} </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="呼叫类型">
          <template slot-scope="scope">
            <div v-if="scope.row.呼叫备注===''|| scope.row.呼叫备注===null"><div style="width: 12px;height: 12px;border-radius: 50%;background-color: rgb(103, 194, 58);display: inline-block;margin:0px 5px 0 0"/>正常工作</div>
            <div v-else ><div style="width: 12px;height: 12px;border-radius: 50%;background-color: red;display: inline-block;margin:0px 5px 0 0"/>{{ scope.row.呼叫备注 }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="200">
          <template slot-scope="scope">
            <el-button
              :disabled="scope.row.文件标识 && scope.row.文件标识 !== 'null'"
              size="mini"
              type="primary"
              icon="el-icon-picture"
              @click="handlePicture(scope.row)">上传图片</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      :visible.sync="dialogVisible"
      width="60%">
      <el-table
        :data="tableDataMassage"
        style="width: 100%;"
        size="mini"
        height="500"
        highlight-current-row
        border>
        <el-table-column align="center" label="工位号">
          <template slot-scope="scope">
            <el-button type="text">{{ scope.row.工位号 }}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="登陆人">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.姓名===''||scope.row.姓名===null" slot="reference" size="small" style="width: 80px" type="warning">未登录</el-tag>
            <el-tag v-else slot="reference" type="success" size="small" style="width: 80px"> {{ scope.row.姓名 }} </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="呼叫类型">
          <template slot-scope="scope">
            <div v-if="scope.row.呼叫备注===''|| scope.row.呼叫备注===null"><div style="width: 12px;height: 12px;border-radius: 50%;background-color: rgb(103, 194, 58);display: inline-block;margin:0px 5px 0 0"/>正常工作</div>
            <div v-else ><div style="width: 12px;height: 12px;border-radius: 50%;background-color: red;display: inline-block;margin:0px 5px 0 0"/>{{ scope.row.呼叫备注 }}</div>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog :visible.sync="dialogFormVisible" title="上传图片" center style="height: 800px" width="30%">
      <el-upload
        id="invoiceScan"
        ref="upload"
        :multiple="true"
        :auto-upload="false"
        :on-success="uploadSuccess"
        :before-upload="beforeUpload"
        :on-exceed="warningExceed"
        :limit="limit"
        :action="upload+'?num='+ OPId"
        list-type="picture-card">
        <i class="el-icon-plus"/>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible=false">取消</el-button>
        <el-button type="primary" style="width: 70px" @click="submitPerson()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { ANDON_countselect } from '@/api/ManufacturingCenter/AntonMonitoring'
import { wsuri, name1, uploadOP } from '@/utils/request'
import { readFile } from '@/utils/request'
import request from '@/utils/request'
export default {
  data() {
    return {
      readFile: readFile,
      limit: 9999,
      tableData: [],
      timer_ANDON: '',
      andonMsg: '',
      Name: '',
      upload: uploadOP,
      dialogFormVisible: false,
      OPId: '',
      massage: [],
      dialogVisible: false,
      tableDataMassage: [],
      value: 0
    }
  },
  mounted() {
    this.ANDON_countselect()
    this.timer_ANDON = setInterval(this.search(), 60 * 1000 * 5) // ANDON看板 5 分钟一刷新
  },
  created() {
    this.initWebpack()
  },
  methods: {
    deletePhoto(row) {
      this.$confirm('此操作将永久删除该图片, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        request({
          url: '',
          method: 'post',
          data: {
            type: '4',
            name: `delete from 设备管理_设备监控_图片 where 工位 = '${row.工位号}'`
          }
        }).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.ANDON_countselect()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除'
        })
      })
    },
    openNew(val) {
      window.open(`http://192.168.1.231:8008/#/EquipmentConditionAnalysis?opname=${val}`, '_blank')
    },
    ANDON_countselect() {
      ANDON_countselect().then(response => {
        this.tableData = response.data
      })
    },
    // 初始化websocket
    initWebpack() {
      this.websock = new WebSocket(wsuri)// 这里面的this都指向vue
      this.websock.onopen = this.websocketopen
      this.websock.onmessage = this.websocketonmessage
      this.websock.onclose = this.websocketclose
      this.websock.onerror = this.websocketerror
    },
    // 打开
    websocketopen() {
      console.log('WebSocket连接成功')
      this.websock.send('{<' + name1 + '>}')
    },
    // 数据接收
    websocketonmessage(msg) {
      console.log(msg)
      this.andonMsg = msg.data.split('|')[3]
      this.Name = msg.data.split('|')[2]
      if (this.andonMsg === '1') {
        this.search()
      }
    },
    // 关闭
    websocketclose() {
      console.log('WebSocket关闭')
    },
    // 失败
    websocketerror() {
      console.log('WebSocket连接失败')
    },
    uploadSuccess(res) { // 上传成功回调
      if (res[0].result === '1') {
        this.$message.success('增加成功')
        this.dialogFormVisible = false
        this.ANDON_countselect()
        this.$refs.upload.clearFiles()
      } else {
        this.$message.error('增加失败')
      }
    },
    beforeUpload(file) { // 上传前检测
      const isJPG = /^image\/.*/.test(file.type)
      if (!isJPG) {
        this.$message.error('只能上传图片。。')
      }
      return isJPG
    },
    warningExceed() {
      this.$message.error('仅可上传五张照片')
    },
    handlePicture(row) {
      this.dialogFormVisible = true
      this.OPId = row.工位号
    },
    submitPerson() {
      this.$refs.upload.submit()
    },
    search() {
      this.tableDataMassage = []
      ANDON_countselect().then(response => {
        this.tableData = response.data
        return response.data
      }).then(data => {
        for (let i = 0; i < data.length; i++) {
          if (data[i].呼叫备注 !== null) {
            this.tableDataMassage.push(data[i])
          }
        }
        this.value = this.tableDataMassage.length
      })
    },
    openWindow() {
      this.dialogVisible = true
    }
  }

}
</script>

<style scoped>
  .el-card{
    margin-left: 2px;
    margin-top: 2px;
    margin-bottom: 2px;
    margin-right: 2px;
  }
</style>
