<template>

  <div class="app-container">

    <el-card>
      <span style="margin: 5px">零件图号</span>
      <el-input v-model="partNumber" clearable size="small" style="width:200px" />
      <el-button size="small" type="success" icon="el-icon-search" style="margin: 0 0 0 10px" @click="getscantable()">查询</el-button>
      <el-button size="small" type="danger" icon="el-icon-remove-outline" style="margin: 0 0 0 10px" @click="Empty()">清空</el-button>
    </el-card>
    <el-card>
      <el-table
        v-loading="listLoading"
        :data="tableData"
        border
        style="width: 100%;height: 650px" >
        <el-table-column
          label="序号"
          align="center"
          type="index"
          width="50"/>
        <el-table-column label="项目名称" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.项目名称 }}
          </template>
        </el-table-column>
        <el-table-column label="机床图号" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.机床图号 }}
          </template>
        </el-table-column>
        <el-table-column label="零件图号" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.零件图号 }}
          </template>
        </el-table-column>

        <el-table-column label="计划数量" align="center" min-width="100">
          <template slot-scope="scope" align="center">
            <el-input v-model="tableData[scope.$index].plannumber" readonly size="small"/>
          </template>
        </el-table-column>
        <el-table-column label="入库数量" align="center" min-width="100">
          <template slot-scope="scope">
            <el-input v-model="tableData[scope.$index].innumber" clearable size="small"/>
          </template>
        </el-table-column>
        <el-table-column label="仓库" align="center" min-width="100">
          <template slot-scope="scope">
            <el-select v-model="inventoryNumber" filterable clearable size="small" placeholder="仓库名称" @change="Getlocate">
              <el-option
                v-for="item in inventoryName"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="货位" align="center" min-width="120">
          <template slot-scope="scope">
            <el-select v-model="locateNumber" filterable clearable size="small" placeholder="货位名称">
              <el-option
                v-for="item in locateName"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center">
          <template slot-scope="scope" align="center">
            <el-input v-model="tableData[scope.$index].note" clearable size="small"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="150">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" class="el-icon-success" @click="Innumber">入库</el-button>
            <el-button
              :disabled="scope.row.是否禁用"
              size="mini"
              type="danger"
              class="el-icon-delete"
              @click="handleDelete(scope.$index, scope.row)">移除</el-button>
          </template>
        </el-table-column>

      </el-table>

    </el-card>
  </div>
</template>

<script>
import { searchTable, insertOne, getinventory, getlocate } from '@/api/Inventory/Inboundscanning'
import { mapGetters } from 'vuex'
import { wsuri, name } from '@/utils/request'
import print from '@/vendor/print'
export default {
  data() {
    return {
      ace: 0,
      a: '',
      tableData: [],
      partNumber: '',
      listLoading: false,
      craftplannumber: '',
      radio2: 3,
      PeopleNumber: '',
      inventoryName: [], // 仓库名称下拉框
      inventoryNumber: '',
      locateName: [], // 货位名称下拉框
      locateNumber: ''
    }
  },
  computed: {
    ...mapGetters([
      'id'
    ])
  },
  created() {
    this.initWebpack()
    this.getpeopleid()
    this.Getinvent()
  },
  methods: {
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableData.splice(index, 1)
        this.$message({
          message: '已被成功移除',
          type: 'success'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 获取人员编号
    getpeopleid() {
      this.PeopleNumber = this.id
    },
    aaa() {
      print('#print')
    },
    initWebpack() { // 初始化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连接成功')
      this.websock.send('{<' + name + '>}')
    },
    websocketonmessage(msg) { // 数据接收
      this.partNumber = msg.data.split('|')[3]
      this.getscantable()
    },
    websocketclose() { // 关闭
      console.log('WebSocket关闭')
    },
    websocketerror() { // 失败
      console.log('WebSocket连接失败')
    },
    // 扫描查询
    getscantable() {
      if (this.partNumber === '') {
        this.$message.error('请输入零件号')
      } else {
        this.listLoading = true
        searchTable(this.partNumber).then(response => {
          if (response.data.length === 0) {
            this.listLoading = false
            this.$message.warning('无此零件')
          } else if (response.data[0].剩余数量 === 0) {
            this.$message.warning('该零件已入库完毕')
          } else {
            for (let i = 0; i < this.tableData.length; i++) {
              if (response.data[0].工艺计划流水号 === this.tableData[i].工艺计划流水号) {
                this.ace = this.ace + 1
              }
            }
            if (this.ace === 0) {
              this.tableData.push({
                plannumber: response.data[0].剩余数量,
                innumber: response.data[0].入库数量,
                capture: '',
                note: '',
                工艺计划流水号: response.data[0].工艺计划流水号,
                项目名称: response.data[0].项目名称,
                机床图号: response.data[0].机床图号,
                零件图号: response.data[0].零件图号
              })
            } else {
              this.$message.error('此件已在列表中')
            }
          }
        }).then(() => {
          this.listLoading = false
          this.ace = 0
        })
      }
    },
    // 修改入库
    Innumber() {
      this.$confirm('此操作将不可逆转,且如果入库数量大于计划数量，则全部入库,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.locateNumber === '') {
          this.$message.error('请选择货位')
        } else {
          for (let i = 0; i < this.tableData.length; i++) {
            if (this.tableData[i].innumber === '0') {
              this.$message.error('请输入正确数量')
            } else {
              if (this.tableData[i].innumber > this.tableData[i].plannumber) {
                this.tableData[i].innumber = this.tableData[i].plannumber
              }
              insertOne(this.tableData[i].工艺计划流水号, this.tableData[i].innumber, this.PeopleNumber, this.tableData[i].capture, this.inventoryNumber, this.locateNumber, this.tableData[i].note).then(response => {
                this.$message({
                  type: 'success',
                  message: '零件入库成功!'
                })
              }).then(() => {
                this.Empty()
              })
            }
          }
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消入库'
        })
      })
    },
    // 清空
    Empty() {
      this.tableData = []
    },
    // 仓库查询
    Getinvent() {
      this.getSelect(getinventory, ['仓库名称', '仓库流水号'], 'inventoryName')
    },
    // 根据项目获取机床
    Getlocate() {
      this.locateName = []
      this.locateNumber = ''
      if (this.inventoryNumber) {
        this.getSelect(getlocate, ['货位名称', '货位流水号'], 'locateName', this.inventoryNumber)
      }
    }
  }
}
</script>

<style scoped>
  .el-card {
    margin-bottom: 15px;
  }
</style>
