<template>
    <div class="invoice_container">
      <div class="title">
        <el-row>
          <el-col :span="24">
            <div class="top">
              <div class="top" >
               <span style="margin-top: 10px;margin-bottom: 10px;">导入文件：</span>

              <el-upload
                class="upload-demo"
                ref="upload"
                style="margin-bottom: 10px"
                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-button style="margin-left: 555px;" @click="DownloadTemplate">查询模板下载</el-button>

              </el-upload>
              </div>
             <div class="demo-input-suffix">
                <el-input  placeholder="客户名称" class="input-demo" v-model="formCondition.companyName"></el-input>
                <el-input  placeholder="etc卡号" class="input-demo" v-model="formCondition.cardId"></el-input>
                <el-input  placeholder="税号" class="input-demo" v-model="formCondition.buyerTaxpayerCode"></el-input>
                 <el-input  placeholder="交易id" class="input-demo" v-model="formCondition.tradeId"></el-input>
              </div>
              <div class="demo-input-suffix" style="margin-top: 5px;margin-left: 10px;">
                <el-date-picker v-model="formCondition.invoiceMakeTime" type="daterange" value-format="yyyy-MM-dd HH:mm:SS" range-separator="至" start-placeholder="开票时间始" end-placeholder="开票时间止"></el-date-picker>
                <el-input  placeholder="发票代码" class="input-demo" v-model="formCondition.invoiceCode"></el-input>
                <el-input  placeholder="发票号码" class="input-demo" v-model="formCondition.invoiceNum"></el-input>
                <el-button type="success" style="margin-left: 1%;" @click="loadData">查询</el-button>
                               <el-button type="primary" style="margin-left: 1%;" @click="exportExcel">导出报表</el-button>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
      <!-- 表格部分 -->
        <template>
        <el-table
          class="table"
          v-loading="loading"
          ref="multipleTable"
          :data="invoiceTable"
          :height="heightt"
          border
          tooltip-effect="dark">
          <el-table-column
            label="企业编号"
            prop="companyNum"
            show-overflow-tooltip>
          </el-table-column>
           <el-table-column
            prop="companyName"
            label="客户名称"
            show-overflow-tooltip>
          </el-table-column>
           <el-table-column
            prop="buyerTaxpayerCode"
            label="购方税号"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="plateNum"
            label="车牌号"
            show-overflow-tooltip>
          </el-table-column>
           <el-table-column
            prop="cardId"
            label="ETC卡号"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="tradeId"
            label="交易Id"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="invoiceCode"
            label="发票代码"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="invoiceNum"
            label="发票号码"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="invoiceMakeTime"
            label="开票时间"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="exTime"
            label="交易时间"
            show-overflow-tooltip>
          </el-table-column>
          <!-- <el-table-column
            prop="enStation"
            label="入口收费站"
            width="100"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="exStation"
            label="出口收费站"
            width="100"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="sellerName"
            label="销方名称"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="sellerTaxpayerCode"
            label="销方税号"
            show-overflow-tooltip>
          </el-table-column> -->
          <el-table-column
            prop="totalAmount"
            label="价税合计"
            show-overflow-tooltip>
             <template slot-scope="scope">
               <span>{{scope.row.totalAmount/100| rounding}}</span>
            </template>
          </el-table-column>
           <el-table-column
            prop="totalTaxAmount"
            label="税额"
            show-overflow-tooltip>
             <template slot-scope="scope">
               <span>{{scope.row.totalTaxAmount/100| rounding}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="fee"
            label="金额"
            show-overflow-tooltip>
            <template slot-scope="scope">
               <span>{{scope.row.fee/100| rounding}}</span>
            </template>
          </el-table-column>
           <el-table-column
            prop="taxRate"
            label="税率"
            show-overflow-tooltip>
            <template slot-scope="scope">
               <span>{{scope.row.taxRate| rounding}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="invoiceHtmlUrl"
            label="预览地址"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="invoiceUrl"
            label="下载地址"
            show-overflow-tooltip>
          </el-table-column>
           <el-table-column
            prop="tradeStatus"
            label="状态"
            show-overflow-tooltip>
             <template slot-scope="scope">
              <span v-if="scope.row.tradeStatus == 1" style="color: green">待开票</span>
              <span v-else-if="scope.row.tradeStatus == 2" style="color: red">开票中</span>
              <span v-else-if="scope.row.tradeStatus == 3" style="color: red">已开票</span>
            </template>
          </el-table-column>
           <el-table-column
            prop="calculateTime"
            label="计费时间"
            show-overflow-tooltip>
          </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, 20, 50, 100]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
      </div>
    </div>
</template>
<script type="text/javascript">
import FileSaver from "file-saver";
import XLSX from "xlsx";
      export default {
        data(){
          return{
            formCondition:{

            },
            formUserList: {
            "file": ""
            },
           invoiceTable:[],
            hightt:'0px',
            current: 1,
            pagesize: 8,
            total:''
          }
        },
        created() {
          this.heightt = tableHeight;
          this.loadData();
        },
        filters: {
            rounding (value) {
              return value.toFixed(2)
            }
        },
        methods:{
          // 列表展示
          async loadData() {
            const formData = new FormData();
            formData.append('current', this.current);
            formData.append('size', this.pagesize);
            for(var i in this.formCondition){
                formData.append(i,this.formCondition[i]);
            }
            const response = await this.$http.post('selfCar/findSelfCarInvoices', formData);
            if (response.data.code === 0) {
              this.invoiceTable = response.data.data.records;
              this.total = response.data.data.total;
            }
          },

             // 下载模板
          DownloadTemplate() {
            var url = hostUrl+"noCar/templateDownload?fileName=7"
            window.location.href= url;

          },
           handleRemove(file, fileList) {
            console.log(file, fileList);
          },

          handlePreview(file) {
            console.log(file);
          },
          handleSuccess (a) {
            this.formUserList.file = a.raw;
          },

                // 批量上传模板信息
          async batchUpload() {
          this.fullscreenLoading = true;
          const formData = new FormData();
          formData.append('file', this.formUserList.file);
          const response = await this.$http.post(`selfCar/batchImportSelfcarInvoices`,formData);
          var {data: { code, msg, data }} = response;
          if(code === 0 && msg === '1') {
              this.fullscreenLoading = false;
              this.invoiceTable = response.data.data;
              this.total = response.data.data.length;
          }else {
            this.fullscreenLoading = false;
            this.$message.error('数据存在错误，请检查文件中数据');
          }
        },

          // 分页方法
          handleSizeChange(val) {
            this.pagesize = val;
            this.loadData();
            console.log(`每页 ${val} 条`);
          },
          handleCurrentChange(val) {
            this.current = val;
              this.loadData();
            // console.log(`当前页: ${val}`);
          },
       async     exportExcel() {
         let curr = this.current;
      let pagesize1 = this.pagesize;
      this.current = 1; 
      this.pagesize = this.total;
      await this.loadData();
      // 设置当前日期
      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(".table"),{ 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.current = curr; 
      this.pagesize = pagesize1;
      this.loadData();
      return wbout;
    },
        }
      };
</script>
<style>
.invoice_container {
  border: 1px solid #d9d9d9;
  border-radius: 10px;
}
.invoice_container .title {
  font-size: 5px;
  margin-bottom: 20px;
}
.invoice_container .top {
  padding-top: 20px;
  padding-left: 20px;
}
.invoice_container .text {
  display: inline-block;
  color: #000;
  font-size: 16px ;
  margin-left: 1%;
}
.invoice_container .input-demo {
  display: inline-block;
  width: 20%;
  margin-left: 1%;
}
.invoice_container .block {
  font-size: 5px;
  text-align: center;
  margin-top: 25px;
  margin-bottom: 25px;
}
.invoice_container .el-dialog {
  width: 60%;
}
.invoice_container .el-dialog__header, .el-dialog__body {
  padding: 0 20px;
}
.invoice_container .tou {
  font-size: 20px;
  height: 30px;
  line-height: 30px;
  padding-top: 15px;
}
.invoice_container .line {
  margin-top: 15px;
  margin-bottom: 15px;
  width: 100%;
  height: 2px;
  background-color: #d9d9d9;
}
.invoice_container .xinxi {
  text-align: center;
  margin: 15px auto;
  font-size: 18px;
}
</style>
