<template>
  <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"
    >
      <template #tableTitle>
        <n-button type="primary" @click="exportOrders"> 导出数据 </n-button>
      </template>
    </BasicTable>
  </n-card>
</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 { rechargeList } from '@/api/echarts/rechargeList';
  import { columns } from './columns';
  import { useRouter } from 'vue-router';
  import { timestampToTimeNF } from '@/utils/dateUtil';
  import { encodeParams } from '@/utils/array';
  import { userInfoObj } from '@/views/account/accountList/userInfo';

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

  const schemas: FormSchema[] = [
    {
      field: 'type',
      component: 'NSelect',
      label: '下单渠道',
      componentProps: {
        placeholder: '请选择下单渠道',
        options: [
          {
            label: '安卓平台',
            value: 1,
          },
          {
            label: '苹果平台',
            value: 2,
          },
        ],
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.type = e;
        },
      },
    },
    {
      field: 'createtime',
      component: 'NDatePicker',
      label: '选择日期',
      // defaultValue: [1183135260000, 1183135260000],
      componentProps: {
        type: 'datetimerange',
        clearable: true,
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.begin = timestampToTimeNF(e[0]);
          params.value.end = timestampToTimeNF(e[1]);
        },
      },
    },
  ];

  const router = useRouter();
  const formRef: any = ref(null);
  const message = useMessage();
  const actionRef = ref();

  const showModal = ref(false);
  const formBtnLoading = ref(false);
  const formParams = reactive({
    type: 0,
    begin: '',
    end: '',
    playerId: props.userId,
  });

  const params = ref({
    pageSize: 10,
    type: 0,
    begin: '',
    end: '',
    playerId: props.userId,
  });

  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 rechargeList({ ...formParams, ...params.value, ...res });
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function handleEdit(record: Recordable) {
    console.log('点击了编辑', record);
    router.push({ name: 'basic-info', params: { id: record.id } });
  }

  function handleDelete(record: Recordable) {
    console.log('点击了删除', record);
    message.info('点击了删除');
  }

  function handleSubmit(values: Recordable) {
    console.log(values);
    reloadTable();
  }

  function exportOrders() {
    // this.$confirm("确认导出?", "⚠️警告").then(async () => {
    // let params = {
    //   page: 1,
    //   begin: this.begin,
    //   end: this.end,
    //   type: this.platform,
    //   playerId: this.playerID,
    //   token: this.token,
    // };
    window.location.href = '/api/gm/payorder/export?' + encodeParams(params.value);
  }

  function handleReset(values: Recordable) {
    params.value = {
      pageSize: 10,
      type: 0,
      begin: '',
      end: '',
      playerId: props.userId,
    };
    console.log(values);
  }
</script>

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