<template>
  <div class="page-box" v-loading="loading > 0">
    <Header :title="title"></Header>
    <div class="container">
      <!-- tab -->
      <div class="content">
        <!-- 表格 -->
        <div class="filter-box">
          <div class="filter">
            <div class="filter-item">
              <div class="label">排序方式：</div>
              <el-select
                v-model="filter.lockedDesc"
                size="small"
                class="select"
                placeholder="请选择"
              >
                <el-option label="按照余额" :value="false"> </el-option>
                <el-option label="按照锁仓值" :value="true"> </el-option>
              </el-select>
            </div>

            <div class="filter-item">
              <div class="label">最大值：</div>
              <el-input
                class="input"
                v-model="filter.max"
                placeholder="请输入内容"
                size="small"
              ></el-input>
            </div>
            <div class="filter-item">
              <div class="label">最小值：</div>
              <el-input
                class="input"
                v-model="filter.min"
                placeholder="请输入内容"
                size="small"
              ></el-input>
            </div>
            <div class="filter-item">
              <div class="label">锁仓最大值：</div>
              <el-input
                class="input"
                v-model="filter.maxLocked"
                placeholder="请输入内容"
                size="small"
              ></el-input>
            </div>
            <div class="filter-item">
              <div class="label">锁仓最小值：</div>
              <el-input
                class="input"
                v-model="filter.minLocked"
                placeholder="请输入内容"
                size="small"
              ></el-input>
            </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="order-table table no-border-table"
          ref="orderTable"
          :header-cell-style="{
            border: 'none',
            color: '#333',
            background: '#f6f6f6',
            borderBottom: '1px solid #E8E8E8',
            height: '54px',
          }"
          :row-style="{
            height: '54px',
          }"
        >
          <el-table-column
            prop="uid"
            align="center"
            label="用户ID"
          ></el-table-column>
          <el-table-column
            prop="balance"
            align="center"
            label="积分余额"
          ></el-table-column>
          <el-table-column
            prop="locked"
            align="center"
            label="锁仓积分余额"
          ></el-table-column>
          <el-table-column label="操作" align="center" width="160">
            <div
              slot-scope="scope"
              style="display: flex; justify-content: center"
            >
              <div style="display: flex; justify-content: center">
                <button
                  size="mini"
                  class="table-btn"
                  @click="toDetail(scope.row.uid)"
                >
                  用户详情
                </button>
              </div>
            </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';
import { role, roleMap } from '@/config.js';
export default {
  components: {
    Header
  },
  name: 'balanceRecord',
  data() {
    return {
      currentPage: 1, // 当前页码
      size: 20, // 每页条数
      total: 0, // 总页数
      loading: 0, // 加载中
      title: {
        // 页面标题
        firstTitile: '资金管理',
        secondTitle: '用户积分列表'
      },
      filter: {
        lockedDesc: false,
        min: null,
        max: null,
        minLocked: null,
        maxLocked: null
      },
      payType: [
        // {
        //   name: '全部',
        //   id: 0
        // },
        {
          name: '订单支付',
          id: 2
        },
        {
          name: '充值余额',
          id: 3
        }
      ],
      roleMap: roleMap,
      tabActive: 1,
      statusType: [],
      list: [], // 列表
      date: [],
      showProgressDialog: false,
      progress: 0,
      payTypeMap: {
        2: '订单支付',
        3: '充值余额'
      }
    };
  },

  mounted() {
    // this.statusType = this.hzType;
    // 获取列表
    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.filter.min = this.filter.min || null;
      this.filter.max = this.filter.max || null;
      this.filter.minLocked = this.filter.minLocked || null;
      this.filter.maxLocked = this.filter.maxLocked || null;
      this.loading++;
      this.httpGet(this.$root.getIntegraRecordlList, {
        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 = {
        lockedDesc: false,
        min: null,
        max: null,
        minLocked: null,
        maxLocked: null
      };
      this.date = [];
      this.getList();
    },
    // 用户详情
    toDetail(uid) {
      window.open('user-detail?uid=' + uid, true);
      // this.$router.push({
      //   name: 'userDetail',
      //   query: {
      //     uid
      //   }
      // });
    },
    async order2excel() {
      this.loading++;
      let list = [];
      let totalPage = null;
      let data = {
        page: this.currentPage,
        size: this.size,
        ...this.filter
      };
      await this.httpGet(this.$root.getWxPayList, 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.getWxPayList, 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 = {
          用户ID: item.uid,
          金额: item.amount,
          类型: this.payTypeMap[item.payType],
          关联单号: item.oid,
          时间: item.created
        };
        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;
      }
    }
  }

  .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;
  }
}

.tabs {
  width: 100%;
  background: #fff;
  height: 60px;
  flex-x(flex-start);
  padding: 0 30px;
  box-sizing: border-box;
  margin-top: 8px;
  flex-shrink: 0;

  .tab {
    background: #fff;
    cursor: pointer;
    padding: 0 15px;
    position: relative;
    width: auto;
    margin-right: 100px;
    height: 100%;
    flex-x(center);
    word(16px, #999);

    &.active {
      word(16px, #333);

      .tab-line {
        width: 100%;
        position: absolute;
        background: #4678E6;
        height: 4px;
        border-radius: 2px;
        left: 0;
        bottom: 3px;
      }
    }
  }
}
</style>
