// 计费详单
<template>
  <div class="steel_inbound">
    <div class="sache">
      <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="exportAllExcel()"
        ><i class="el-icon-download"></i>导出(Excel)</el-button
      >
      <span style="margin-left: 1rem;">合计时间：</span>
      <el-input
        v-model="totalTime"
        :disabled="true"
        style="width: 140px;"
      ></el-input>
      <span style="margin-left: 1rem;">合计金额：</span>
      <el-input
        v-model="totalMoney"
        :disabled="true"
        style="width: 100px;"
      ></el-input>
    </div>
    <div class="table">
      <el-tabs v-model="activeName">
        <!-- 未结算 -->
        <el-tab-pane label="未结算" name="first">
          <dilTable v-bind.sync="options1" ref="excelDom" @func="func">
          </dilTable>
        </el-tab-pane>
        <!-- 已结算 -->
        <el-tab-pane label="已结算" name="second">
          <dilTable v-bind.sync="options2"> </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 {
      startTime: null,
      endTime: null,
      totalMoney: null,
      totalTime: null,
      //单价
      Fee: null,
      //加载
      selectLineLoading: false,
      input: "",
      activeName: "first",
      ssoId: null,
      //详单Id
      detailsId: null,
      options1: {
        // first请求数据的地址
        requestUrl: ""
      },
      options2: {
        // second请求数据的地址
        requestUrl: ""
      },
      //是否打开修改详单地址弹出框
      dialogFormVisible: false,
      //原详单单价
      originalPriceValue: null,
      //新详单单价
      newPriceValue: null,
      //原详单地址
      originalAddress: null,
      //新详单地址
      newAddress: null,
      //新单价Id
      priceId: null,
      //是否打开运输单价的模态框
      addressDrawer: false,
      //运输单价的表格
      Address: {
        requestUrl: "/api/v1/ams/getAddressDeliveryAddress?apiId=255",
        selectionType: "radio"
      },
      // addressText:null,
      //缓存当前选中的运输单价
      priceMap: {},
      formLabelWidth: "125px",
      tableTitle: "内转计时计费详单"
    };
  },
  created() {
    this.options1GetRequestUrl();
    this.options2GetRequestUrl();
  },
  methods: {
    exportAllExcel() {
      let startTime = null
      let endTime = null
      let orgCode = null
      let carrierSsoId = null
      if (this.startTime && this.endTime) {
        startTime = sjTime(this.startTime)
        endTime = sjTime(this.endTime)
      }
      if (getCookie('orgCode') == 'chengyunshang') {
        carrierSsoId = getCookie('userId')
      }
      if (
        getCookie('orgCode') == 'dagangadmin' ||
        getCookie('orgCode') == 'zidonghuabu' ||
        getCookie('orgCode') == 'wuliuchuyunzhongxin'
      ) {
        orgCode = null
      } else {
        orgCode = getCookie('orgCode')
      }
      if (this.activeName == 'first') {
        this.tableTitle = '未结算计费详单'
        this.axios
          .post(
            "/api/v1/bms/getInwardWeightDetailsOrder?apiId=465&orderType=21&detailStatus=0" +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&i" +
            new Date()
          )
          .then(res => {
            console.log(res.data.data)
            console.log(res.data.data)
            this.exportAllList = res.data.data.list
            this.noSettleDetailsColumn=res.data.data.columnData
            this.exportAllReportToExcel(
              this.tableTitle,
              this.exportAllList,
              this.noSettleDetailsColumn
            )
          })
      } else if (this.activeName == 'second') {
        this.tableTitle = '已结算计费详单'
        this.axios
          .post(
            "/api/v1/bms/getInwardWeightDetailsOrder?apiId=465&orderType=21&detailStatus=0" +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&i" +
            new Date()
          )
          .then(res => {
            console.log(res.data.data)
            this.exportAllList = res.data.data.list
            this.noSettleDetailsColumn=res.data.data.columnData
            this.exportAllReportToExcel(
              this.tableTitle,
              this.exportAllList,
              this.settleDetailsColumn
            )
          })
      }
    },
    //导出excel
    exportAllReportToExcel(tableTitle, dataArr, columnData) {
      var title = tableTitle
      let tHeader = []
      let filterVal = []
      columnData.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 = dataArr.map(v => filterVal.map(j => v[j])) //3.formatJson格式转换
        export_json_to_excel(tHeader, data, title) // (title)导出的表格名称
      })
    },
    //未结算数据
    options1GetRequestUrl() {
      if (getCookie("orgCode") == "chengyunshang") {
        this.ssoId = getCookie("userId");
        this.options1.requestUrl =
          "/api/v1/bms/getInwardWeightDetailsOrder?apiId=465&orderType=21&detailStatus=0&carrierSsoId=" +
          getCookie("userId");
      } else if (
        getCookie("orgCode") == "dagangadmin" ||
        getCookie("orgCode") == "zidonghuabu" ||
        getCookie("orgCode") == "wuliuchuyunzhongxin"
      ) {
        this.options1.requestUrl =
          "/api/v1/bms/getInwardWeightDetailsOrder?apiId=465&orderType=21&detailStatus=0";
      } else {
        this.options1.requestUrl =
          "/api/v1/bms/getInwardWeightDetailsOrder?apiId=465&orderType=21&detailStatus=0&userId=" +
          getCookie("orgCode");
      }
    },
    //已结算数据
    options2GetRequestUrl() {
      if (getCookie("orgCode") == "chengyunshang") {
        this.ssoId = getCookie("userId");
        this.options2.requestUrl =
          "/api/v1/bms/getInwardWeightDetailsOrder?apiId=465&orderType=21&detailStatus=1&carrierSsoId=" +
          getCookie("userId");
      } else if (
        getCookie("orgCode") == "dagangadmin" ||
        getCookie("orgCode") == "zidonghuabu" ||
        getCookie("orgCode") == "wuliuchuyunzhongxin"
      ) {
        this.options2.requestUrl =
          "/api/v1/bms/getInwardWeightDetailsOrder?apiId=465&orderType=21&detailStatus=1";
      } else {
        this.options2.requestUrl =
          "/api/v1/bms/getInwardWeightDetailsOrder?apiId=465&orderType=21&detailStatus=1&userId=" +
          getCookie("orgCode");
      }
    },
    toInsert() {
      this.$router.push("/insertCheckInventory");
    },
    func(res) {
      console.log(res);
      var totalAllTime = 0;
      var totalAllMoney = 0;
      res.list.forEach(e => {
        totalAllTime = totalAllTime + e.resultTime;
        totalAllMoney = totalAllMoney + e.detailsAmount;
      });
      this.totalMoney = totalAllMoney;
      this.totalTime = totalAllTime;
    },
    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 (
            getCookie("orgCode") == "dagangadmin" ||
            getCookie("orgCode") == "zidonghuabu" ||
            getCookie("orgCode") == "wuliuchuyunzhongxin"
          ) {
            this.options1.requestUrl =
              "/api/v1/bms/getInwardWeightDetailsOrder?apiId=465&orderType=21&detailStatus=0" +
              "&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&i" +
              new Date();
            this.options2.requestUrl =
              "/api/v1/bms/getInwardWeightDetailsOrder?apiId=465&orderType=21&detailStatus=1" +
              "&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&i" +
              new Date();
          } else if (getCookie("orgCode") == "chengyunshang") {
            this.options1.requestUrl =
              "/api/v1/bms/getInwardWeightDetailsOrder?apiId=465&orderType=21&detailStatus=0&carrierSsoId=" +
              getCookie("userId") +
              "&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&i" +
              new Date();
            this.options2.requestUrl =
              "/api/v1/bms/getInwardWeightDetailsOrder?apiId=465&orderType=21&detailStatus=1&carrierSsoId=" +
              getCookie("userId") +
              "&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&i" +
              new Date();
          } else {
            this.options1.requestUrl =
              "/api/v1/bms/getInwardWeightDetailsOrder?apiId=465&orderType=21&detailStatus=0&userId=" +
              getCookie("orgCode") +
              "&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&i" +
              new Date();
            this.options2.requestUrl =
              "/api/v1/bms/getInwardWeightDetailsOrder?apiId=465&orderType=21&detailStatus=1&userId=" +
              getCookie("orgCode") +
              "&startTime=" +
              startTime +
              "&endTime=" +
              endTime +
              "&i" +
              new Date();
          }
        } else {
          this.startTime = null;
          this.endTime = null;
          this.$message.warning("开始时间要比结束时间早");
        }
      } else {
      }
    },
    //运输订单点击浏览的事件
    select() {
      this.addressDrawer = true;
    },
    //运输单价模态窗口的框计算
    selectAddressClick() {
      this.Price.requestUrl =
        "/api/v1/ams/getAddressDeliveryAddress?apiId=255&con=" +
        this.addressText;
    }
  }
};
</script>

<style lang="scss" scode>
.steel_inbound {
  .sache {
    margin-left: 20px;
    margin-top: 20px;
    .el-input {
      width: 20%;
      margin-right: 1.25rem;
    }
  }
  .table {
    margin-left: 20px;
    margin-top: 20px;
  }
}
</style>
