<template>
  <div class="page-box" v-loading="loading > 0">
    <Header :title="title"></Header>
    <div class="container">
      <div class="content">
        <!-- 表格 -->
        <div class="filter-box">
          <div class="filter">
            <div class="filter-item">
              <div class="label">用户ID：</div>
              <el-input
                class="input"
                v-model="filter.uid"
                placeholder="请输入内容"
                size="small"
              ></el-input>
            </div>

            <div class="filter-item">
              <div class="label">审核状态：</div>
              <el-select
                v-model="filter.status"
                size="small"
                class="select"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in statusType"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>

            <div class="filter-item">
              <div class="label">申请时间：</div>
              <el-date-picker
                v-model="date"
                type="datetimerange"
                align="right"
                unlink-panels
                class="date-input"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd HH:mm:ss"
                :default-time="['00:00:00', '23:59:59']"
                size="small"
              >
              </el-date-picker>
            </div>
          </div>
          <div class="btn-box">
            <button class="search" @click="search">筛选</button>
            <button class="reset" @click="reset">重置</button>
            <button class="search" style="margin-left: 60px" @click="order2excel">导出</button>
          </div>
        </div>
        <el-table
          :data="list"
          class="table no-border-table"
          ref="table"
          :header-cell-style="{
            border: 'none',
            color: '#333',
            background: '#f6f6f6',
            borderBottom: '1px solid #E8E8E8',
            height: '54px',
          }"
          :row-style="{
            height: '54px',
          }"
        >
          <el-table-column
            prop="created"
            align="center"
            label="提审时间"
          ></el-table-column>
          <el-table-column
            prop="uid"
            align="center"
            label="用户ID"
          ></el-table-column>
          <el-table-column
            prop="amount"
            align="center"
            label="充值金额/元"
          ></el-table-column>
          <el-table-column
            prop="thirdId"
            align="center"
            label="充值单号"
          ></el-table-column>
          <el-table-column prop="status" align="center" label="审核状态">
            <div slot-scope="scope">
              {{ statusMap[scope.row.status] }}
            </div>
          </el-table-column>
          <el-table-column prop="amount" align="center" label="截图凭证">
            <div slot-scope="scope">
              <!-- <img class="master-pic" :src="scope.row.pic" alt="" /> -->
              <el-image
                class="master-pic"
                :src="scope.row.pic"
                :preview-src-list="[scope.row.pic]"
              >
              </el-image>
            </div>
          </el-table-column>
          <el-table-column label="操作" align="center" width="180">
            <div
              slot-scope="scope"
              style="display: flex; justify-content: center"
              v-if="scope.row.status === 0"
            >
              <button
                size="mini"
                class="table-btn"
                @click="handleAudit(scope.row.id, 1)"
              >
                通过
              </button>
              <button
                size="mini"
                class="table-btn"
                @click="handleAudit(scope.row.id, 2)"
              >
                拒绝
              </button>
            </div>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页按钮 -->
      <div class="footer">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          background
          layout="total, prev, pager, next, jumper"
          :page-size="size"
          :total="total"
        ></el-pagination>
      </div>
    </div>
    <el-dialog
      :show-close="false"
      :close-on-click-modal="false"
      title="生成中..."
      :visible.sync="showProgressDialog"
      width="30%"
      center
    >
      <el-progress
        :percentage="progress"
        :text-inside="true"
        :stroke-width="18"
      ></el-progress>
    </el-dialog>
  </div>
</template>

