<template>
  <div class="invoice">
    <!-- 头部细节部分 -->
      <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>
            <div class="top">
              <el-button @click="DownloadTemplate">下载模板</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="num"></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="batchNumber"
            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="运单编号"
            prop="num"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            label="查询状态"
            show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.isSuccess == 1">成功</span>
              <span v-else-if="scope.row.isSuccess == 2">失败</span>
              <span v-else-if="scope.row.isSuccess == 3">无结果</span>
            </template>
          </el-table-column>
          <el-table-column
            label="公司名称"
            prop="userCompany"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="userName"
            label="用户名称"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="userPhone"
            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>
export default{
  data() {
    return {
      loading: false,
      fullscreenLoading: false,
      usertable: [],
      usertabletwo: [],
      batchId: '',
      addList: false,
       hightt:'0px',
      current: 1,
      pagesize: 8,
      total: 0,
      currenttwo: 1,
      pagesizetwo: 8,
      totaltwo: 0,
      num: '',
      formUserList: {
        "roleId": "",
        "userId": "",
        "file": ""
      },
    }
  },
  created() {
    this.heightt = tableHeight;
    this.loadData();
  },
  methods: {
    firstLoadData(){
            this.current = 1;
            this.pagesize = 8;
            this.loadData();
          },
    // 列表展示
    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', 4);
        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', 4);
        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 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(`numInvoice/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('userId', this.formUserList.userId);
        formData.append('batchId', this.batchId);
        const response = await this.$http.post(`numInvoice/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('num', this.num);
        const response = await this.$http.post(`numInvoice/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('userId', this.formUserList.userId);
        formData.append('batchId', this.batchId);
        formData.append('num', this.num);
        const response = await this.$http.post(`numInvoice/list`, formData);
        if (response.data.code === 0) {
          this.loading = false;
          this.usertabletwo = response.data.data.records;
          this.totaltwo = response.data.data.total;
        }
      }
    },
    // 下载模板
    DownloadTemplate() {
      var url = 'http://invoice.back.jkcredit.com/numInvoice/downTemp';

      window.location.href= url;

    },
    // 批量上传模板信息
    async batchUpload() {
         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)'
                               });
      this.formUserList.userId = sessionStorage.getItem('userId');
      const formData = new FormData();
      formData.append('userId', this.formUserList.userId);
      formData.append('file', this.formUserList.file);
      const response = await this.$http.post(`numInvoice/excel`,formData);
      var {data: { code, msg, data }} = response;
      if(code === 0 && msg === 'success') {
        this.loadData();
        loading.close();
        this.$message.success('上传成功');
        this.$refs.upload.clearFiles();
      }else if(code === 1 && msg == null && data == null) {
        loading.close();
        this.$message.error('数据存在错误,请检查文件中数据');
      }else {
       loading.close();
        this.$message.error("数据存在错误,请检查文件中数据");
      }
    },

    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;
      if(this.num !== '') {
        this.queryLookTwo();
      }else{
        this.loadDataCar();
      };
      console.log(`每页 ${val} 条`);
    },
    handleCurrent(val) {
      this.currenttwo = val;
      if(this.num !== '') {
        this.queryLookTwo();
      }else{
        this.loadDataCar();
      };
      console.log(`当前页: ${val}`);
    }
  }
};
</script>

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