<template>
  <div>
    <el-dialog
      :destroy-on-close="true"
      title="从公共库中选择"
      :visible.sync="Visible"
      width="50%"
    >
      <TableHeader>
        <div slot="right" class="flex">
          <el-select
            v-model="department"
            class="autoWidth"
            size="small"
            placeholder="选择部门"
            filterable
          >
            <template slot="prefix">
              {{
                (deptList.find((s) => s.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"
            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>
      <y-table
        ref="multipleTable"
        size="small"
        :data="tableData"
        :column="columns"
        :loading="loading"
        border
        tooltip-effect="dark"
        style="width: 100%"
        max-height="460px"
        :row-key="(row) => row.id"
        @selection-change="handleSelectionChange"
      >
        <template slot="materialName" slot-scope="scope">
          <p class="short">{{ scope.row.materialName }}</p>
          <p class="full-name">材料全称：{{ scope.row.materiaFullName }}</p>
        </template>
      </y-table>
      <y-pagination
        :total="total"
        :page.sync="page"
        :pageSize.sync="size"
        @change="getPbuList"
      ></y-pagination>

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

<script>
import TableHeader from "@/components/TableHeader.vue";
import { addPubdatum } from "@/api/materials";
import { getPubdatumList } from "@/api/libray";
import { mapGetters } from "vuex";
import local from "@/utils/local";
export default {
  components: {
    TableHeader,
  },
  props: {
    libVisible: {
      type: Boolean,
      required: true,
      default: false,
    },
    matterId: {
      required: true,
      default: "",
    },
  },
  data() {
    return {
      siteId: local.getLocal("sampleSiteId")
        ? local.getLocal("sampleSiteId")
        : "",
      department: "",
      searchVal: "",
      tableData: [],
      loading: false,
      addLoading: false,
      page: 1,
      total: 10,
      size: 10,
      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",
          width: "300",
          showOverflowTooltip: true,
        },
        {
          label: "材料名称",
          slot: true,
          prop: "materialName",
          showOverflowTooltip: true,
        },

        {
          label: "加入时间",
          prop: "createTime",
          align: "center",
          width: "140",
        },
      ],
    };
  },
  computed: {
    Visible: {
      get() {
        return this.libVisible;
      },
      set(val) {
        this.$emit("update:libVisible", val);
      },
    },
    ...mapGetters(["deptList"]),
  },
  created() {},
  methods: {
    // 公共库材料
    async getPbuList() {
      this.loading = true;
      let res = await getPubdatumList({
        page: this.page,
        size: this.size,
        deptCode: this.department,
        materiaFullName: `%${this.searchVal}%`,
      });
      this.loading = false;
      if (res.data.code === 1) {
        let { data, total } = res.data.data;
        this.tableData = data;
        this.total = total;
      }
    },
    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.handleClose();
        this.$emit("ok");
      }
    },
    handleSarch() {
      this.page = 1;
      this.$refs.multipleTable.clearSelection();
      this.getPbuList();
    },
    resetSearch() {
      this.searchVal = "";
      this.department = "";
      this.page = 1;
      this.$refs.multipleTable.clearSelection();
      this.getPbuList();
    },

    handleSelectionChange(select) {
      this.selectedRowKeys = select.map((i) => i.id);
    },
    // 关闭
    handleClose() {
      Object.assign(this.$data, this.$options.data());
      this.$refs.multipleTable.clearSelection();
      this.Visible = false;
    },
  },
};
</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>
