<template>
  <div>
    <n-card :bordered="false" class="proCard" title="钻石消费统计">
      <BasicForm
        @register="register"
        @submit="handleSubmit"
        @reset="handleReset"
        @keyup.enter="handleSubmit"
        ref="formRef"
      >
        <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"
        :pagination="false"
        :resizeHeightOffset="-20000"
      />
    </n-card>
    <Coin />
  </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 { changedLogsStatistics } from '@/api/echarts/consumptionDetails';
  import { consumptionTypes } from '@/enums/consumptionEnum';
  import { UtilsTool } from '@/utils/macd/UtilsTool';
  import Coin from './Coin.vue';
  import { defaultTimeOptions, defRangeShortcuts } from '@/utils/dateUtil';
  import { useUserStoreWidthOut } from '@/store/modules/user';

  const useUserStore = useUserStoreWidthOut();
  const type = ref(1);
  const params = ref<any>({
    pageSize: 10,
    // createtime: defaultTimeOptions(30),
    server_id: 1,
    channel_id: '',
  });

  function typeMap() {
    const att: any = [];
    for (const key in consumptionTypes) {
      att.push({ value: key, label: consumptionTypes[key] + '(' + key + ')' });
    }
    return att;
  }

  const columns = [
    {
      title: '操作来源',
      dataIndex: 'source',
      key: 'source',
      render(row) {
        return h(
          NTag,
          {
            style: {
              marginRight: '6px',
            },
            type: 'info',
            bordered: false,
          },
          {
            default: () => consumptionTypes[row.source] ?? '未知',
          }
        );
      },
    },
    {
      title: '消费人数',
      dataIndex: 'user_count',
      key: 'user_count',
    },
    {
      title: '消费次数',
      dataIndex: 'counts',
      key: 'counts',
    },
    {
      title: '消费金额',
      dataIndex: 'amount',
      key: 'amount',
      render(row) {
        return UtilsTool.unitConversion(row.amount * -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);
        },
      },
    },
    {
      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: 'source',
      component: 'NSelect',
      label: '操作来源',
      componentProps: {
        defaultValue: null,
        placeholder: '请选择',
        options: typeMap(),
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
    // {
    //   field: 'createtime',
    //   component: 'NDatePicker',
    //   label: '选择日期',
    //   componentProps: {
    //     type: 'daterange',
    //     clearable: true,
    //     shortcuts: defRangeShortcuts(),
    //     defaultValue: defaultTimeOptions(90),
    //     onUpdateValue: (e: any) => {
    //       console.log(e);
    //     },
    //   },
    // },
  ];

  const formRef: any = ref(null);
  const actionRef = ref();
  const formParams = reactive({});

  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 changedLogsStatistics({
      ...formParams,
      ...params.value,
      ...res,
      ...formRef.value.formModel,
      ...{ type: type.value },
    });
  };

  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,
      // createtime: defaultTimeOptions(90),
      server_id: 1,
      channel_id: '',
    };
    console.log(values);
    reloadTable();
  }
</script>

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