<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"
        :canResize="true"
        :resizeHeightOffset="-20000"
        :pagination="false"
      />
    </n-card>
  </div>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { NTag } from 'naive-ui';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { advDetails } from '@/api/echarts/UserAdv';
  import {defaultStatisticsTimeOptions, defRangeShortcuts, timestampToDate} from '@/utils/dateUtil';
  import { advPositions } from '@/convert/adv/map';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  const useUserStore = useUserStoreWidthOut();
  const columns = reactive([
    {
      title: '广告点id',
      dataIndex: 'position_id',
      key: 'position_id',
      width: 100,
    },
    {
      title: '广告点位置',
      dataIndex: 'position',
      key: 'position',
      render(row) {
        return h(
          NTag,
          {
            style: {
              marginRight: '6px',
            },
            type: 'info',
            bordered: false,
          },
          {
            default: () => advPositions[row.position_id] ?? '未知',
          }
        );
      },
      width: 150,
    },
    {
      title: '展示次数',
      dataIndex: 'show_times',
      key: 'show_times',
      width: 200,
    },
    {
      title: '展示人数',
      dataIndex: 'show_users',
      key: 'show_users',
      width: 200,
    },
    {
      title: '点击次数',
      dataIndex: 'click_times',
      key: 'click_times',
      width: 200,
    },
    {
      title: '点击人数',
      dataIndex: 'click_users',
      key: 'click_users',
      width: 200,
    },
    {
      title: '领取次数',
      dataIndex: 'reward_times',
      key: 'reward_times',
      width: 200,
    },
    {
      title: '领取人数',
      dataIndex: 'reward_users',
      key: 'reward_users',
      width: 200,
    },
  ]);

  const params = ref<any>({
    pageSize: 100,
    day: timestampToDate(defaultStatisticsTimeOptions()),
    days: 0,
    channel_id: '',
    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;
        },
      },
    },
    {
      field: 'eventId',
      component: 'NSelect',
      label: '注册天数',
      componentProps: {
        placeholder: '请选择',
        options: [
          {
            label: '当天',
            value: '0',
          },
          {
            label: '前第1天',
            value: '1',
          },
          {
            label: '前第2天',
            value: '2',
          },
          {
            label: '前第3天',
            value: '3',
          },
          {
            label: '前第4天-7天',
            value: '4_7',
          },
          {
            label: '前第8天-14天',
            value: '8_14',
          },
          {
            label: '前第15-21天',
            value: '15_21',
          },
          {
            label: '前第21_28天',
            value: '21_28',
          },
          {
            label: '29天之前',
            value: '29_',
          },
        ],
        onUpdateValue: (e: any) => {
          params.value.days = e;
        },
      },
    },
    {
      field: 'makeDate',
      component: 'NDatePicker',
      label: '选择日期',
      defaultValue: defaultStatisticsTimeOptions(),
      componentProps: {
        type: 'date',
        clearable: true,
        shortcuts: defRangeShortcuts(),
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.day = timestampToDate(e);
        },
      },
    },
  ];

  const actionRef = ref();

  const formParams = reactive({
    day: '',
    days: 0,
    channel_id: '',
  });

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

  const loadDataTable = (res) => {
    let data = advDetails({ ...formParams, ...params.value, ...res });
    let inGameShowTimes = 0;
    let getRewardTimes = 0;
    let inGameShowUsers = 0;
    let gameRewardUsers = 0;
    let clickTimes = 0;
    let clickUsers = 0;

    columns[2].title = '展示次数(0)';
    columns[3].title = '展示人数(0)';
    columns[4].title = '点击次数(0)';
    columns[5].title = '点击人数(0)';
    columns[6].title = '领取次数(0)';
    columns[7].title = '领取人数(0)';
    data.then((res) => {
      if (res.list == null) {
        return;
      }
      res.list.forEach((info) => {
        console.log(info.reward_times);
        inGameShowTimes += info.show_times;
        getRewardTimes += info.reward_times;
        inGameShowUsers += info.show_users;
        gameRewardUsers += info.reward_users;
        clickTimes += info.click_times;
        clickUsers += info.click_users;
      });
      columns[2].title = '展示次数(' + inGameShowTimes + ')';
      columns[3].title = '展示人数(' + inGameShowUsers + ')';
      columns[4].title = '点击次数(' + clickTimes + ')';
      columns[5].title = '点击人数(' + clickUsers + ')';
      columns[6].title = '领取次数(' + getRewardTimes + ')';
      columns[7].title = '领取人数(' + gameRewardUsers + ')';
    });

    return data;
  };

  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: 100,
      day: timestampToDate(defaultStatisticsTimeOptions()),
      days: 0,
      channel_id: '',
      server_id: null,
    };
    reloadTable();
  }
</script>

<style scoped></style>
