<template>
  <div>
    <n-card :bordered="false" 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="-30000"
      />
    </n-card>
  </div>
</template>

<script lang="ts" setup>
  import { Component, h, reactive, ref } from 'vue';
  import { BasicTable } from '@/components/Table';
  import { AddRetention } from '@/api/echarts/AddRetention';
  import { defaultStatisticsTimeOptions, timestampToDate } from '@/utils/dateUtil';
  import { FormSchema, useForm } from '@/components/Form';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  import { NButton, NIcon, NTooltip } from 'naive-ui';
  import { HelpCircleOutline } from '@vicons/ionicons5';

  const useUserStore = useUserStoreWidthOut();

  const params = ref({
    server_id: null,
    channel_id: '',
    flag: -1,
  });

  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;
        },
      },
    },
    {
      field: 'flag',
      component: 'NSelect',
      label: '玩家分类',
      componentProps: {
        placeholder: '请选择玩家分类',
        options: useUserStore.config.flag_option,
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.flag = e;
        },
      },
    },
  ];

  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) {
    params.value.channel_id = '';
    params.value.server_id = null;
    params.value.flag = -1;
    console.log(values);
    reloadTable();
  }

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

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

  function calcBar(t, r) {
    if (t == 0 || r == 0) {
      return '-';
    }
    return r + ' / ' + ((r / t) * 100).toFixed(1) + '%';
  }

  const columns = [
    {
      title: '日期',
      dataIndex: 'date',
      key: 'date',
      width: 80,
    },
    {
      dataIndex: 'guideFinishedCount',
      key: 'guideFinishedCount',
      width: 50,
      title(_column) {
        return renderTooltip(
          h(
            NButton,
            {
              ghost: true,
              strong: true,
              tertiary: true,
              size: 'small',
              text: true,
              iconPlacement: 'right',
            },
            { default: () => '条件1', icon: renderIcon(HelpCircleOutline) }
          ),
          '首日注册玩家通关引导关卡的人数'
        );
      },
    },
    {
      dataIndex: 'count',
      key: 'count',
      width: 50,
      title(_column) {
        return renderTooltip(
          h(
            NButton,
            {
              ghost: true,
              strong: true,
              tertiary: true,
              size: 'small',
              text: true,
              iconPlacement: 'right',
            },
            { default: () => '条件2', icon: renderIcon(HelpCircleOutline) }
          ),
          '首日注册玩家进入第一章第一个房间的人数'
        );
      },
    },
    {
      dataIndex: 'day_1_active',
      key: 'day_1_active',
      width: 50,
      title(_column) {
        return renderTooltip(
          h(
            NButton,
            {
              ghost: true,
              strong: true,
              tertiary: true,
              size: 'small',
              text: true,
              iconPlacement: 'right',
            },
            { default: () => '次留 / 占比', icon: renderIcon(HelpCircleOutline) }
          ),
          '次留人数 / 条件2 = 次留百分比'
        );
      },
      render(row) {
        return calcBar(row.count, row.day_1_active);
      },
    },
    {
      title: '2日留',
      dataIndex: 'day_2_active',
      key: 'day_2_active',
      width: 50,
      render(row) {
        return calcBar(row.count, row.day_2_active);
      },
    },
    {
      title: '3日留',
      dataIndex: 'day_3_active',
      key: 'day_3_active',
      width: 50,
      render(row) {
        return calcBar(row.count, row.day_3_active);
      },
    },
    {
      title: '4日留',
      dataIndex: 'day_4_active',
      key: 'day_4_active',
      width: 50,
      render(row) {
        return calcBar(row.count, row.day_4_active);
      },
    },
    {
      title: '5日留',
      dataIndex: 'day_5_active',
      key: 'day_5_active',
      width: 50,
      render(row) {
        return calcBar(row.count, row.day_5_active);
      },
    },
    {
      title: '6日留',
      dataIndex: 'day_6_active',
      key: 'day_6_active',
      width: 50,
      render(row) {
        return calcBar(row.count, row.day_6_active);
      },
    },
    {
      title: '7日留',
      dataIndex: 'day_7_active',
      key: 'day_7_active',
      width: 50,
      render(row) {
        return calcBar(row.count, row.day_7_active);
      },
    },
    {
      title: '14日留',
      dataIndex: 'day_14_active',
      key: 'day_14_active',
      width: 50,
      render(row) {
        return calcBar(row.count, row.day_14_active);
      },
    },
    {
      title: '30日留',
      dataIndex: 'day_30_active',
      key: 'day_30_active',
      width: 50,
      render(row) {
        return calcBar(row.count, row.day_30_active);
      },
    },
  ];

  const actionRef = ref();
  const formParams = reactive({
    day: timestampToDate(defaultStatisticsTimeOptions()),
  });

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

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

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

<style scoped></style>
