<template>
  <!-- 万州港-达州计量委托界面 -->
  <div class="trainTransport">
    <div class="top">
      <el-input
        class="el-input"
        placeholder="请输入内容"
        v-model="input"
        clearable
      >
      </el-input>
      <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="exportExcel">
        <i class="el-icon-download"></i>导出(Excel)
      </el-button>
       <el-button type="primary" @click="refresh()">
        <i class="el-icon-refresh"></i>刷新
      </el-button>
    </div>

    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="待计量" name="first">
        <dilTable
          v-bind.sync="option"
          @selection-change="selectionChange"
        >
        </dilTable>
      </el-tab-pane>
      <el-tab-pane label="已计量" name="second">
        <dilTable
          ref="weighted"
          v-bind.sync="option2"
          @selection-change="selectionChange2"
        >
        </dilTable>
      </el-tab-pane>
    </el-tabs>
    <div class="count" v-show="sum1>0 && activeName == 'second'" style="margin-right:50px;font-size:18px;">
      <div style="display:flex;flex-direction:row-reverse">标重总吨位：{{sum1}}</div>
      <div style="display:flex;flex-direction:row-reverse">净重总吨位：{{sum2}}</div>
    </div>
  </div>
</template>
<script>
import { sjTime } from "@/utils/sharedJsFile";
import bignumber,{BigNumber} from 'bignumber.js';
import { XLSX$Utils } from 'xlsx';
export default {
  data() {
    return {
      activeName: "first",
      time: [],
      input: "",
      option: {
        // 表格请求数据的地址
        requestUrl: "/api/v1/tms/getLoadResultToSendMC?apiId=491&resultType=3",
        // 控制显示多选列
        selectionType: "select",
      },
      option2: {
        // 表格请求数据的地址
        requestUrl: "/api/v1/tms/getWeightResult?apiId=491&resultType=3",
        // 控制显示多选列
        selectionType: "select",
      },
      selectionListMap: [],
      selectionListMap2: [],
      resultIdList: [],
      startTime: null,
      endTime: null,
      tableTitle: "国产矿计量表",
      sum1:0.0,
      sum2:0.0,
    };
  },
  methods: {
    handleClick(tag, event) {
      console.log(tag, event);
    },

    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) {
          //判断是否是承运商
          if (this.activeName == "first") {
            this.option.requestUrl =
              "/api/v1/tms/getLoadResultToSendMC?apiId=491&resultType=3" +
              "&con=" +
              this.input +
              "&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&i=" +
              new Date();
          } else {
            this.option2.requestUrl =
              "/api/v1/tms/getWeightResult?apiId=491&resultType=3" +
              "&con=" +
              this.input +
              "&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&i=" +
              new Date();
          }
        }
      } else {
        if (this.activeName == "first") {
          this.option.requestUrl =
            "/api/v1/tms/getLoadResultToSendMC?apiId=491&resultType=3" +
            "&con=" +
            this.input +
            "&i=" +
            new Date();
        } else {
          this.option2.requestUrl =
            "/api/v1/tms/getWeightResult?apiId=491&resultType=3" +
            "&con=" +
            this.input +
            "&i=" +
            new Date();
        }
      }
    },
    selectionChange(selection) {
      this.selectionListMap = [];
      console.log(selection);
      this.selectionListMap = selection;
    },
    selectionChange2(selection) {
      let that=this;
      this.selectionListMap2 = [];
      console.log(selection);
      this.selectionListMap2 = selection;
      let sum1 = 0;
      let sum2 = new BigNumber(0);
      selection.forEach(element => {
        sum1+=element.resultBillableTonnage;
        sum2=sum2.plus(element.resultNetWeight);
      });
      this.sum1=sum1;
      this.sum2=sum2.toNumber();
    },
    refresh(){
      this.$router.go(0);
    },
    exportExcel(){
      let excel=this.$refs.excelDom;
      console.log(XLSX);
      console.log(XLSX.version);
      if(this.selectionListMap2.length<=0){
         this.$message({
          message: "请先勾选数据!",
          type: "warning"
        });
        return;
      };
      //创建工作簿对象
      let wb = XLSX.utils.book_new();
      //数据
      let sheetData = [
        ['卸车明细表'],
        ['序号','发站','到站','发货日期','到货日期','卸货地点','品名','矿种','计费吨位','车号','计量吨位','计费吨位合计']
      ];
      let totalRealWeight=new bignumber(0);
      let totalWeight=0;
      this.selectionListMap2.forEach((selection)=>{
          totalRealWeight=totalRealWeight.plus(selection.resultNetWeight);
          totalWeight+=selection.resultBillableTonnage;
          let arr=[
            selection.ROW_ID,
            selection.sendStationName,
            selection.arrivalStationName,
            selection.sendDate,
            selection.arrivalDate,
            selection.resultForeignShipName,
            selection.supplierName+selection.materialName,
            null,
            selection.resultBillableTonnage,
            selection.resultWagonNo,
          ]
          sheetData.push(arr);
      });
      console.log("totalReal：",totalRealWeight.toNumber());
      console.log("total：",totalWeight);
      sheetData[2].push(totalRealWeight.toNumber());
      sheetData[2].push(totalWeight);
      console.log("sheetData",sheetData);
      //把数据转化成excel的页
      let sheet = XLSX.utils.aoa_to_sheet(sheetData);
      //合并单元格 
      sheet['!merges'] = [
          {
              s: { c: 0, r: 0 },// 合并开始位置 c:列位置 r:表示行位置
              e: { c: 11, r: 0 } // 合并结束位置 
          },
          {
              s: { c: 10, r: 2 },// 合并开始位置 c:列位置 r:表示行位置
              e: { c: 10, r: sheetData.length-1 } // 合并结束位置 
          },
          {
              s: { c: 11, r: 2 },// 合并开始位置 c:列位置 r:表示行位置
              e: { c: 11, r: sheetData.length-1 } // 合并结束位置 
          }
      ]
      //把这一页加入excel
      XLSX.utils.book_append_sheet(wb, sheet, "第一页");
      XLSX.writeFile(wb, '计量作业.xlsx');
    }
  },
};
</script>
<style lang='scss'>
.trainTransport {
  .top {
    padding: 1.25rem 0.375rem;
    .el-input {
      width: 20%;
      margin-right: 1.25rem;
    }
  }
}
</style>