<template>
  <div class='m-container'>
    <!-- <h2>错误类型管理</h2> -->
    <div class="m-core">
      <div class='newBand' v-permission="'hotWordLabelManage/add'"
           @click="openTypes('create')">添加</div>
      <!-- 列表 -->
      <div class="tableWrap">
        <el-table :data='tableList'
                  :header-cell-style="{background:'#EDEEF0',color:'#444'}">
          <el-table-column align='center'
                           prop="id"
                           label="编号">
          </el-table-column>
          <el-table-column align='center'
                           prop="name"
                           label="热词标签">
          </el-table-column>
          <el-table-column align='center'
                           label="操作">
            <template slot-scope="scope">
              <el-button @click="openTypes('update', scope.row)"
                         v-permission="'hotWordLabelManage/update'"
                         type="text">修改</el-button>
              <el-button @click="onTypesDel(scope.row)"
                         v-permission="'hotWordLabelManage/del'"
                         type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
        :saveKey="'errorManager'"
        sync
        :total.sync="pageInfo.total"
                    :page.sync="pageInfo.page"
                    :limit.sync="pageInfo.limit"
                    :page-sizes="pageInfo.page_size"
                    @pagination="getList" />
      </div>
    </div>
    <el-dialog :title="formTitle[formActionTitle]"
               :visible.sync="typeStatus"
               @close="onFormClose('ruleForm')"
               width="500px">
      <el-form :model="form"
               :rules="rules"
               ref="ruleForm">
        <el-form-item label="热词内容"
                      prop="name"
                      :label-width="formLabelWidth">
          <el-input v-model.trim="form.name"
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="typeStatus = false">取 消</el-button>
        <el-button type="primary"
                   @click="onTypesSubmit('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import pagination from '@/components/Pagination/index'
import { hotWordLabelManage, hotWordLabelManageAdd, hotWordLabelManageUpdate, hotWordLabelManageDel } from '@/api/systemManage'
export default {
  components: { pagination },
  name: 'errormanager',
  data () {
    return {
      tableList: [],
      subjectList: [], // 声部列表
      formActionTitle: 'create',
      formTitle: {
        create: '添加热词标签',
        update: '修改热词标签'
      },
      typeStatus: false, // 添加教学点
      formLabelWidth: '120px',
      form: {
        name: null, // 热词标签
      },
      rules: {
        name: [{ required: true, message: '请输入热词标签', trigger: 'blur' }]
      },
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      }
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    onTypesSubmit (formName) { // 添加数据
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formActionTitle == 'create') {
            if (this.form.id) { // 判断有没有Id，如果有则删除
              delete this.form.id
            }
            hotWordLabelManageAdd(this.form).then(res => {
              this.messageTips('添加', res)
            })
          } else if (this.formActionTitle == 'update') {
            hotWordLabelManageUpdate(this.form).then(res => {
              this.messageTips('修改', res)
            })
          }
        } else {
          return false;
        }
      })
    },
    onTypesDel (row) {
      this.$confirm("是否确认删除热词标签", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      .then(() => {
        hotWordLabelManageDel(row.id).then(res => {
          this.messageTips('删除', res)
        })
      })
    },
    messageTips (title, res) {
      if (res.code == 200) {
        this.$message.success(title + '成功')
        this.typeStatus = false
        this.getList()
      } else {
        this.$message.error(res.msg)
      }
    },
    getList () {
      hotWordLabelManage({
        rows: this.pageInfo.limit,
        page: this.pageInfo.page
      }).then(res => {
        let result = res.data
        if (res.code == 200) {
          this.tableList = result.rows
          this.pageInfo.total = result.total
        }
      })
    },
    openTypes (type, row) {
      this.typeStatus = true
      this.formActionTitle = type
      // 修改的时候赋值
      if (type == 'update') {
        this.form = {
          id: row.id,
          name: row.name
        }
      }
    },
    onFormClose (formName) { // 关闭弹窗重置验证
      this.form = {
        name: null, // 训练模块名称
        subjectIds: []
      }
      this.$refs[formName].resetFields()
    },

  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-date-editor.el-input {
  width: 100% !important;
}
::v-deep .el-select {
  width: 98% !important;
}
</style>
