<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="font-weight: bolder">作业状态</span>
      </div>
      <el-table
        :data="jobTicket"
        :header-cell-style="{ 'text-align': 'center' }"
        :cell-style="{ 'text-align': 'center' }"
      >
<!--        <el-table-column label="作业编号" prop="ticketCode">-->
<!--        </el-table-column>-->
        <el-table-column label="作业名称" prop="ticketName">
        </el-table-column>
        <el-table-column label="状态" align="center" prop="ticketStatus">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.ticket_status"
              :value="scope.row.ticketStatus"
            />
          </template>
        </el-table-column>
        <el-table-column label="上锁钥匙" prop="lockKeyName">
        </el-table-column>
        <el-table-column label="解锁钥匙" prop="colockKeyName">
        </el-table-column>
      </el-table>
    </el-card>

    <div style="margin-top: 10px; height: 620px; overflow-y: auto">
      <el-col :span="6" style="height: 1000px">
        <el-card class="box-card1">
          <div slot="header" class="clearfix">
            <span style="font-weight: bolder">人员状态</span>
          </div>
          <el-table :data="lockUserList">
            <el-table-column label="上锁人" prop="userName"></el-table-column>
            <el-table-column label="状态" align="center" prop="jobStatus">
              <template slot-scope="scope">
                <div
                  class="box"
                  style="
                    width: 30px;
                    height: 30px;
                    border: 1px solid black;
                    background-color: white;
                    margin: 0 auto;
                  "
                >
                  <span
                    v-if="
                      scope.row.jobStatus == 0 ||
                      scope.row.jobStatus == 1 ||
                      scope.row.jobStatus == 2 ||
                      scope.row.jobStatus == 3
                    "
                  ></span>
                  <!-- 显示错号 -->
                  <span
                    v-else-if="scope.row.jobStatus == 4"
                    style="font-size: 20px; line-height: 30px"
                  >🔒</span
                  >
                  <!-- 显示对号 -->
                  <span
                    v-else-if="scope.row.jobStatus == 5"
                    style="font-size: 20px; line-height: 30px"
                  >✓</span
                  >
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
        <el-card class="box-cardgsr">
          <el-table :data="colockUserList">
            <el-table-column label="共锁人" prop="userName"></el-table-column>
            <el-table-column label="状态" align="center" prop="jobStatus">
              <template slot-scope="scope">
                <div
                  class="box"
                  style="
                    width: 30px;
                    height: 30px;
                    border: 1px solid black;
                    background-color: white;
                    margin: 0 auto;
                  "
                >
                  <span
                    v-if="
                      scope.row.jobStatus == 0 ||
                      scope.row.jobStatus == 1 ||
                      scope.row.jobStatus == 2 ||
                      scope.row.jobStatus == 3
                    "
                  ></span>
                  <!-- 显示错号 -->
                  <span
                    v-else-if="scope.row.jobStatus == 4"
                    style="font-size: 20px; line-height: 30px"
                  >🔒</span
                  >
                  <!-- 显示对号 -->
                  <span
                    v-else-if="scope.row.jobStatus == 5"
                    style="font-size: 20px; line-height: 30px"
                  >✓</span
                  >
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="box-card2">
          <div slot="header" class="clearfix">
            <span style="font-weight: bolder">隔离点状态</span>
          </div>
          <el-table :data="ticketPointsList">
            <el-table-column label="隔离点" prop="pointName" width="80">
            </el-table-column>

            <el-table-column label="状态" align="center" prop="pointStatus">
              <template slot-scope="scope">
                <div
                  class="box"
                  style="
                    width: 30px;
                    height: 30px;
                    border: 1px solid black;
                    background-color: white;
                    margin: 0 auto;
                  "
                >
                  <span v-if="scope.row.pointStatus == 0" style=""></span>
                  <!-- 显示错号 -->
                  <span
                    v-else-if="scope.row.pointStatus == 1"
                    style="font-size: 20px; line-height: 30px"
                  >🔒</span
                  >
                  <!-- 显示对号 -->
                  <span
                    v-else-if="scope.row.pointStatus == 2"
                    style="font-size: 20px; line-height: 30px"
                  >✓</span
                  >
                </div>
              </template>
            </el-table-column>
            <el-table-column label="挂锁名称" prop="lockName">
            </el-table-column>
            <el-table-column label="锁具机构" prop="locksetName"></el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </div>
