<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-search" size="small" style="margin: 10px 0 0 10px" @click="searchTable();pageCurrent=1;pageSize=10;total = 0">查询</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>
        <el-table-column
          label=" "
          align="center"
          type="index"
          width="50"/>
        <el-table-column label="零件名称" align="center" width="310px">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="零件图号" align="center" width="330px">
          <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" fixed="right">
          <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, Order, JurisdictionControl } from '@/api/ManufacturingCenter/MachiningSequenceAdjustment'
export default {
  data() {
    return {
      num: 0,
      count: 0,
      dialogFormVisible: false,
      gridData: [],
      Machine: [],
      MachineValue: '',
      loading0: false,
      tableData: [], // 表格数据
      pageSize: 10,
      pageCurrent: 1,
      total: 0
    }
  },
  watch: {
    MachineValue(val) {
      this.searchTable()
    }
  },
  created() {
    this.Jurisdiction()
    this.fetchData()
  },
  methods: {
    // 初始化获取工位
    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
        }).then(() => {
          this.loading0 = false
        })
      } else {
        this.$message.error('请选择工位')
      }
    },
    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) {
      JurisdictionControl(row.MES终端号, 1).then(response => {
        this.Jurisdiction()
      })
    },
    JurisdictionControl2(row) {
      JurisdictionControl(row.MES终端号, 2).then(response => {
        this.Jurisdiction()
      })
    },
    // 上移
    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>
  .el-card{
    margin-bottom: 15px;
  }
  .el-input{
    width: 210px;
  }
  .item {
    float: right;
    margin-top: 10px;
    margin-right: 40px;
  }
</style>
