<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">
        <y-table
          :max-height="650"
          :data="tableData"
          :column="columns"
          border
          :loading="loading"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange"
          ref="multipleTable"
        >
          <template slot="materialName" slot-scope="scope">
            <p class="short">{{ scope.row.materialName }}</p>
            <p class="full-name">材料全称：{{ scope.row.materiaFullName }}</p>
          </template>

          <template slot="action" 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>
        </y-table>
      </div>
      <y-pagination
        :total="total"
        :page.sync="page"
        :pageSize.sync="size"
        @change="getPbuList"
      ></y-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 local from "@/utils/local";
import { getPubdatumList, delPubdatum } from "@/api/libray";
import { mapGetters } from "vuex";
export default {
  components: {
    TableHeader,
    PreviewMaterals,
  },
  data() {
    return {
      department: "",
      searchVal: "",
      tableData: [],
      page: 1,
      size: 10,
      total: 10,
      loading: false,
      selectKeys: [],
      depList: [],
      columns: [
        {
          label: "全选",
          type: "selection",
          width: "55",
          align: "center",
          reserveSelection: true,
        },
        {
          label: "序号",
          type: "index",
          width: "55",
          align: "center",
          index: (index) => {
            return (this.page - 1) * this.size + index + 1;
          },
        },
        {
          label: "部门名称",
          prop: "deptName",
          align: "center",
          showOverflowTooltip: true,
          width: "200",
        },
        {
          label: "材料名称",
          slot: true,
          prop: "materialName",
          showOverflowTooltip: true,
        },

        {
          label: "添加时间",
          prop: "createTime",
          align: "center",
          width: "150",
        },

        {
          label: "操作",
          slot: true,
          prop: "action",
          align: "center",
          width: "100",
        },
      ],
      libVisible: false,
      materalsInfo: {},
      previewVisible: false,
      siteId: local.getLocal("writeSiteId"),
    };
  },
  created() {
    this.getPbuList();
  },
  computed: {
    ...mapGetters(["deptList"]),
  },
  methods: {
    // 获取公共库材料
    async getPbuList() {
      this.loading = true;
      let res = await getPubdatumList({
        page: this.page,
        size: this.size,
        materiaFullName: `%${this.searchVal}%`,
        deptCode: this.department,
        siteId: this.siteId,
      });
      this.loading = false;
      if (res.data.code === 1) {
        let { data, total } = res.data.data;
        if (!data.length && this.page > 1) {
          this.page -= 1;
          this.getPbuList();
        }
        this.tableData = data;
        this.total = total;
      }
    },

    // 加入材料
    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.page = 1;
      this.$refs.multipleTable.clearSelection();
      this.getPbuList();
    },
    // 重置
    handleReset() {
      this.department = "";
      this.searchVal = "";
      this.page = 1;
      this.$refs.multipleTable.clearSelection();
      this.getPbuList();
    },
    // 选中
    handleSelectionChange(select) {
      this.selectKeys = select;
    },

    // 预览
    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();
          }
        })
        .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>
