<template>
  <div>
    <n-card :bordered="false" class="proCard" title="玩家近90天广告观看统计">
      <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 { advUserDetails } from '@/api/echarts/UserAdv';
  import {
    defaultStatisticsTimeOptions,
    defRangeShortcuts,
    timestampToDate,
  } from '@/utils/dateUtil';
  import { advPositions } from '@/convert/adv/map';
  import { userInfoObj } from '@/views/account/accountList/userInfo';

  interface Props {
    userId?: number | string;
    userInfo?: any;
  }
  const props = withDefaults(defineProps<Props>(), { userId: 0, userInfo: userInfoObj });

  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: 'click_times',
      key: 'click_times',
      width: 200,
    },
    {
      title: '领取次数',
      dataIndex: 'reward_times',
      key: 'reward_times',
      width: 200,
    },
  ]);

  const params = ref<any>({
    pageSize: 100,
    event_at: null,
  });

  const schemas: FormSchema[] = [
    {
      field: 'event_at',
      component: 'NDatePicker',
      label: '选择日期',
      componentProps: {
        type: 'datetimerange',
        clearable: true,
        shortcuts: defRangeShortcuts(),
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.event_at = e;
        },
      },
    },
  ];

  const actionRef = ref();
  const formParams = reactive({
    event_at: null,
  });

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

  const loadDataTable = (res) => {
    let data = advUserDetails({
      ...formParams,
      ...params.value,
      ...res,
      ...{ user_id: props.userId },
    });

    let inGameShowTimes = 0;
    let getRewardTimes = 0;
    let clickTimes = 0;

    columns[2].title = '展示次数(0)';
    columns[3].title = '点击次数(0)';
    columns[4].title = '领取次数(0)';
    data.then((res) => {

      if (res.list == null) {
        return;
      }

      res.list.forEach((info) => {
        inGameShowTimes += info.show_times;
        getRewardTimes += info.reward_times;
        clickTimes += info.click_times;
      });

      columns[2].title = '展示次数(' + inGameShowTimes + ')';
      columns[3].title = '点击次数(' + clickTimes + ')';
      columns[4].title = '领取次数(' + getRewardTimes + ')';
    });

    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,
      event_at: null,
    };
    reloadTable();
  }
</script>

<style scoped></style>
