<template>
  <div class="app-container">

    <el-card>
      <span>工位号:</span>
      <el-select v-model="MachineValue" filterable size="small" style="width:220px;margin-bottom:10px" placeholder="工位号" @change="searchTable()">
        <el-option
          v-for="item in Machine"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          <div style="float: left">{{ item.label }}</div>
          <div style="float: right; color: #8492a6; font-size: 13px">{{ item.value2 }}</div>
        </el-option>
      </el-select>

      <el-button type="success" class="el-icon-refresh" size="small" style="margin: 10px 0 0 10px" @click="Refresh">刷新工位</el-button>
      <el-badge :value="num" :max="99" class="item">
        <el-button size="small" type="text" @click="dialogFormVisible=true;Jurisdiction()">调序权限</el-button>
      </el-badge>
    </el-card>

    <el-card>
      <el-table v-loading="loading0" :data="tableData" height="650" style="width: 100%;" border @expand-change="searchTable2">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-table :data="tableData2" border style="text-align: center" size="mini">
              <el-table-column label="零件加工计划进度(工艺名称,计划完成时间,实际完成时间,员工名称)" >
                <el-table-column label="[1]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[0]">
                      <h4>{{ process[0] }}</h4><h4>{{ planDate[0] }} </h4><h4> {{ realDate[0] }}</h4><h4> {{ worker[0] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[2]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[1]">
                      <h4>{{ process[1] }}</h4><h4>{{ planDate[1] }} </h4><h4> {{ realDate[1] }}</h4><h4> {{ worker[1] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[3]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[2]">
                      <h4>{{ process[2] }}</h4><h4>{{ planDate[2] }} </h4><h4> {{ realDate[2] }}</h4><h4> {{ worker[2] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[4]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[3]">
                      <h4>{{ process[3] }}</h4><h4>{{ planDate[3] }} </h4><h4> {{ realDate[3] }}</h4><h4> {{ worker[3] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[5]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[4]">
                      <h4>{{ process[4] }}</h4><h4>{{ planDate[4] }} </h4><h4> {{ realDate[4] }}</h4><h4> {{ worker[4] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[6]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[5]">
                      <h4>{{ process[5] }}</h4><h4>{{ planDate[5] }} </h4><h4> {{ realDate[5] }}</h4><h4> {{ worker[5] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[7]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[6]">
                      <h4>{{ process[6] }}</h4><h4>{{ planDate[6] }} </h4><h4> {{ realDate[6] }}</h4><h4> {{ worker[6] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[8]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[7]">
                      <h4>{{ process[7] }}</h4><h4>{{ planDate[7] }} </h4><h4> {{ realDate[7] }}</h4><h4> {{ worker[7] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[9]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[8]">
                      <h4>{{ process[8] }}</h4><h4>{{ planDate[8] }} </h4><h4> {{ realDate[8] }}</h4><h4> {{ worker[8] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[10]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[9]">
                      <h4>{{ process[9] }}</h4><h4>{{ planDate[9] }} </h4><h4> {{ realDate[9] }}</h4><h4> {{ worker[9] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[11]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[10]">
                      <h4>{{ process[10] }}</h4><h4>{{ planDate[10] }} </h4><h4> {{ realDate[10] }}</h4><h4> {{ worker[10] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[12]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[11]">
                      <h4>{{ process[11] }}</h4><h4>{{ planDate[11] }} </h4><h4> {{ realDate[11] }}</h4><h4> {{ worker[11] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[13]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[12]">
                      <h4>{{ process[12] }}</h4><h4>{{ planDate[12] }} </h4><h4> {{ realDate[12] }}</h4><h4> {{ worker[12] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[14]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[13]">
                      <h4>{{ process[13] }}</h4><h4>{{ planDate[13] }} </h4><h4> {{ realDate[13] }}</h4><h4> {{ worker[13] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[15]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[14]">
                      <h4>{{ process[14] }}</h4><h4>{{ planDate[14] }} </h4><h4> {{ realDate[14] }}</h4><h4> {{ worker[14] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[16]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[15]">
                      <h4>{{ process[15] }}</h4><h4>{{ planDate[15] }} </h4><h4> {{ realDate[15] }}</h4><h4> {{ worker[15] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[17]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[16]">
                      <h4>{{ process[16] }}</h4><h4>{{ planDate[16] }} </h4><h4> {{ realDate[16] }}</h4><h4> {{ worker[16] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[18]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[17]">
                      <h4>{{ process[17] }}</h4><h4>{{ planDate[17] }} </h4><h4> {{ realDate[17] }}</h4><h4> {{ worker[17] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[19]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[18]">
                      <h4>{{ process[18] }}</h4><h4>{{ planDate[18] }} </h4><h4> {{ realDate[18] }}</h4><h4> {{ worker[18] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[20]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[19]">
                      <h4>{{ process[19] }}</h4><h4>{{ planDate[19] }} </h4><h4> {{ realDate[19] }}</h4><h4> {{ worker[19] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[21]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[20]">
                      <h4>{{ process[20] }}</h4><h4>{{ planDate[20] }} </h4><h4> {{ realDate[20] }}</h4><h4> {{ worker[20] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[22]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[21]">
                      <h4>{{ process[21] }}</h4><h4>{{ planDate[21] }} </h4><h4> {{ realDate[21] }}</h4><h4> {{ worker[21] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[23]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[22]">
                      <h4>{{ process[22] }}</h4><h4>{{ planDate[22] }} </h4><h4> {{ realDate[22] }}</h4><h4> {{ worker[22] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[24]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[23]">
                      <h4>{{ process[23] }}</h4><h4>{{ planDate[23] }} </h4><h4> {{ realDate[23] }}</h4><h4> {{ worker[23] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[25]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[24]">
                      <h4>{{ process[24] }}</h4><h4>{{ planDate[24] }} </h4><h4> {{ realDate[24] }}</h4><h4> {{ worker[24] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[26]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[25]">
                      <h4>{{ process[25] }}</h4><h4>{{ planDate[25] }} </h4><h4> {{ realDate[25] }}</h4><h4> {{ worker[25] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[27]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[26]">
                      <h4>{{ process[26] }}</h4><h4>{{ planDate[26] }} </h4><h4> {{ realDate[26] }}</h4><h4> {{ worker[26] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[28]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[27]">
                      <h4>{{ process[27] }}</h4><h4>{{ planDate[27] }} </h4><h4> {{ realDate[27] }}</h4><h4> {{ worker[27] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[29]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[28]">
                      <h4>{{ process[28] }}</h4><h4>{{ planDate[28] }} </h4><h4> {{ realDate[28] }}</h4><h4> {{ worker[28] }}</h4>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="[30]" align="center" width="120px">
                  <template slot-scope="scope">
                    <div :class="colorCode[29]">
                      <h4>{{ process[29] }}</h4><h4>{{ planDate[29] }} </h4><h4> {{ realDate[29] }}</h4><h4> {{ worker[29] }}</h4>
                    </div>
                  </template>
                </el-table-column>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column label="零件名称" align="center" width="220px">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="零件图号" align="center" width="270px">
          <template slot-scope="scope">
            {{ scope.row.零件图号_短 }}
          </template>
        </el-table-column>
        <el-table-column label="待加工工序" align="center">
          <template slot-scope="scope">
            {{ scope.row.工艺名称 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300" align="center">
          <template slot-scope="scope">
            <div>
              <el-button
                size="mini"
                icon="el-icon-arrow-up"
                type="primary"
                @click="upOne(scope.row)"/>
              <el-button
                size="mini"
                icon="el-icon-arrow-down"
                type="primary"
                @click="downOne(scope.row)"/>
              <el-button
                size="mini"
                icon="el-icon-upload2"
                type="primary"
                @click="TOP(scope.row)"/>
              <el-button
                size="mini"
                icon="el-icon-download"
                type="primary"
                @click="Down(scope.row)"/>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          v-if="!loading0"
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>

    <el-dialog :visible.sync="dialogFormVisible" width="60%">
      <el-table :data="gridData" height="500" size="mini" border>
        <el-table-column property="date" label="工位号" width="300" align="center">
          <template slot-scope="scope">
            {{ scope.row.MES终端号 }}
          </template>
        </el-table-column>
        <el-table-column property="name" label="调序权限" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.调序权限==='1'" type="success" size="small">已开放</el-tag>
            <el-tag v-else type="info" size="small">未开放</el-tag>
          </template>
        </el-table-column>
        <el-table-column property="date" label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.调序权限==='0'"
              size="mini"
              type="success"
              @click="JurisdictionControl1(scope.row)">开启</el-button>
            <el-button
              v-if="scope.row.调序权限==='1'"
              size="mini"
              type="danger"
              @click="JurisdictionControl2(scope.row)">关闭</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import { getMachine, getMachine2, searchtable, getTable2, Order, JurisdictionControl } from '@/api/ManufacturingCenter/MachiningSequenceAdjustment'
import { wsuri, name } from '@/utils/request'
export default {
  data() {
    return {
      opname: '', // 用来通知工位刷新数据
      msg: '',
      num: 0,
      count: 0,
      dialogFormVisible: false,
      gridData: [],
      Machine: [],
      MachineValue: '',
      loading0: false,
      tableData2: [], // 工艺数据
      process: [],
      planDate: [],
      worker: [],
      colorCode: [],
      realDate: [],
      result: [],
      零件图号: [],
      tableData: [], // 表格数据
      pageSize: 10,
      pageCurrent: 1,
      total: 0
    }
  },
  watch: {
    MachineValue(val) {
      this.searchTable()
    }
  },
  created() {
    this.Jurisdiction()
    this.fetchData()
    this.initWebsocket()
  },
  methods: {
    initWebsocket() { // 初始化websocket
      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连接成功')
      // MessageBox('提示', '连接成功')
      this.websock.send('LIN,' + name)
    },
    websocketonmessage(msg) { // 数据接收
      console.log(msg)
      this.msg = msg
    },
    websocketclose() { // 关闭
      console.log('WebSocket关闭')
    },
    websocketerror() { // 失败
      console.log('WebSocket连接失败')
      // MessageBox('提示', '连接失败');
      // this.socketConnect = false
    },
    Refresh() {
      // this.initWebsocket()
      this.websock.send('TOBS?MES?' + this.MachineValue + '?MES|SequenceAdjustment|' + this.MachineValue + '|1')
    },
    // 初始化获取工位
    fetchData() {
      this.MachineValue = ''
      this.Machine = []
      getMachine().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Machine.push({
            label: response.data[i].终端编号,
            value: response.data[i].终端编号,
            value2: response.data[i].姓名
          })
        }
      }).then(() => {
        this.MachineValue = this.Machine[0].value
      })
    },
    searchTable() {
      if (this.MachineValue !== '' && this.MachineValue !== null) {
        this.loading0 = true
        searchtable(this.MachineValue, this.pageCurrent, this.pageSize).then(response => {
          this.tableData = response.data.rows
          this.total = response.data.total
          this.loading0 = false
        })
      } else {
        this.$message.error('请选择工位')
      }
    },
    searchTable2(row) {
      this.process = []
      this.planDate = []
      this.worker = []
      this.colorCode = []
      this.realDate = []
      this.tableData2 = []
      getTable2(row.零件图号).then(response => {
        for (let k = 0; k < response.data.length; k++) {
          this.process[k] = response.data[k].工艺名称简称
          this.planDate[k] = response.data[k].计划日期_短
          this.realDate[k] = response.data[k].完成日期_短
          if (response.data[k].工艺编号 === 29) {
            response.data[k].员工姓名 = '备料'
          }
          if (response.data[k].工序间是否外协 === 2) {
            response.data[k].员工姓名 = '外协'
          }
          if (response.data[k].员工姓名 === '超级管理员') {
            response.data[k].员工姓名 = '无'
          }
          this.worker[k] = response.data[k].员工姓名
          if (response.data[k].工序状态 === 5) { // 灰色是已经完成 未入库
            this.colorCode[k] = 'grey'
          } else {
            this.colorCode[k] = response.data[k].进度颜色
          }
          if (response.data[k].工序间是否外协 === 2) {
            this.colorCode[k] = 'lightblue'
          }
        }
        this.tableData2.push(response.data[0])
      })
    },
    Jurisdiction() {
      this.count = 0
      getMachine2().then(response => {
        this.gridData = response.data
        for (let i = 0; i < response.data.length; i++) {
          if (response.data[i].调序权限 === '1') { this.count = this.count + 1 }
        }
        this.num = this.count
      })
    },
    JurisdictionControl1(row) {
      this.opname = row.MES终端号
      JurisdictionControl(row.MES终端号, 1).then(response => {
        this.Jurisdiction()
        return this.opname
      }).then(data => {
        this.websock.send('TOBS?MES?' + data + '?MES|PartsSequentialAuthority|' + data + '|1')
      })
    },
    JurisdictionControl2(row) {
      this.opname = row.MES终端号
      JurisdictionControl(row.MES终端号, 2).then(response => {
        this.Jurisdiction()
        return this.opname
      }).then(data => {
        this.websock.send('TOBS?MES?' + data + '?MES|PartsSequentialAuthority|' + data + '|0')
      })
    },
    // 上移
    upOne(row) {
      Order(this.MachineValue, row.工序流水号, '上移').then(response => {
        this.searchTable()
      })
    },
    // 下移
    downOne(row) {
      Order(this.MachineValue, row.工序流水号, '下移').then(response => {
        this.searchTable()
      })
    },
    // 置顶
    TOP(row) {
      Order(this.MachineValue, row.工序流水号, '置顶').then(response => {
        this.searchTable()
      })
    },
    // 置底
    Down(row) {
      Order(this.MachineValue, row.工序流水号, '置底').then(response => {
        this.searchTable()
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>
  .item {
    float: right;
    margin-top: 10px;
    margin-right: 40px;
  }
  .el-form-item{
    margin-bottom: 22px;
  }
  .el-card{
    margin-bottom: 15px;
  }
  span{
    margin: 10px 0 10px 10px;
  }
  h4{
    border: 1px solid transparent;
    margin: 0;
    font-weight: normal;
    width: 100px;
    height: 24px;
  }
  .white{
    background-color: ghostwhite;
    border: 1px solid #1f2d3d;
  }
  .grey{
    background-color: grey;
    border: 1px solid #1f2d3d;
  }
  .red{
    background-color: orangered;
    border: 1px solid #1f2d3d;
  }
  .lightblue{
    background-color: deepskyblue;
    border: 1px solid #1f2d3d;
  }
  .yellow{
    background-color: yellow;
    border: 1px solid #1f2d3d;
  }
  .blank{
    background-color: transparent;
    border: 0px solid white;
  }
  .lightgreen{
    background-color: lightgreen;
    border: 1px solid #1f2d3d;
  }
</style>
