<template>
  <div class="allInwardReport">
    <div class="top">
      <el-input v-model="input" placeholder="请输入条件查询"></el-input>
      <span>净重时间：</span>
      <el-date-picker
        v-model="startTime"
        type="datetime"
        placeholder="选择日期"
      >
      </el-date-picker>
      <span>至</span>
      <el-date-picker v-model="endTime" type="datetime" placeholder="选择日期">
      </el-date-picker>
      <el-button type="primary" @click="onClick">查询</el-button>
      <el-button type="primary" @click="exportSignleReport">单个导出</el-button>
      <el-button type="primary" @click="exportAllReport">导出全部</el-button>
    </div>
    <div class="table">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="纯内转报表" name="first">
          <dilTable ref="excelDom" v-bind.sync="first" @func="func"></dilTable>
        </el-tab-pane>
        <el-tab-pane label="采购内转报表" name="second">
          <dilTable ref="excelDom" v-bind.sync="second"></dilTable>
        </el-tab-pane>
        <el-tab-pane label="销售内转报表" name="third"
          ><dilTable ref="excelDom" v-bind.sync="third"></dilTable
        ></el-tab-pane>
        <el-tab-pane label="采购燃料报表" name="fourth"
          ><dilTable ref="excelDom" v-bind.sync="fourth"></dilTable
        ></el-tab-pane>
        <el-tab-pane label="厂外-厂内内转报表" name="fifth"
          ><dilTable ref="excelDom" v-bind.sync="fifth"></dilTable
        ></el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>
