<template>
  <div>
    <el-dialog
      :destroy-on-close="true"
      title="从公共库中选择"
      :visible.sync="Visible"
      width="70%"
      :close-on-click-modal="false"
    >
      <TableHeader>
        <div slot="right" class="flex">
          <el-input
            size="small"
            style="width: 200px"
            v-model="searchVal"
            class="ml10 mr10"
            placeholder="请输入材料全称搜索"
            @keyup.native.enter="handleSarch"
          ></el-input>
          <el-button size="small" type="primary" @click="handleSarch"
            >搜 索
          </el-button>
          <el-button size="small" @click="resetSearch">重 置</el-button>
        </div>
      </TableHeader>
      <el-table
        ref="multipleTable"
        v-loading="loading"
        size="small"
        :data="tableData"
        reserve-selection
        border
        tooltip-effect="dark"
        style="width: 100%"
        max-height="500px"
        :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="300"
        >
        </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="140"
        >
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        :current="current"
        :size="size"
        @currentChange="changePagination"
        @sizeChange="changeSize"
      ></Pagination>

      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="Visible = false">取 消</el-button>
        <el-button
          :loading="loading"
          size="small"
          type="primary"
          @click="handleOk"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import TableHeader from "@/components/TableHeader.vue";
import Pagination from "@/components/Pagination.vue";
import { getPubdatumList } from "@/api/libray";
import { addPubdatum } from "@/api/materials";
import local from "@/utils/local";
import { mapGetters } from "vuex";

export default {
  components: {
    TableHeader,
    Pagination,
  },
  props: {
    libVisible: {
      type: Boolean,
      required: true,
      default: false,
    },
    matterId: {
      required: false,
      default: 0,
    },
  },
  created() {},
  data() {
    return {
      siteId: local.getLocal("sampleSiteId")
        ? local.getLocal("sampleSiteId")
        : "",
      department: "",
      searchVal: "",
      tableData: [],
      current: 1,
      total: 10,
      size: 10,
      selectedRowKeys: [],
      loading: false,
      addLoading: false,
    };
  },
  computed: {
    Visible: {
      get() {
        return this.libVisible;
      },
      set(val) {
        this.$emit("update:libVisible", val);
      },
    },
    ...mapGetters(["deptList"]),
  },
  methods: {
    // 公共库材料列表
    async getPbuList() {
      this.loading = true;
      let res = await getPubdatumList({
        page: this.current,
        size: this.size,
        materiaFullName: `%${this.searchVal}%`,
      });
      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;
      }
    },
    async handleOk() {
      this.addLoading = true;
      let res = await addPubdatum({
        ids: this.selectedRowKeys.join(","),
        matterId: this.matterId,
        siteId: this.siteId,
      });
      let { code, msg } = res.data;
      this.addLoading = false;
      if (code === 1) {
        this.$message.success(msg);
        this.$refs.multipleTable.clearSelection();
        this.Visible = false;
        this.$emit("ok");
      }
    },
    handleSarch() {
      this.current = 1;
      this.$refs.multipleTable.clearSelection();
      this.getPbuList();
    },
    resetSearch() {
      this.current = 1;
      this.searchVal = "";
      this.$refs.multipleTable.clearSelection();
      this.getPbuList();
    },
    changePagination(cur) {
      this.current = cur;
      this.getPbuList();
    },

    changeSize(size) {
      this.size = size;
      this.getPbuList();
    },
    handleSelectionChange(select) {
      this.selectedRowKeys = select.map((i) => i.id);
      // console.log(select);
    },
  },
};
</script>

<style lang="less" scoped>
.short {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.full-name {
  color: rgb(172, 170, 170);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>