<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="margin-left: 10px">机型：</span>
        <el-select v-model="engineTypeValue" placeholder="请选择产品型号" clearable size="small" style="width: 150px">
          <el-option
            v-for="item in engineType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <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>
        <el-button type="primary" plain size="small" icon="el-icon-order" @click="searchTable()">查询</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: 100%"
        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="测量项目">
          <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="值下限">
          <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>
          </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="theoreticalValue" style="display: inline-block">
          <el-input
            v-model="form.measuringPosition"
            :rows="2"
            clearable
            size="small"/>
        </el-form-item>
        <el-form-item label="测量项目:" prop="theoreticalValue" style="display: inline-block">
          <el-input
            v-model="form.measuringProject"
            :rows="2"
            clearable
            size="small"/>
        </el-form-item>
        <el-form-item label="理论值:" prop="theoreticalValue" style="display: inline-block">
          <el-input
            v-model="form.theoreticalValue"
            :rows="2"
            clearable
            size="small"/>
        </el-form-item>
        <el-form-item label="值上限:" prop="upLimitValue" style="display: inline-block">
          <el-input
            v-model="form.upLimitValue"
            :rows="2"
            clearable
            size="small"/>
        </el-form-item>
        <el-form-item label="值下限:" prop="lowLimitValue" style="display: inline-block">
          <el-input
            v-model="form.lowLimitValue"
            :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="submitUpdate('form')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      engineTypeValue: '',
      engineType: [],
      tableData: [], // 总数据数组
      stationNumber: [],
      stationNumberValue: '',
      dialogFormVisible: false,
      form: {
        numberCode: '',
        theoreticalValue: '',
        upLimitValue: '',
        lowLimitValue: '',
        measuringPosition: '',
        measuringProject: ''
      },
      rules: {
        theoreticalValue: [{ required: true, message: '请输入', trigger: 'blur' }],
        upLimitValue: [{ required: true, message: '请输入', trigger: 'blur' }],
        lowLimitValue: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getStationNumber()
    this.getProductType()
  },
  methods: {
    // 初始化获取产品型号
    getProductType() {
      this.engineType = []
      var param = []
      var Data = this.CreateData('11', 'MES_基本变量_机型代码_查询', param)
      this.ExecDatabase(Data).then(response => {
        this.engineTypeValue = response.data[0].发动机型号
        this.searchTable()
        for (let i = 0; i < response.data.length; i++) {
          this.engineType.push({
            label: response.data[i].发动机型号,
            value: response.data[i].发动机型号
          })
        }
      })
    },
    getStationNumber() {
      this.stationNumber = []
      var param = []
      var Data = this.CreateData('11', '物料批次扫描切换_工位号_查询', param)
      this.ExecDatabase(Data).then(response => {
        this.stationNumberValue = response.data[0].工位号
        this.searchTable()
        for (let i = 0; i < response.data.length; i++) {
          this.stationNumber.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'
      }
    },
    // 查询
    searchTable() {
      this.tableData = []
      var param = []
      param[0] = ['发动机型号代码', this.engineTypeValue]
      param[1] = ['工位号', this.stationNumberValue]
      var Data = this.CreateData('11', '测量数据_测量范围_查询_简化', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
      })
    },
    callOff() {
      this.dialogFormVisible = false
      this.$refs['form'].resetFields()
    },
    // 编辑消息
    edit(row) {
      console.log(row, '数字')
      this.dialogFormVisible = true
      this.form.theoreticalValue = row.理论值
      this.form.upLimitValue = row.值上限
      this.form.lowLimitValue = row.值下限
      this.form.numberCode = row.数值代码
      this.form.measuringPosition = row.测量位置
      this.form.measuringProject = row.测量项目
    },
    submitUpdate(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['数值代码', this.form.numberCode]
          param[1] = ['理论值', this.form.upLimitValue]
          param[2] = ['值上限', this.form.upLimitValue]
          param[3] = ['值下限', this.form.lowLimitValue]
          console.log(param)
          var Data = this.CreateData('12', '基本数据_测量范围_修改', param)
          this.ExecDatabase(Data).then(response => {
            console.log(response, '修改结果')
            if (response.data[0].result === '1') {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.searchTable()
              this.dialogFormVisible = false
            } else {
              this.$message.error('修改失败！')
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</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>

