<template>
    <div class="sefCarRec_container">
      <div class="title">
        <el-row>
          <el-col :span="24">
            <div class="top">
               <div class="demo-input-suffix">
                  <el-input  placeholder="客户名称" class="input-demo" v-model="formCondition.companyName"></el-input>
                  <el-input  placeholder="企业编号" class="input-demo" v-model="formCondition.companyNum"></el-input>
                  <el-input  placeholder="etc卡号" class="input-demo" v-model="formCondition.etcNum"></el-input>
                   <el-input  placeholder="车牌号" class="input-demo" v-model="formCondition.carNum"></el-input>
              </div>
                <div class="demo-input-suffix" style="margin-top: 5px;margin-left: 10px;">
                 
                  <el-select v-model="formCondition.recStatus" placeholder="备案状态">
                      <el-option
                        v-for="item in recLists"
                        :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="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="selfcarRecCarTable"
          :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="businessType"
            label="业务类型"
            show-overflow-tooltip>
             <template slot-scope="scope">
              <span v-if="scope.row.businessType == 0">自有车</span>
              <span v-if="scope.row.businessType == 1">外协车</span>
              <span v-if="scope.row.businessType == 2">无车</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="carNum"
            label="车牌号"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="carColor"
            label="车辆颜色"
            show-overflow-tooltip>
             <template slot-scope="scope">
              <span v-if="scope.row.carColor == 0">蓝色</span>
              <span v-else-if="scope.row.carColor == 1" >黄色</span>
              <span v-else-if="scope.row.carColor == 2" >黑色</span>
              <span v-else-if="scope.row.carColor == 3" >白色</span>
              <span v-else-if="scope.row.carColor == 4" >渐变绿色</span>
              <span v-else-if="scope.row.carColor == 5" >黄绿渐变色</span>
              <span v-else-if="scope.row.carColor == 6" >蓝白渐变色</span>
              <span v-else-if="scope.row.carColor == 7" >未确定</span>
              <span v-else-if="scope.row.carColor == 8" >未确定</span>
              <span v-else-if="scope.row.carColor == 9" >未确定</span>
            </template>
          </el-table-column>
           <el-table-column
            prop="custPhone"
            label="手机号"
            show-overflow-tooltip>
          </el-table-column>
           <el-table-column
            prop="etcNum"
            label="ETC卡号"
            show-overflow-tooltip>
          </el-table-column>
            <el-table-column
            prop="interType"
            label="对接方式"
            show-overflow-tooltip>
             <template slot-scope="scope">
              <span v-if="scope.row.interType == 0">接口</span>
              <span v-else-if="scope.row.interType == 1">平台</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="recStatus"
            label="备案状态"
            show-overflow-tooltip>
             <template slot-scope="scope">
              <span v-if="scope.row.recStatus == 1" style="color: green">备案成功</span>
              <span v-else-if="scope.row.recStatus == 0" style="color: red">备案失败</span>
            </template>
          </el-table-column>
           <el-table-column
            prop="succTime"
            label="成功时间"
            show-overflow-tooltip>
            
          </el-table-column>
          <el-table-column
            prop="failTime"
            label="失败时间"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="failReason"
            label="失败原因"
            show-overflow-tooltip>
          </el-table-column>
           <el-table-column
            label="操作"
            width="280">
            <template slot-scope="scope" show-overflow-tooltip>
              <el-link v-if="scope.row.businessType == 1 || scope.row.businessType == 0" style="cursor:pointer; color: blue;" @click="selfCarUnBind(scope.row.etcNum)">解绑</el-link>
            </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, 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{
            selfcarRecCarTable:[],
            recLists:[{"label":"备案失败","value":"1"},{"label":"备案成功","value":"2"}],
            formCondition:{
              businessType: "0"
            },
             hightt:'0px',
            current: 1,
            pagesize: 8,
            total:''
          }
        },
        created() {
          this.heightt = tableHeight;
          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);
           for(var i in this.formCondition){
                formData.append(i,this.formCondition[i]);
            }
            const response = await this.$http.post(`noCar/findCarRec`, formData);
            if (response.data.code === 0) {
              this.selfcarRecCarTable = response.data.data.records;
              this.total = response.data.data.total;
            }
          },
          async selfCarUnBind(etcNum){
                this.$confirm('此操作将解绑ETC:'+etcNum+', 是否继续?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                  const formData = new FormData();
                  formData.append('etcNum',etcNum);
                  const request =  this.$http.post(`selfCar/selfCarUnBind`, formData);
                  request.then(
                      data => {
                        if (data.data.code === 0) {
                           loadData();
                           this.$message({
                              type: 'success',
                              message: '解绑成功'
                           });
                         }else{
                           console.log(data)
                           this.$message({
                              type: 'error',
                              message: ''+data.data.msg
                          });
                         }
                      }
                  );
                }).catch(() => {
                  //几点取消的提示
                });
          },
          // 分页方法
          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 = "自有车ETC备案查询列表_"+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>
.sefCarRec_container {
  border: 1px solid #d9d9d9;
  border-radius: 10px;
}
.sefCarRec_container .title {
  font-size: 5px;
  margin-bottom: 20px;
}
.sefCarRec_container .top {
  padding-top: 20px;
  padding-left: 20px;
}
.sefCarRec_container .text {
  display: inline-block;
  color: #000;
  font-size: 16px ;
  margin-left: 1%;
}
.sefCarRec_container .input-demo {
  display: inline-block;
  width: 20%;
  margin-left: 1%;
}
.sefCarRec_container .block {
  font-size: 5px;
  text-align: center;
  margin-top: 25px;
  margin-bottom: 25px;
}
.sefCarRec_container .el-dialog {
  width: 60%;
}
.sefCarRec_container .el-dialog__header, .el-dialog__body {
  padding: 0 20px;
}
.sefCarRec_container .tou {
  font-size: 20px;
  height: 30px;
  line-height: 30px;
  padding-top: 15px;
}
.sefCarRec_container .line {
  margin-top: 15px;
  margin-bottom: 15px;
  width: 100%;
  height: 2px;
  background-color: #d9d9d9;
}
.sefCarRec_container .xinxi {
  text-align: center;
  margin: 15px auto;
  font-size: 18px;
}
</style>