<template>
  <div class="app-container">
    <Com-search :search="searchObj" @handleSearch="handleSearch">
      <template #selectTree>
        <treeselect
          v-model="orgNo"
          :options="deptOptions"
          :show-count="true"
          placeholder="请选择所属单位"
          :normalizer="unitNamenormalizer"
        />
      </template>
      <!-- <template #btn>
        <el-button type="primary">导出</el-button>
      </template> -->
    </Com-search>
    <Com-table
      :tableFormat="tableFormat"
      :tableData="tableData"
      :loading="loading"
    >
      <template #operation="scope">
        <el-button type="danger" @click="handleDelete(scope.row)"
          >删除</el-button
        >
        <el-button type="primary" @click="handleDetail(scope.row)"
          >详情</el-button
        >
        <el-button type="primary" @click="handleExport(scope.row)"
          >导出</el-button
        >
        <el-button type="primary" @click="handlePrint(scope.row)"
          >打印</el-button
        >
      </template>
    </Com-table>
    <pagination
      :total="pages.total"
      :page.sync="pages.pageNo"
      :limit.sync="pages.pageSize"
      @pagination="getPageData"
    />
    <detail ref="detail"></detail>
  </div>
</template>

<script>
import {
  adsMatPoiseOrderDpage,
  adsMatPoiseOrderDdeletedById,
} from "@/api/home";
import { tableFormat, searchObj } from "./utils/index.json";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import http from "@/utils/axios";
import Detail from "./components/detail.vue";
export default {
  components: { Treeselect, Detail },
  data() {
    return {
      orgNo: undefined,
      searchObj: searchObj,
      searchData: {
        orgNo: undefined,
        orderNo: "",
        time: [],
        startTime: "",
        endTime: "",
      },
      deptOptions: [],
      tableFormat: tableFormat,
      tableData: [],
      loading: false,
      pages: {
        pageNo: 1,
        pageSize: 50,
        total: 0,
      },
    };
  },
  mounted() {
    this.getPageData();
    this.getDeptTree();
  },
  methods: {
    handleDelete(row) {
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let obj = {
            id: row.id,
          };
          adsMatPoiseOrderDdeletedById(obj).then((res) => {
            if (res && res.code == 200) {
              this.$message.success("操作成功！");
              this.getPageData();
            } else {
              this.$message.success("操作失败！");
            }
          });
        })
        .catch(() => {});
    },
    handleDetail(row) {
      let obj = {
        data: row,
      };
      this.$refs.detail.handleInit(obj);
    },
    handlePrint(row) {
      http({
        method: "post",
        url: `/adsMatPoiseOrderD/printOrgPoiseOrder`,
        data: {
          id: row.id,
        },
        headers: {
          "Content-Type": "application/json",
          charset: "utf-8",
        },
        responseType: "blob",
        timeout: 1000000,
      }).then((res) => {
        if (!res || res.size <= 0) {
          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;
      setTimeout(() => {
        ordonnance.print();
      }, 100);
    },
    // 导出
    handleExport(row) {
      this.download(
        "adsMatPoiseOrderD/exportOrgPoiseOrder",
        {
          id: row.id,
        },
        `${new Date().getTime()}.xlsx`
      );
    },
    unitNamenormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.orgNo,
        label: node.label,
      };
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    handleSearch(obj) {
      this.searchData = obj;
      this.pages.pageNo = 1;
      this.getPageData();
    },
    getPageData() {
      let searchData = JSON.parse(JSON.stringify(this.searchData));
      searchData.orgNo = this.orgNo;
      if (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,
      };
      adsMatPoiseOrderDpage(params).then((res) => {
        if (res && res.code == 200) {
          this.tableData = res.data.records || [];
          this.pages.total = res.data.total;
        }
      });
    },
  },
};
</script>

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