<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <div class="search">
          <div class="sl">
            <el-form-item label="文档名称" prop="roleName">
              <el-input
                v-model="queryParams.docName"
                placeholder="请输入文档名称"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </div>
          <div class="sr">
            <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>
          </div>
        </div>
      </el-form>
    </el-card>
    <el-card class="lrtt">
      <div class="lt">
        <el-input placeholder="请输入..." prefix-icon="el-icon-search"></el-input>
        <div class="divide"></div><!--divide 分隔-->
        <el-tree :data="docCategory" :props="docCategoryProps" @node-click="handleNodeClick">
					<span class="custom-tree-node" slot-scope="{ node, data }">
					    <span>{{ node.label }}</span>
                <el-dropdown>
                  <span class="el-dropdown-link"><i class="el-icon-more"></i></span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item  @click.native="handleDocCategoryAdd(data)"><i class="el-icon-plus"></i>添加</el-dropdown-item>
                    <el-dropdown-item v-if="data.types != 'system'" @@click.native="handleDocCategoryUpdate(data)"><i class="el-icon-edit"></i>编辑</el-dropdown-item>
                    <el-dropdown-item v-if="data.types != 'system'" @@click.native="handleDocCategoryDelete(data)"><i class="el-icon-delete"></i>删除</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
          </span>
        </el-tree>
      </div><!--lt 左-->
      <div class="rt">
        <div class="operate">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleAdd"
          >上传文档</el-button>
          <el-button type="primary" icon="el-icon-position">发布</el-button>
          <el-button icon="el-icon-delete" @click="handleDelete">批量删除</el-button>
        </div><!--operate 操作按钮-->

        <el-table v-loading="loading" :data="docList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="文档编号" prop="docCode" width="120" />
          <el-table-column label="文档名称" prop="docName" :show-overflow-tooltip="true" />
          <el-table-column label="类别" prop="docType" :show-overflow-tooltip="true" width="80" />
          <el-table-column label="负责人" prop="docPrincipals" :show-overflow-tooltip="true" width="80" />
          <el-table-column label="归属部门" prop="docRespDept" :show-overflow-tooltip="true" width="150" />
          <el-table-column label="来源" prop="docSource" width="100" />
          <el-table-column label="关联工具" prop="roleSort" width="100" />
          <el-table-column label="上传状态" prop="docUploadProgress" width="100" >
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.docStatus == 'ysc'">已上传</el-tag>
              <el-tag type="danger" v-else>上传失败</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="180">
            <template slot-scope="scope" v-if="scope.row.roleId !== 1">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handlePriew(scope.row)"
              >预览</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
              <el-button type="text" icon="el-icon-download">下载</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </el-card>



    <el-dialog :title="docCategoryTitle" :visible.sync="docCategoryOpen"  width="50%" append-to-body>
      <el-form ref="docCategoryForm" :model="docCategoryForm" :rules="docCategoryRules" label-width="80px">
        <el-form-item label="父分类" prop="parentId">
          <treeselect v-model="docCategoryForm.parentId" :options="docCategory" :show-count="true" placeholder="如果不选择，默认为顶级节点" />
        </el-form-item>
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="docCategoryForm.categoryName" placeholder="请输入分类名称" maxlength="50" show-word-limit/>
        </el-form-item>
        <el-form-item label="分类描述" prop="categoryDescription">
          <el-input v-model="docCategoryForm.categoryDescription"
                    type="textarea" :rows="3" maxlength="500" show-word-limit
                    placeholder="请输入分类描述" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="docCategorySubmitForm">确 定</el-button>
        <el-button @click="docCategoryCancel">取 消</el-button>
      </span>
    </el-dialog>

    <el-dialog :title="viewDialogTitle" :visible.sync="viewDialogOpen" fullscreen width="500px" append-to-body>
      <i-frame :src="previewUrl" />
    </el-dialog>

    <el-drawer :visible.sync="open" :modal-append-to-body="false" size="75%">
      <template #title>
        <span class="title">新增文档资源</span>
        <div class="drawer-head-btn">
          <el-button type="primary" @click="$refs.editDocumentRef.submitForm()">确 定</el-button>
          <el-button @click="$refs.editDocumentRef.cancel()">取 消</el-button>
        </div><!--drawer-head-btn 抽屉顶部按钮区域-->
      </template>
      <edit-document ref="editDocumentRef" @submit="editDocumentSubmit"/>
    </el-drawer>

    <upload-progress/>
  </div>
</template>

