<!--文章类型管理-->
<template>
  <div class="app-container">
    <el-form ref="queryForm" :inline="true" :model="queryParams" size="small">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name"/>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:dept:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd(undefined)"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-sort"
          plain
          size="mini"
          type="info"
          @click="toggleExpandAll"
        >展开/折叠
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="tableData"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      row-key="id">
      <el-table-column label="名称" prop="name" width="260"></el-table-column>
      <el-table-column label="排序" prop="orderNum" width="200"></el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleEdit(scope.row.id)"
          >修改
          </el-button>
          <el-button
            icon="el-icon-plus"
            size="mini"
            type="text"
            @click="handleAdd(scope.row.id)"
          >新增
          </el-button>
          <el-button
            v-if="scope.row.parentId != 0"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleRemove(scope.row.id)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="open" append-to-body width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col>
            <el-form-item label="上级类型" prop="parentId">
              <treeselect v-model="form.parentId"
                          :normalizer="normalizer"
                          :options="typeOptions"/>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name"/>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="显示顺序" prop="orderNum">
              <el-input-number :min="0" v-model="form.orderNum" style="width: 100%"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {add, detail, edit, list, remove} from '@/api/liandao/articleType.js'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "articleType",
  components: {Treeselect},
  created() {
    this.getList()
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      queryParams: {
        name: '',
      },
      tableData: [],
      // 表单数据
      form: {},
      // 弹出层标题
      title: '',
      open: false,
      rules: {
        name: [{required: true, message: "名称不能为空", trigger: "blur"},],
        orderNum: [{required: true, message: "显示顺序不能为空", trigger: "blur"},],
      },
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      typeOptions: [],
    }
  },
  methods: {
    /** 查询数据列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(resp => {
          if (resp.code == 200) {
            this.tableData = resp.data;
            this.loading = false;
          }
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        orderNum: undefined,
        parentId: undefined,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd(id) {
      this.reset();
      if (id != undefined) {
        this.form.parentId = id;
      }
      this.open = true;
      this.title = "新增";
      this.getTypeOptions();
    },
    getTypeOptions() {
      this.typeOptions = []
      list().then(resp => {
          if (resp.code == 200 && resp.data.length > 0) {
            this.typeOptions = resp.data;
          }
        }
      );
    },
    /** 修改按钮操作 */
    handleEdit(id) {
      this.reset();
      detail(id).then(resp => {
        if (resp.code === 200) {
          this.form = {...resp.data}
          this.open = true;
          this.title = "修改";
          this.getTypeOptions();
        }
      })
    },
    handleRemove(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(id).then(resp => {
          if (resp.code === 200) {
            this.$message.success('删除成功！')
            this.resetQuery()
          }
        })
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            edit(this.form).then(resp => {
              if (resp.code === 200) {
                this.$message.success("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.$message.error(resp.msg);
              }
            });
          } else {
            add(this.form).then(resp => {
              if (resp.code === 200) {
                this.$message.success("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.$message.error(resp.msg);
              }
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
  }
}
</script>

