<template>
  <div class="divBox relative">
    <el-card class="box-card">
      <div class="clearfix">
        <div class="container">
          <el-form inline size="small" :model="userFrom" label-width="90px">
            <el-row>
              <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                <el-form-item label="用户搜索：">
                  <el-input
                    v-model="userFrom.keywords"
                    placeholder="请输入昵称或手机号"
                    clearable
                    class="selWidth"
                    @keyup.enter.native="seachList"
                  />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                <el-form-item label="时间选择：">
                  <el-date-picker
                    v-model="timeVal"
                    value-format="yyyy-MM-dd"
                    format="yyyy-MM-dd"
                    size="small"
                    type="daterange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    @change="onchangeTime"
                  />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                <el-form-item>
                  <el-button type="primary" icon="el-icon-search" size="small" @click="seachList">搜索</el-button>
                  <el-button size="small" @click="handleReset">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>

      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        size="mini"
        highlight-current-row
        :header-cell-style="{ fontWeight: 'bold' }"
      >
        <el-table-column label="用户信息" min-width="200">
          <template slot-scope="scope">
            <div class="user-info-cell">
              <el-avatar :size="36" :src="scope.row.avatar" icon="el-icon-user" class="mr10" />
              <div>
                <div>{{ scope.row.nickname || '-' }}</div>
                <div class="uid-text">UID: {{ scope.row.uid }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="手机号" min-width="130">
          <template slot-scope="scope">
            <span>{{ scope.row.phone | phoneDesensitize }}</span>
          </template>
        </el-table-column>
        <el-table-column label="积分" min-width="100">
          <template slot-scope="scope">
            <span class="integral-val">{{ scope.row.integral != null ? scope.row.integral : '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="个人奖金" min-width="110">
          <template slot-scope="scope">
            <span>{{ scope.row.selfBonus != null ? ('¥' + scope.row.selfBonus) : '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="注册时间" min-width="150" />
        <el-table-column label="操作" min-width="120" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="viewIntegralDetail(scope.row)">
              查看积分明细
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="block mt20">
        <el-pagination
          :page-sizes="[15, 30, 45, 60]"
          :page-size="userFrom.limit"
          :current-page="userFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { getExternalUserList } from '@/api/integralExternal';

export default {
  name: 'IntegralExternalUser',
  data() {
    return {
      listLoading: false,
      tableData: {
        data: [],
        total: 0,
      },
      userFrom: {
        keywords: '',
        dateLimit: '',
        page: 1,
        limit: 15,
      },
      timeVal: [],
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      const params = { ...this.userFrom };
      if (!params.keywords) delete params.keywords;
      if (!params.dateLimit) delete params.dateLimit;

      getExternalUserList(params)
        .then((res) => {
          this.tableData.data = res.list || [];
          this.tableData.total = res.total || 0;
        })
        .catch(() => {})
        .finally(() => {
          this.listLoading = false;
        });
    },
    seachList() {
      this.userFrom.page = 1;
      this.getList();
    },
    handleReset() {
      this.userFrom = { keywords: '', dateLimit: '', page: 1, limit: 15 };
      this.timeVal = [];
      this.getList();
    },
    onchangeTime(e) {
      this.timeVal = e;
      this.userFrom.dateLimit = e ? e.join(',') : '';
      this.seachList();
    },
    handleSizeChange(val) {
      this.userFrom.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.userFrom.page = val;
      this.getList();
    },
    viewIntegralDetail(row) {
      this.$router.push({
        path: '/integral-external/user/integral-detail',
        query: {
          uid: row.uid,
          nickname: row.nickname || '',
          integral: row.integral != null ? row.integral : '',
          selfBonus: row.selfBonus != null ? row.selfBonus : '',
        },
      });
    },
  },
};
</script>

<style scoped lang="scss">
.user-info-cell {
  display: flex;
  align-items: center;
}
.mr10 {
  margin-right: 10px;
}
.uid-text {
  font-size: 11px;
  color: #999;
}
.integral-val {
  font-weight: bold;
  color: #e6a23c;
}
.mt20 {
  margin-top: 20px;
}
.block {
  text-align: right;
}
.selWidth {
  width: 200px;
}
</style>