<script>
import { listDocument, getDocument, delDocument, addDocument, updateDocument } from "@/api/document/document";
import { deptTreeSelect } from "@/api/system/user";
import { documentTree,addCategory,updateCategory,delCategory,getCategory } from "@/api/documentCategory/documentCategory.js";

import { Base64 } from 'js-base64';
import iFrame from "@/components/iFrame/index"
import editDocument from "./editDocument";
import uploadProgress from "./uploadProgress";
import { w3cwebsocket as WebSocket } from 'websocket';
import Treeselect from "@riophae/vue-treeselect";

export default {
  name: "Document",
  components: { iFrame, editDocument, uploadProgress, Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 部门名称
      deptName: undefined,
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 文档树
      docCategory: undefined,
      docCategoryProps: {
        children: "children",
        label: "label"
      },
      // 总条数
      total: 0,
      // 表格数据
      docList: [],
      // 弹出层标题
      title: "",
      viewDialogTitle: "",
      viewDialogOpen: false,
      open: false,
      // 日期范围
      dateRange: [],
      previewUrl: '',
      progress: 0,
      fileList: [],
      websocket: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        docName: undefined
      },
      // 表单参数
      form: {
        docName: '',
        docType: '',
        docPrincipals: '',
        docRespDept: '',
        docSource: ''
      },
      // 表单校验
      rules: {
        docType: [
          { required: true, message: "类别不能为空", trigger: "blur" }
        ],
        docSource: [
          { required: true, message: "文档来源不能为空", trigger: "blur" }
        ]
      },
      docCategoryRules: {
        categoryName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人名称不能为空", trigger: "blur" }
        ],
        createById: [
          { required: true, message: "创建人id不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      },
      docCategoryTitle: "",
      docCategoryOpen: false,
      // 文档资源参数
      docCategoryForm: {
        categoryName: null,
        categoryDescription: null,
        parentId: null
      },
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
    this.getDocumentTree();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询树形下拉树结构 */
    getDocumentTree() {
      documentTree().then(response => {
        this.docCategory = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.toolRespDept = data.id;
      this.handleQuery();
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listDocument(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.docList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
    },
    handlePriew(row){
      console.log('mmmmmmmmmmm',process.env.VUE_APP_BASE_API + row.docUrl)
      this.previewUrl = process.env.VUE_APP_TOOL_TECH_FILE_VIEW_API + '/onlinePreview?url=' + encodeURIComponent(Base64.encode(process.env.VUE_APP_BASE_API + row.docUrl));
      this.viewDialogTitle = '文档在线预览'
      this.viewDialogOpen = true;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.docId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    editDocumentSubmit: function() {
      this.open = false;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const docIds = row.docId || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delDocument(docIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 新增按钮操作 */
    handleDocCategoryAdd(data) {
      console.info("data================", data)
      this.docCategoryOpen = true;
      // this.docCategoryForm.parentId = data.id;
      this.$nextTick(() => {
        this.resetDocCategoryForm();
        this.docCategoryForm.parentId = data.id;
      })
      this.docCategoryTitle = "添加文档资源分类";
    },
    /** 删除按钮操作 */
    handleDocCategoryDelete(row) {
      this.$modal.confirm('是否删除该项数据？').then(function() {
        return delCategory(row.id);
      }).then(() => {
        this.getDocumentTree();
        this.$modal.msgSuccess("删除成功");
      }).catch((err) => {
        console.info("err============", err)
      });
    },
    /** 修改按钮操作 */
    handleDocCategoryUpdate(row) {
      this.resetDocCategoryForm();
      const id = row.id;
      getCategory(id).then(response => {
        this.docCategoryForm = response.data;
        this.docCategoryOpen = true;
        this.docCategoryTitle = "修改文档资源分类";
      });
    },
    /** 提交按钮 */
    docCategorySubmitForm() {
      this.$refs["docCategoryForm"].validate(valid => {
        if (valid) {
          if (this.docCategoryForm.id != null) {
            updateCategory(this.docCategoryForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.docCategoryOpen = false;
              this.getDocumentTree();
            });
          } else {
            this.$set(this.docCategoryForm, "type", "user")
            addCategory(this.docCategoryForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.docCategoryOpen = false;
              this.getDocumentTree();
            });
          }
        }
      });
    },
    // 取消按钮
    docCategoryCancel() {
      this.docCategoryOpen = false;
      this.resetDocCategoryForm();
    },
    /** 重置 **/
    resetDocCategoryForm(){
      this.$refs.docCategoryForm.resetFields();
    }

  }
};
</script>

