<template>
  <div>
    <div class="menu-title">
      反馈类型管理
    </div>
    <div class="menu-content">
      <div>
        <el-form :inline="true" :model="qo" class="demo-form-inline">
          <el-form-item>
            <el-input v-model="qo.LIKES_name" placeholder="请输入名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="qo.pageNo=1;queryData()">搜 索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div style="margin: 0px 0 20px 0">
        <el-button type="primary" @click="createRow()">添加</el-button>
      </div>
      <div>
        <el-table :data="result.records" style="width: 100%">
          <el-table-column type="index" label="行号" width="60"></el-table-column>
          <el-table-column prop="name" label="名称"></el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click.native.prevent="modifyRow(scope.row)" type="text" size="small">
                编辑
              </el-button>
              <el-button @click.native.prevent="removeRow(scope.row)" type="text" size="small">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page-box">
        <el-pagination background
                       @current-change="handleCurrentChange"
                       :current-page="qo.pageNo"
                       :page-size="qo.pageSize"
                       layout="total, prev, pager, next"
                       :total="result.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog
        :title="dialogName"
        :visible.sync="cmdDialogVisible"
        width="40%">
      <el-form ref="cmd" label-width="80px" :rules="rules" :model="cmd">
        <el-form-item label="名称" prop="name">
          <el-input v-model="cmd.name" placeholder="请输入名称"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cmdDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitModify">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request';

var _this;
export default {
  name: "feedbackType",
  data() {
    return {
      qo: {
        pageNo: 1,
        pageSize: 10,
        LIKES_name: '',
      },
      result: {
        records: [],
        total: 0
      },
      dialogName: '编辑',
      cmdDialogVisible: false,
      cmd: {},
      rules: {
        name: [
          {required: true, message: '请输入名称'}
        ],
      },
    }
  },
  mounted() {
    _this = this;
    _this.queryData();
  },
  methods: {
    queryData() {
      request({
        url: '/feedback-type/query_pages',
        method: 'post',
        data: _this.qo
      }).then(res => {
        _this.result.records = res.data.records;
        _this.result.total = res.data.total;
      });
    },
    handleCurrentChange(val) {
      _this.qo.pageNo = val;
      _this.queryData();
    },
    removeRow(item) {
      _this.$confirm('此操作将永久删除记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        request({
          url: '/feedback-type/remove/' + item.id,
          method: 'post'
        }).then(res => {
          _this.$message.success("删除成功");
          _this.queryData();
        });
      }).catch(() => {
      });
    },
    createRow() {
      _this.dialogName = '新建';
      _this.cmdDialogVisible = true;
      _this.$nextTick(_=> {
        _this.$refs['cmd'].clearValidate();
        _this.cmd = {
          name: '',
        };
      })
    },
    modifyRow(item) {
      _this.dialogName = '编辑';
      _this.cmdDialogVisible = true;
      _this.$nextTick(_=> {
        _this.$refs['cmd'].clearValidate();
        _this.cmd = JSON.parse(JSON.stringify(item));
      })
    },
    submitModify() {
      this.$refs.cmd.validate((valid) => {
        if (valid) {
          if (_this.cmd.id) {
            request({
              url: '/feedback-type/modify',
              method: 'post',
              data: _this.cmd
            }).then(res => {
              _this.$message.success("编辑成功");
              _this.queryData();
              _this.cmdDialogVisible = false;
            });
          } else { // 新建
            request({
              url: '/feedback-type/create',
              method: 'post',
              data: _this.cmd
            }).then(res => {
              _this.$message.success("添加成功");
              _this.cmdDialogVisible = false;
            });
          }
        }
      });
    }
  }
}
</script>

<style lang="scss">
.input-new-tag {
  margin-bottom: 12px;
  height: 32px;
  width: 80px;

  input {
    height: 32px !important;
    width: 80px;
  }
}

.el-tag {
  margin-right: 10px;
  margin-bottom: 10px;
  height: 32px;
}

.button-new-tag {
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;

}
</style>
