<template>
  <div class="waybill">
    <!-- 头部细节部分 -->
      <div class="title">
        <el-row>
          <el-col :span="24">
            <div class="top">
               <span style="margin-top: 10px;" >导入文件:</span>
              <el-upload
                class="upload-demo"
                ref="upload"
                action="http://invoice.back.jkcredit.com/dishonestuser/upload"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :on-change="handleSuccess"
                :auto-upload="false">
                <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                <el-button style="margin-left: 10px;" size="small" type="success" @click="batchUpload" v-loading.fullscreen.lock="fullscreenLoading">提交</el-button>
              </el-upload>

            </div>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <div class="top">

              <el-input  placeholder="主体账号名称" class="input-demo" v-model="companyName"></el-input>
              <el-input  placeholder="批次号" class="input-demo" v-model="batchNum"></el-input>
              <el-button style="margin-left: 1%;" @click="DownloadTemplate">下载模板</el-button>
              <el-button type="success" style="margin-left: 1%;" @click="firstLoadData">查询</el-button>
              <!-- <el-button type="info" style="margin-left: 1%;">重置</el-button> -->
            </div>
          </el-col>
        </el-row>

      </div>
    <!-- 头部细节部分结束 -->
    <!-- 表格部分 -->
      <template>
        <el-table
         class="table"
          v-loading="loading"
          ref="multipleTable"
          :data="usertable"
          :height="heightt"
          border
          tooltip-effect="dark">
          <el-table-column
            label="用户名"
            prop="customerName"
            show-overflow-tooltip>
          </el-table-column>
          <!-- <el-table-column
            label="用户手机号"
            prop="customerPhone"
            show-overflow-tooltip>
          </el-table-column> -->
          <el-table-column
            prop="company"
            label="主账号名称"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="batchNumber"
            label="批次号"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            label="创建时间"
            show-overflow-tooltip>
            <template slot-scope="scope" show-overflow-tooltip>
              <span>{{scope.row.createTime | fmtDate}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="80">
            <template slot-scope="scope" show-overflow-tooltip>
              <span style="cursor:pointer;color: blue;" @click="checkLook(scope.row.batchNumber)">查看</span>
            </template>
          </el-table-column>
        </el-table>
      </template>

    <!-- 分页 -->
      <div class="block">
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[6, 8, 10]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
      </div>

    <!-- 查看 -->
      <el-dialog
        @close="handleEditDialogClose"
        :visible.sync="addList"
        style="font-size: 0px;">
        <template>
          <div class="tou">运单列表</div>
          <div class="line"></div>
          <div class="styleType">
            <el-input  placeholder="运单编号" class="theinput" v-model="billNum"></el-input>
            <el-input  placeholder="车牌号" class="theinput" v-model="plateNumber"></el-input>
            <el-input  placeholder="税号" class="theinput" v-model="taxPlayerCode"></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="twoLoadData">查询</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-table
            class="ttt"
            v-loading="loading"
            ref="multipleTable"
            :data="usertabletwo"
            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
            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
            prop="billwayStatus"
            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>
      </el-dialog>

  </div>
</template>

<script>
import FileSaver from "file-saver";
import XLSX from "xlsx";
export default{
  data() {
    return {
      loading: false,
      fullscreenLoading: false,
      customerName: '',
      companyName: '',
      billNum: '',
      plateNumber: '',
      taxPlayerCode: '',
       hightt:'0px',
      status: '',
      addList: false,
      success:'',
      current: 1,
      pagesize: 8,
      total: 0,
      currenttwo: 1,
      pagesizetwo: 8,
      totaltwo: 0,
      batchNum: '',
      batchNumberQ:'',
      usertable: [],
      usertabletwo: [],
      formUserList: {
       "customerName": "",
       "file": "",
       "roleId": ""
      },
      optionone: [{
        value: '1',
        label: '成功'
      }, {
        value: '2',
        label: '失败'
      }],
      isSuccess: '',
    }
  },
  created() {
    this.heightt = tableHeight;
    this.formUserList.customerName = sessionStorage.getItem('userName');
    this.loadData();
  },
  methods: {
    firstLoadData(){
            this.current = 1;
            this.pagesize = 8;
            this.loadData();
          },
    // 列表展示
    async loadData() {

       const formData = new FormData();
        formData.append('current', this.current);
        formData.append('size', this.pagesize);
        formData.append('customerName',this.formUserList.customerName);
         formData.append('company', this.companyName);
        formData.append('batchNumber', this.batchNum);
        formData.append('operType', 1);
        const response = await this.$http.post(`noCar/findBatchList`, formData);
        if (response.data.code === 0) {
          this.loading = false;
          this.usertable = response.data.data.records;
          this.total = response.data.data.total;
        }
    },
    // 下载模板
    DownloadTemplate() {
      var url = hostUrl+"noCar/templateDownload?fileName=1"
      window.location.href= url;

    },
    // 批量上传模板信息
    async batchUpload() {
      debugger
      const isLt50M = this.formUserList.file.size / 1024 / 1024 < 50;
      let extName = this.formUserList.file.name.substring(this.formUserList.file.name.lastIndexOf(".")).toLowerCase();
       let AllUpExt = ".xlsx";
       if( extName != AllUpExt){
          this.$message.error('文件格式不正确!');
          return false;
    }

      if (!isLt50M) {
                this.$message.error('上传文件大小不能超过50MB!');
                return false;
       }
      const loading = this.$loading({
                                           lock: true,
                                           text: '系统正在努力接收中,过程大概需要几分钟的时间,请您耐心等待...',
                                           spinner: 'el-icon-loading',
                                           background: 'rgba(0, 0, 0, 0.7)'
                                         });
      const formData = new FormData();
      formData.append('customerName', this.formUserList.customerName);
      formData.append('file', this.formUserList.file);
      const response = await this.$http.post(`noCar/batchImprotBillWay`,formData);
      var {data: { code, msg, data }} = response;
      if(code === 0 && msg === 'success') {
        this.loadData();
        loading.close();
        this.$message.success('上传成功');
      }else if(code === 1 && msg == null && data == null) {
       loading.close();
        this.$message.error('数据存在错误,请检查文件中数据');
      }else {
       loading.close();
        this.$message.error(msg);
      }
    },
    // 查看批次数据
    async checkLook(id) {
      this.addList = true;
      this.batchNumberQ = id;
      this.loadDataCar();
    },
    handleEditDialogClose(){
       this.addList = false;
    },
    twoLoadData(){
            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('customerName', this.formUserList.customerName);
        formData.append('batchNum', this.batchNumberQ);
        formData.append('billNum', this.billNum);
        formData.append('success', this.success);
         formData.append('plateNum', this.plateNumber);
        formData.append('taxPlayerCode', this.taxPlayerCode);
        formData.append('hisFlag', 0);
        const response = await this.$http.post(`noCar/findImportBillWay`, formData);
        if (response.data.code === 0) {
          this.loading = false;
          this.usertabletwo = response.data.data.records;
          this.totaltwo = response.data.data.total;
        }
    },
   async    exportExcel() {
          let curr = this.currenttwo;
      let pagesize1 = this.pagesizetwo;
      this.currenttwo = 1;
      this.pagesizetwo = this.totaltwo;
      await this.loadDataCar();
      // 设置当前日期
      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;
      // console.log(name)
      /* generate workbook object from table */
      //  .table要导出的是哪一个表格
      var wb = XLSX.utils.table_to_book(document.querySelector(".ttt"),{ raw: true });
      /* get binary string as output */
      var wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array"
      });
      try {
        //  name+'.xlsx'表示导出的excel表格名字
        FileSaver.saveAs(
          new Blob([wbout], { type: "application/octet-stream" }),
          name + ".xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
       this.currenttwo = curr;
      this.pagesizetwo = pagesize1;
      this.loadDataCar();
      return wbout;
    },
     handleRemove(file, fileList) {
      console.log(file, fileList);
    },

    handlePreview(file) {
      console.log(file);
    },
    handleSuccess (a) {
      this.formUserList.file = a.raw;
    },
    // 分页方法
    handleSizeChange(val) {
      this.pagesize = val;
       this.loadData();
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.current = val;
      this.loadData();
      console.log(`当前页: ${val}`);
    },
    handleSize(val) {
      this.pagesizetwo = val;
      this.loadDataCar();
      console.log(`每页 ${val} 条`);
    },
    handleCurrent(val) {
      this.currenttwo = val;
     this.loadDataCar();
      console.log(`当前页: ${val}`);
    }
  }
};
</script>

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