<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="父目录ID" prop="parentId">
        <el-input v-model="queryParams.parentId" placeholder="请输入父目录ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="目录名称" prop="dirName">
        <el-input v-model="queryParams.dirName" placeholder="请输入目录名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @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 type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['doc:dir:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="dirList" row-key="dirId" :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="目录名称" align="left" prop="dirName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)"
            v-hasPermi="['doc:dir:add']">新增</el-button>
          <el-button v-if="scope.row.parentId != 0" size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)" v-hasPermi="['doc:dir:edit']">修改</el-button>
          <el-button v-if="scope.row.parentId != 0" size="mini" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)" v-hasPermi="['doc:dir:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 个人空间相关信息 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        总容量：{{spaceData.spaceCap}} GB 已使用容量：{{spaceData.usedCap}} GB 可用容量：{{spaceData.avlCap}} GB
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleExpand">扩容申请</el-button>
      </el-col>
    </el-row>
    <!-- 添加或修改目录信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上级目录" prop="parentId">
          <treeselect v-model="form.parentId" :options="dirOptions" :normalizer="normalizer" placeholder="请选择上级目录" />
        </el-form-item>
        <el-form-item label="目录名称" prop="dirName">
          <el-input v-model="form.dirName" placeholder="请输入目录名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </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>

    <!-- 扩容申请对话框 -->
    <el-dialog :title="title" :visible.sync="openSpace" width="500px" append-to-body>
      <el-form ref="formSpace" :model="formSpace" :rules="rulesSpace" label-width="80px">
        <el-form-item label="扩充容量" prop="expandCap">
          <el-input-number v-model="formSpace.expandCap" placeholder="扩充容量"></el-input-number><label>GB</label>
        </el-form-item>
        <el-form-item label="申请理由" prop="remark">
          <el-input v-model="formSpace.remark" type="textarea" rows="5" placeholder="请输入申请理由" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormSpace">确 定</el-button>
        <el-button @click="cancelSpace">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {
    listDir,
    getDir,
    delDir,
    addDir,
    updateDir,
    personalList
  } from "@/api/doc/dir";

  import {
    getPersonalSpace
  } from "@/api/doc/space";

  import {
    listExpand,
    getExpand,
    delExpand,
    addExpand,
    updateExpand
  } from "@/api/doc/expand";

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

  export default {
    name: "Dir",
    components: {
      Treeselect
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 目录信息表格数据
        dirList: [],
        // 目录信息树选项
        dirOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        openSpace: false,
        // 是否展开，默认全部展开
        isExpandAll: true,
        // 重新渲染表格状态
        refreshTable: true,
        // 查询参数
        queryParams: {
          parentId: null,
          dirName: null,
        },
        //个人空间数据
        spaceData: {},
        // 表单参数
        form: {},
        formSpace: {},
        // 表单校验
        rules: {
          dirName: [{
            required: true,
            message: "目录名称不能为空",
            trigger: "blur"
          }],
          parentId: [{
            required: true,
            message: "上级目录不能为空",
            trigger: "blur"
          }],
        },
        // 表单校验
        rulesSpace: {
          expandCap: [{
            required: true,
            message: "扩容容量不能为空",
            trigger: "blur"
          }],
          remark: [{
            required: true,
            message: "申请理由不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
      this.spaceInfo();
    },
    methods: {
      /** 查询个人空间相关信息 */
      spaceInfo() {
        this.loading = true;
        getPersonalSpace().then(response => {
          this.spaceData = response.data;
          this.loading = false;
        });
      },
      /** 查询目录信息列表 */
      getList() {
        this.loading = true;
        personalList().then(response => {
          this.dirList = this.handleTree(response.data, "dirId");
          this.loading = false;
        });
      },
      /** 转换目录信息数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.dirId,
          label: node.dirName,
          children: node.children
        };
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 取消按钮
      cancelSpace() {
        this.openSpace = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          dirId: null,
          spaceId: null,
          parentId: null,
          dirName: null,
          dirPath: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          isDel: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        // this.getTreeselect();
        if (row != null && row.dirId) {
          this.form.parentId = row.dirId;
          // } else {
          // 	this.form.parentId = 0;
        }
        this.open = true;
        this.title = "添加目录信息";
        personalList().then(response => {
          this.form.spaceId = response.data[0].spaceId;
          this.dirOptions = this.handleTree(response.data, "dirId");
        });
      },
      /** 扩容申请 */
      handleExpand(row) {
        console.log(this.spaceData);
        return false;


        this.formSpace.currentCap = this.spaceData.spaceCap;
        this.formSpace.spaceId = this.spaceData.spaceId;
        this.formSpace.spaceName = this.spaceData.spaceName;
        this.reset();
        // this.getTreeselect();
        if (row != null && row.dirId) {
          this.form.parentId = row.dirId;
          // } else {
          // 	this.form.parentId = 0;
        }
        this.openSpace = true;
        this.title = "扩容申请";
        personalList().then(response => {
          this.form.spaceId = response.data[0].spaceId;
          this.dirOptions = this.handleTree(response.data, "dirId");
        });
      },
      /** 展开/折叠操作 */
      toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        if (row != null) {
          this.form.parentId = row.dirId;
        }
        getDir(row.dirId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改目录信息";
        });
        personalList().then(response => {
          this.dirOptions = this.handleTree(response.data, "dirId");
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.dirId != null) {
              updateDir(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addDir(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 提交按钮 */
      submitFormSpace() {
        console.log(this.formSpace);
        this.$refs["formSpace"].validate(valid => {
          if (valid) {
            addExpand(this.formSpace).then(response => {
              this.$modal.msgSuccess("扩容申请提交成功");
              this.openSpace = false;
            });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$modal.confirm('是否确认删除目录信息编号为"' + row.dirId + '"的数据项？').then(function() {
          return delDir(row.dirId);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    }
  };
</script>
