<template>
  <div class="app-container">
    <el-card style="height: 850px">
      <el-table :data="tableData" border size="small" stripe highlight-current-row max-height="740px" style="width: 450px">
        <el-table-column type="index" align="center" label="序号" width="70px"/>
        <el-table-column label="不合格原因" align="center" width="250px" >
          <template slot-scope="scope">
            {{ scope.row.不合格原因文本 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="mini" icon="el-icon-edit" style="margin-right:10px" @click="handleEdit(scope.row)"/>
            <el-button type="text" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
      <el-tooltip effect="dark" content="添加新设备" placement="top">
        <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="ADD()"/>
      </el-tooltip>
    </el-card>
    <el-dialog v-el-drag-dialog :title="title" :visible.sync="dialogFormVisible" center style="min-height: 300px" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="110px" class="demo-ruleForm">
        <el-row>
          <el-form-item label="不合格原因" prop="不合格原因" >
            <el-input v-model="form.不合格原因" type="textarea" placeholder="不合格原因" size="small" style="width:300px" clearable />
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOff('form')">取消</el-button>
        <el-button v-show="title==='新增'" type="primary" @click="submitAdd('form')">确定</el-button>
        <el-button v-show="title==='编辑'" type="primary" @click="submitEdit('form')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      routineCheckType: [],
      dialogFormVisible: false,
      title: '',
      equipmentValue: '',
      equipmentName: '',
      opName: '',
      process: [],
      form: {
        不合格原因: ''
      },
      rules: {
        不合格原因: [{ required: true, message: '不合格原因' }]
      },
      original: '',
      total: 0, // 分页总数
      tableData: [], // 设备信息表
      pageCurrent: 1, // 分页当前页
      pageSize: 20 // 分页每页显示条数
    }
  },
  computed: {
    ...mapGetters([
      'id',
      'token'
    ])
  },
  created() {
    this.searchTable()
  },
  methods: {
    searchTable() {
      var param = []
      var Data = this.CreateData('11', '基础数据_不合格原因_查询', param)
      this.ExecDatabase(Data).then(response => {
        this.tableData = response.data
      })
    },
    ADD() {
      this.title = '新增'
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.form.不合格原因 = ''
      this.dialogFormVisible = true
    },
    handleEdit(row) {
      this.title = '编辑'
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.equipmentValue = row.不合格原因
      this.form.不合格原因 = row.不合格原因文本
      this.dialogFormVisible = true
    },
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['不合格原因', this.form.不合格原因]
          var Data = this.CreateData('12', '基础数据_不合格原因_增加', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('添加成功')
              this.searchTable()
              this.dialogFormVisible = false
            } else {
              this.$message.error('添加失败')
            }
          })
        }
      })
    },
    submitEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['id', this.equipmentValue]
          param[1] = ['不合格原因', this.form.不合格原因]
          var Data = this.CreateData('12', '基础数据_不合格原因_修改', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('编辑成功')
              this.searchTable()
              this.dialogFormVisible = false
            } else {
              this.$message.error('编辑失败')
            }
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['id', row.不合格原因]
        var Data = this.CreateData('12', '基础数据_不合格原因_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.pageCurrent = 1
            this.searchTable()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSizeChanges(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChanges(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

