<template>
  <div class="carupload">
    <!-- 头部细节部分 -->
      <div class="title">
        <el-row>
          <el-col :span="24">
            <div class="top">
              <el-upload
                class="upload-demo"
                ref="upload"
                action="http://invoice.back.jkcredit.com/"
                :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>
                 <div style="margin-top: 10px;">导入文件：</div>
              </el-upload>

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

        <el-row>
          <el-col :span="24">
            <div class="top">
              <el-input  placeholder="用户名" class="input-demo" style="margin: 0;" v-model="userName"></el-input>
              <el-input  placeholder="用户所在公司" class="input-demo" v-model="userCompany"></el-input>
              <el-input  placeholder="批次号" class="input-demo" v-model="batchNumber"></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="userName"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            label="用户手机号"
            prop="userPhone"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="userCompany"
            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;" @click="checkLook(scope.row.id)">查看</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="userPhone"></el-input>
              <el-input  placeholder="车牌号" class="theinput" v-model="plateNumber"></el-input>
              <label>车牌颜色:</label>
              <el-select v-model="plateColor" placeholder="请选择车牌颜色">
                <el-option
                  v-for="item in optionone"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              <!-- <el-input  placeholder="车牌颜色" class="theinput" ></el-input> -->
              <el-button type="success" style="margin-left: 1%;" @click="queryLookTwo">查询</el-button>
              <!-- <el-button type="info" style="margin-left: 1%;">重置</el-button> -->
            </div>
          <el-table
            v-loading="loading"
            ref="multipleTable"
            :data="usertabletwo"
            border
            tooltip-effect="dark">
            <el-table-column
              label="用户名"
              prop="userName"
              show-overflow-tooltip>
            </el-table-column>
            <el-table-column
              label="用户手机号"
              prop="userPhone"
              show-overflow-tooltip>
            </el-table-column>
            <el-table-column
              prop="userCompany"
              label="用户所在公司"
              show-overflow-tooltip>
            </el-table-column>
            <el-table-column
              prop="plateNumber"
              label="车牌号"
              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="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="是否备案成功"
              show-overflow-tooltip>
                <template slot-scope="scope">
                <span v-if="scope.row.isSuccess == 1">备案成功(有ETC)</span>
                <span v-else-if="scope.row.isSuccess == 2">备案失败(没有ETC)</span>
                <span v-else-if="scope.row.isSuccess == 3">备案失败(车辆已备案)</span>
              </template>
            </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>
