<template>
  <div class="purchasFuelNewMonitor">
    <div class="frameCalculation">
      <span style="width:68px">计毛时间：</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" class="btn" @click="onclick">
        <i class="el-icon-search"></i>查询
      </el-button>
      <el-button type="primary" @click="exportData()"><i class="el-icon-download"></i>导出(Excel)</el-button>
      <el-button type="primary" @click="refresh">
        <i class="el-icon-refresh"></i>刷新
      </el-button>
      <span style="margin-left: 1rem;width:68px">合计净重：</span>
      <el-input v-model="totalNumber" :disabled="true" style="width: 150px;"></el-input>
      <span style="margin-left: 1rem;width:68px">合计车数：</span>
      <el-input v-model="totalCapacity" :disabled="true" style="width: 150px;"></el-input>
    </div>
    <div class="table">
      <dilTable ref="excelDom" v-bind.sync="option" :isHeigth="isHeigth" :shiyHeigth="shiyHeigth" :isKuang="isKuang" @func="func" @requestQuery="requestQuery" :pageSize = "pageSize"></dilTable>
    </div>
  </div>
</template>

<script>
import { sjTime } from '@/utils/sharedJsFile'
import { getCookie } from "@/utils/util.js";
export default {
  data() {
    return {
      shiyHeigth:140,
      isHeigth:true,
      pageSize:100,
      isKuang:false,
      option: {
        requestUrl: "",
      },
      startTime:null,
      endTime:null,
      //合计净重
      totalNumber: 0,
      //合计车数
      totalCapacity: 0,
      tableTitle:'销售统计报表'
    };
  },
  created(){
    //初始化表头
    if (getCookie("orgCode") == "chengyunshang") {
      this.option.requestUrl =
        "/api/v1/tms/getAllSaleReport?apiId=423&startTime=null&endTime=null&carrierSsoId=" + getCookie("userId") +"&i=" +new Date();
    } else {
      this.option.requestUrl =
        "/api/v1/tms/getAllSaleReport?apiId=423&startTime=null&endTime=null&carrierSsoId=" + null +"&i=" +new Date();
    }
    //时间段初始化
    const start = new Date(new Date(new Date().toLocaleDateString()).getTime());
    var startTime = this.dataFormat(sjTime(start));
    var endTime = this.dataFormat(sjTime(new Date()));
    this.tableTitle = startTime + ' 至 ' + endTime;
  },
  methods: {
    refresh(){
      this.$router.go(0);
    },
    func(res){
      console.log(res)
      var resultNetWeightTotal = 0
      res.list.forEach(e => {
        resultNetWeightTotal = resultNetWeightTotal+e.resultNetWeight
      });
      this.totalNumber = resultNetWeightTotal.toFixed(2) + " t"
      this.totalCapacity = res.total;
      //获取总记录条数作为合计车数
      // this.totalCapacity = res.total;
      this.isKuang = false;
    },
    onclick() {
      let startTime = null;
      let endTime = null;
      if(this.startTime && this.endTime){
        startTime = sjTime(this.startTime);
        endTime = sjTime(this.endTime);
        //修改报表名称
        this.tableTitle = this.dataFormat(startTime) + ' 至 ' + this.dataFormat(endTime);
        //加上时间重置表头
        if (getCookie("orgCode") == "chengyunshang") {
          this.option.requestUrl =
            "/api/v1/tms/getAllSaleReport?apiId=423&startTime=" + startTime + "&endTime=" + endTime + "&carrierSsoId=" + getCookie("userId") +"&i=" +new Date();
        } else {
          this.option.requestUrl =
            "/api/v1/tms/getAllSaleReport?apiId=423&startTime=" + startTime + "&endTime=" + endTime + "&carrierSsoId=" + null +"&i=" +new Date();
        }
      }else{
        if(this.startTime){
          startTime = sjTime(this.startTime);
        }else if(this.endTime){
          endTime = sjTime(this.endTime);
        }
        //加上时间重置表头
        if (getCookie("orgCode") == "chengyunshang") {
          this.option.requestUrl =
            "/api/v1/tms/getAllSaleReport?apiId=423&startTime=" + startTime + "&endTime=" + endTime + "&carrierSsoId=" + getCookie("userId") +"&i=" +new Date();
        } else {
          this.option.requestUrl =
            "/api/v1/tms/getAllSaleReport?apiId=423&startTime=" + startTime + "&endTime=" + endTime + "&carrierSsoId=" + null +"&i=" +new Date();
        }
      }
    },
    //格式化日期
    dataFormat(value){
      let date = new Date(value);
        let y = date.getFullYear();
        let MM = date.getMonth() + 1;
        MM = MM < 10 ? ('0' + MM) : MM;
        let d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        let h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        let m = date.getMinutes();
        m = m < 10 ? ('0' + m) : m;
        let s = date.getSeconds();
        s = s < 10 ? ('0' + s) : s;
        return y + '-' + MM + '-' + d + ' ' + h + '：' + m + '：' + s;
    }

  },
};
</script>

<style lang="scss">
.purchasFuelNewMonitor {
  .frameCalculation {
    width: 100%;
    height: 100px;
    display: flex;
    align-items: center;
    padding-left: 50px;
    .el-date-editor{
      margin: 20px;
    }
  }
}
</style>