<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="margin-left: 10px">工位号：</span>
        <el-select v-model="stationNumberValue" placeholder="请选择工位号" size="small" clearable style="width: 150px" @change="searchTable">
          <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" readonly style="width:170px;" placeholder="" size="small"/>
        <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="add()">新增</el-button>
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :cell-class-name="tableRowClassName"
        height="680px"
        class="main"
        tooltip-effect="dark"
        style="width: 70%"
        border
        size="mini">
        <el-table-column type="index" width="60" label="序号" align="center"/>
        <el-table-column align="center" label="工位号" width="100">
          <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="120">
          <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="120">
          <template slot-scope="scope">
            {{ scope.row.检测设备 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" size="small" @click="edit(scope.row)"/>
            <el-button type="text" icon="el-icon-delete" size="small" style="color: #ff0000" @click="deleteOrder(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
    </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="stationNumberValue" style="display: inline-block">
          <el-select v-model="form.stationNumberValue" placeholder="请选择工位号" size="small">
            <el-option
              v-for="item in stationNumber"
              :key="item.label"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
        </el-form-item>
        <el-form-item label="测试方法" prop="testMethod" style="display: inline-block">
          <el-input v-model="form.testMethod" size="small" placeholder="请输入测试方法"/>
        </el-form-item>
        <el-form-item label="检测要求" prop="testContent" style="display: inline-block">
          <el-input v-model="form.testContent" size="small" placeholder="请输入检测要求"/>
        </el-form-item>
        <el-form-item label="工具/设备" prop="testTool" style="display: inline-block">
          <el-input v-model="form.testTool" size="small" placeholder="请输入工具/设备"/>
        </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="addRhythm('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="stationNumberValue" style="display: inline-block">
          <el-input v-model="editForm.stationNumberValue" readonly size="small" placeholder=""/>
        </el-form-item>
        <el-form-item label="测试方法" prop="testMethod" style="display: inline-block">
          <el-input v-model="editForm.testMethod" size="small" placeholder="请输入测试方法"/>
        </el-form-item>
        <el-form-item label="检测要求" prop="testContent" style="display: inline-block">
          <el-input v-model="editForm.testContent" size="small" placeholder="请输入检测要求"/>
        </el-form-item>
        <el-form-item label="工具/设备" prop="testTool" style="display: inline-block">
          <el-input v-model="editForm.testTool" size="small" placeholder="请输入工具/设备"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff('editForm')">取消</el-button>
        <el-button type="primary" size="small" @click="submitEdit('editForm')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rhythm: '',
      engineTypeValue: '',
      engineType: [],
      tableData: [], // 总数据数组
      stationNumber: [],
      stationNumberValue: '',
      stationName: '',
      dialogFormVisible: false,
      dialogFormVisible1: false,
      form: {
        testMethod: '',
        stationNumberValue: '',
        opCodeName: '',
        testTool: '',
        testContent: ''
      },
      editForm: {
        id: '',
        stationNumberValue: '',
        opCodeName: '',
        testMethod: '',
        testTool: '',
        testContent: ''
      },
      rules: {
        stationNumberValue: [{ required: true, message: '请选择工位', trigger: 'change' }],
        testMethod: [{ required: true, message: '请输入测试方法', trigger: 'blur' }],
        testTool: [{ required: true, message: '请输入工具/设备', trigger: 'blur' }],
        testContent: [{ required: true, message: '请输入检测要求', trigger: 'blur' }]
      },
      editRules: {
        testMethod: [{ required: true, message: '请输入测试方法', trigger: 'blur' }],
        testTool: [{ required: true, message: '请输入工具/设备', trigger: 'blur' }],
        testContent: [{ required: true, message: '请输入检测要求', trigger: 'blur' }]
      }
    }
  },
  watch: {
    stationNumberValue() {
      for (let i = 0; i < this.stationNumber.length; i++) {
        if (this.stationNumber[i].value === this.stationNumberValue) {
          this.stationName = this.stationNumber[i].labelName
        }
      }
      if (!this.stationNumberValue) {
        this.stationName = ''
      }
    }
  },
  created() {
    this.getStationNumber()
    this.searchTable()
  },
  methods: {
    // 初始化获取订单号
    getStationNumber() {
      this.stationNumber = []
      var param = []
      var Data = this.CreateData('11', 'MES_计划BOM_工位与名称_查询', 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].工位号,
            labelName: response.data[i].工位名称
          })
        }
      })
    },
    // 初始化获取产品型号
    getProductType() {
      this.engineType = []
      var param = []
      var Data = this.CreateData('11', 'CATL_基础信息_机型信息_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length > 0) this.engineTypeValue = response.data[0].机型ID
        for (let i = 0; i < response.data.length; i++) {
          this.engineType.push({
            label: response.data[i].机型号,
            value: response.data[i].机型ID,
            labelName: response.data[i].机型号
          })
        }
      })
    },
    // 表格颜色变化
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 0) {
        return 'black'
      } else {
        return 'red'
      }
    },
    // 查询信息
    searchTable() {
      this.tableData = []
      var param = []
      param[0] = ['工位号', this.stationNumberValue]
      var Data = this.CreateData('11', 'CATL_基础_合格证项目_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
      })
    },
    add() {
      this.dialogFormVisible = true
    },
    callOff() {
      this.dialogFormVisible = false
      this.dialogFormVisible1 = false
      this.$refs['form'].resetFields()
      this.$refs['editForm'].resetFields()
    },
    // 新增节拍
    addRhythm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['工位号', this.form.stationNumberValue]
          param[1] = ['测试方法', this.form.testMethod]
          param[2] = ['检测要求', this.form.testContent]
          param[3] = ['检测设备', this.form.testTool]
          var Data = this.CreateData('12', 'CATL_基础_合格证项目_增加', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.searchTable()
            } else {
              this.$message.error('新增失败！')
            }
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    // 编辑
    edit(row) {
      this.dialogFormVisible1 = true
      this.editForm.id = row.ID
      this.editForm.stationNumberValue = row.工位号
      this.editForm.testMethod = row.测试方法
      this.editForm.testTool = row.检测设备
      this.editForm.testContent = row.检测要求
    },
    //  提交编辑
    submitEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['ID', this.editForm.id]
          param[1] = ['测试方法', this.editForm.testMethod]
          param[2] = ['检测要求', this.editForm.testContent]
          param[3] = ['检测设备', this.editForm.testTool]
          var Data = this.CreateData('12', 'CATL_基础_合格证项目_编辑', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '编辑成功',
                type: 'success'
              })
              this.searchTable()
            } else {
              this.$message.error('编辑失败！')
            }
          })
          this.dialogFormVisible1 = false
        } else {
          return false
        }
      })
    },
    // 删除订单
    deleteOrder(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['ID', row.ID]
        var Data = this.CreateData('12', 'CATL_基础_合格证项目_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message.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>

