<template>
  <div class="app-container">
    <div style="width: 100%;overflow:auto">
      <div style="white-space: nowrap">
        <el-card style="width: 1375px">
          <el-select v-model="Machine" filterable clearable size="small" style="margin:0px 10px 0px 10px;width: 180px;" placeholder="机床号" @change="getGroup">
            <el-option
              v-for="item in Machines"
              :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-select v-model="Group" size="small" clearable placeholder="组号" style="margin:0px 10px;width: 120px;" @change="getTableData">
            <el-option
              v-for="item in Groups"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
          <el-input v-model="Partpaint" placeholder="零件号" clearable size="small" style="width: 180px;margin:0px 10px;"/>
          <el-button type="primary" plain class="el-icon-search" size="small" style="margin: 0px 10px;" @click="pageCurrent = 1;pageSize = 50;getTableData()">查询</el-button>
          <el-button :disabled="Name===1" type="success" plain class="el-icon-finished" size="small" style="margin-left: 220px" @click="submit">完成</el-button>
        </el-card>
        <el-row>
          <el-card style="width: 870px; margin-left: 0px;display: inline-block;" >
            <el-table
              :data="tableData"
              loading="loading"
              class="table"
              highlight-current-row
              border
              stripe
              height="750"
              style="width: 100%"
              @current-change="getCurrentRow">
              <el-table-column align="center" type="index" label="序号" width="50"/>
              <el-table-column align="center" label="机床号" show-overflow-tooltip width="120">
                <template slot-scope="scope">
                  {{ scope.row.机床图号 }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="组号" show-overflow-tooltip width="120">
                <template slot-scope="scope">
                  {{ scope.row.组号 }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="零件图号" show-overflow-tooltip min-width="190">
                <template slot-scope="scope">
                  {{ scope.row.零件图号 }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="零件名称" show-overflow-tooltip width="140">
                <template slot-scope="scope">
                  {{ scope.row.零件名称 }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="数量" width="80">
                <template slot-scope="scope">
                  {{ scope.row.投产数量 }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="加工完成" width="110">
                <template slot-scope="scope">
                  {{ scope.row.完成日期 }}
                </template>
              </el-table-column>
            </el-table>
            <div class="block">
              <el-pagination
                :current-page="pageCurrent"
                :page-sizes="[50, 200, 300, 400]"
                :page-size="pageSize"
                :total="total"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"/>
            </div>
          </el-card>
          <el-card style="width: 500px; margin-left: 1px;display: inline-block;vertical-align: top;">
            <el-table
              :data="tableData1"
              loading="loading"
              class="table"
              style="max-height: 830px"
              highlight-current-row
              empty-text="   "
              border
              @row-click="getRow">
              <el-table-column type="index" label="序号" width="50"/>
              <el-table-column align="center" label="工艺名" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.工艺名称 }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="操作者" show-overflow-tooltip min-width="90">
                <template slot-scope="scope">
                  {{ scope.row.操作者 }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="排产日期" min-width="110">
                <template slot-scope="scope">
                  {{ scope.row.排产日期 }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="完成日期" min-width="110">
                <template slot-scope="scope">
                  {{ scope.row.完成日期 }}
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import { getMachines, getGroups, getTable, gettabledata1, submitEdit, searchPartNumber } from '@/api/ManufacturingCenter/MaterialArrival'
import Cookie from 'js-cookie'
import { mapGetters } from 'vuex'
import { wsuri, name4 } from '@/utils/request'

export default {
  data() {
    return {
      Machine: '', // 机床号
      Machines: [],
      Group: '', // 组号
      Groups: [],
      number: '', // 零件图号
      tableData: [], // 工序表
      loading: '',
      checkbox_Machine: '', // 机床
      checkbox_Group: '', // 分组
      checkbox_number: '',
      pageCurrent: 1,
      pageSize: 50,
      total: 0,
      Name: 0,
      Partpaint: '',
      tableData1: [], // 工序表
      radio: '',
      completeTime: '', // 完成时间
      row1: [],
      isShow: true,
      processSerialNumber: '', // 工序流水号
      startTime: '', // 开始时间
      endTime: '',
      dateOfProduction: '', // 排产时间
      title: '',
      dialogFormVisible: false,
      form: {
        人员: '',
        人员编号: '',
        完成时间: ''
      },
      rules: {
        人员: [{ required: true, message: '请选择操作员', trigger: 'change' }],
        人员编号: [{ required: true, message: '人员编号', trigger: 'change' }],
        完成时间: [{ required: true, message: '请选择实际完成时间', trigger: 'change' }]
      },
      dialogFormVisible1: false,
      data2: [],
      List1: [],
      ssks: '',
      Number1: '', // 人员编号
      row2: [],
      khbmbh: ''
    }
  },
  computed: {
    ...mapGetters([
      'id',
      'token'
    ])
  },
  created() {
    this.initWebpack()
    this.getMachine()
  },
  methods: {
    getMachine() {
      if (Cookie.get('machineValue') !== undefined) {
        this.Machine = ''
        this.Machines = []
        this.Group = ''
        this.Groups = []
        getMachines().then(response => {
          for (let i = 0; i < response.data.length; i++) {
            this.Machines.push({
              label: response.data[i].机床图号,
              value: response.data[i].机床流水号,
              value2: response.data[i].项目名称
            })
          }
          if (Cookie.get('group') !== undefined) {
            this.Machine = parseInt(Cookie.get('machineValue'))
            this.getGroup()
            this.getTableData()
            this.Group = parseInt(Cookie.get('group'))
            // this.getTableData()
            Cookie.remove('machineValue')
            Cookie.remove('group')
          } else {
            // this.getTableData()
            Cookie.remove('machineValue')
            Cookie.remove('group')
          }
        })
      } else {
        this.Machine = ''
        this.Machines = []
        this.Group = ''
        this.Groups = []
        getMachines().then(response => {
          for (let i = 0; i < response.data.length; i++) {
            this.Machines.push({
              label: response.data[i].机床图号,
              value: response.data[i].机床流水号,
              value2: response.data[i].项目名称
            })
          }
        })
      }
    }, // 获取组号
    getGroup() {
      this.Group = ''
      this.Groups = []
      getGroups(this.Machine).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Groups.push({
            label: response.data[i].组号,
            value: response.data[i].组件流水号
          })
        }
      })
      this.getTableData()
    }, // 按条件查询
    getTableData() {
      this.tableData = []
      this.Loading = true
      if (this.Machine === '') {
        this.checkbox_Machine = 0
      } else {
        this.checkbox_Machine = 1
      }
      if (this.Group === '') {
        this.checkbox_Group = 0
      } else {
        this.checkbox_Group = 1
      }
      if (this.Partpaint === '') {
        this.checkbox_number = 0
      } else {
        this.checkbox_number = 1
      }
      getTable(this.checkbox_Machine, this.Machine, this.checkbox_Group, this.Group, this.checkbox_number, this.Partpaint, this.startTime, this.endTime, this.pageCurrent, this.pageSize).then(response => {
        if (response.data.rows.length !== 0) {
          for (let i = 0; i < response.data.rows.length; i++) {
            this.tableData.push({
              工艺计划流水号: response.data.rows[i].工艺计划流水号,
              序号: response.data.rows[i].序号,
              零件图号: response.data.rows[i].零件图号,
              零件名称: response.data.rows[i].零件名称,
              机床图号: response.data.rows[i].机床图号,
              组号: response.data.rows[i].组号,
              投产数量: response.data.rows[i].批次数量,
              完成日期: response.data.rows[i].机加工实际完成时间 ? response.data.rows[i].机加工实际完成时间.split(' ')[0] : '' // 排产时间
            })
          }
        }
        this.total = response.data.total
        this.loading = false
      })
    }, // 获取工序表
    getCurrentRow(row) {
      this.completeTime = ''
      this.dateOfProduction = ''
      this.processSerialNumber = ''
      this.radio = true
      this.tableData1 = []
      this.row2 = row
      gettabledata1(row.工艺计划流水号).then(response => {
        if (response.data.length !== 0) {
          for (let i = 0; i < response.data.length; i++) {
            this.tableData1.push({
              工艺名称: response.data[i].工艺名称,
              排产日期: response.data[i].排产时间 ? response.data[i].排产时间.split(' ')[0] : '',
              完成日期: response.data[i].工序完成日期 ? response.data[i].工序完成日期.split(' ')[0] : '',
              操作者: response.data[i].操作者,
              工序流水号: response.data[i].工序流水号
            })
          }
        }
      })
    },
    // 初始化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('{<' + name4 + '>}')
    },
    // 数据接收
    websocketonmessage(msg) {
      this.partNumber = msg.data.split('|')[3]
      this.partNumber = this.partNumber.slice(1)
      this.getPaint(this.partNumber)
    },
    // 关闭
    websocketclose() {
      console.log('WebSocket关闭')
    },
    // 失败
    websocketerror() {
      console.log('WebSocket连接失败')
    },
    getPaint() {
      searchPartNumber(this.partNumber).then(response => {
        this.Partpaint = response.data[0].零件图号
        return this.Partpaint
      }).then(() => {
        this.getTableData()
      })
    },
    submit() {
      submitEdit(this.id, this.processSerialNumber).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('操作成功')
          this.getCurrentRow(this.row2)
        } else {
          this.$message.error('补录失败')
        }
      })
    },
    getRow(row) {
      this.processSerialNumber = row.工序流水号
      row.完成日期 ? this.Name = 1 : this.Name = 0
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageCurrent = 1
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.getTableData()
    }
  }
}
</script>

<style>
  .CompleteColor{
    background: #9bd7fc !important;
  }
</style>
