<template>
  <div class="app-container">
    <div class="mymain-container">
      <div class="btn-group clearfix">
        <div class="fl">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            @click="addOrEdit('add')"
            >新增标签</el-button
          >
        </div>
        <div class="fr"></div>
      </div>

      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="dataList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
        >
          <el-table-column align="center" label="标签名称" prop="tag">
            <template slot-scope="scope">
              <el-tag>{{ scope.row.tag }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="创建时间"
            prop="createTime"
          ></el-table-column>
          <el-table-column
            align="center"
            label="商户"
            prop="companyName"
          ></el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrEdit('edit', scope.row)"
                >编辑</el-button
              >
              <el-popconfirm
                style="margin-left: 10px"
                title="确定删除吗？"
                @confirm="handleDelete(scope.row.commentTagId)"
                v-if="scope.row.adminRoleId != 1"
              >
                <el-button slot="reference" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination clearfix">
        <div class="fr">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="listTotal"
          >
          </el-pagination>
        </div>
      </div>
    </div>

    <!-- 新增编辑 -->
    <el-dialog
      :title="addFormType == 'add' ? '新增标签' : '编辑标签'"
      :visible.sync="addFormVisible"
      :show-close="false"
      width="40%"
      :close-on-click-modal="false"
    >
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="addFormRules"
        label-position="left"
        label-width="80px"
      >
        <el-form-item label="标签名称" prop="name">
          <el-input
            v-model="addForm.name"
            autocomplete="off"
            placeholder="请输入标签名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAddForm">取 消</el-button>
        <el-button type="primary" @click="submitAddForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getTagList, addTag, editTag, deleteTag } from "@/api/tag/evaluate_tag";

export default {
  data() {
    return {
      baseURL: process.env.VUE_APP_BASE_API,
      dataList: null, // 列表数据
      listLoading: true, // 列表加载loading
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数

      editItem: null,
      addFormType: "add",
      addFormVisible: false,
      addForm: {
        name: "",
      },
      addFormRules: {
        name: [{ required: true, message: "请输入标签名称", trigger: "blur" }],
      },
    };
  },
  computed: {
    ...mapGetters(["userid", "name"]),
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;

      let params = {
        pageNo: this.currentPage,
        pageSize: this.pageSize,
      };

      getTagList(params).then((res) => {
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
      });
    },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getList();
    },

    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },

    // 操作 - 删除
    handleDelete(id) {
      deleteTag({ commentTagId: id }).then((res) => {
        this.getList();
        this.$successMsg();
      });
    },

    // 新增编辑
    addOrEdit(type, item) {
      this.addFormType = type;
      this.addFormVisible = true;
      if (type == "edit") {
        this.editItem = item;
        this.addForm.name = this.editItem.tag;
      }
    },

    // 取消 新增编辑
    cancelAddForm() {
      this.addFormVisible = false;
      this.$refs.addForm.resetFields();
    },

    // 提交 新增编辑
    submitAddForm() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          let params = {
            tag: this.addForm.name,
          };
          if (this.addFormType == "edit") {
            params.commentTagId = this.editItem.commentTagId;
            editTag(params).then((res) => {
              this.cancelAddForm();
              this.getList();
              this.$successMsg("编辑成功");
            });
          } else {
            addTag(params).then((res) => {
              this.cancelAddForm();
              this.getList();
              this.$successMsg("新增成功");
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss">
.tree-dialog {
  .el-dialog__body {
    padding: 20px;
    .el-tree {
      max-height: calc(100vh - 140px - 54px - 70px);
      overflow-y: scroll;
      padding: 0 30px;
      > .el-tree-node {
        padding: 15px 0;
        border: 1px dashed #ddd;
        margin-bottom: 15px;
        border-radius: 10px;
      }
    }
  }
}
</style>