<!--    <div class="message-list">-->
<!--      <div class="message_title">-->
<!--        <div class="dropdown">-->
<!--          <el-tooltip class="item" effect="dark" content="展开" placement="left">-->
<!--            <button class="dropdown-button" @click="changeShowDropdown">-->
<!--              <i class="el-icon-s-operation"></i>-->
<!--            </button>-->
<!--          </el-tooltip>-->
<!--          <div class="dropdown-content" v-if="showDropdown">-->
<!--            <el-tooltip class="item" effect="dark" content="作业状态" placement="left">-->
<!--              <button class="dropdown-item">-->
<!--                <i class="el-icon-plus"></i>-->
<!--              </button>-->
<!--            </el-tooltip>-->
<!--            <el-tooltip class="item" effect="dark" content="上锁状态" placement="left">-->
<!--              <button class="dropdown-item">-->
<!--                <i class="el-icon-circle-plus"></i>-->
<!--              </button>-->
<!--            </el-tooltip>-->
<!--            <el-tooltip class="item" effect="dark" content="共锁状态" placement="left">-->
<!--              <button class="dropdown-item">-->
<!--                <i class="el-icon-circle-plus-outline"></i>-->
<!--              </button>-->
<!--            </el-tooltip>-->
<!--          </div>-->
<!--        </div>-->
<!--        作业执行-操作日志-->
<!--      </div>-->
<!--      <el-timeline style="padding-top: 15px">-->
<!--        <el-timeline-item v-for="(v,i) in items" :timestamp="v.date" :key="i" placement="top" color="#0bbd87">-->
<!--          <el-card style="width: 450px">-->
<!--            <h4>{{ v.index }}</h4>-->
<!--            <p>{{ v.content }}</p>-->
<!--          </el-card>-->
<!--        </el-timeline-item>-->
<!--      </el-timeline>-->
<!--    </div>-->
  </div>
</template>

<script>
import { getJobPlayTicketInfo } from '@/api/mes/jobplay/jobplay'
import { connectWebsocket, closeWebsocket, sendMsg } from '@/utils/websocket'

