<template>
  <div class="app-container">
    <Com-search :search="searchObj" @handleSearch="handleSearch">
      <template #btn>
        <el-button
          v-if="$store.state.user.roles[0] == 'qxwzb_zz'"
          style="margin-right: 15px"
          type="primary"
          @click="handleExport"
          >生成区县补货总计划</el-button
        >
      </template>
    </Com-search>
    <Com-table
      ref="table"
      :tableFormat="tableFormat"
      :tableData="tableData"
      :loading="loading"
    >
      <template #operation="scope">
        <el-button type="primary" @click="handleDetail(scope.row)"
          >查看详情</el-button
        >
        <el-button
          type="primary"
          v-loading="pdfLoading"
          @click="handlePrint(scope.row)"
          >打印补货计划</el-button
        >
        <el-button type="danger" @click="handleDelete(scope.row)"
          >删除</el-button
        >
      </template>
    </Com-table>
    <pagination
      :total="pages.total"
      :page.sync="pages.pageNum"
      :limit.sync="pages.pageSize"
      @pagination="getPageData"
    />
    <detail ref="detail" :get-data="getPageData"></detail>
  </div>
</template>

<script>
import { tableFormat, searchObj } from "./utils/index.json";
import {
  replenishmentDpage,
  replenishmentDdeleteById,
  adsMatReplenishmentDsaveReplenishment,
} from "@/api/home";
import detail from "./components/detail.vue";
import http from "@/utils/axios";
export default {
  components: { detail },
  data() {
    return {
      tableData: [],
      tableFormat: tableFormat,
      loading: false,
      pages: {
        pageNo: 1,
        pageSize: 50,
        total: 0,
      },
      searchObj: searchObj,
      searchData: {
        orderNo: "",
        startTime: "",
        endTime: "",
      },
      pdfLoading: false,
    };
  },
  mounted() {
    console.log(this.$store.state.user, "dddddddddd");
    this.getPageData();
  },
  methods: {
    handleExport() {
      adsMatReplenishmentDsaveReplenishment({}).then((res) => {
        if (res && res.code == 200) {
          this.$message.success("操作成功");
          this.getPageData();
        } else {
          this.$message.warning(res.msg || "操作失败");
        }
      });
      // this.download(
      //   "adsMatReplenishmentD/saveReplenishment",
      //   {},
      //   `${new Date().getTime()}.xlsx`
      // );
    },
    handlePrint(row) {
      http({
        method: "post",
        url: `/adsMatReplenishmentD/printReplenishment`,
        data: {
          id: row.id,
        },
        headers: {
          "Content-Type": "application/json",
          charset: "utf-8",
        },
        responseType: "blob",
        timeout: 1000000,
      }).then((res) => {
        console.log(res, res.size, 3333);
        if (!res || res.size <= 0) {
          console.log(2222222222);
          return;
        }
        const pdfUrl = window.URL.createObjectURL(
          new Blob([res], { type: "application/pdf" })
        );
        var date = new Date().getTime();
        var ifr = document.createElement("iframe");
        ifr.style.frameborder = "no";
        ifr.style.display = "none";
        ifr.style.pageBreakBefore = "always";
        ifr.setAttribute("id", "printPdf" + date);
        ifr.setAttribute("name", "printPdf" + date);
        ifr.src = pdfUrl;
        document.body.appendChild(ifr);
        this.doPrint("printPdf" + date);
        window.URL.revokeObjectURL(ifr.src); // 释放URL 对象
      });
    },
    doPrint(val) {
      let ordonnance = document.getElementById(val).contentWindow;
      console.log(ordonnance, "ordonnance");
      setTimeout(() => {
        ordonnance.print();
        this.pdfLoading = false;
      }, 100);
    },
    // 详情
    handleDetail(row) {
      let obj = {
        data: row,
      };
      this.$refs.detail.handleInit(obj);
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除数据, 是否继续?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {
            id: row.id,
          };
          replenishmentDdeleteById(params).then((res) => {
            if (res && res.code == 200) {
              this.getPageData();
              this.$message.success("删除成功");
            } else {
              this.$message.warning("删除失败");
            }
          });
        })
        .catch(() => {});
    },
    // 搜索
    handleSearch(obj) {
      this.searchData = obj;
      this.pages.pageNo = 1;
      this.pages.total = 0;
      this.getPageData();
    },
    getPageData() {
      let searchData = JSON.parse(JSON.stringify(this.searchData));
      if (searchData && searchData.time && searchData.time.length) {
        searchData.startTime = searchData.time[0];
        searchData.endTime = searchData.time[1];
      } else {
        searchData.startTime = null;
        searchData.endTime = null;
      }
      delete searchData.time;
      let params = {
        ...this.pages,
        ...searchData,
      };
      replenishmentDpage(params).then((res) => {
        if (res && res.code === 200) {
          this.tableData = res.data.records || [];
          this.pages.total = res.data.total;
          this.loading = false;
        } else {
          this.loading = false;
          this.$message.warning(res.msg || "加载失败");
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  position: absolute;
  width: 100%;
  height: calc(100% - 60px - 60px);
}
</style>
