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

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { PayPerClick } from '@/api/echarts/PayPerClick';
  import { useRouter } from 'vue-router';
  import {defaultStatisticsTimeOptions, defRangeShortcuts, timestampToDate} from '@/utils/dateUtil';
  import { GIdsMap } from '@/views/echarts/goods/map';
  import { findIndex } from '@/utils/array';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  const useUserStore = useUserStoreWidthOut();
  const columns = [
    {
      title: '付费点id',
      dataIndex: 'position_id',
      key: 'position_id',
      width: 100,
    },
    {
      title: '游戏内展示次数',
      dataIndex: 'show_times',
      key: 'show_times',
      width: 200,
    },
    {
      title: '玩家点击次数',
      dataIndex: 'click_times',
      key: 'click_times',
      width: 200,
    },
    {
      title: '领取奖励次数',
      dataIndex: 'success_times',
      key: 'success_times',
      width: 200,
    },
    {
      title: '游戏内展示人数',
      dataIndex: 'show_users',
      key: 'show_users',
      width: 200,
    },
    {
      title: '玩家点击人数',
      dataIndex: 'click_users',
      key: 'click_users',
      width: 200,
    },
    {
      title: '领取奖励人数',
      dataIndex: 'success_users',
      key: 'success_users',
      width: 200,
    },
    {
      title: '对应商品信息',
      dataIndex: 'goods_id',
      key: 'goods_id',
      render(row) {
        console.log('row.goods_id:' + row.goods_id);
        const index = findIndex(row.goods_id, GIdsMap);
        if (index === false) {
          return `未知商品(` + row.goods_id + ')';
        }
        return GIdsMap[index].type_name + '(goods_num：' + GIdsMap[index].goods_num + ')';
      },
      width: 200,
    },
    {
      title: '备注',
      dataIndex: 'comment',
      key: 'comment',
      width: 200,
    },
  ];

  const params = ref<any>({
    pageSize: 10,
    day: timestampToDate(defaultStatisticsTimeOptions()),
    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: '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({
    name: '',
    address: '',
    date: null,
    day: '',
    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 PayPerClick({ ...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(defaultStatisticsTimeOptions()),
      channel_id: '',
      server_id: null,
    };
    reloadTable();
  }
</script>

<style scoped></style>
