// 计费详单
<template>
  <div class="steel_inbound">
    <div class="sache">
      <el-form :inline="true" style="margin-top :5px">
        <el-form-item>
          <el-select
            v-model="screen"
            placeholder="请选择需筛选的内容"
            clearable
            class="elSelect"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-input
            style="width:200px"
            v-model="input"
            placeholder="请输入查询条件"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="screen1"
            placeholder="请选择需筛选的内容"
            clearable
            class="elSelect"
          >
            <el-option
              v-for="item in optionsF"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-input
            style="width:200px"
            v-model="input1"
            placeholder="请输入查询条件"
            clearable
          ></el-input>
        </el-form-item>
        <span>净重时间：</span>
        <el-date-picker
          v-model="startTime"
          type="datetime"
          placeholder="选择日期"
          style="width:200px"
        >
        </el-date-picker>
        <span>至</span>
        <el-date-picker
          v-model="endTime"
          type="datetime"
          placeholder="选择日期"
          style="width:200px"
        >
        </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
        >
        <el-button type="primary" @click="newSettle">生成详单</el-button>
      </el-form>
    </div>
    <div class="table">
      <el-tabs v-model="activeName" tab-click="handClick">
        <!-- 未结算 -->
        <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" @func="func2"> </dilTable>
        </el-tab-pane>
        <el-tab-pane label="未生成详单" name="third" v-if="isShow">
          <dilTable v-bind.sync="options3" @func="func3"> </dilTable>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { getCookie } from '@/utils/util.js'
