<script lang="ts" setup>
import { reactive, ref, watch } from 'vue';

import { pageResidentInviteInvitee } from '#/api/legacy/resident-activity';
import UserProfileLink from '#/views/operate/components/user-profile-link.vue';
import { formatDate } from '#/views/system/shared';

import {
  Drawer,
  Pagination,
  Table,
  Tag,
} from 'antdv-next';

const props = defineProps<{
  inviterUserId?: number | string | null;
  monthKey?: string;
  open: boolean;
  sysOrigin?: string;
}>();

const emit = defineEmits<{
  close: [];
}>();

const loading = ref(false);
const total = ref(0);
const list = ref<Array<Record<string, any>>>([]);

const query = reactive<Record<string, any>>({
  cursor: 1,
  inviterUserId: '',
  limit: 10,
  monthKey: '',
  sysOrigin: '',
});

const columns = [
  { dataIndex: 'inviteeProfile', key: 'inviteeProfile', title: '被邀请人', width: 280 },
  { dataIndex: 'bindTime', key: 'bindTime', title: '绑定时间', width: 180 },
  { dataIndex: 'rechargeCoins', key: 'rechargeCoins', title: '累计充值金币', width: 160 },
  { dataIndex: 'validUser', key: 'validUser', title: '有效用户', width: 120 },
  { dataIndex: 'validTime', key: 'validTime', title: '达成时间', width: 180 },
  { dataIndex: 'rewardedRuleIds', key: 'rewardedRuleIds', title: '触发档位', width: 220 },
];

async function loadData(reset = false) {
  if (!props.open || !query.sysOrigin || !query.inviterUserId) {
    list.value = [];
    total.value = 0;
    return;
  }
  if (reset) {
    query.cursor = 1;
  }
  loading.value = true;
  try {
    const result = await pageResidentInviteInvitee({ ...query });
    list.value = result.records || [];
    total.value = result.total || 0;
  } finally {
    loading.value = false;
  }
}

function handlePageChange(page: number, pageSize: number) {
  query.cursor = page;
  query.limit = pageSize;
  void loadData();
}

watch(
  () => [props.open, props.sysOrigin, props.inviterUserId, props.monthKey],
  () => {
    query.sysOrigin = String(props.sysOrigin || '');
    query.inviterUserId = String(props.inviterUserId || '');
    query.monthKey = String(props.monthKey || '');
    void loadData(true);
  },
  { immediate: true },
);
</script>

<template>
  <Drawer
    :open="open"
    title="邀请明细"
    width="980"
    @close="emit('close')"
  >
    <Table
      :columns="columns"
      :data-source="list"
      :loading="loading"
      :pagination="false"
      row-key="relationId"
      :scroll="{ x: 1100 }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'inviteeProfile'">
          <UserProfileLink
            :profile="{
              account: record.inviteeAccount,
              id: record.inviteeUserId,
              userAvatar: record.inviteeAvatar,
              userNickname: record.inviteeNickname,
            }"
          />
        </template>
        <template v-else-if="column.key === 'bindTime'">
          {{ formatDate(record.bindTime) }}
        </template>
        <template v-else-if="column.key === 'validUser'">
          <Tag :color="record.validUser ? 'success' : 'default'">
            {{ record.validUser ? '是' : '否' }}
          </Tag>
        </template>
        <template v-else-if="column.key === 'validTime'">
          {{ record.validTime ? formatDate(record.validTime) : '-' }}
        </template>
        <template v-else-if="column.key === 'rewardedRuleIds'">
          {{ record.rewardedRuleIds || '-' }}
        </template>
      </template>
    </Table>

    <div v-if="total > 0" class="pager">
      <Pagination
        :current="query.cursor"
        :page-size="query.limit"
        :total="total"
        show-size-changer
        @change="handlePageChange"
        @showSizeChange="handlePageChange"
      />
    </div>
  </Drawer>
</template>

<style scoped>
.pager {
  display: flex;
  justify-content: flex-end;
  margin-top: 16px;
}
</style>
