<template>
  <div>
    <n-card :bordered="false" class="proCard" title="活动关卡">
      活动关卡数据每10分钟同步一次
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="actionRef"
        @update:checked-row-keys="onCheckedRow"
        :scroll-x="1090"
        :flex-height="false"
        :pagination="false"
        :resizeHeightOffset="-3000"
      />
    </n-card>
  </div>
</template>

<script lang="ts" setup>
  import { Component, h, ref } from 'vue';
  import { BasicTable } from '@/components/Table';
  import { ActivityList } from '@/api/echarts/LevelProgress';
  import { userInfoObj } from '@/views/account/accountList/userInfo';
  import { timestampToTime } from '@/utils/dateUtil';
  import { NButton, NIcon, NTooltip } from 'naive-ui';
  import { HelpCircleOutline } from '@vicons/ionicons5';

  const props = withDefaults(defineProps<Props>(), { userId: 0, userInfo: userInfoObj });

  interface Props {
    userId?: number | string;
    userInfo?: any;
  }

  const renderTooltip = (trigger, content) => {
    return h(NTooltip, null, {
      trigger: () => trigger,
      default: () => content,
    });
  };

  function renderIcon(icon: Component) {
    return () => h(NIcon, null, { default: () => h(icon) });
  }

  const columns = [
    {
      title: '关卡',
      render: (rows, _) => {
        return rows.name + '(' + rows.id + ')';
      },
    },
    {
      title: '困难度',
      dataIndex: 'difficultyName',
      key: 'difficultyName',
    },
    {
      title: '难度系数',
      dataIndex: 'degree',
      key: 'degree',
      title(_column) {
        return renderTooltip(
          h(
            NButton,
            {
              ghost: true,
              strong: true,
              tertiary: true,
              size: 'small',
              text: true,
              iconPlacement: 'right',
            },
            { default: () => '难度系数', icon: renderIcon(HelpCircleOutline) }
          ),
          '远征代表的是通关房间数'
        );
      },
    },
    {
      dataIndex: 'rewardMulti',
      key: 'rewardMulti',
      title(_column) {
        return renderTooltip(
          h(
            NButton,
            {
              ghost: true,
              strong: true,
              tertiary: true,
              size: 'small',
              text: true,
              iconPlacement: 'right',
            },
            { default: () => '奖励倍数', icon: renderIcon(HelpCircleOutline) }
          ),
          '当前只有佣兵要塞、九层妖塔、奇诡皇陵支持'
        );
      },
    },
    {
      title: '奖励倍数刷新时间',
      dataIndex: 'rewardEffectTime',
      key: 'rewardEffectTime',
      render: (rows, _) => {
        if (rows.rewardEffectTime < 1) {
          return '-';
        }
        return timestampToTime(rows.rewardEffectTime);
      },
    },
    {
      title: '总挑战次数',
      dataIndex: 'challenge_count',
      key: 'challenge_count',
    },
    {
      title: '通关次数',
      dataIndex: 'clearance_count',
      key: 'clearance_count',
    },
    {
      title: '失败次数',
      dataIndex: 'fail_count',
      key: 'fail_count',
      render: (rows, _) => {
        return rows.fail_count;
      },
    },
  ];

  const actionRef = ref();

  const loadDataTable = async (_res) => {
    let lists = await ActivityList({
      ...{ player_id: props.userId },
    });
    console.log('lists:' + JSON.stringify(lists));
    lists.list.sort((a, b) => {
      return a.id < b.id ? -1 : 1;
    });

    return lists;
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }
</script>

<style lang="less" scoped></style>
