<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" class="btn" @click="batchSendMeasureCommission" v-if="activeName == 'first'">
        重发计量委托
      </el-button>
      <el-button type="primary" @click="exportData()"
      v-if="activeName == 'second'"
        ><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  ref="excelDom" v-bind.sync="option" @selection-change="selectionChange">
        </dilTable>
      </el-tab-pane>
      <el-tab-pane label="已计量" name="second">
        <dilTable ref="excelDom" 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';
export default {
  data() {
    return {
      activeName: "first",
      time: [],
      input: "",
      option: {
        // 表格请求数据的地址
        requestUrl: "/api/v1/tms/getLoadResultToSendMC?apiId=491&resultType=2",
        // 控制显示多选列
        selectionType: "select",
      },
      option2: {
        // 表格请求数据的地址
        requestUrl: "/api/v1/tms/getWeightResult?apiId=491&resultType=2",
        // 控制显示多选列
        selectionType: "select",
      },
      selectionListMap: [],
      selectionListMap2: [],
      resultIdList: [],
      startTime: null,
      endTime: null,
      tableTitle: "海港-老区轨道衡计量表",
    };
  },
  methods: {
    handleClick(tag, event) {
      console.log(tag, event);
    },
    batchSendMeasureCommission(){
      if(!this.selectionListMap || this.selectionListMap.length<=0){
        this.$message.warning("请先勾选！");
        return;
      }
      this.$confirm('确定重新发送计量委托？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            let map={
              resultIdList:this.selectionListMap
            }
            this.axios
              .post('/api/v1/tms/batchSendMeasureCommission',map)
              .then(res => {
                this.$message.success(res.data.data);
            })
        }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });          
        });
    },
    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=2" +
              "&con=" +
              this.input +
              "&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&i=" +
              new Date();
          } else {
            this.option2.requestUrl =
              "/api/v1/tms/getWeightResult?apiId=491&resultType=2" +
              "&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=2" +
            "&con=" +
            this.input +
            "&i=" +
            new Date();
        } else {
          this.option2.requestUrl =
            "/api/v1/tms/getWeightResult?apiId=491&resultType=2" +
            "&con=" +
            this.input +
            "&i=" +
            new Date();
        }
      }
    },
    selectionChange(selection) {
      this.selectionListMap = [];
      console.log(selection);
      this.selectionListMap = selection;
    },
    selectionChange2(selection) {
      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);
    }
  },
};
</script>
<style lang='scss'>
.trainTransport {
  .top {
    padding: 1.25rem 0.375rem;
    .el-input {
      width: 20%;
      margin-right: 1.25rem;
    }
  }
}
</style>