<template>
  <div>
    <n-card :bordered="false" class="proCard" title="七日登陆基本信息统计">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
        <template #statusSlot="{ model, field }">
          <n-input v-model:value="model[field]" />
        </template>
      </BasicForm>
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="actionRef"
        @update:checked-row-keys="onCheckedRow"
        :scroll-x="1090"
        :pagination="{ pageSize: 15 }"
        :resizeHeightOffset="-3000"
      />
    </n-card>
    <Task />
    <Award />
  </div>
</template>

<script lang="ts" setup>
  import { reactive, ref, Component, h } from 'vue';
  import { NTooltip, NIcon, NButton } from 'naive-ui';
  import { HelpCircleOutline } from '@vicons/ionicons5';
  import { BasicTable } from '@/components/Table';
  import Task from './components/Task.vue';
  import Award from './components/Award.vue';
  import { defaultStatisticsTimeOptions, timestampToDate } from '@/utils/dateUtil';
  import { SevenBasic } from '@/api/echarts/seven';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  import { FormSchema, useForm } from '@/components/Form';
  const useUserStore = useUserStoreWidthOut();
  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: '日期',
      dataIndex: 'date',
      key: 'date',
      width: 120,
    },
    {
      dataIndex: 'player_count',
      key: 'player_count',
      width: 120,
      title(column) {
        return renderTooltip(
          h(
            NButton,
            {
              ghost: true,
              strong: true,
              tertiary: true,
              size: 'small',
              text: true,
              iconPlacement: 'right',
            },
            { default: () => '参与总玩家', icon: renderIcon(HelpCircleOutline) }
          ),
          '当天打开界面总玩家数'
        );
      },
    },
    {
      // title: '新增有效人数',
      dataIndex: 'valid_count',
      key: 'valid_count',
      width: 120,
      title(column) {
        return renderTooltip(
          h(
            NButton,
            {
              ghost: true,
              strong: true,
              tertiary: true,
              size: 'small',
              text: true,
              iconPlacement: 'right',
            },
            { default: () => '有效新增', icon: renderIcon(HelpCircleOutline) }
          ),
          '当天内新注册玩家，且已完成新手引导关卡的人数'
        );
      },
    },
    {
      dataIndex: 'new_player_count',
      key: 'new_player_count',
      width: 150,
      title(column) {
        return renderTooltip(
          h(
            NButton,
            {
              ghost: true,
              strong: true,
              tertiary: true,
              size: 'small',
              text: true,
              iconPlacement: 'right',
            },
            { default: () => '参与新玩家', icon: renderIcon(HelpCircleOutline) }
          ),
          '打开界面的新增玩家'
        );
      },
    },
    {
      title: '界面打开总数 / 人均',
      dataIndex: 'open_count',
      key: 'open_count',
      width: 150,
      render(row) {
        return (
          row.open_count +
          ' / ' +
          (row.player_count > 0 ? (row.open_count / row.player_count).toFixed(2) : 0)
        );
      },
    },
    {
      title: '当天任务完成总数',
      dataIndex: 'all_task_suc_count',
      key: 'all_task_suc_count',
      width: 150,
      render(row) {
        return row.all_task_suc_count;
      },
    },
    {
      title: '第1天解锁',
      dataIndex: 'unlock1',
      key: 'unlock1',
      width: 100,
      render(row) {
        if (row.unlock1 == 0) {
          return '-';
        }
        return row.unlock1;
      },
    },
    {
      title: '第2天解锁',
      dataIndex: 'unlock2',
      key: 'unlock2',
      width: 100,
      render(row) {
        if (row.unlock2 == 0) {
          return '-';
        }
        return row.unlock2;
      },
    },
    {
      title: '第3天解锁',
      dataIndex: 'unlock3',
      key: 'unlock3',
      width: 100,
      render(row) {
        if (row.unlock3 == 0) {
          return '-';
        }
        return row.unlock3;
      },
    },
    {
      title: '第4天解锁',
      dataIndex: 'unlock4',
      key: 'unlock4',
      width: 100,
      render(row) {
        if (row.unlock4 == 0) {
          return '-';
        }
        return row.unlock4;
      },
    },
    {
      title: '第5天解锁',
      dataIndex: 'unlock5',
      key: 'unlock5',
      width: 100,
      render(row) {
        if (row.unlock5 == 0) {
          return '-';
        }
        return row.unlock5;
      },
    },
    {
      title: '第6天解锁',
      dataIndex: 'unlock6',
      key: 'unlock6',
      width: 100,
      render(row) {
        if (row.unlock6 == 0) {
          return '-';
        }
        return row.unlock6;
      },
    },
    {
      title: '第7天解锁',
      dataIndex: 'unlock7',
      key: 'unlock7',
      width: 100,
      render(row) {
        if (row.unlock7 == 0) {
          return '-';
        }
        return row.unlock7;
      },
    },
  ];
  const actionRef = ref();
  const formParams = reactive({
    pageSize: 10,
    channel_id: '',
    day: timestampToDate(defaultStatisticsTimeOptions()),
  });

  const params = ref<any>({
    pageSize: 10,
    channel_id: '',
    day: timestampToDate(defaultStatisticsTimeOptions()),
    server_id: null,
  });

  const schemas: FormSchema[] = [
    {
      field: 'server_id',
      component: 'NSelect',
      label: '服务器',
      componentProps: {
        placeholder: '请选择服务器',
        defaultValue: params.value.server_id,
        options: useUserStore.config.server_option,
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.server_id = e;
        },
      },
    },
    {
      field: 'channel_id',
      component: 'NTreeSelect',
      label: '推广渠道',
      componentProps: {
        placeholder: '请选择推广渠道',
        clearFilterAfterSelect: true,
        clearable: true,
        filterable: true,
        defaultExpandAll: true,
        options: useUserStore.config.channel_tree_option,
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.channel_id = e;
        },
      },
    },
  ];

  const loadDataTable = async (res) => {
    return await SevenBasic({ ...formParams, ...params.value, ...res });
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    // loadDataTable({});
    actionRef.value.reload();
  }

  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 80,
    schemas,
  });

  function handleSubmit(values: Recordable) {
    console.log(values);
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
    params.value = {
      pageSize: 10,
      channel_id: '',
      day: timestampToDate(defaultStatisticsTimeOptions()),
      server_id: null,
    };
    reloadTable();
  }
</script>

<style scoped></style>
