<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <el-button type="primary" plain size="small" icon="el-icon-refresh" @click="searchData">刷新数据</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        element-loading-text="数据加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.2)"
        highlight-current-row
        tooltip-effect="dark"
        height="790px"
        style="width: 100%;"
        border
        size="mini">
        <el-table-column type="index" width="60" label="序号" align="center"/>
        <el-table-column align="center" label="发动机型号" prop="发动机型号"/>
        <el-table-column align="center" label="发动机机型标志" prop="发动机机型标志"/>
        <el-table-column align="center" label="模组识别码" prop="模组识别码"/>
        <el-table-column align="center" label="PACK识别码" prop="PACK识别码"/>
        <el-table-column align="center" label="是否禁用" width="100">
          <template slot-scope="scope">
            <el-tag v-if="Number(scope.row.是否禁用) === 1" size="mini" type="danger">是</el-tag>
            <el-tag v-else size="mini" type="success">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog :visible.sync="dialogVisible" title="修改机型代码" center width="450px">
      <el-form ref="editForm" :model="editForm" :rules="rules" label-position="right" label-width="110px">
        <el-form-item label="发动机型号">
          <el-input v-model="editForm.发动机型号" readonly size="small" style="width: 250px"/>
        </el-form-item>
        <el-form-item label="发动机机型标志">
          <el-input v-model="editForm.发动机机型标志" readonly size="small" style="width: 250px"/>
        </el-form-item>
        <el-form-item label="模组识别码" prop="模组识别码">
          <el-input v-model="editForm.模组识别码" clearable size="small" style="width: 250px" placeholder="请输入模组识别码"/>
        </el-form-item>
        <el-form-item label="PACK识别码" prop="PACK识别码">
          <el-input v-model="editForm.PACK识别码" clearable size="small" style="width: 250px" placeholder="请输入PACK识别码"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" size="small" @click="submitEdit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      tableData: [],
      dialogVisible: false,
      editForm: {
        序号: '',
        发动机型号: '',
        发动机机型标志: '',
        模组识别码: '',
        PACK识别码: ''
      },
      rules: {
        模组识别码: [{ required: true, message: '请输入模组识别码', trigger: 'blur' }],
        PACK识别码: [{ required: true, message: '请输入PACK识别码', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.searchData()
  },
  methods: {
    // 查询数据
    searchData() {
      this.loading = true
      this.tableData = []
      const param = []
      const Data = this.CreateData('11', 'MES_基本变量_机型代码_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data && response.data.length > 0) {
          this.tableData = response.data
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 打开编辑对话框
    handleEdit(row) {
      if (this.$refs.editForm) {
        this.$refs.editForm.resetFields()
      }
      this.editForm.序号 = row.序号
      this.editForm.发动机型号 = row.发动机型号
      this.editForm.发动机机型标志 = row.发动机机型标志
      this.editForm.模组识别码 = row.模组识别码 || ''
      this.editForm.PACK识别码 = row.PACK识别码 || ''
      this.dialogVisible = true
    },
    // 提交修改
    submitEdit() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          const param = []
          param[0] = ['序号', this.editForm.序号]
          param[1] = ['模组识别码', this.editForm.模组识别码]
          param[2] = ['PACK识别码', this.editForm.PACK识别码]
          const Data = this.CreateData('12', 'MES_基本变量_机型代码_修改', param)
          this.ExecDatabase(Data).then(response => {
            this.$message.success('修改成功')
            this.dialogVisible = false
            this.searchData()
          }).catch(() => {
            this.$message.error('修改失败')
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.topDiv {
  margin-bottom: 10px;
}
</style>
