<template>
  <div class="library-manage bgw flex flexc">
    <TabHeader icon="el-icon-notebook-2" label="公共库管理"></TabHeader>
    <div class="pd15 flex1 auto-scroll">
      <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="选择部门"
            filterable
          >
            <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="请输入材料全称搜索"
            @keyup.native.enter="handleSearch"
          ></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"
          v-loading="loading"
          :data="tableData"
          border
          tooltip-effect="dark"
          style="width: 100%"
          :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>
      <Pagination
        :total="total"
        :current="current"
        :size="size"
        @currentChange="changePagination"
        @sizeChange="changeSize"
      ></Pagination>
    </div>

    <!-- 添加材料 -->
    <!--    <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 Pagination from "@/components/Pagination.vue";
import TabHeader from "@/components/TabHeader.vue";
import { getPubdatumList, delPubdatum } from "@/api/libray";
import { mapGetters } from "vuex";
export default {
  components: {
    TableHeader,
    PreviewMaterals,
    Pagination,
    TabHeader,
  },
  data() {
    return {
      department: "",
      searchVal: "",
      tableData: [],
      current: 1,
      size: 10,
      total: 10,
      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;
        if (!data.length && this.current > 1) {
          this.current -= 1;
          this.getPbuList();
        }
        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.$refs.multipleTable.clearSelection();
      this.getPbuList();
    },
    // 重置
    handleReset() {
      this.department = "";
      this.searchVal = "";
      this.$refs.multipleTable.clearSelection();
      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 () => {
          let res = await delPubdatum({ id });
          let { code, msg } = res.data;
          if (code === 1) {
            this.$message.success(msg);
            this.getPbuList();
            this.$refs.multipleTable.clearSelection();
            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>
