<template>
  <div class="adWatch-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12" />
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item><el-input v-model.trim="queryForm.keyword" clearable placeholder="请输入查询条件" /></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-right-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText">
      <el-table-column align="center" label="ID" prop="id" show-overflow-tooltip width="220" />

      <el-table-column align="left" label="用户信息" min-width="200" show-overflow-tooltip>
        <template #default="{ row }">
          <div v-if="row.user && row.user.id" class="author-cell">
            <el-image class="author-avatar" :preview-src-list="[getThumbUrl(row.user.avatar)]" :src="getThumbUrl(row.user.avatar)" />
            <div class="author-meta">
              <div>
                <strong>昵称：</strong>
                {{ row.user.nickname || '--' }}
              </div>
              <div>
                <strong>ID：</strong>
                {{ row.user.id }}
              </div>
            </div>
          </div>
          <div v-else>
            <div>User ID: {{ row.userId }}</div>
          </div>
        </template>
      </el-table-column>

      <el-table-column align="center" label="广告位ID" prop="adPlacementId" show-overflow-tooltip />

      <el-table-column align="center" label="是否看完" show-overflow-tooltip>
        <template #default="{ row }">
          <el-tag v-if="row.consumed" type="success">是</el-tag>
          <el-tag v-else type="danger">否</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="观看日期" prop="day" show-overflow-tooltip />

      <el-table-column align="center" label="完成时间" show-overflow-tooltip width="160">
        <template #default="{ row }">
          {{ formatTime(row.consumedAt) }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="创建时间" show-overflow-tooltip width="160">
        <template #default="{ row }">
          {{ formatTime(row.createdAt) }}
        </template>
      </el-table-column>
    </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 { getAdWatchTicketList } from '@/api/system/index'
  import { formatTime } from '@/utils'
  import { getThumbUrl } from '@/utils/blessing'

  export default {
    name: 'AdWatchRecord',
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          keyword: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      getThumbUrl,
      formatTime,
      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
        const { data } = await getAdWatchTicketList(this.queryForm)
        this.list = data.list
        this.total = data.totalCount
        this.listLoading = false
      },
    },
  }
</script>

<style scoped>
  .author-cell {
    display: flex;
    align-items: center;
    gap: 12px;
  }
  .author-avatar {
    width: 50px;
    height: 50px;
    object-fit: cover;
    border-radius: 50%;
  }
  .author-meta {
    display: flex;
    flex-direction: column;
  }
</style>
