<script>
import { dicts } from "./dicts";
import useColumns from "./columns";
import { LIST, exportAll } from "@/api/business/purchase/catalogue";

export default {
  name: "PuchaseCatalogue",
  dicts: dicts,
  components: {
    SeeButton: () => import("./see/index.vue"),
    InvButton: () => import("./invalid/index.vue"),
    EnaButton: () => import("./enable/index.vue"),
    ExpButton: () => import("./export/index.vue"),
    ElSuperTable: () => import("@/components/super-table/index.vue"),
    ElSuperUxTable: () => import("@/components/super-ux-table/index.vue"),
    ElSuperSearch: () => import("@/components/super-search/index.vue"),
  },
  data() {
    const { TableColumns, SearchColumns } = useColumns();
    const params = this.$init.params(SearchColumns);
    return {
      size: "mini",
      loading: false,
      params: params,
      tableData: [],
      selectData: [],
      TableColumns: TableColumns,
      SearchColumns: SearchColumns,
      page: { pageNum: 1, pageSize: 50, total: 0 },
    };
  },
  computed: {},
  created() {
    this.useQuery(this.params, this.page);
  },
  methods: {
    //
    async fetchList(prop, page) {
      try {
        this.loading = true;
        const { pageNum, pageSize } = page;
        const { code, rows, total } = await LIST(
          { ...prop },
          { pageNum, pageSize }
        );
        if (code === 200) {
          this.tableData = rows.map((item, index) => ({
            ...item,
            $index: (pageNum - 1) * pageSize + index + 1,
          }));
          this.page.total = total;
        }
      } catch (err) {
        // catch
      } finally {
        // finally
        this.loading = false;
      }
    },
    // 查 询
    useQuery(prop, page) {
      const { invalid, createTime } = prop;
      const [invalidStart, invalidEnd] = invalid || [];
      const [createTimeStart, createTimeEnd] = createTime || [];
      this.fetchList(
        {
          ...prop,
          invalidStart,
          invalidEnd,
          createTimeStart,
          createTimeEnd,
          invalid: null,
          createTime: null,
        },
        page
      );
    },
    // 重 置
    useReset() {
      this.page.pageNum = 1;
      this.page.pageSize = 10;
      this.params = this.$init.params(this.SearchColumns);
      this.useQuery(this.params, this.page);
    },
    // 选 择
    useSelect(prop) {
      this.selectData = prop;
    },
    // 明 细
    async useSee(prop) {
      const { open } = this.$refs.SeeButton;
      await open([prop]);
    },
    // 导出全部
    exportAll() {
      this.$modal.loading("正在导出数据，请稍后...");
      exportAll(this.params)
        .then((res) => {
          this.$modal.closeLoading();
          const blob = new Blob([res], {
            type: "application/vnd.ms-excel;charset=UTF-8",
          }); // 创建一个类文件对象：Blob对象表示一个不可变的、原始数据的类文件对象
          const downloadElement = document.createElement("a"); //创建a标签
          const href = window.URL.createObjectURL(blob); // 创建下载的链接
          // var temp = res.headers["content-disposition"];
          // var fileName = decodeURIComponent(temp.split("filename=")[1]); // 中文需要转码 (前端乱码）
          // var name = fileName.split(";")[0]; //切割成文件名
          downloadElement.href = href; //下载地址
          downloadElement.download =
            "价格目录全部导出" + this.parseTime(new Date().getTime()) + ".xlsx";
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放blob对象
        })
        .catch((err) => {
          this.$modal.closeLoading();
        });
    },
  },
};
</script>

<template>
  <el-card
    v-loading="loading"
    :body-style="{
      height: '100%',
      padding: 0,
      display: 'flex',
      'flex-direction': 'column',
    }"
  >
    <el-super-search
      v-model="params"
      :size="size"
      :dict="dict"
      :columns="SearchColumns"
      @reset="useReset"
      @submit="useQuery(params, page)"
    ></el-super-search>
    <div class="my-4" style="text-align: right">
      <el-button-group>
        <ena-button
          :size="size"
          :select-data="selectData"
          status="Y"
          type="primary"
          @success="useQuery(params, page)"
        ></ena-button>
        <!-- <ena-button
          :size="size"
          :select-data="selectData"
          status="N"
          @success="useQuery(params, page)"
        ></ena-button> -->
      </el-button-group>
      <el-button-group>
        <inv-button
          ref="InvButton"
          :size="size"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></inv-button>
        <see-button
          v-show="false"
          ref="SeeButton"
          :size="size"
          :dict="dict"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></see-button
      ></el-button-group>
      <el-button-group>
        <exp-button
          :size="size"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></exp-button>
      </el-button-group>
      <el-button-group>
        <el-button size="mini" @click="exportAll">全部导出</el-button>
      </el-button-group>
    </div>
    <el-super-ux-table
      v-model="tableData"
      :size="size"
      :dict="dict"
      :page="page"
      :columns="TableColumns"
      index
      checkbox
      pagination
      convenitentOperation
      storage-key="PuchaseCatalogueSuperTable1"
      @row-dblclick="useSee"
      @selection-change="useSelect"
      @pagination="useQuery(params, page)"
    >
    </el-super-ux-table>
  </el-card>
</template>
<style scoped lang="scss">
.el-card {
  width: calc(100% - 32px);
  height: calc(100vh - 32px);
  margin: 16px;
  padding: 16px;
  border-radius: 8px;
}
.el-button-group + .el-button-group {
  margin: 0 0 0 10px;
}
</style>
