<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="6" :xs="24">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="handleAdd()"
          >新增目录</el-button
        >
        <div class="head-container">
          <el-input
            v-model="catelogueName"
            placeholder="请输入目录名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-top: 10px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="treeList"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            :render-content="renderContent"
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="14" :xs="24" :offset="1">
        <div v-if="open">
          <el-divider content-position="left"
            ><h2>{{ title }}</h2></el-divider
          >
          <el-form
            ref="form"
            :model="form"
            :rules="rules"
            label-width="120px"
            style="margin-top: 50px"
          >
            <el-form-item label="父目录" prop="parentId">
              <treeselect
                v-model="form.parentId"
                :options="treeList"
                :show-count="true"
                placeholder="请选择上级目录"
              />
            </el-form-item>
            <el-form-item label="目录名称" prop="catalogueName">
              <el-input
                v-model="form.catalogueName"
                placeholder="请输入目录名称"
              />
            </el-form-item>
            <!-- <el-form-item label="封面图片" prop="coverImg">
              <image-upload v-model="form.coverImg" />
            </el-form-item> -->
            <el-form-item label="排序" prop="sort">
              <el-input-number
                :controls="false"
                v-model="form.sort"
                placeholder="从小到大"
              />
            </el-form-item>
            <el-form-item label="描述" prop="remark">
              <el-input
                v-model="form.remark"
                placeholder="请输入描述"
                type="textarea"
              />
            </el-form-item>
          </el-form>
          <div class="form-footer">
            <el-button type="primary" @click="submitForm" :loading="submitting"
              >确 定</el-button
            >
            <el-button @click="cancel">取 消</el-button>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  listCatalogue,
  getCatalogue,
  delCatalogue,
  addCatalogue,
  updateCatalogue,
} from "@/api/system/integration/catalogue";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Catalogue",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      submitting: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 目录表格数据
      catalogueList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100000,
        catalogueType: "integration",
      },
      // 表单参数
      form: {},
      treeList: [],
      // 表单校验
      rules: {
        catalogueName: [
          { required: true, message: "目录名称不能为空", trigger: "change" },
        ],
        coverImg: [
          { required: true, message: "请上传封面图片", trigger: "change" },
        ],
      },
      catelogueName: "",
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  watch: {
    catelogueName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
  },
  methods: {
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    renderContent(h, { node, data, store }) {
      return (
        <div class="custom-tree-node">
          <div class="label">{node.label}</div>
          <div class="action">
            <el-button
              size="mini"
              type="text"
              on-click={() => this.handleAdd(data.id)}
            >
              <i class="el-icon-plus"></i>
            </el-button>
            <el-button
              size="mini"
              type="text"
              on-click={() => this.handleUpdate(data)}
            >
              <i class="el-icon-edit"></i>
            </el-button>
            <el-button
              size="mini"
              type="text"
              on-click={() => this.handleDelete(data)}
            >
              <i class="el-icon-delete"></i>
            </el-button>
          </div>
        </div>
      );
    },
    // 节点单击事件
    handleNodeClick(data) {
      console.log(data);
    },
    /** 查询目录列表 */
    getList() {
      this.loading = true;
      listCatalogue(this.queryParams).then((response) => {
        let treeList = [];
        if (response.rows && response.rows.length > 0) {
          response.rows.forEach((p) => {
            treeList.push({
              id: p.id,
              parentId: p.parentId,
              label: p.catalogueName,
              children: [],
            });
          });
        }
        this.treeList = this.handleTree(treeList);
        console.log(this.treeList);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        catalogueType: null,
        catalogueName: null,
        sort: null,
        remark: null,
        coverImg: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd(parentId = null) {
      this.reset();
      if (parentId > 0) {
        this.form.parentId = parentId;
      }
      this.open = true;
      this.title = "添加整合目录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCatalogue(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改整合目录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.catalogueType = "integration";
          this.submitting = true;
          if (this.form.id != null) {
            updateCatalogue(this.form).then((response) => {
              this.submitting = false;
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(() => {
              this.submitting = false;
            });
          } else {
            addCatalogue(this.form).then((response) => {
              this.submitting = false;
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch(() => {
              this.submitting = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除目录编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCatalogue(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/integration/catalogue/export",
        {
          ...this.queryParams,
        },
        `catalogue_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped>
.form-footer {
  text-align: right;
}
</style>
