<template>
  <div class="purchasFuelNewMonitor">
    <div class="frameCalculation">
      <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" class="btn" @click="onclick">
        <i class="el-icon-search"></i>查询
      </el-button>
      <el-button type="primary" @click="exportToExcel('厂内内转装车统计')"><i class="el-icon-download"></i>导出(Excel)</el-button>   
      <span style="margin-left: 1rem;">合计车数：</span>
      <el-input v-model="totalCapacity" :disabled="true" style="width: 100px;"></el-input>
    </div>
    <div class="table">
      <dilTable id="excelDom" ref="excelDom" v-bind.sync="option" @func="func"></dilTable>
    </div>
  </div>
</template>

<script>
import { sjTime } from '@/utils/sharedJsFile';
export default {
  data() {
    return {   
      totalCapacity:null,
      option: {
        requestUrl: "",
      },
      startTime: null,
      endTime: null,
      tableTitle:'达钢装货统计报表'
    };
  }, 
  created(){
          this.option.requestUrl = '/api/v1/tms/getLoading?apiId=453&startTime=null&endTime=null&i=' +new Date()
  },
  methods: {
     exportToExcel(tableTitle="表格标题") {
        //创建工作簿对象
        let wb = XLSX.utils.book_new();
        //查询数据
        let map={isExcel:1};
        this.axios.post(this.option.requestUrl,map).then(res=>{
          let columnData=res.data.data.columnData;
          let data=[];
          if(!res.data.data.list || res.data.data.list.length<=0){
            this.$message({
              message:"请先查询，再导出!",
              type:"warning",
              duration:1000
            });
            return;
          }else{
            //替换表头
            console.log("columnData",columnData);
            res.data.data.list.forEach((item,index)=>{
              let temp={};
              columnData.forEach((col)=>{
                temp[col.label] = item[col.prop];
              });
              data.push(temp);
            });
            console.log("data",data);
            //将json数组转换成sheet
            let table_sheet = XLSX.utils.json_to_sheet(data);
            //为工作簿添加sheet
            XLSX.utils.book_append_sheet(wb, table_sheet, "第一页");
            //导出
            XLSX.writeFile(wb, tableTitle+'.xlsx');
          }
        });
    },
    func(res){
        console.log(res.list)     
        var currentCapacityTotal = 0
        res.list.forEach(e => {
          currentCapacityTotal = currentCapacityTotal+e.capacityNumber       
        });
        this.totalCapacity = currentCapacityTotal;
    },
    getRequestUrl(){
        this.option.requestUrl = '/api/v1/tms/getLoading?apiId=453&startTime=null&endTime=null&i=' +new Date();
     
    },
    onclick() {
      let startTime = null;
      let endTime = null;
      if(this.startTime){
        startTime = sjTime(this.startTime);
      }
      if(this.endTime){
        endTime = sjTime(this.endTime);
      }
      if(startTime && endTime){
        if(startTime < endTime){
          this.option.requestUrl = '/api/v1/tms/getLoading?apiId=453' + "&startTime=" + startTime + "&endTime=" + endTime + "&i=" +new Date(); 
        }else{
          this.startTime = null;
          this.endTime = null;
          this.$message.warning('开始时间要比结束时间早')
        }
      }else{
        console.log(startTime && endTime)
        this.getRequestUrl()
      }
    },
  },
};
</script>

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