<template>
  <div class="n-layout-page-header">
    <n-card :bordered="false" title="新注册玩家付费统计">
      对新注册玩家前期的充值付费情况进行统计，数据根据充值订单实时更新
    </n-card>
  </div>
  <n-card :bordered="false" class="proCard">
    <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"
      :flex-height="false"
      :canResize="true"
      :resizeHeightOffset="-10000"
    >
      <template #tableTitle>
        <n-button type="primary" @click="exportOrders" :loading="loadingRef"> 导出数据 </n-button>
      </template>
    </BasicTable>
  </n-card>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { sevenStatisticsList } from '@/api/echarts/rechargeList';
  import { columns } from './columns';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  import { delNullProperty, encodeParams } from '@/utils/array';
  import { defRangeShortcuts } from '@/utils/dateUtil';

  const params = ref<any>({
    pageSize: 10,
    created_at: null,
    channel_id: '',
    platform: 0,
    server_id: null,
    flag: -1,
  });

  const useUserStore = useUserStoreWidthOut();
  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;
        },
      },
    },
    {
      field: 'platform',
      component: 'NSelect',
      label: '下单渠道',
      componentProps: {
        placeholder: '请选择下单渠道',
        options: [
          {
            label: '全部',
            value: 0,
          },
          {
            label: '安卓平台',
            value: 1,
          },
          {
            label: '苹果平台',
            value: 2,
          },
        ],
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.platform = e;
        },
      },
    },
    {
      field: 'created_at',
      component: 'NDatePicker',
      label: '选择日期',
      componentProps: {
        type: 'datetimerange',
        clearable: true,
        shortcuts: defRangeShortcuts(),
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.created_at = e;
        },
      },
    },
  ];

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

  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 sevenStatisticsList({ ...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) {
    params.value = {
      pageSize: 10,
      created_at: null,
      server_id: null,
      channel_id: '',
      flag: -1,
    };
    console.log(values);
  }

  const loadingRef = ref(false);
  function exportOrders() {
    loadingRef.value = true;
    window.location.href =
      '/api/gm/payorder/sevenStatistics?' +
      encodeParams(
        delNullProperty({ ...params.value, is_export: 1, ...{ authorization: useUserStore.token } })
      );
    setTimeout(() => {
      loadingRef.value = false;
    }, 2000);
  }
</script>

<style lang="less" scoped></style>
