<template>
  <div>
    <exTableNew
      v-loading="loading"
      :table="table"
      :data="tableData"
      :columns="table1"
      :size="size"
      @screen-reset="searchList()"
      @screen-submit="searchList()"
      @selection="selection_change"
    >
      <template #table-header="{}">
        <div style="width: 100%">
          <el-row style="padding: 0 0 0 80px">
            <el-col :span="6" style="width: 150px">
              <el-date-picker
                v-model="parmValue.date"
                type="date"
                :size="searchSize"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                @change="searchList()"
              >
              </el-date-picker>
            </el-col>

            <el-col :span="4" style="width: 66px; float: right">
              <el-button
                type="warning"
                class="fr"
                :size="searchSize"
                @click="restSearch"
              >
                重置
              </el-button>
            </el-col>
            <el-col :span="4" style="width: 66px; float: right">
              <el-button
                type="primary"
                style="margin-left: 30px"
                @click="download"
                :size="searchSize"
                class="fr"
              >
                导出
              </el-button>
            </el-col>
            <el-col :span="3" style="width: 66px; float: right">
              <el-button
                :size="searchSize"
                type="primary"
                style="float: right; margin-left: 5px"
                @click="searchList"
              >
                刷新
              </el-button>
            </el-col>
          </el-row>
        </div>
      </template>
    </exTableNew>

  </div>
</template>
   <script>
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import urlConfig from "@/apis/url-config";
import asyncRequest from "@/apis/service/reportQuery/saleReport";
import periodDatePickerActive from "../period-date-picker/main.vue";
import exTableNew from "../ex-table/main.vue";
import { table1 } from "./columns";
import { mapGetters } from "vuex";

export default {
  name: "saleReport",
  mixins: [mixinPage, resToken],
  components: {
    periodDatePickerActive,
    exTableNew,
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
  },
  data() {
    return {
      //选中数组
      changeList: [],
      //全局url
      fileUrl: urlConfig.baseURL,
      //loading
      loading: false,
      //请求参数集合
      parmValue: {
        date: null, //筛选日期
      },
      // 表格 - 数据集合
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      // pageInfo: {
      //   size: 15,
      //   curr: 1,
      //   total: 0,
      // },
      // 表格表头 - 列参数
      table1: table1,
    };
  },
  mounted() {
    this.searchList();
  },

  methods: {
    //初始化http请求
    async searchList() {
      // return;
      this.loading = true;

      const res = await asyncRequest.saleexportconsultday(this.parmValue);
      if (res && res.code === 0 && res.data) {
        console.log(res);
        this.tableData = res.data.list;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.$message.warning(res.message);
      }
      this.loading = false;
    },

    //重置
    restSearch() {
      this.parmValue = {
        date: "", //筛选日期
      };
      // 表格 - 分页
      // this.pageInfo = {
      //   size: 15,
      //   curr: 1,
      //   total: 0,
      // };
      this.searchList();
    },

    //选中触发函数
    selection_change(e) {
      const { list } = e;
      //选中的数组集合
      this.changeList = list.length > 0 ? JSON.parse(JSON.stringify(list)) : [];
    },
    //导出文件
    async download() {
       if ((this.parmValue.date ?? "") === "") {
         this.$message.warning("请选择导出文件的日期");
        return;
      }
      let model = JSON.parse(JSON.stringify(this.parmValue));
      if (!this.loading) {
        this.loading = true;
        let httpType = `aplication/zip`;
        axios({
          method: "post",
          url: this.fileUrl + "admin/saleexportconsultdayexport",
          responseType: "blob",
          data: model,
          headers: {
            // 'Content-Type': 'multipart/form-data',
            // Accept: "application/vnd.ms-excel"
            Accept: httpType,
          },
        })
          .then((res) => {
            // console.log(res)
            // console.log(this.fileUrl)
            // return;
            if (res && res.status == 200 && res.data) {
              let url = window.URL.createObjectURL(
                new Blob([res.data], {
                  // type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8",
                  type: httpType,
                })
              );
              let link = document.createElement("a");
              link.style.display = "none";
              link.href = url;
              let excelName = "日报.zip";
              link.setAttribute("download", excelName);
              document.body.appendChild(link);
              link.click();
              link.remove();
              window.URL.revokeObjectURL(url); //释放掉blob对象
              this.$message.success(`导出成功！`);
              setTimeout(() => {
                this.loading = false;
              }, 500);
            } else {
              this.$message.error(res.data.message);
              setTimeout(() => {
                this.loading = false;
              }, 500);
            }
          })
          .catch((error) => {
            console.log(error);
            this.loading = false;
          });
      }
    },
  },
};
</script>
   <style lang="scss" scoped>
.purchaseOrder {
  // text-align: right;
}
</style>
   