<template>
  <div class="waybillList">
    <!-- 头部细节部分 -->
      <div class="title">
        <el-row>
          <el-col :span="24">
            <div class="top">
              <el-select v-model="companyName" placeholder="请选择公司名称">
                <el-option
                  v-for="item in companyList"
                  :key="item.companyName"
                  :label="item.companyName"
                  :value="item.companyName">
                </el-option>
              </el-select>
              <el-input  placeholder="运单编号" class="theinput" v-model="billNum"></el-input>
              <el-input  placeholder="车牌号" class="theinput" v-model="plateNum"></el-input>
           <el-select v-model="success" placeholder="是否成功">
                <el-option
                  v-for="item in optionone"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              <el-button type="success" style="margin-left: 1%;" @click="firstLoadData">查询</el-button>
              <el-button type="success" style="margin-left: 1%;" @click="exportExcel">导出</el-button>
            <!-- <el-button type="info" style="margin-left: 1%;">重置</el-button> -->
            </div>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <div class="top">
              <el-date-picker
                v-model="startBegin"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:SS"
                placeholder="运单上传开始指令始">
              </el-date-picker>

              <el-date-picker
                class="input-demo"
                v-model="startEnd"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:SS"
                placeholder="运单上传开始指令止">
              </el-date-picker>

              <el-date-picker
                class="input-demo"
                v-model="endBegin"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:SS"
                placeholder="运单上传结束指令始">
              </el-date-picker>

              <el-date-picker
                class="input-demo"
                v-model="endEnd"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:SS"
                placeholder="运单上传结束指令止">
              </el-date-picker>

            </div>
          </el-col>
        </el-row>
      </div>
    <!-- 头部细节部分结束 -->
    <!-- 表格部分 -->
        <template>
          <el-table
           class="table"
            v-loading="loading"
            ref="multipleTable"
            :data="usertabletwo"
            :height="heightt"
            border
            tooltip-effect="dark">
          <el-table-column
            label="运单编号"
            prop="billNum"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="taxplayerCode"
            label="税号"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            label="车牌号码"
            prop="plateNum"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            label="车牌颜色"
            show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.plateColor == 0">蓝色</span>
              <span v-else-if="scope.row.plateColor == 1">黄色</span>
              <span v-else-if="scope.row.plateColor == 2">黑色</span>
              <span v-else-if="scope.row.plateColor == 3">白色</span>
              <span v-else-if="scope.row.plateColor == 4">渐变绿色</span>
              <span v-else-if="scope.row.plateColor == 5">黄绿渐变色</span>
              <span v-else-if="scope.row.plateColor == 6">蓝白渐变色</span>
              <span v-else-if="scope.row.plateColor == 9">未确定</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="startTime"
            label="运单开始时间"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="sourceAddr"
            label="运单开始地址"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="destAddr"
            label="运单目的地址"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="predictEndTime"
            label="运单预计完成时间"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="fee"
            label="运单费用(元)"
            show-overflow-tooltip>
            <template slot-scope="scope" show-overflow-tooltip>
                <span>{{scope.row.fee / 100}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="运单状态"
            show-overflow-tooltip>
            <template slot-scope="scope">

               <span v-if="scope.row.billwayStatus == 1" style="color: green">未结束</span>
                <span v-else-if="scope.row.billwayStatus == -2" style="color: red">失败</span>
                 <span v-else-if="scope.row.billwayStatus == -3" style="color: red">指令上传失败</span>
              <span v-else-if="scope.row.billwayStatus == 2" style="color: green">开票中</span>
              <span v-else-if="scope.row.billwayStatus == 3" style="color: green">开票完成</span>
              <span v-else-if="scope.row.billwayStatus == 4" style="color: red">超时运单</span>
              </template>
          </el-table-column>
           <el-table-column
            label="是否成功"
            show-overflow-tooltip>
            <template slot-scope="scope">

               <span v-if="scope.row.billwayStatus == -2 || scope.row.billwayStatus == -3" style="color: red">失败</span>
                <span v-else-if="scope.row.billwayStatus != -2 && scope.row.billwayStatus != -3" style="color: green">成功</span>

              </template>
          </el-table-column>
           <el-table-column
            prop="failReason"
            label="失败原因"
            show-overflow-tooltip>
          </el-table-column>
          </el-table>
        </template>

        <div class="blocks">
          <el-pagination
          @size-change="handleSize"
          @current-change="handleCurrent"
          :current-page="currenttwo"
          :page-sizes="[6, 8, 10]"
          :page-size="pagesizetwo"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totaltwo">
          </el-pagination>
        </div>


  </div>
</template>

<script>
import FileSaver from "file-saver";
import XLSX from "xlsx";
export default{
  data() {
    return {

      loading: false,
      billNum: '',
      plateNum: '',
      batchNumber: '',
      startBegin: '',
      startEnd: '',
      endBegin: '',
      success:'',
      endEnd: '',
       hightt:'0px',
      // taxPlayerCode: '',
      // status: '',
      currenttwo: 1,
      pagesizetwo: 8,
      totaltwo: 0,
      optionone: [{
        value: '1',
        label: '成功'
      }, {
        value: '2',
        label: '失败'
      }],
      // batch: '',
      companyList:[],
      companyName:'',
      usertabletwo: [],
      customerName:'',
    }
  },
  created() {
    this.heightt = tableHeight;
     this.customerName = sessionStorage.getItem('userName');
     this.initCompanyList();

  },
  methods: {
        firstLoadData(){
            this.currenttwo = 1;
            this.pagesizetwo = 8;
            this.loadDataCar();
          },
    // 列表展示
    async loadDataCar() {
      const formData = new FormData();
      formData.append('current', this.currenttwo);
      formData.append('size', this.pagesizetwo);
      formData.append('success', this.success);
      formData.append('customerName', this.customerName);
      formData.append('companyName', this.companyName);
      formData.append('billNum', this.billNum);
       formData.append('billNumN', this.billNum);
      formData.append('plateNum', this.plateNum);
     // formData.append('batchNumber', this.batchNumber);
      formData.append('startBegin', this.startBegin);
      formData.append('startEnd', this.startEnd);
      formData.append('endBegin', this.endBegin);
      formData.append('endEnd', this.endEnd);
        // formData.append('isSuccess', 1);
      const response = await this.$http.post(`noCar/findBillWayCust`, formData);
      if (response.data.code === 0) {
         this.loading = false;
        this.usertabletwo = response.data.data.records;
        this.totaltwo = response.data.data.total;
        }
    },
    async initCompanyList(){
            const response = await this.$http.post(`lowerService/customeRecQueryList`, {"customerName":sessionStorage.getItem('userName')});
            if (response.data.code === 0) {
              this.companyList = response.data.data;
            }  if(this.companyList == null || typeof this.companyList == 'undefined' || this.companyList =='' || this.companyList.length==0){
                                                     this.companyList = [{'companyName':'.'}];
                                                   }
            this.companyName = this.companyList[0]['companyName'];
            this.loadDataCar();
    },
    //查看

     //导出功能
    exportOut() {
      var url = `http://invoice.back.jkcredit.com/carFreeCarrierBill/billExport?&userId=${this.formUserList.userId}`;

      window.location.href= url;
    },
     formartNum(wb){
            var sheet = wb['Sheets']['Sheet1'];
            var replaceTemp = [];
            debugger;
            for(var i in sheet){
              if(sheet[i]['v'] == '运单费用(元)'){
                replaceTemp.push(i.replace(/[0-9]/g,''));
                continue;
              }
              if(replaceTemp.includes(i.replace(/[0-9]/g,''))){
                sheet[i]['t']='n';
              }
            }
          },
async  exportExcel() {
            const loading = this.$loading({
                               lock: true,
                               text: '系统正在努力接收中，过程大概需要几分钟的时间，请您耐心等待...',
                               spinner: 'el-icon-loading',
                               background: 'rgba(0, 0, 0, 0.7)'
                             });
            
            const formData = new FormData();
      formData.append('current', 1);
      formData.append('size', this.totaltwo);
      formData.append('success', this.success);
      formData.append('customerName', this.customerName);
      formData.append('companyName', this.companyName);
      formData.append('billNum', this.billNum);
       formData.append('billNumN', this.billNum);
      formData.append('plateNum', this.plateNum);
     // formData.append('batchNumber', this.batchNumber);
      formData.append('startBegin', this.startBegin);
      formData.append('startEnd', this.startEnd);
      formData.append('endBegin', this.endBegin);
      formData.append('endEnd', this.endEnd);
        // formData.append('isSuccess', 1);
      const response = await this.$http.post(`noCar/findBillWayCust`, formData);
            if (response.data.code === 0) {
               // 设置当前日期
                    let time = new Date();
                    //console.log(time);
                    let year = time.getFullYear();
                    let month = time.getMonth() + 1;
                    let day = time.getDate();
                    let name = "无车运单查询列表_"+year + "" + month + "" + day;
                    let cloums = [
                      {"title":"公司名称","key":"companyName"},
                          {"title":"运单号","key":"billNum"},
                          {"title":"税号","key":"taxplayerCode"},
                          {"title":"车牌号码","key":"plateNum"},
                          {"title":"运单开始时间","key":"startTime"},
                          {"title":"运单结束时间","key":"predictEndTime"},
                          {"title":"运单开始地址","key":"sourceAddr"},
                          {"title":"运单结束地址","key":"destAddr"},
                          {"title":"运单费用(元)","key":"fee"},
                          {"title":"运单状态","key":"billwayStatus"},
                          {"title":"失败原因","key":"failReason"},
                          {"title":"运单类型","key":"hisFlag"},
                    ];
                  
                   this.exportExcelComm(cloums,response.data.data.records,name,loading)
              
            }
          },
          formatJson (filterVal, jsonData) {
            return jsonData.map(v => filterVal.map(j => {
              if(j == 'billwayStatus'){
                 if(v[j] == 1){
                   return "未结束";
                 } else if(v[j] == -2){
                   return "上传失败";
                 }else if(v[j] == -3){
                   return "指令结束上传失败";
                 }else if(v[j] == 2){
                   return "开票中";
                 }else if(v[j] == 3){
                   return "开票完成";
                 }else {
                   return "超时运单";
                 }
              }else if(j=='hisFlag'){
                if(v[j] == 0){
                  return "实时运单";
                }else{
                  return "历史运单";
                }
              }else if(j =='fee'){
                  return v[j]/100;
              }else{
                  return v[j];
              }
              
              }));
          },
          // 导出Excel
          exportExcelComm(columns,list,excelName,loading){
                  require.ensure([], () => {
                      const { export_json_to_excel } = require('@/vendor/Export2Excel');
                      let tHeader = []
                      let filterVal = []
                      columns.forEach(item =>{
                          tHeader.push(item.title)
                          filterVal.push(item.key)
                      })
                      const data = this.formatJson(filterVal, list);
                      export_json_to_excel(tHeader, data, excelName);
                      console.log(1111);
                     console.log(new Date().getMilliseconds)
                     loading.close();
                     debugger
                  })
            },
    // 分页方法
    handleSize(val) {
      this.pagesizetwo = val;
      this.loadDataCar();
      console.log(`每页 ${val} 条`);
    },
    handleCurrent(val) {
      this.currenttwo = val;
     this.loadDataCar();
      console.log(`当前页: ${val}`);
    }
  }
};
</script>

<style>
.waybillList {
  border: 1px solid #d9d9d9;
  border-radius: 10px;
}
.waybillList .upload-demo {
  display: inline-block;
}
.waybillList .title {
  font-size: 5px;
  margin-bottom: 20px;
}
.waybillList .top {
  padding-top: 20px;
  padding-left: 20px;
}
.waybillList .text {
  display: inline-block;
  color: #000;
  font-size: 16px ;
  margin-left: 1%;
}
.waybillList .tou {
  font-size: 20px;
  height: 30px;
  line-height: 30px;
  padding-top: 15px;
}
.waybillList .line {
  margin-top: 15px;
  margin-bottom: 15px;
  width: 100%;
  height: 2px;
  background-color: #d9d9d9;
}
.waybillList .theinput {
  display: inline-block;
  width: 20%;
}
.waybillList .styleType {
  margin-top: 20px;
  margin-bottom: 20px;
}
.waybillList .input-demo {
  display: inline-block;
  width: 20%;
  margin-left: 1%;
}
.waybillList .block {
  font-size: 5px;
  text-align: center;
  margin-top: 15px;
  margin-bottom: 20px;
}
.waybillList .blocks {
  font-size: 5px;
  text-align: center;
  margin-top: 20px;
  padding-bottom: 20px;
}

</style>
