<template>
  <div>
    <n-card :bordered="false" class="proCard">
      <div class="n-layout-page-header">
        <n-card :bordered="false" title="限时礼包综合统计">
          当前选择时间区间内 所有限时礼包共被触发 {{ allTriggerCount }} 次
        </n-card>
      </div>
      <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"
        :resizeHeightOffset="-3000"
        :pagination="{ pageSize: 15 }"
      />
    </n-card>
  </div>
</template>

<script lang="ts" setup>
  import { Component, h, reactive, ref } from 'vue';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { List } from '@/api/echarts/limitgift';
  import {defRangeShortcuts, timestampToDate} from '@/utils/dateUtil';
  import { NButton, NIcon, NTooltip } from 'naive-ui';
  import { HelpCircleOutline } from '@vicons/ionicons5';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  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 allTriggerCount = ref(0);

  const columns = [
    {
      title: '礼包ID',
      dataIndex: 'id',
      key: 'id',
    },
    {
      title: '礼包类型',
      dataIndex: 'type',
      key: 'type',
      render: (rows, _) => {
        allTriggerCount.value = rows.allTriggerCount;
        return rows.type;
      },
    },
    {
      title(column) {
        return renderTooltip(
          h(
            NButton,
            {
              ghost: true,
              strong: true,
              tertiary: true,
              size: 'small',
              text: true,
              iconPlacement: 'right',
            },
            { default: () => '触发值', icon: renderIcon(HelpCircleOutline) }
          ),
          '' +
            '类型1参数：[登录时触发概率]\n' +
            '类型2参数：[等级]\n' +
            '类型3参数：[激活角色ID]\n' +
            '类型4参数：[角色ID,角色等级]\n' +
            '类型5参数：[角色ID,进阶等级]\n' +
            '类型6参数：[通关章节ID]\n' +
            '类型7参数：[章节ID,闯关进度]\n' +
            '类型8参数：[佩戴件数,装备品质]\n'
        );
      },
      dataIndex: 'value',
      key: 'value',
      render: (rows, _) => {
        return rows.value;
      },
    },
    {
      title(column) {
        return renderTooltip(
          h(
            NButton,
            {
              ghost: true,
              strong: true,
              tertiary: true,
              size: 'small',
              text: true,
              iconPlacement: 'right',
            },
            { default: () => '出现次数 / 出现率', icon: renderIcon(HelpCircleOutline) }
          ),
          '出现率 = 出现次数 / 总触发次数'
        );
      },
      dataIndex: 'showCount',
      key: 'showCount',
      render: (rows, _) => {
        let rate = 0;
        if (rows.allTriggerCount > 0 && rows.showCount > 0) {
          rate = ((rows.showCount / rows.allTriggerCount) * 100).toFixed(2);
        }
        return rows.showCount + ' / ' + rate + '%';
      },
    },
    {
      title(column) {
        return renderTooltip(
          h(
            NButton,
            {
              ghost: true,
              strong: true,
              tertiary: true,
              size: 'small',
              text: true,
              iconPlacement: 'right',
            },
            { default: () => '购买次数 / 购买率', icon: renderIcon(HelpCircleOutline) }
          ),
          '购买率 = 购买次数 / 出现次数'
        );
      },
      dataIndex: 'payCount',
      key: 'payCount',
      render: (rows, _) => {
        let rate = 0;
        if (rows.payCount > 0 && rows.showCount > 0) {
          rate = ((rows.payCount / rows.showCount) * 100).toFixed(2);
        }
        return rows.payCount + ' / ' + rate + '%';
      },
    },
  ];

  const params = ref<any>({
    pageSize: 10,
    day: timestampToDate(new Date() - 86400000),
    endDay: timestampToDate(new Date() - 86400000),
    channel_id: '',
    server_id: 1,
  });

  const schemas: FormSchema[] = [
    {
      field: 'server_id',
      component: 'NSelect',
      label: '服务器',
      componentProps: {
        placeholder: '请选择服务器',
        defaultValue: params.value.server_id,
        options: useUserStore.config.server_simple_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: 'createtime',
      component: 'NDatePicker',
      label: '选择日期',
      componentProps: {
        type: 'daterange',
        clearable: true,
        shortcuts: defRangeShortcuts(),
        defaultValue: [new Date() - 86400000, new Date() - 86400000],
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.day = timestampToDate(e[0]);
          params.value.endDay = timestampToDate(e[1]);
        },
      },
    },
  ];

  const actionRef = ref();
  const formParams = reactive({
    day: '',
    endDay: '',
    channel_id: '',
  });

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

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

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

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

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

  function handleReset(values: Recordable) {
    console.log(values);
    params.value = {
      pageSize: 10,
      day: timestampToDate(new Date() - 86400000),
      endDay: timestampToDate(new Date() - 86400000),
      channel_id: '',
      server_id: 1,
    };
    reloadTable();
  }
</script>

<style scoped></style>
