<template>
  <div class="app-container">
    <!-- <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <el-tree
          :data="treeList"
          :props="defaultProps"
          :expand-on-click-node="true"
          ref="tree"
          node-key="id"
          default-expand-all
          highlight-current
          @node-click="handleNodeClick"
        />
      </el-col>
      <el-col :span="20" :xs="24"> -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      :class="showSearch ? '' : ' hide'"
      label-width="68px"
    >
      <el-form-item label="文献名称" prop="literatureName">
        <el-input
          v-model="queryParams.literatureName"
          placeholder="支持模糊查询"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布机构" prop="publishOrganization">
        <el-select
          v-model="queryParams.publishOrganization"
          placeholder="请选择发布机构"
          clearable
          filterable
        >
          <el-option
            v-for="dict in dict.type.sys_provide_organization"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主题" prop="theme">
        <el-select
          v-model="queryParams.theme"
          placeholder="请选择主题"
          clearable
          filterable
        >
          <el-option
            v-for="dict in dict.type.sys_theme"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="公开年份" prop="publicYear">
        <el-select
          v-model="queryParams.publicYear"
          placeholder="请选择公开年份"
          clearable
          filterable
        >
          <el-option
            v-for="dict in dict.type.sys_public_year"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间"
        >
        </el-date-picker>
      </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="['system/share:literature:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system/share:literature:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system/share:literature:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system/share:literature:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="literatureList">
      <el-table-column
        label="编号"
        prop="id"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column label="文献名称" prop="literatureName" />
      <el-table-column label="文件名称" prop="fileUrl" min-width="150">
        <template slot-scope="scope">
          <div v-if="scope.row.fileUrl">
            <div v-for="(file, i) in scope.row.fileUrl.split(',')" :key="i">
              <a
                style="color: #46a6ff"
                :href="baseUrl + file"
                target="_blank"
                >{{
                  file.substr(file.lastIndexOf("/") + 1, file.length - 1)
                }}</a
              >
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="发布机构" prop="publishOrganization">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_provide_organization"
            :value="scope.row.publishOrganization"
          />
        </template>
      </el-table-column>
      <el-table-column label="主题" prop="theme">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_theme" :value="scope.row.theme" />
        </template>
      </el-table-column>
      <el-table-column label="公开年份" align="center" prop="publicYear">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_public_year"
            :value="scope.row.publicYear"
          />
        </template>
      </el-table-column>
      <el-table-column label="标签" prop="tag" />
      <!-- <el-table-column
        label="浏览次数"
        align="center"
        prop="viewCount"
        width="80"
      /> -->
      <el-table-column
        label="下载次数"
        align="center"
        prop="downloadCount"
        width="80"
      />
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system/share:literature:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system/share:literature:remove']"
            >删除</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"
    />
    <!-- </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="literatureName">
          <el-input
            v-model="form.literatureName"
            placeholder="请输入文献名称"
          />
        </el-form-item>
        <el-form-item label="发布机构" prop="publishOrganization">
          <el-select
            v-model="form.publishOrganization"
            placeholder="请选择发布机构"
            filterable
          >
            <el-option
              v-for="dict in dict.type.sys_provide_organization"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主题" prop="theme">
          <el-select v-model="form.theme" placeholder="请选择主题" filterable>
            <el-option
              v-for="dict in dict.type.sys_theme"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公开年份" prop="publicYear">
          <el-select
            v-model="form.publicYear"
            placeholder="请选择公开年份"
            filterable
          >
            <el-option
              v-for="dict in dict.type.sys_public_year"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开放类型" prop="publishType">
          <el-select v-model="form.publishType" placeholder="请选择开放类型">
            <el-option
              v-for="dict in dict.type.sys_publish_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联标签" prop="tag">
          <el-select
            v-model="form.tag"
            placeholder="请选择"
            multiple
            style="width: 100%"
            filterable
          >
            <el-option
              v-for="(tag, i) in tagList"
              :label="tag.tagName"
              :value="tag.tagName"
              :key="i"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上传文件" required>
          <file-upload
            v-model="form.fileUrl"
            :fileType="['doc', 'docx', 'xls', 'xlsx', 'pdf']"
            :fileSize="500"
          />
        </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" :loading="submitting"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLiterature,
  getLiterature,
  delLiterature,
  addLiterature,
  updateLiterature,
} from "@/api/system/share/literature";