import { sjTime } from '@/utils/sharedJsFile'
export default {
  data() {
    return {
      options: [
        {
          value: '用车单位',
          label: '用车单位'
        },
        {
          value: '货物名称',
          label: '货物名称'
        },
        {
          value: '车牌号',
          label: '车牌号'
        },
        {
          value: '承运起止范围',
          label: '承运起止范围'
        }
      ],
      optionsF: [
        {
          value: '用车单位',
          label: '用车单位'
        },
        {
          value: '货物名称',
          label: '货物名称'
        },
        {
          value: '车牌号',
          label: '车牌号'
        },
        {
          value: '承运起止范围',
          label: '承运起止范围'
        }
      ],
      screen: null,
      screen1: null,
      startTime: null,
      endTime: null,
      //合计净重
      totalNetWeight: null,
      //合计金额
      totalMoney: null,
      //单价
      Fee: null,
      //加载
      selectLineLoading: false,
      input: null,
      input1: null,
      activeName: 'first',
      ssoId: null,
      //详单Id
      detailsId: null,
      options1: {
        // first请求数据的地址
        requestUrl: ''
      },
      options2: {
        // second请求数据的地址
        requestUrl: ''
      },
      options3: {
        requestUrl: ''
      },
      //是否打开修改详单地址弹出框
      dialogFormVisible: false,
      //原详单单价
      originalPriceValue: null,
      //新详单单价
      newPriceValue: null,
      //原详单地址
      originalAddress: null,
      //新详单地址
      newAddress: null,
      //新单价Id
      priceId: null,
      //运输单价的表格
      Address: {
        requestUrl: '/api/v1/ams/getAddressDeliveryAddress?apiId=255',
        selectionType: 'radio'
      },
      // addressText:null,
      //缓存当前选中的运输单价
      priceMap: {},
      formLabelWidth: '125px',
      tableTitle: '内转计重详单',
      exportAllList: [],
      noSettleDetailsColumn: [],
      settleDetailsColumn: [],
      noDetailsColumn: [],
      orderList1: [],
      orderList2: [],
      orderList3: [],
      loginName: ''
    }
  },
  created() {
    this.loginName = this.loginNames()
    this.options1GetRequestUrl()
    this.options2GetRequestUrl()
    this.options3GetRequestUrl()
  },
  methods: {
    loginNames() {
      return getCookie('loginName')
    },
    handClick() {
      if (this.activeName == 'first') {
        this.options1GetRequestUrl()
      } else if (this.activeName == 'second') {
        this.options2GetRequestUrl()
      } else {
        this.options3GetRequestUrl()
      }
    },
    func(res) {
      console.log(res)
      this.noSettleDetailsColumn = res.columnData
      var totalWeight = 0
      var totalAllMoney = 0
      res.list.forEach(e => {
        totalWeight = totalWeight + e.resultNetWeight
        totalAllMoney = totalAllMoney + e.detailsAmount
      })
      this.totalNetWeight = totalWeight.toFixed(2) + 't'
      this.totalMoney = totalAllMoney.toFixed(2) + '元'
      this.orderList1 = []
      res.list.forEach(e => {
        let map = {
          orderLineSeq: e.orderLineSeq,
          orderId: e.orderId
        }
        this.orderList1.push(map)
      })
    },
    func2(res) {
      this.settleDetailsColumn = res.columnData
    },
    func3(res) {
      console.log(res.list)
      console.log('res')
      this.orderList3 = []
      res.list.forEach(e => {
        let map = {
          orderLineSeq: e.orderLineSeq,
          orderId: e.orderId
        }
        this.orderList3.push(map)
      })
      this.noDetailsColumn = res.columnData
    },
    newSettle() {
      console.log(this.orderList)
      let orderList = []
      if (this.activeName == 'third') {
        orderList = this.orderList3
      } else {
        orderList = this.orderList1
      }
      this.axios
        .post('/api/v1/bms/newInwardSettle', { orderIds: orderList })
        .then(res => {
          console.log(res)
          this.$message.success('生成成功')
          this.options3GetRequestUrl()
        })
    },
    exportAllExcel() {
      let startTime = null
      let endTime = null
      let orgCode = null
      let materialTypeNames = null
      let transRangeValues = null
      let shipperNames = null
      let capacityNo = 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.screen == '车牌号') {
        capacityNo = this.input
      }
      if (this.screen == '承运起止范围') {
        transRangeValues = this.input
      }
      if (this.screen == '货物名称') {
        materialTypeNames = this.input
      }
      if (this.screen == '用车单位') {
        shipperNames = this.input
      }
      if (this.screen1 == '车牌号') {
        capacityNo = this.input1
      }
      if (this.screen1 == '承运起止范围') {
        transRangeValues = this.input1
      }
      if (this.screen1 == '货物名称') {
        materialTypeNames = this.input1
      }
      if (this.screen1 == '用车单位') {
        shipperNames = this.input1
      }
      if (this.activeName == 'first') {
        this.tableTitle = '未结算计费详单'
        this.axios
          .post(
            '/api/v1/bms/getInwardWeightDetailsOrder?apiId=461&orderType=11&detailStatus=0' +
              '&startTime=' +
              startTime +
              '&endTime=' +
              endTime +
              '&shipperNames=' +
              shipperNames +
              '&materialTypeNames=' +
              materialTypeNames +
              '&transRangeValues=' +
              transRangeValues +
              '&capacityNo=' +
              capacityNo +
              '&i' +
              new Date() +
              '&isPage=yes'
          )
          .then(res => {
            console.log(res.data.data)
            this.exportAllList = res.data.data
            this.exportAllReportToExcel(
              this.tableTitle,
              this.exportAllList,
              this.noSettleDetailsColumn
            )
          })
      } else if (this.activeName == 'second') {
        this.tableTitle = '已结算计费详单'
        this.axios
          .post(
            '/api/v1/bms/getInwardWeightDetailsOrder?apiId=461&orderType=11&detailStatus=1' +
              '&startTime=' +
              startTime +
              '&endTime=' +
              endTime +
              '&shipperNames=' +
              shipperNames +
              '&materialTypeNames=' +
              materialTypeNames +
              '&transRangeValues=' +
              transRangeValues +
              '&capacityNo=' +
              capacityNo +
              '&i' +
              new Date() +
              '&isPage=yes' +
              '&userId=' +
              orgCode
          )
          .then(res => {
            this.exportAllList = res.data.data
            this.exportAllReportToExcel(
              this.tableTitle,
              this.exportAllList,
              this.settleDetailsColumn
            )
          })
      } else if (this.activeName == 'third') {
        this.tableTitle = '未生成计费详单数据'
        this.axios
          .post(
            '/api/v1/bms/getNoInwardDetails?apiId=490' +
              '&startTime=' +
              startTime +
              '&endTime=' +
              endTime +
              '&shipperNames=' +
              shipperNames +
              '&materialTypeNames=' +
              materialTypeNames +
              '&transRangeValues=' +
              transRangeValues +
              '&capacityNo=' +
              capacityNo +
              '&i' +
              new Date() +
              '&userId=' +
              orgCode +
              '&isPage=yes'
          )
          .then(res => {
            this.exportAllList = res.data.data
            this.exportAllReportToExcel(
              this.tableTitle,
              this.exportAllList,
              this.noDetailsColumn
            )
          })
      }
    },
    //导出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=461&orderType=11&detailStatus=0&carrierSsoId=' +
          getCookie('userId') +
          '&startTime=null&endTime=null&i=' +
          new Date()
      } else if (
        getCookie('orgCode') == 'dagangadmin' ||
        getCookie('orgCode') == 'zidonghuabu' ||
        getCookie('orgCode') == 'wuliuchuyunzhongxin'
      ) {
        this.options1.requestUrl =
          '/api/v1/bms/getInwardWeightDetailsOrder?apiId=461&orderType=11&detailStatus=0&startTime=null&endTime=null&i=' +
          new Date()
      } else {
        this.options1.requestUrl =
          '/api/v1/bms/getInwardWeightDetailsOrder?apiId=461&orderType=11&detailStatus=0&userId=' +
          getCookie('orgCode') +
          '&startTime=null&endTime=null&i=' +
          new Date()
      }
    },
    //已结算数据
    options2GetRequestUrl() {
      if (getCookie('orgCode') == 'chengyunshang') {
        this.ssoId = getCookie('userId')
        this.options2.requestUrl =
          '/api/v1/bms/getInwardWeightDetailsOrder?apiId=461&orderType=11&detailStatus=1&carrierSsoId=' +
          getCookie('userId') +
          '&startTime=null&endTime=null&i=' +
          new Date()
      } else if (
        getCookie('orgCode') == 'dagangadmin' ||
        getCookie('orgCode') == 'zidonghuabu' ||
        getCookie('orgCode') == 'wuliuchuyunzhongxin'
      ) {
        this.options2.requestUrl =
          '/api/v1/bms/getInwardWeightDetailsOrder?apiId=461&orderType=11&detailStatus=1' +
          '&startTime=null&endTime=null&i=' +
          new Date()
      } else {
        this.options2.requestUrl =
          '/api/v1/bms/getInwardWeightDetailsOrder?apiId=461&orderType=11&detailStatus=1&userId=' +
          getCookie('orgCode') +
          '&startTime=null&endTime=null&i=' +
          new Date()
      }
    },
    options3GetRequestUrl() {
      this.options3.requestUrl =
        '/api/v1/bms/getNoInwardDetails?apiId=490&i=' + new Date()
    },
    toInsert() {
      this.$router.push('/insertCheckInventory')
    },
    onclick() {
      let startTime = null
      let endTime = null
      let orgCode = null
      let materialTypeNames = null
      let transRangeValues = null
      let shipperNames = null
      let capacityNo = 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.screen == '车牌号') {
        capacityNo = this.input
      }
      if (this.screen == '承运起止范围') {
        transRangeValues = this.input
      }
      if (this.screen == '货物名称') {
        materialTypeNames = this.input
      }
      if (this.screen == '用车单位') {
        shipperNames = this.input
      }
      if (this.screen1 == '车牌号') {
        capacityNo = this.input1
      }
      if (this.screen1 == '承运起止范围') {
        transRangeValues = this.input1
      }
      if (this.screen1 == '货物名称') {
        materialTypeNames = this.input1
      }
      if (this.screen1 == '用车单位') {
        shipperNames = this.input1
      }
      if (this.activeName == 'first') {
        this.options1.requestUrl =
          '/api/v1/bms/getInwardWeightDetailsOrder?apiId=461&orderType=11&detailStatus=0' +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&shipperNames=' +
          shipperNames +
          '&materialTypeNames=' +
          materialTypeNames +
          '&transRangeValues=' +
          transRangeValues +
          '&capacityNo=' +
          capacityNo +
          '&i' +
          new Date() +
          '&userId=' +
          orgCode
      }
      if (this.activeName == 'second') {
        this.options2.requestUrl =
          '/api/v1/bms/getInwardWeightDetailsOrder?apiId=461&orderType=11&detailStatus=1' +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&shipperNames=' +
          shipperNames +
          '&materialTypeNames=' +
          materialTypeNames +
          '&transRangeValues=' +
          transRangeValues +
          '&capacityNo=' +
          capacityNo +
          '&i' +
          new Date() +
          '&userId=' +
          orgCode
      }
      if (this.activeName == 'third') {
        this.options3.requestUrl =
          '/api/v1/bms/getNoInwardDetails?apiId=490' +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&shipperNames=' +
          shipperNames +
          '&materialTypeNames=' +
          materialTypeNames +
          '&transRangeValues=' +
          transRangeValues +
          '&capacityNo=' +
          capacityNo +
          '&i' +
          new Date() +
          '&userId=' +
          orgCode
      }
    },
    //运输订单点击浏览的事件
    select() {
      this.addressDrawer = true
    },
    //运输单价模态窗口的框计算
    selectAddressClick() {
      this.Price.requestUrl =
        '/api/v1/ams/getAddressDeliveryAddress?apiId=255&con=' +
        this.addressText
    }
  }
}
</script>

<style lang="scss" scoped>
.steel_inbound {
  .sache {
    margin-left: 20px;
    margin-top: 20px;
    .elSelect /deep/ {
      .el-input--suffix .el-input__inner {
        width: 120px !important;
      }
    }
  }
  .table {
    margin-left: 20px;
    margin-top: 20px;
  }
}
</style>