<script>
import { sjTime } from "@/utils/sharedJsFile";
import { getCookie } from "@/utils/util.js";
export default {
  data() {
    return {
      //导出excel标题
      tableTitle: null,
      //报表所有数据
      totalArr: [],
      //纯内转数据
      inwardArr: [],
      //采购内转数据
      purInward: [],
      //销售内转数据
      saleReport: [],
      purReport: [],
      outFactory: [],
      input: null,
      activeName: "first",
      startTime: null,
      endTime: null,
      first: {
        requestUrl: ""
      },
      second: {
        requestUrl: ""
      },
      third: {
        requestUrl: ""
      },
      fourth: {
        requestUrl: ""
      },
      fifth: {
        requestUrl: ""
      }
    };
  },
  created() {
    this.getRequestUrl();
  },
  methods: {
    func(res) {
      this.dataColumnData = res.columnData;
    },
    exportSignleReport() {
      if (this.startTime && this.endTime) {
        this.openFullScreen1();
      } else {
        this.$confirm(
          "没有输入时间区间，系统将默认导出今天0点到24点的单个报表数据,是否继续?",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        )
          .then(() => {
            this.openFullScreen1();
          })
          .catch(() => {
            this.$message.info("取消导出");
          });
      }
    },
    exportAllReport() {
      if (this.startTime && this.endTime) {
        this.openFullScreen();
      } else {
        this.$confirm(
          "没有输入时间区间，系统将默认导出今天0点到24点的全部报表数据,是否继续?",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        )
          .then(() => {
            this.openFullScreen();
          })
          .catch(() => {
            this.$message.info("取消导出");
          });
      }
    },
    openFullScreen1() {
      let startTime = null;
      let endTime = null;
      let orgCode = null;
      if (this.startTime && this.endTime) {
        startTime = sjTime(this.startTime);
        endTime = sjTime(this.endTime);
      }
      if (
        getCookie("orgCode") != "dagangadmin" &&
        getCookie("orgCode") != "zidonghuabu" &&
        getCookie("orgCode") != "wuliuchuyunzhongxin"
      ) {
        orgCode = getCookie("orgCode");
      }
      const loading = this.$loading({
        lock: true,
        text: "请等待导出",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      if (this.activeName == "first") {
        this.axios
          .post(
            "/api/v1/tms/getInwardInFactory?apiId=486&isPage=yes&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&index=" +
              this.input +
              "&orgCode=" +
              orgCode
          )
          .then(res => {
            this.totalArr = [];
            this.inwardArr = [];
            this.tableTitle = "纯内转统计报表";
            this.inwardArr = res.data.data;
            this.totalArr = this.totalArr.concat(this.inwardArr);
            this.exportAllReportToExcel();
            setTimeout(() => {
              loading.close();
            }, 2000);
          });
      } else if (this.activeName == "second") {
        this.axios
          .post(
            "/api/v1/tms/getPurchaseInwardList?apiId=486&isPage=yes&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&index=" +
              this.input +
              "&orgCode=" +
              orgCode
          )
          .then(res => {
            this.totalArr = [];
            this.purInward = [];
            this.tableTitle = "采购内转统计报表";
            this.purInward = res.data.data;
            this.totalArr = this.totalArr.concat(this.purInward);
            this.exportAllReportToExcel();
            setTimeout(() => {
              loading.close();
            }, 2000);
          });
      } else if (this.activeName == "third") {
        this.axios
          .post(
            "/api/v1/tms/getSaleOrderList?apiId=486&isPage=yes&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&index=" +
              this.input +
              "&orgCode=" +
              orgCode
          )
          .then(res => {
            thia.totalArr = [];
            this.saleReport = [];
            this.tableTitle = "销售内转统计报表";
            this.saleReport = res.data.data;
            this.totalArr = this.totalArr.concat(this.saleReport);
            this.exportAllReportToExcel();
            setTimeout(() => {
              loading.close();
            }, 2000);
          });
      } else if (this.activeName == "fourth") {
        this.axios
          .post(
            "/api/v1/tms/getPurchaseOrderList?apiId=486&isPage=yes&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&index=" +
              this.input +
              "&orgCode=" +
              orgCode
          )
          .then(res => {
            this.totalArr = [];
            this.purReport = [];
            this.tableTitle = "采购燃料统计报表";
            this.purReport = res.data.data;
            this.totalArr = this.totalArr.concat(this.purReport);
            this.exportAllReportToExcel();
            setTimeout(() => {
              loading.close();
            }, 2000);
          });
      } else if (this.activeName == "fifth") {
        this.axios
          .post(
            "/api/v1/tms/getOutFactoryInwardList?apiId=486&isPage=yes&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&index=" +
              this.input +
              "&orgCode=" +
              orgCode
          )
          .then(res => {
            this.totalArr = [];
            this.outFactory = [];
            this.tableTitle = "厂外-厂内统计报表";
            this.outFactory = res.data.data;
            this.totalArr = this.totalArr.concat(this.outFactory);
            this.exportAllReportToExcel();
            setTimeout(() => {
              loading.close();
            }, 2000);
          });
      }
    },
    openFullScreen() {
      let startTime = null;
      let endTime = null;
      this.totalArr = [];
      if (this.startTime && this.endTime) {
        startTime = sjTime(this.startTime);
        endTime = sjTime(this.endTime);
      }
      const loading = this.$loading({
        lock: true,
        text: "请等待导出",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      if (
        getCookie("orgCode") == "dagangadmin" ||
        getCookie("orgCode") == "zidonghuabu" ||
        getCookie("orgCode") == "wuliuchuyunzhongxin"
      ) {
        this.axios
          .post(
            "/api/v1/tms/getInwardInFactory?apiId=486&orgCode=null&isPage=yes&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&index=" +
              this.input
          )
          .then(res => {
            this.inwardArr = [];
            this.inwardArr = res.data.data;
            this.totalArr = this.totalArr.concat(this.inwardArr);
            this.axios
              .post(
                "/api/v1/tms/getPurchaseInwardList?apiId=486&orgCode=null&isPage=yes&startTime=" +
                  startTime +
                  "&endTime=" +
                  endTime +
                  "&index=" +
                  this.input +
                  "&i=" +
                  new Date()
              )
              .then(res => {
                this.purInward = [];
                this.purInward = res.data.data;
                this.totalArr = this.totalArr.concat(this.purInward);
                this.axios
                  .post(
                    "/api/v1/tms/getSaleOrderList?apiId=486&orgCode=null&isPage=yes&startTime=" +
                      startTime +
                      "&endTime=" +
                      endTime +
                      "&index=" +
                      this.input +
                      "&i=" +
                      new Date()
                  )
                  .then(res => {
                    this.saleReport = [];
                    this.saleReport = res.data.data;
                    this.totalArr = this.totalArr.concat(this.saleReport);
                    this.axios
                      .post(
                        "/api/v1/tms/getPurchaseOrderList?apiId=486&orgCode=null&isPage=yes&startTime=" +
                          startTime +
                          "&endTime=" +
                          endTime +
                          "&index=" +
                          this.input +
                          "&i=" +
                          new Date()
                      )
                      .then(res => {
                        this.purReport = [];
                        this.purReport = res.data.data;
                        this.totalArr = this.totalArr.concat(this.purReport);
                        this.axios
                          .post(
                            "/api/v1/tms/getOutFactoryInwardList?apiId=486&orgCode=null&isPage=yes&startTime=" +
                              startTime +
                              "&endTime=" +
                              endTime +
                              "&index=" +
                              this.input +
                              "&i=" +
                              new Date()
                          )
                          .then(res => {
                            this.outFactory = [];
                            this.outFactory = res.data.data;
                            this.totalArr = this.totalArr.concat(
                              this.outFactory
                            );
                            this.tableTitle = "全部内转车辆报表";
                            this.exportAllReportToExcel();
                            setTimeout(() => {
                              loading.close();
                            }, 2000);
                          });
                      });
                  });
              });
          });
      } else {
        this.axios
          .post(
            "/api/v1/tms/getInwardInFactory?apiId=486&isPage=yes&orgCode=" +
              getCookie("orgCode") +
              "&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&index=" +
              this.input
          )
          .then(res => {
            this.inwardArr = [];
            this.inwardArr = res.data.data;
            this.totalArr = this.totalArr.concat(this.inwardArr);
            this.axios
              .post(
                "/api/v1/tms/getPurchaseInwardList?apiId=486&isPage=yes&orgCode=" +
                  getCookie("orgCode") +
                  "&startTime=" +
                  startTime +
                  "&endTime=" +
                  endTime +
                  "&index=" +
                  this.input
              )
              .then(res => {
                this.purInward = [];
                this.purInward = res.data.data;
                this.totalArr = this.totalArr.concat(this.purInward);
                this.axios
                  .post(
                    "/api/v1/tms/getSaleOrderList?apiId=486&isPage=yes&orgCode=" +
                      getCookie("orgCode") +
                      "&startTime=" +
                      startTime +
                      "&endTime=" +
                      endTime +
                      "&index=" +
                      this.input
                  )
                  .then(res => {
                    this.saleReport = [];
                    this.saleReport = res.data.data;
                    this.totalArr = this.totalArr.concat(this.saleReport);
                    this.axios
                      .post(
                        "/api/v1/tms/getPurchaseOrderList?apiId=486&isPage=yes&orgCode=" +
                          getCookie("orgCode") +
                          "&startTime=" +
                          startTime +
                          "&endTime=" +
                          endTime +
                          "&index=" +
                          this.input
                      )
                      .then(res => {
                        this.purReport = [];
                        this.purReport = res.data.data;
                        this.totalArr = this.totalArr.concat(this.purReport);
                        this.axios
                          .post(
                            "/api/v1/tms/getOutFactoryInwardList?apiId=486&isPage=yes&orgCode=" +
                              getCookie("orgCode") +
                              "&startTime=" +
                              startTime +
                              "&endTime=" +
                              endTime +
                              "&index=" +
                              this.input
                          )
                          .then(res => {
                            this.outFactory = [];
                            this.outFactory = res.data.data;
                            this.totalArr = this.totalArr.concat(
                              this.outFactory
                            );
                            this.tableTitle = "全部内转车辆报表";
                            this.exportAllReportToExcel();
                            setTimeout(() => {
                              loading.close();
                            }, 2000);
                          });
                      });
                  });
              });
          });
      }
    },
    //导出excel
    exportAllReportToExcel() {
      var title = this.tableTitle;
      let tHeader = [];
      let filterVal = [];
      this.dataColumnData.forEach(e1 => {
        if (tHeader.indexOf(e1.label) === -1) {
          tHeader.push(e1.label);
        }
        if (filterVal.indexOf(e1.prop) === -1) {
          filterVal.push(e1.prop);
        }
      });
      //导出为excel
      this.downloadLoading = true;
      require.ensure([], () => {
        const {
          export_json_to_excel
        } = require("@/assets/excel/Export2Excel.js"); //这里必须使用绝对路径，使用@/+存放export2Excel的路径
        // let list = this.$refs.excelDom.dataTabel;
        let data = this.totalArr.map(v => filterVal.map(j => v[j])); //3.formatJson格式转换
        export_json_to_excel(tHeader, data, title); // (title)导出的表格名称
      });
    },
    getRequestUrl() {
      if (
        getCookie("orgCode") == "dagangadmin" ||
        getCookie("orgCode") == "zidonghuabu" ||
        getCookie("orgCode") == "wuliuchuyunzhongxin"
      ) {
        this.first.requestUrl =
          "/api/v1/tms/getInwardInFactory?apiId=486&orgCode=" +
          null +
          "&i=" +
          new Date();
        this.second.requestUrl =
          "/api/v1/tms/getPurchaseInwardList?apiId=486&orgCode=" +
          null +
          "&i=" +
          new Date();
        this.third.requestUrl =
          "/api/v1/tms/getSaleOrderList?apiId=486&orgCode=" +
          null +
          "&i=" +
          new Date();
        this.fourth.requestUrl =
          "/api/v1/tms/getPurchaseOrderList?apiId=486&orgCode=" +
          null +
          "&i=" +
          new Date();
        this.fifth.requestUrl =
          "/api/v1/tms/getOutFactoryInwardList?apiId=486&orgCode=" +
          null +
          "&i=" +
          new Date();
      } else {
        this.first.requestUrl =
          "/api/v1/tms/getInwardInFactory?apiId=486&orgCode=" +
          getCookie("orgCode") +
          "&i=" +
          new Date();
        this.second.requestUrl =
          "/api/v1/tms/getPurchaseInwardList?apiId=486&orgCode=" +
          getCookie("orgCode") +
          "&i=" +
          new Date();
        this.third.requestUrl =
          "/api/v1/tms/getSaleOrderList?apiId=486&orgCode=" +
          getCookie("orgCode") +
          "&i=" +
          new Date();
        this.fourth.requestUrl =
          "/api/v1/tms/getPurchaseOrderList?apiId=486&orgCode=" +
          getCookie("orgCode") +
          "&i=" +
          new Date();
        this.fifth.requestUrl =
          "/api/v1/tms/getOutFactoryInwardList?apiId=486&orgCode=" +
          getCookie("orgCode") +
          "&i=" +
          new Date();
      }
    },
    onClick() {
      console.log("index", this.input);
      let startTime = null;
      let endTime = null;
      if (this.startTime && this.endTime) {
        startTime = sjTime(this.startTime);
        endTime = sjTime(this.endTime);
      }
      if (
        getCookie("orgCode") == "dagangadmin" ||
        getCookie("orgCode") == "zidonghuabu" ||
        getCookie("orgCode") == "wuliuchuyunzhongxin"
      ) {
        if (this.activeName == "first") {
          this.first.requestUrl =
            "/api/v1/tms/getInwardInFactory?apiId=486&orgCode=" +
            null +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&index=" +
            this.input +
            "&i=" +
            new Date();
        } else if (this.activeName == "second") {
          this.second.requestUrl =
            "/api/v1/tms/getPurchaseInwardList?apiId=486&orgCode=" +
            null +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&index=" +
            this.input +
            "&i=" +
            new Date();
        } else if (this.activeName == "third") {
          this.third.requestUrl =
            "/api/v1/tms/getSaleOrderList?apiId=486&orgCode=" +
            null +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&index=" +
            this.input +
            "&i=" +
            new Date();
        } else if (this.activeName == "fourth") {
          this.fourth.requestUrl =
            "/api/v1/tms/getPurchaseOrderList?apiId=486&orgCode=" +
            null +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&index=" +
            this.input +
            "&i=" +
            new Date();
        } else if (this.activeName == "fifth") {
          this.fifth.requestUrl =
            "/api/v1/tms/getOutFactoryInwardList?apiId=486&orgCode=" +
            null +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&index=" +
            this.input +
            "&i=" +
            new Date();
        }
      } else {
        if (this.activeName == "first") {
          this.first.requestUrl =
            "/api/v1/tms/getInwardInFactory?apiId=486&orgCode=" +
            getCookie("orgCode") +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&index=" +
            this.input +
            "&i=" +
            new Date();
        } else if (this.activeName == "second") {
          this.second.requestUrl =
            "/api/v1/tms/getPurchaseInwardList?apiId=486&orgCode=" +
            getCookie("orgCode") +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&index=" +
            this.input +
            "&i=" +
            new Date();
        } else if (this.activeName == "third") {
          this.third.requestUrl =
            "/api/v1/tms/getSaleOrderList?apiId=486&orgCode=" +
            getCookie("orgCode") +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&index=" +
            this.input +
            "&i=" +
            new Date();
        } else if (thia.activeName == "fourth") {
          this.fourth.requestUrl =
            "/api/v1/tms/getPurchaseOrderList?apiId=486&orgCode=" +
            getCookie("orgCode") +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&index=" +
            this.input +
            "&i=" +
            new Date();
        } else if (thia.activeName == "fifth") {
          this.fifth.requestUrl =
            "/api/v1/tms/getOutFactoryInwardList?apiId=486&orgCode=" +
            getCookie("orgCode") +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&index=" +
            this.input +
            "&i=" +
            new Date();
        }
      }
    },
    handleClick(tab, event) {
      this.getRequestUrl();
    }
  }
};
</script>
<style lang="scss" scoped>
.allInwardReport {
  .top {
    margin-left: 20px;
    margin-top: 20px;
    .el-input {
      width: 250px;
    }
  }
  .table {
    margin-left: 20px;
    margin-top: 20px;
  }
}
</style>