import { listTag } from "@/api/system/base/tag";
import { getDynamicsApiUrl } from "@/utils/dxp";
export default {
  name: "Literature",
  dicts: [
    "sys_theme",
    "sys_public_year",
    "sys_provide_organization",
    "sys_publish_type",
  ],
  data() {
    return {
      baseUrl: getDynamicsApiUrl(process.env.VUE_APP_BASE_API),
      // 遮罩层
      loading: true,
      submitting: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文献管理表格数据
      literatureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        literatureName: null,
        publishOrganization: null,
        theme: null,
        publicYear: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        literatureName: [
          { required: true, message: "文献名称不能为空", trigger: "change" },
        ],
        publishOrganization: [
          {
            required: true,
            message: "发布机构不能为空",
            trigger: "change",
          },
        ],
        theme: [
          {
            required: true,
            message: "主题不能为空",
            trigger: "change",
          },
        ],
        publicYear: [
          {
            required: true,
            message: "公开年份不能为空",
            trigger: "change",
          },
        ],
        publishType: [
          {
            required: true,
            message: "请选择开放类型",
            trigger: "change",
          },
        ],
      },
      tagList: [],
      treeList: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  created() {
    this.getTagList();
    this.getList();
    // const self = this;
    // setTimeout(function () {
    //   self.getTree();
    // }, 500);
  },
  methods: {
    // 节点单击事件
    handleNodeClick(data) {
      console.log(data);
      if (!data.type) {
        return;
      }

      if (data.type === "po") {
        this.queryParams.publishOrganization = data.id;
      } else if (data.type === "t") {
        this.queryParams.theme = data.id;
      } else if (data.type === "py") {
        this.queryParams.publicYear = data.id;
      }

      this.handleQuery();
    },
    getTree() {
      const treeList = [];
      // 按发布机构
      const publicOrg = {
        id: "po",
        label: "按发布机构分类",
        children: [],
      };

      this.dict.type.sys_provide_organization.forEach((p) => {
        publicOrg.children.push({
          id: p.value,
          label: p.label,
          type: "po",
        });
      });
      treeList.push(publicOrg);

      // 按主题
      const theme = {
        id: "t",
        label: "按主题分类",
        children: [],
      };

      this.dict.type.sys_theme.forEach((p) => {
        theme.children.push({
          id: p.value,
          label: p.label,
          type: "t",
        });
      });
      treeList.push(theme);

      // 按公开年度
      const publicYear = {
        id: "py",
        label: "按公开年度分类",
        children: [],
      };

      this.dict.type.sys_public_year.forEach((p) => {
        publicYear.children.push({
          id: p.value,
          label: p.label,
          type: "py",
        });
      });
      treeList.push(publicYear);
      this.treeList = treeList;
    },
    getTagList() {
      listTag({}).then((response) => {
        this.tagList = response.rows;
      });
    },
    /** 查询文献管理列表 */
    getList() {
      this.loading = true;
      listLiterature(this.queryParams).then((response) => {
        this.literatureList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        literatureName: null,
        publishOrganization: null,
        theme: null,
        publicYear: null,
        tag: null,
        fileUrl: null,
        viewCount: null,
        downloadCount: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: 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() {
      this.reset();
      this.open = true;
      this.title = "添加文献";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getLiterature(id).then((response) => {
        this.form = response.data;
        if (this.form.tag && this.form.tag.length > 0) {
          this.form.tag = this.form.tag.split(",");
        }
        this.open = true;
        this.title = "修改文献";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.tag && this.form.tag.length > 0) {
            this.form.tag = this.form.tag.join(",");
          } else {
            this.form.tag = null;
          }
          this.submitting = true;
          if (this.form.id != null) {
            updateLiterature(this.form).then((response) => {
              this.submitting = false;
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(() => {
              this.submitting = false;
            });
          } else {
            addLiterature(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 delLiterature(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/share/literature/export",
        {
          ...this.queryParams,
        },
        `literature_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
