<template>
  <div class="user-sign-stat-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.userId" clearable placeholder="请输入用户ID" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText">
      <el-table-column align="center" label="用户信息" width="200">
        <template #default="{ row }">
          <div v-if="row.user" style="display: flex; align-items: center; justify-content: center">
            <el-image
              v-if="row.user.avatar"
              :src="getThumbUrl(row.user.avatar)"
              style="width: 40px; height: 40px; border-radius: 50%; margin-right: 10px"
            />
            <div>
              <div>{{ row.user.nickname }}</div>
              <div style="font-size: 12px; color: #999">ID: {{ row.user.id }}</div>
            </div>
          </div>
          <div v-else>{{ row.userId }}</div>
        </template>
      </el-table-column>

      <el-table-column align="center" label="应用ID" prop="appId" show-overflow-tooltip />
      <el-table-column align="center" label="最后签到日期" prop="lastSignDate" show-overflow-tooltip />
      <el-table-column align="center" label="当前连续签到天数" prop="continuousDays" show-overflow-tooltip />
      <el-table-column align="center" label="累计签到天数" prop="totalDays" show-overflow-tooltip />
    </el-table>

    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { getUserSignStatList } from '@/api/system/system'
  import { getThumbUrl } from '@/utils/blessing'

  export default {
    name: 'UserSignStat',
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          userId: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      getThumbUrl,
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        try {
          const { data } = await getUserSignStatList(this.queryForm)
          this.list = data.list
          this.total = data.totalCount
        } catch (error) {
          console.error(error)
        } finally {
          this.listLoading = false
        }
      },
    },
  }
</script>

<style scoped>
  .user-sign-stat-container {
    padding: 20px;
  }
</style>
