<template>
  <div>
    <n-card :bordered="false" class="proCard">
      <div class="n-layout-page-header">
        <n-card :bordered="false" title="消费变动记录">
          <!--          记录了平台玩家在游戏中的钻石、金币消耗和增加明细，统计时间从2022年9月1日开始。-->
        </n-card>
      </div>
      <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="{ pageSize: userId > 0 ? 10 : 15 }"
      />
    </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 { consumptionDetails } from '@/api/echarts/consumptionDetails';
  import { defRangeShortcuts, timestampToTime } from '@/utils/dateUtil';
  import { userInfoObj } from '@/views/account/accountList/userInfo';
  import { consumptionTypes } from '@/enums/consumptionEnum';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  import { endOfToday, startOfToday } from 'date-fns';

  const useUserStore = useUserStoreWidthOut();

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

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

  const columns = [
    {
      title: '序号',
      dataIndex: 'id',
      key: 'id',
      width: 80,
    },
    {
      title: '玩家ID',
      dataIndex: 'user_id',
      key: 'user_id',
      width: 100,
    },
    {
      title: '玩家昵称',
      dataIndex: 'nickName',
      key: 'nickName',
      width: 130,
    },
    {
      title: '操作来源',
      dataIndex: 'source',
      key: 'source',
      render(row) {
        return h(
          NTag,
          {
            style: {
              marginRight: '6px',
            },
            type: 'info',
            bordered: false,
          },
          {
            default: () => consumptionTypes[row.source] ?? '未知',
          }
        );
      },
      width: 150,
    },
    {
      title: '变动前金币',
      dataIndex: 'coin_sum',
      key: 'coin_sum',
      render(row) {
        if (row.coin == 0) {
          return '-';
        }
        return row.coin_sum - row.coin;
      },
      width: 100,
    },
    {
      title: '变动金币',
      dataIndex: 'coin',
      key: 'coin',
      render(row) {
        if (row.coin == 0) {
          return '-';
        }
        return row.coin;
      },
      width: 100,
    },
    {
      title: '变动后剩余金币',
      dataIndex: 'coin_sum',
      key: 'coin_sum',
      render(row) {
        if (row.coin == 0) {
          return '-';
        }
        return row.coin_sum;
      },
      width: 150,
    },
    {
      title: '变动前钻石',
      dataIndex: 'coin_sum',
      key: 'coin_sum',
      render(row) {
        if (row.diamond == 0) {
          return '-';
        }
        return row.diamond_sum - row.diamond;
      },
      width: 100,
    },
    {
      title: '变动钻石',
      dataIndex: 'diamond',
      key: 'diamond',
      render(row) {
        if (row.diamond == 0) {
          return '-';
        }
        return row.diamond;
      },
      width: 100,
    },
    {
      title: '变动后剩余钻石',
      dataIndex: 'diamond_sum',
      key: 'diamond_sum',
      render(row) {
        if (row.diamond == 0) {
          return '-';
        }
        return row.diamond_sum;
      },
      width: 150,
    },
    {
      title: '变动数量',
      dataIndex: 'times',
      key: 'times',
      render: (row, _) => {
        if (row.times == 0) {
          return '-';
        }
        return row.times;
      },
      width: 100,
    },
    {
      title: '变动时间',
      dataIndex: 'time',
      key: 'time',
      render: (row, _) => {
        return timestampToTime(row.time);
      },
      width: 180,
    },
  ];

  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: 'source',
      component: 'NSelect',
      label: '操作来源',
      componentProps: {
        defaultValue: null,
        placeholder: '请选择',
        options: typeMap(),
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'createtime',
      component: 'NDatePicker',
      label: '选择日期',
      defaultValue:
        props.userId > 0
          ? [startOfToday().getTime() - 86400 * 1000 * 29, endOfToday().getTime()]
          : [startOfToday().getTime() - 86400 * 1000 * 6, endOfToday().getTime()],
      componentProps: {
        type: 'datetimerange',
        clearable: true,
        shortcuts: defRangeShortcuts(),
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'user_id',
      component: 'NInputNumber',
      label: '玩家ID',
      defaultValue: props.userId > 0 ? props.userId : null,
      componentProps: {
        disabled: props.userId > 0,
        placeholder: '请输入玩家ID',
        showButton: false,
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
  ];

  const formRef: any = ref(null);
  const actionRef = ref();
  const params = ref<any>({
    pageSize: 10,
    player_id: props.userId > 0 ? props.userId : null,
    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 consumptionDetails({
      ...params.value,
      ...res,
      ...formRef.value.formModel,
    });
  };

  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,
      channel_id: '',
    };
    params.value.player_id = props.userId > 0 ? props.userId : null;
    console.log(values);
    reloadTable();
  }
</script>

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