<script type="text/ecmascript-6">
import Header from '../../components/common/header';
import myUtils from '../../components/common/whatever2excel';
export default {
  components: {
    Header
  },
  name: 'balanceExamine',
  data() {
    return {
      currentPage: 1, // 当前页码
      size: 20, // 每页条数
      total: 0, // 总页数
      loading: 0, // 加载中
      title: {
        // 页面标题
        firstTitile: '资金管理',
        secondTitle: '余额充值审核'
      },
      filter: {
        uid: null,
        status: null
      },
      statusType: [
        {
          name: '全部',
          value: null
        },
        {
          name: '待审核',
          value: 0
        },
        {
          name: '审核通过',
          value: 1
        },
        {
          name: '审核拒绝',
          value: 2
        }
      ],
      statusMap: {
        0: '待审核',
        1: '审核通过',
        2: '审核拒绝'
      },
      list: [], // 用户列表
      date: [],
      showProgressDialog: false,
      progress: 0
    };
  },

  mounted() {
    // 获取列表
    this.getList();
  },
  computed: {},
  methods: {
    // 切换页码
    handleCurrentChange(page) {
      this.currentPage = page;
      this.getList();
    },
    // 获取提现列表
    getList() {
      this.filter.begin = this.date[0] || '';
      this.filter.end = this.date[1] || '';
      this.loading++;
      this.httpGet(this.$root.xxRechargeList, {
        page: this.currentPage,
        size: this.size,
        ...this.filter
      }).then(
        (res) => {
          this.loading--;
          this.list = res.list;
          this.total = res.total;
        },
        (res) => {
          this.loading--;
          this.$message.error(res);
        }
      );
    },
    // 过滤
    search() {
      this.currentPage = 1;
      this.getList();
    },
    // 重置过滤
    reset() {
      this.filter = {};
      this.date = [];
      this.getList();
    },
    handleAudit(id, status) {
      this.$confirm(
        `<p class='title'><i class='icon el-icon-question'></i>提醒</p><p class='text'>确定要${
          status === 1 ? '通过' : '驳回'
        }该审核吗？</p>`,
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          cancelButtonClass: 'cancel-btn',
          confirmButtonClass: 'confirm-btn',
          customClass: 'confirm-box',
          dangerouslyUseHTMLString: true,
          showClose: false,
          type: 'none'
        }
      ).then(() => {
        this.loading++;
        this.httpPut(this.$root.xxRechargeAudit, {
          id,
          status
        }).then(
          (res) => {
            this.$message({
              message: '操作成功',
              type: 'success'
            });
            this.loading--;
            this.getList();
          },
          (res) => {
            this.loading--;
            this.$message.error(res);
          }
        );
      });
    },
    async order2excel() {
      this.loading++;
      let list = [];
      let totalPage = null;
      let data = {
        page: this.currentPage,
        size: this.size,
        ...this.filter
      };
      await this.httpGet(this.$root.xxRechargeList, data).then(
        (res) => {
          this.loading--;
          totalPage = Math.ceil(parseInt(res.total) / this.size);
        },
        () => {
          this.loading--;
        }
      );
      this.showProgressDialog = true;
      this.progress = 0;
      await this.getAllPages(totalPage, list).then(
        () => {
          this.showProgressDialog = false;
          this.jsonFormatAndToExcel(list);
        },
        (err) => {
          console.error(err);
          this.showProgressDialog = false;
        }
      );
    },
    async getAllPages(totalPage, list) {
      for (let i = 1; i <= totalPage; i++) {
        let data = {
          page: i,
          size: this.size,
          ...this.filter
        };
        await this.httpGet(this.$root.xxRechargeList, data).then((res) => {
          list.push(...res.list);
          this.progress = Math.floor((i / totalPage) * 100);
          console.log('page' + i, list.length);
        });
        // console.log(5555, list);
      }
    },
    jsonFormatAndToExcel(list) {
      let formatList = [];
      list.forEach((item) => {
        let obj = {
          提审时间: item.created,
          用户ID: item.uid,
          '充值金额/元': item.amount,
          充值单号: item.thirdId,
          审核状态: this.statusMap[item.status]
        };
        formatList.push(obj);
      });
      myUtils.json2excel(formatList);
    }
  }
};
</script>
<style lang="stylus" rel="stylesheet/stylus">
@import '~assets/public.styl';
</style>
<style lang="stylus" rel="stylesheet/stylus" scoped>
@import '~assets/main.styl';

.page-box {
  height: 100%;
  width: 100%;
  flex-y(flex-start, flex-start);
  overflow-y: hidden;
  background: bg-color;

  .container {
    box-sizing: border-box;
    height: 100%;
    width: 100%;
    overflow-y: auto;
    flex-y(flex-start, flex-start);
    padding: 10px;

    // content 表格
    .content {
      flex: 1;
      width: 100%;
      font-size: 14px;
      background: white;
      box-sizing: border-box;
      padding: 24px 32px;
      border-radius: 2px;

      // 按钮
      .btn {
        width: 100px;
        height: 32px;
        line-height: 32px;
        confirm-btn();
        font-size: 14px;
        margin: 16px 0;
      }

      // 表格按钮
      .table-btn {
        // width: 65px;
        height: 24px;
        line-height: 24px;
        color: gray3;
        font-size: 12px;
        margin-right: 8px;
        cancel-btn();
        padding: 0 8px;
      }

      .filter-item {
        margin-right: 12px;
      }

      .select, .input {
        width: 125px;
      }

      .date-input {
        width: 360px;
      }

      .btn-box {
        margin: 0;
      }

      .table {
        margin-top: 24px !important;

        .master-pic {
          width: auto;
          height: 36px;
        }
      }
    }
  }

  .footer {
    width: 100%;
    padding: 24px;
    box-sizing: border-box;
    flex-x(flex-end);
    background: white;
  }

  .dialog-content {
    flex-y();
  }

  .form {
    width: 100%;
    flex-center();
    margin: 8px 0;
  }
}
</style>
