<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">
          <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-select v-model="moduleDescriptionValue" placeholder="请选择模块描述" clearable size="small" style="width: 150px">
          <el-option
            v-for="item in moduleDescription"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <el-button type="primary" plain size="small" icon="el-icon-order" @click="searchTable()">查询</el-button>
        <el-button type="distribution" icon="el-icon-circle-plus-outline" size="small" @click="addTo">新增</el-button>
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :cell-class-name="tableRowClassName"
        height="770px"
        class="main"
        tooltip-effect="dark"
        style="width: 60%"
        border
        size="mini">
        <el-table-column type="index" width="60" label="序号" align="center"/>
        <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="80px">
          <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="editInformation(scope.row)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" size="small" @click="deleteInformation(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :visible.sync="dialogFormVisible" :title="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="stationNumber" style="display: inline-block">
          <el-select v-model="form.stationNumber" placeholder="请选择工位号" clearable size="small" style="width: 200px">
            <el-option
              v-for="item in stationNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
        </el-form-item>
        <el-form-item label="标签ID:" prop="moduleDescription" style="display: inline-block">
          <el-select v-model="form.moduleDescription" placeholder="请选择模块描述" clearable size="small" style="width: 200px">
            <el-option
              v-for="item in moduleDescription"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
        </el-form-item>
        <el-form-item label="是否禁用:" prop="isDisable" style="display: inline-block">
          <el-select v-model="form.isDisable" placeholder="请选择是否禁用" size="small" style="width: 200px">
            <el-option
              v-for="item in isDisable"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff('form')">取消</el-button>
        <el-button v-if="title === '编辑'" type="primary" size="small" @click="submitUpdate('form')">确定</el-button>
        <el-button v-if="title === '添加'" type="primary" size="small" @click="submitAdd('form')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [], // 总数据数组
      stationNumber: [],
      stationNumberValue: '',
      moduleDescription: [],
      moduleDescriptionValue: '',
      tagIDArr: [
        {
          label: 'lbl_datagrid_quality_name',
          value: 'lbl_datagrid_quality_name'
        }, {
          label: 'lbl_datagrid_quality_item1',
          value: 'lbl_datagrid_quality_item1'
        }, {
          label: 'lbl_datagrid_quality_item2',
          value: 'lbl_datagrid_quality_item2'
        }
      ],
      isDisable: [
        {
          label: '是',
          value: 1
        }, {
          label: '否',
          value: 0
        }
      ],
      isDisableValue: 0,
      labelText: '',
      labelNumber: '',
      dialogFormVisible: false,
      title: '',
      form: {
        stationNumber: '',
        moduleDescription: '',
        isDisable: 0
      },
      rules: {
        stationNumber: [{ required: true, message: '请选择工位号', trigger: 'change' }],
        moduleDescription: [{ required: true, message: '请选择模块', trigger: 'change' }],
        isDisable: [{ required: true, message: '请选择是否禁用', trigger: 'change' }]
      }
    }
  },
  created() {
    this.searchTable1()
    this.getStationNumber()
    this.getModuleDescription()
  },
  methods: {
    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].工位号
          })
        }
      })
    },
    getModuleDescription() {
      this.moduleDescription = []
      var param = []
      var Data = this.CreateData('11', 'MES界面显示_模块列表_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.moduleDescription.push({
            label: response.data[i].模块描述,
            value: response.data[i].模块编号
          })
        }
      })
    },
    // 表格颜色变化
    tableRowClassName({ row, rowIndex }) {
      // console.log(row, rowIndex)
      if (rowIndex % 2 === 0) {
        return 'black'
      } else {
        return 'red'
      }
    },
    // 查询MES消息
    searchTable() {
      this.tableData = []
      // let stationNumberValue_check
      // this.stationNumberValue ? stationNumberValue_check = true : (stationNumberValue_check = false, this.stationNumberValue = '')
      var param = []
      param[0] = ['模块编号', this.moduleDescriptionValue]
      param[1] = ['工位号', this.stationNumberValue]
      // param[2] = ['工位号', this.stationNumberValue]
      // param[3] = ['check工位号', stationNumberValue_check]
      var Data = this.CreateData('11', '工艺管理_界面模块维护_信息查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
      })
    },
    // 查询MES消息
    searchTable1() {
      this.tableData = []
      var param = []
      var Data = this.CreateData('11', '工艺管理_界面模块维护_初始化查询', param)
      this.ExecDatabase(Data).then(response => {
        console.log(response, 66)
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
      })
    },
    addTo() {
      this.title = '添加'
      this.dialogFormVisible = true
    },
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['工位号', this.form.stationNumber]
          param[1] = ['模块编号', this.form.moduleDescription]
          param[2] = ['是否禁用', this.form.isDisable]
          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.dialogFormVisible = false
              this.searchTable()
            } else {
              this.$message.error('增加失败！')
            }
          })
        } else {
          return false
        }
      })
    },
    callOff() {
      this.dialogFormVisible = false
      this.$refs['form'].resetFields()
      this.form.stationNumber = ''
      this.form.tagID = ''
      this.form.tagText = ''
      this.form.isDisable = 0
      this.title = ''
    },
    editInformation(row) {
      this.labelNumber = row.标签编号
      this.form.stationNumber = row.工位号
      this.form.moduleDescription = row.模块编号
      if (row.是否禁用 === '是') {
        this.form.isDisable = 1
      } else {
        this.form.isDisable = 0
      }
      this.title = '编辑'
      this.dialogFormVisible = true
    },
    // 编辑消息
    submitUpdate(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['工位号', this.form.stationNumber]
          param[1] = ['模块编号', this.form.moduleDescription]
          param[2] = ['是否禁用', this.form.isDisable]
          param[3] = ['标签编号', this.labelNumber]
          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.dialogFormVisible = false
              this.searchTable()
            } else {
              this.$message.error('修改失败！')
            }
          })
        } else {
          return false
        }
      })
    },
    deleteInformation(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['标签编号', row.标签编号]
        var Data = this.CreateData('12', '工艺管理_界面模块维护_信息删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '订单删除成功',
              type: 'success'
            })
            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>