export default{
  data() {
    return {
      loading: false,
      fullscreenLoading: false,
      userName: '',
      userCompany: '',
       hightt:'0px',
      batchNumber: '',
      userPhone: '',
      plateNumber: '',
      plateColor: '',
      optionone: [{
          value: '0',
          label: '蓝色'
        }, {
          value: '1',
          label: '黄色'
        }, {
          value: '2',
          label: '黑色'
        }, {
          value: '3',
          label: '白色'
        }, {
          value: '4',
          label: '渐变绿色'
        }, {
          value: '5',
          label: '黄绿渐变色'
        }, {
          value: '6',
          label: '蓝白渐变色'
        }, {
          value: '9',
          label: '未确定'
        }],
      addList: false,
      current: 1,
      pagesize: 8,
      total: 0,
      currenttwo: 1,
      pagesizetwo: 8,
      totaltwo: 0,
      batch: '',
      batchId: '',
      usertable: [],
      usertabletwo: [],
      formUserList: {
       "userId": "",
       "file": "",
       "roleId": ""
      },
    }
  },
  created() {
    this.heightt = tableHeight;
    this.loadData();
  },
  methods: {
                   firstLoadData(){
            this.current = 1;
            this.pagesize = 8;
            this.queryLook();
          },
    // 列表展示
    async loadData() {
      this.formUserList.userId = sessionStorage.getItem('userId');
      this.formUserList.roleId = sessionStorage.getItem('roleId');
      if(this.formUserList.roleId == 1) {
        const formData = new FormData();
        formData.append('current', this.current);
        formData.append('size', this.pagesize);
        formData.append('type', 1);
        const response = await this.$http.post(`batch/list`, formData);
          if (response.data.code === 0) {
          this.loading = false;
          this.usertable = response.data.data.records;
          this.total = response.data.data.total;
        };
      }else {
        const formData = new FormData();
        formData.append('current', this.current);
        formData.append('size', this.pagesize);
        formData.append('userId', this.formUserList.userId);
        formData.append('type', 1);
        const response = await this.$http.post(`batch/list`, formData);
          if (response.data.code === 0) {
            this.loading = false;
            this.usertable = response.data.data.records;
            this.total = response.data.data.total;
        };
      };
    },
    //查询
    async queryLook() {
      if(this.formUserList.roleId == 1) {
        const formData = new FormData();
        formData.append('current', this.current);
        formData.append('size', this.pagesize);
        formData.append('type', 1);
        formData.append('userName', this.userName);
        formData.append('userCompany', this.userCompany);
        formData.append('batchNumber', this.batchNumber);
        const response = await this.$http.post(`batch/list`, formData);
          if (response.data.code === 0) {
          this.loading = false;
          this.usertable = response.data.data.records;
          this.total = response.data.data.total;
        };
      }else {
        const formData = new FormData();
        formData.append('current', this.current);
        formData.append('size', this.pagesize);
        formData.append('userId', this.formUserList.userId);
        formData.append('type', 1);
        formData.append('userName', this.userName);
        formData.append('userCompany', this.userCompany);
        formData.append('batchNumber', this.batchNumber);
        const response = await this.$http.post(`batch/list`, formData);
          if (response.data.code === 0) {
            this.loading = false;
            this.usertable = response.data.data.records;
            this.total = response.data.data.total;
        };
      };
    },
    // 下载模板
    DownloadTemplate() {
      var url = 'http://invoice.back.jkcredit.com/carFreeCarrierRegister/downTemp';

      window.location.href= url;

    },
    // 批量上传模板信息
    async batchUpload() {
      this.fullscreenLoading = true;
      const formData = new FormData();
      formData.append('userId', this.formUserList.userId);
      formData.append('file', this.formUserList.file);
      const response = await this.$http.post(`carFreeCarrierRegister/excel`,formData);
      var {data: { code, msg, data }} = response;
      if(code === 0 && msg === 'success') {
        this.loadData();
        this.fullscreenLoading = false;
        this.$message.success('上传成功');
      }else if(code === 1 && msg == null && data == null) {
        this.fullscreenLoading = false;
        this.$message.error('数据存在错误，请检查文件中数据');
      }else {
        this.fullscreenLoading = false;
        this.$message.error(msg);
      }
    },
    // 查看批次数据
    async checkLook(id) {
      this.addList = true;
      this.batchId = id;
      this.loadDataCar();
    },
    // 查看批次数据
    async loadDataCar() {
      if(this.formUserList.roleId == 1) {
        const formData = new FormData();
        formData.append('current', this.currenttwo);
        formData.append('size', this.pagesizetwo);
        formData.append('batchId', this.batchId);
        const response = await this.$http.post(`carFreeCarrierRegister/list`, formData);
          if (response.data.code === 0) {
          this.loading = false;
          this.usertabletwo = response.data.data.records;
          this.totaltwo = response.data.data.total;
        }
      }else {
        const formData = new FormData();
        formData.append('current', this.currenttwo);
        formData.append('size', this.pagesizetwo);
        formData.append('batchId', this.batchId);
        formData.append('userId', this.formUserList.userId);
        const response = await this.$http.post(`carFreeCarrierRegister/list`, formData);
        if (response.data.code === 0) {
          this.loading = false;
          this.usertabletwo = response.data.data.records;
          this.totaltwo = response.data.data.total;
        }
      }
    },
    //查看（二)
    async queryLookTwo(){
      if(this.formUserList.roleId == 1) {
        const formData = new FormData();
        formData.append('current', this.currenttwo);
        formData.append('size', this.pagesizetwo);
        formData.append('batchId', this.batchId);
        formData.append('userPhone', this.userPhone);
        formData.append('plateNumber', this.plateNumber);
        formData.append('plateColor', this.plateColor);
        const response = await this.$http.post(`carFreeCarrierRegister/list`, formData);
          if (response.data.code === 0) {
          this.loading = false;
          this.usertabletwo = response.data.data.records;
          this.totaltwo = response.data.data.total;
        }
      }else {
        const formData = new FormData();
        formData.append('current', this.currenttwo);
        formData.append('size', this.pagesizetwo);
        formData.append('batchId', this.batchId);
        formData.append('userId', this.formUserList.userId);
        formData.append('userPhone', this.userPhone);
        formData.append('plateNumber', this.plateNumber);
        formData.append('plateColor', this.plateColor);
        const response = await this.$http.post(`carFreeCarrierRegister/list`, formData);
        if (response.data.code === 0) {
          this.loading = false;
          this.usertabletwo = response.data.data.records;
          this.totaltwo = response.data.data.total;
        }
      }
    },

    // 清空表单数据
    handleEditDialogClose() {
      this.userPhone = '';
      this.plateNumber = '';
      this.plateColor = '';
      this.current = 1;
      this.pagesize = 8;
      this.currenttwo = 1;
      this.pagesizetwo = 8;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleSuccess (a) {
      this.formUserList.file = a.raw;
    },
    // 分页方法
    handleSizeChange(val) {
      this.pagesize = val;
      if(this.userName !== '' || this.userCompany !== '' || this.batchNumber !== '') {
        this.queryLook();
      }else{
        this.loadData();
      };
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.current = val;
      if(this.userName !== '' || this.userCompany !== '' || this.batchNumber !== '') {
        this.queryLook();
      }else{
        this.loadData();
      };
      console.log(`当前页: ${val}`);
    },
    handleSize(val) {
      this.pagesizetwo = val;
      if(this.userPhone !== '' || this.plateNumber !== '' || this.plateColor !== '') {
        this.queryLookTwo();
      }else{
        this.loadDataCar();
      };
      console.log(`每页 ${val} 条`);
    },
    handleCurrent(val) {
      this.currenttwo = val;
      if(this.userPhone !== '' || this.plateNumber !== '' || this.plateColor !== '') {
        this.queryLookTwo();
      }else{
        this.loadDataCar();
      };
      console.log(`当前页: ${val}`);
    }
  }
};
</script>

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