<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="margin-left: 10px">工位号：</span>
        <el-select v-model="stationNumberValue" placeholder="请选择工位号" clearable size="small" style="width: 150px" @change="assignmentStationName()" @clear="clearStationName">
          <el-option
            v-for="item in stationNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <span style="margin-left: 10px">工位名称：</span>
        <el-input
          v-model="stationName"
          :rows="2"
          clearable
          size="small"
          @clear="clearStationName()"/>
        <el-button type="primary" plain size="small" icon="el-icon-order" @click="searchTable()">查询</el-button>
        <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="add()">新增</el-button>
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :cell-class-name="tableRowClassName"
        class="main"
        tooltip-effect="dark"
        style="width: 100%;max-height: 700px"
        border
        size="mini">
        <el-table-column align="center" label="工位号">
          <template slot-scope="scope">
            {{ scope.row.工位号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="线外工位号">
          <template slot-scope="scope">
            {{ scope.row.线外工位号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否禁用">
          <template slot-scope="scope">
            {{ scope.row.是否禁用 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" size="small" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" size="small" @click="deleteOutLine(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[15, 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" title="线外工位维护" center style="min-height: 200px;" width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="110px">
        <el-form-item label="工位号:" prop="addStationNumberValue" style="display: inline-block">
          <el-select v-model="form.addStationNumberValue" placeholder="请选择工位号" clearable size="small">
            <el-option
              v-for="item in stationNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              clearable
              size="mini"/>
          </el-select>
        </el-form-item>
        <el-form-item label="线外工位:" prop="outLineOP" style="width: 150px;display: inline-block">
          <el-input
            v-model="form.outLineOP"
            :rows="2"
            clearable
            size="small"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff('form')">取消</el-button>
        <el-button type="primary" size="small" @click="submitAdd('form')">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogFormVisible1" title="线外工位编辑" center style="min-height: 200px;" width="400px">
      <el-form ref="editForm" :model="editForm" :rules="editRules" label-position="right" label-width="110px">
        <el-form-item label="工位号:" prop="addStationNumberValue" style="display: inline-block">
          <el-select v-model="editForm.editStationNumberValue" placeholder="请选择工位号" clearable size="small">
            <el-option
              v-for="item in stationNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              clearable
              size="mini"/>
          </el-select>
        </el-form-item>
        <el-form-item label="线外工位:" prop="outLineOP" style="display: inline-block">
          <el-input
            v-model="editForm.outLineOP"
            :rows="2"
            clearable
            size="small"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff('form')">取消</el-button>
        <el-button type="primary" size="small" @click="submitEdit('editForm')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      stationNumber_check: '',
      stationName: '',
      tableData: [], // 总数据数组
      stationNumber: [],
      stationNumberValue: '',
      dialogFormVisible: false,
      dialogFormVisible1: false,
      form: {
        addStationNumberValue: '',
        outLineOP: ''
      },
      editForm: {
        id: '',
        editStationNumberValue: '',
        outLineOP: ''
      },
      rules: {
        addStationNumberValue: [{ required: true, message: '请先择工位', trigger: 'blur' }],
        outLineOP: [{ required: true, message: '请输入线外工位号', trigger: 'blur' }]
      },
      editRules: {
        editStationNumberValue: [{ required: true, message: '请先择工位', trigger: 'blur' }],
        outLineOP: [{ required: true, message: '请输入线外工位号', trigger: 'blur' }]
      },
      total: 0, // 总条数
      pageList: 15, // 每页显示条数
      pageSize: 15, // 每页显示条数
      pageCurrent: 1 // 后台获取页
    }
  },
  created() {
    this.searchTable()
    this.getStationNumber()
  },
  methods: {
    clearStationName() {
      this.stationName = ''
      this.searchTable()
    },
    assignmentStationName() {
      if (this.stationNumberValue !== '') {
        for (let i = 0; i < this.stationNumberValue.length; i++) {
          if (this.stationNumber[i].label === this.stationNumberValue) {
            this.stationName = this.stationNumber[i].name
            this.searchTable()
          }
        }
      }
    },
    getStationNumber() {
      this.stationNumber = []
      var param = []
      var Data = this.CreateData('11', '物料批次扫描切换_工位号_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.stationNumber.push({
            label: response.data[i].工位号,
            value: response.data[i].工位号,
            name: response.data[i].工位名称
          })
        }
      })
    },
    // 表格颜色变化
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 0) {
        return 'black'
      } else {
        return 'red'
      }
    },
    // 查询
    searchTable() {
      this.tableData = []
      this.stationNumberValue ? this.stationNumber_check = 1 : this.stationNumber_check = 0
      var param = []
      param[0] = ['工位号', this.stationNumberValue]
      param[1] = ['工位号_ischeck', this.stationNumber_check]
      param[2] = ['PageCurrent', this.pageCurrent]
      param[3] = ['PageSize', this.pageSize]
      param[4] = ['PageCount', '1111', 'int', '1']
      param[5] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', '线外工位验证_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.result.length !== 0) {
          this.tableData = response.data.result
        }
        this.total = parseInt(response.data.output[0].ItemCount)
      })
    },
    add() {
      this.dialogFormVisible = true
    },
    callOff() {
      this.dialogFormVisible = false
      this.dialogFormVisible1 = false
      this.$refs['form'].resetFields()
      this.$refs['editForm'].resetFields()
    },
    // 编辑消息
    edit(row) {
      this.dialogFormVisible1 = true
      this.editForm.id = row.ID
      this.editForm.editStationNumberValue = row.工位号
      this.editForm.outLineOP = row.线外工位号
    },
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['工位号', this.form.addStationNumberValue]
          param[1] = ['线外工位号', this.form.outLineOP]
          console.log(param)
          var Data = this.CreateData('12', '线外工位验证_增加', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.searchTable()
              this.dialogFormVisible = false
            } else {
              this.$message.error('新增失败！')
            }
          })
        } else {
          return false
        }
      })
    },
    submitEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['ID', this.editForm.id]
          param[1] = ['工位号', this.editForm.editStationNumberValue]
          param[2] = ['线外工位号', this.editForm.outLineOP]
          console.log(param)
          var Data = this.CreateData('12', '线外工位验证_修改', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.searchTable()
              this.dialogFormVisible1 = false
            } else {
              this.$message.error('修改失败！')
            }
          })
        } else {
          return false
        }
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    },
    // 删除
    deleteOutLine(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['ID', row.ID]
        var Data = this.CreateData('12', '线外工位验证_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '订单删除成功',
              type: 'success'
            })
            this.pageCurrent = 1
            this.pageSize = 15
            this.searchTable()
          } else {
            this.$message.error('订单删除失败！')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>
  .topCard{
    margin: 5px;
  }
  .topDiv{
    margin-bottom: 10px;
  }
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  .text {
    font-size: 14px;
  }

  .item {
    padding: 18px 0;
  }

  .box-card {
    position: absolute;
    right: 6px;
    top: 80px;
    width: 300px;
  }
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
</style>

