<template>
  <div class="library-manage">
    <el-card class="box-card" shadow="never">
      <div slot="header" class="clearfix">
        <span>公共库管理</span>
      </div>
      <TableHeader>
        <div slot="left">
          <!--          <el-button size="small" type="primary" @click="handleAddMaterials"
            >加入材料</el-button
          >-->
          <el-button size="small" type="danger" @click="handleDelAll"
            >批量移除</el-button
          >
        </div>
        <div slot="right" class="flex">
          <el-select
            v-model="department"
            size="small"
            class="autoWidth"
            placeholder="选择部门"
          >
            <template slot="prefix">
              {{
                (deptList.find((v) => v.deptNumber === department) || {}).name
              }}
            </template>
            <el-option
              v-for="item in deptList"
              :key="item.deptNumber"
              :label="item.name"
              :value="item.deptNumber"
            >
            </el-option>
          </el-select>
          <el-input
            size="small"
            v-model="searchVal"
            style="width: 200px"
            class="ml10 mr10"
            placeholder="请输入材料全称搜索"
          ></el-input>
          <el-button size="small" type="primary" @click="handleSearch"
            >搜索</el-button
          >
          <el-button size="small" @click="handleReset">重置</el-button>
        </div>
      </TableHeader>
      <!-- 表格 -->
      <div class="table-content">
        <el-table
          ref="multipleTable"
          size="small"
          v-loading="loading"
          :data="tableData"
          border
          tooltip-effect="dark"
          style="width: 100%"
          max-height="676px"
          :row-key="(row) => row.id"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center">
          </el-table-column>
          <el-table-column
            type="index"
            label="序号"
            width="55"
            align="center"
            :index="(index) => (current - 1) * size + index + 1"
          >
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            label="部门名称"
            align="center"
            prop="deptName"
            width="200"
          >
          </el-table-column>
          <el-table-column prop="name" show-overflow-tooltip label="材料名称">
            <template slot-scope="scope">
              <p class="short">{{ scope.row.materialName }}</p>
              <p class="full-name">材料全称：{{ scope.row.materiaFullName }}</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="添加时间"
            align="center"
            width="150"
          >
          </el-table-column>
          <el-table-column label="操作" align="center" width="100">
            <template slot-scope="scope">
              <div class="flex jca">
                <span class="primary pointer" @click="handlePreview(scope.row)"
                  >预览</span
                >
                <span class="delete pointer" @click="handleDel(scope.row.id)"
                  >移除</span
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination" v-if="total">
        <el-pagination
          background
          layout="total,sizes,prev,pager,next,jumper"
          :pager-count="5"
          :total="total"
          :current-page="current"
          :page-size="size"
          :page-sizes="pageSizes"
          @current-change="changePagination"
          @size-change="changeSize"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 添加材料 -->
    <!--    <AddMaterals
      ref="AddMaterals"
      :depList="depList"
      :libVisible.sync="libVisible"
    ></AddMaterals>-->
    <!-- 材料预览 -->
    <PreviewMaterals
      :materialsInfo="materalsInfo"
      :previewVisible.sync="previewVisible"
    ></PreviewMaterals>
  </div>
</template>

<script>
import TableHeader from "@/components/TableHeader.vue";
import PreviewMaterals from "./modal/PreviewMaterals.vue";
import { getPubdatumList, delPubdatum } from "@/api/libray";
import { mapGetters } from "vuex";
export default {
  components: {
    TableHeader,
    PreviewMaterals,
  },
  data() {
    return {
      department: "",
      searchVal: "",
      tableData: [],
      current: 1,
      size: 10,
      total: 10,
      pageSizes: [10, 20, 30, 40, 50, 100, 200],
      loading: false,
      selectKeys: [],
      depList: [],
      libVisible: false,
      materalsInfo: {},
      previewVisible: false,
    };
  },
  created() {
    this.getPbuList();
  },
  computed: {
    ...mapGetters(["deptList"]),
  },
  methods: {
    // 系统事项列表
    async getPbuList() {
      this.loading = true;
      let res = await getPubdatumList({
        page: this.current,
        size: this.size,
        materiaFullName: `%${this.searchVal}%`,
        deptCode: this.department,
      });
      this.loading = false;
      if (res.data.code === 1) {
        let { data, total } = res.data.data;
        this.tableData = data;
        this.total = total;
        this.$refs.multipleTable.bodyWrapper.scrollTop = 0;
      }
    },

    // 加入材料
    handleAddMaterials() {
      this.libVisible = true;
    },
    // 批量移除
    handleDelAll() {
      if (!this.selectKeys.length) {
        this.$message.warning("请先勾选数据");
        return;
      }

      let ids = this.selectKeys.map((v) => v.id).join(",");
      this.handleDel(ids);
    },
    // 搜索
    handleSearch() {
      this.current = 1;
      this.getPbuList();
    },
    // 重置
    handleReset() {
      this.department = "";
      this.searchVal = "";
      this.getPbuList();
    },
    // 选中
    handleSelectionChange(select) {
      this.selectKeys = select;
    },
    // 翻页
    changePagination(cur) {
      this.current = cur;
      this.getPbuList();
    },

    // 改变没有显示数量
    changeSize(size) {
      this.size = size;
      this.getPbuList();
    },
    // 预览
    handlePreview(row) {
      this.materalsInfo = row;
      this.previewVisible = true;
    },
    // 移除
    handleDel(id) {
      this.$confirm(
        "此操作将从公共库中移除该材料信息，是否继续？",
        "系统提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          cancelButtonClass: "btn-custom-cancel",
          type: "warning",
        }
      )
        .then(async () => {
          console.log(id);
          let res = await delPubdatum({ id });
          let { code, msg } = res.data;
          if (code === 1) {
            this.$message.success(msg);
            this.getPbuList();
            this.selectKeys = [];
          }
        })
        .catch(() => {
          console.log("取消成功！");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.library-manage {
  width: 100%;
  height: 100%;
  .short {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .full-name {
    color: rgb(172, 170, 170);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  // .table-content {
  //   height: 550px;
  // }
}
</style>