<template>
  <div>
    <el-dialog title="从公共库中选择" :visible.sync="Visible" width="70%">
      <TableHeader>
        <div slot="right" class="flex">
          <el-input
            size="small"
            style="width: 200px"
            v-model="searchVal"
            class="ml10 mr10"
            placeholder="请输入事项名称搜索"
          ></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>
      <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>
      <span slot="footer" class="dialog-footer">
        <el-button
          :loading="loading"
          size="small"
          type="primary"
          @click="handleOk"
          >确 定</el-button
        >
        <el-button size="small" @click="Visible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

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

export default {
  components: {
    TableHeader,
  },
  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: [],
      pageSizes: [10, 20, 30, 40, 50, 100, 200],
      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,
        materialName: 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.getPbuList();
    },
    resetSearch() {
      this.current = 1;
      this.searchVal = "";
      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>