export default {
  name: 'jobPlayDetail',
  dicts: ['ticket_type', 'ticket_status'],

  data() {
    return {
      showDropdown: false,//作业执行切换按钮
      jobTicket: [],
      lockUserList: [],
      colockUserList: [],
      ticketPointsList: [],
      ticketId: null,
      intervalId: null,
      items: [
        {
          index: '2020-8-14 13:20:30',
          content: '开始前端。。',
          date: '2018/5/4'
        },
        { index: '2020-8-15 13:20:30', content: '写前端。。', date: '2018/5/5' },
        {
          index: '2020-8-16 13:20:30',
          content: '还在写前端。。',
          date: '2018/5/6'
        },
        {
          index: '2020-8-17 13:20:30',
          content: '仍在写前端。。',
          date: '2018/5/7'
        },
        { index: '2020-8-18 13:20:30', content: '不想写前端。。', date: '2018/5/8' },
        { index: '2020-8-19 13:20:30', content: '还得写前端。。。。。', date: '2018/5/9' }
      ],

      isFetching: false // 标志位，控制接口请求是否已经执行过
    }
  },
  watch: {
    '$route.query.ticketId': {
      immediate: true,
      handler(newValue, oldValue) {
        this.getJobPlayInfo()
      }
    },
    $route(to, from) {
      if (to.path === '/mes/job/jobplay/jobPlayDetail') {
        this.initWebSocket()
      }
    }
  },
  created() {
    this.getJobPlayInfo()
    this.initWebSocket()
  },

  // 页面销毁就停止webStocket
  beforeDestroy() {
    this.destory()
  },

  methods: {
    changeShowDropdown() {
      console.log('我点击率')
      this.showDropdown = !this.showDropdown
    },
    getJobPlayInfo() {
      let ticketId
      if (this.$route.query.ticketId) {
        ticketId = this.$route.query.ticketId
      } else {
        ticketId = localStorage.getItem('ticketId')
      }
      getJobPlayTicketInfo(ticketId).then((res) => {
        console.log(res, '作业执行详细信息')
        this.jobTicket = [res.data.jobTicket].map((item) => ({
          ...item,
          lockKeyName: res.data.lockKeyName,
          colockKeyName: res.data.colockKeyName
        }))
        this.lockUserList = res.data.lockUserList
        this.colockUserList = res.data.colockUserList
        this.ticketPointsList = res.data.ticketPointsList
      })
    },
    initWebSocket() {
      const timestep = Date.now()
      connectWebsocket(`ws://192.168.1.127:9090/websocket/jobTicket/${timestep}`, { w: 'S' }, (res) => {
          console.log(res, 'websocket接收服务器的数据')
          const resMsg = res
          if (resMsg == 'refresh') {
            this.handleRefresh()
          }
        },
        (err) => {
          console.log('断开重连')
        })
    },
    handleRefresh() {
      if (this.isFetching) {
        console.log('接口请求正在进行中，忽略本次 refresh 消息')
        return
      }

      this.isFetching = true // 设置标志位为 true，表示接口请求正在进行中

      let ticketId
      if (this.$route.query.ticketId) {
        ticketId = this.$route.query.ticketId
      } else {
        ticketId = localStorage.getItem('ticketId')
      }

      getJobPlayTicketInfo(ticketId).then((res) => {
        console.log(res, '作业执行详细信息')
        this.jobTicket = [res.data.jobTicket].map((item) => ({
          ...item,
          lockKeyName: res.data.lockKeyName,
          colockKeyName: res.data.colockKeyName
        }))
        this.lockUserList = res.data.lockUserList
        this.colockUserList = res.data.colockUserList
        this.ticketPointsList = res.data.ticketPointsList
      }).catch((error) => {
        console.error('获取作业执行详细信息失败', error)
      }).finally(() => {
        this.isFetching = false // 重置标志位为 false，允许下次 refresh 消息执行接口请求
      })
    },
    destory() {
      closeWebsocket()//断开链接
    },
    // 发消息
    sendMsg() {
      sendMsg('aaa')
    }

  }
}
</script>


<style scoped lang="scss">
.message-list {
  height: 820px;
  width: 570px;
  background: white;
  box-shadow: 2px 2px 5px 8px rgba(246, 246, 246, 0.6);
  position: absolute;
  right: 50px;
  top: 0;
  overflow: auto;
}

.message_title {
  position: sticky;
  top: 0;
  z-index: 999;
  height: 40px;
  text-align: center;
  line-height: 40px;
  display: block;
  background: white;
  font-size: 18px;
  font-weight: bolder;

}

.dropdown {
  position: absolute;
  left: 2px;
  top: 7px;

  display: inline-block;
}

.dropdown-button {
  background-color: #409EFF;
  color: white;
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;

  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dropdown-button:hover {
  background-color: #66b1ff;
}

.dropdown-content {
  display: flex;
  flex-direction: column;
  position: absolute;
  background-color: transparent;
  min-width: 160px;
  //box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
  top: 53px;
  left: -5px;
}

.dropdown-item {
  background-color: #409EFF;
  color: white;
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 5px;
}

.dropdown-item:hover {
  background-color: #66b1ff;
}

.dropdown-item i {
  font-size: 18px;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 1010px;
  margin: 15px;
}

.box-card1 {
  width: 400px;
  margin: 15px;
}

.box-cardgsr {
  width: 400px;
  height: auto;
  margin: 15px;
}

.box-card2 {
  width: 580px;
  margin: 15px;
}

.status-container {
  width: 100%;
  height: 100%;
  background: pink;
  display: flex;
}

.box {
  width: 30px;
  height: 30px;
  border: 1px solid #e7e5e5;
  background-color: rgb(209, 77, 77);
}

.error-icon {
  color: red;
  font-weight: bold;
}

.success-icon {
  color: green;
  font-weight: bold;
}

//滚动条的宽度
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
  background-color: #e4e4e4;
  border-radius: 6px;
}

//滚动条的滑块
::-webkit-scrollbar-thumb {
  background-color: #a1a3a9;
  border-radius: 6px;
}
</style>
