<template>
  <div class="Print-page flex flexc">
    <Header>
      <div slot="title" class="title">打印空白材料</div>
    </Header>
    <div class="main flex1 flex flexc">
      <PageTop>
        <div slot="count" class="count">
          共计<span class="data-count">{{ total }}</span
          >份材料
        </div>
        <SearchBox
          slot="search"
          width="834px"
          @click="getMaterialsList"
          v-model="searchVal"
        ></SearchBox>
      </PageTop>
      <!-- 材料列表 -->
      <div class="materals-box flex1">
        <div
          class="materals-list flex flexwrap jcb"
          v-if="materailsList.length"
        >
          <div
            class="materals-item flex flexc aic"
            v-for="v in materailsList"
            :key="v.id"
            @click="handleprint(v)"
          >
            <div class="pic-box">
              <img :src="api + v.preViewPath" />
            </div>
            <div class="for-short">{{ v.materialName }}</div>
            <div class="materals-name">
              {{ v.materiaFullName }}
            </div>
          </div>
          <div
            class="list"
            v-for="item in row - (materailsList.length % row)"
            v-show="materailsList.length % row > 0"
            :key="'list' + item"
          ></div>
        </div>
        <el-empty :image-size="200" v-else></el-empty>
        <!-- 分页 -->
        <div class="tac mt20">
          <el-pagination
            prev-text="上一页"
            next-text="下一页"
            hide-on-single-page
            layout="prev,next"
            :total="total"
            :current-page="current"
            :page-size="size"
            @current-change="changePage"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <!-- 材料预览 -->
    <PreviewModal :previewInfo="formInfo" v-show="PreviewModalShow">
      <el-button
        :loading="printLoading"
        class="print-btn"
        @click="handlePrint(api + formInfo.preViewPath)"
        >打印此表</el-button
      >
      <el-button class="next-btn" @click="PreviewModalShow = false"
        >关闭</el-button
      >
    </PreviewModal>
  </div>
</template>

<script>
import Header from "@/components/Header.vue";
import PageTop from "@/components/PageTop.vue";
import SearchBox from "@/components/SearchBox.vue";
import { getMaterialsList } from "@/api";
import PreviewModal from "@/components/PreviewModal.vue";
import local from "@/utils/local";
import { printFn } from "@/utils/print";
export default {
  components: {
    Header,
    PageTop,
    SearchBox,
    PreviewModal,
  },
  data() {
    return {
      api: local.getLocal("baseUrl")
        ? local.getLocal("baseUrl")
        : "http://192.168.0.98:11074",
      row: 7,
      searchVal: "",
      total: 0,
      current: 1,
      size: 14,
      visible: false,
      materailsList: [],
      PreviewModalShow: false,
      formInfo: {}, // 预览数据
      printLoading: false, // 打印loading
    };
  },
  created() {
    this.getMaterialsList();
  },
  watch: {
    searchVal(newVal) {
      if (newVal === "") {
        this.getMaterialsList();
      }
    },
  },
  methods: {
    // 获取材料列表
    async getMaterialsList() {
      let res = await getMaterialsList({
        page: this.current,
        size: this.size,
        materialName: this.searchVal,
      });
      let { total, data } = res.data.data;
      this.total = total;
      this.materailsList = data;
    },

    changePage(cur) {
      this.current = cur;
      this.getMaterialsList();
    },
    handleprint(row) {
      this.formInfo = row;
      this.PreviewModalShow = true;
    },
    // 打印空白表单
    handlePrint(data) {
      printFn({
        data,
      });
    },
  },
};
</script>

<style lang="less" scoped>
.Print-page {
  width: 100%;
  height: 100%;
  background-color: #f5f5f5;
}
.main {
  padding: 0px 40px;
  padding-bottom: 40px;
  .data-count {
    color: #ff0000;
  }
}
.materals-box {
  width: 100%;
  padding: 30px;
  background: #ffffff;
  border-radius: 16px;
}
.materals-list {
  width: 100%;
  align-content: flex-start;
  overflow-y: auto;
  .list {
    content: "";
    width: 13%;
    border: 1px solid transparent;
    padding: 5px;
    overflow: hidden;
  }
}
/deep/.btn-prev {
  margin-right: 80px;
}
/deep/.btn-prev,
/deep/ .btn-next {
  width: 78px;
  height: 25px;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #888888;
  line-height: 28px;
  span {
    font-size: 28px;
  }
}
.materals-item {
  width: 13%;
  margin-top: 10px;
  height: 320px;
  .pic-box {
    width: 180px;
    height: 200px;
    padding: 16px 24px;
    background: #f4f6fc;
    border-radius: 16px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .for-short {
    margin-top: 18px;
    margin-bottom: 8px;
    font-size: 24px;
    font-family: Source Han Sans CN;
    color: #333333;
  }
  .materals-name {
    font-size: 18px;
    font-family: Source Han Sans CN;
    color: #777777;
    line-height: 30px;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }
}
.prev-btn,
.next-btn {
  width: 130px;
  height: 54px;
  border: 1px solid var(--main-theme-color1);
  border-radius: 8px;
  font-size: 24px;
  font-family: Source Han Sans CN;
  color: var(--main-theme-color1);
}
.print-btn {
  width: 130px;
  height: 54px;
  background: linear-gradient(
    -90deg,
    var(--gradient-ramp-assist3),
    var(--gradient-ramp-theme3)
  );
  box-shadow: 0px 6px 12px 1px rgba(88, 193, 158, 0.2);
  border-radius: 8px;
  font-size: 24px;
  font-family: Source Han Sans CN;
  color: #ffffff;
}
</style>