<template>
  <div>
    <n-card :bordered="false" class="proCard" title="兑换码列表">
      <BasicForm
        @register="register"
        @submit="handleSubmit"
        @reset="handleReset"
        @keyup.enter="handleSubmit"
        ref="formRef2"
      >
        <template #statusSlot="{ model, field }">
          <n-input v-model:value="model[field]" />
        </template>
      </BasicForm>

      <BasicTable
        :columns="redeemCodeListcolumns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="actionRef"
        :actionColumn="actionColumn"
        @update:checked-row-keys="onCheckedRow"
        :scroll-x="1090"
      >
      </BasicTable>
    </n-card>
    <RetrofitModal ref="RetrofitModalRef" :groupId="groupId" />
  </div>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { useRouter } from 'vue-router';
  import { BasicTable, TableAction } from '@/components/Table';
  import { FormSchema, useForm } from '@/components/Form/index';
  import { Del, RedeemCodeList } from '@/api/cdk/cdk';
  import { redeemCodeListcolumns } from './columns';
  import { useMessage, useDialog } from 'naive-ui';
  import RetrofitModal from './modal/exchangeRecord.vue';
  import {defRangeShortcuts} from "@/utils/dateUtil";

  const formRef2 = ref({});
  const dialog = useDialog();
  const message = useMessage();
  const actionRef = ref();
  const router = useRouter();
  const sn = router.currentRoute.value.params.sn;
  const RetrofitModalRef = ref();
  const groupId = ref(0);

  const schemas: FormSchema[] = [
    {
      field: 'sn',
      component: 'NInput',
      label: '批次编号',
      componentProps: {
        placeholder: '请输入批次编号',
        showButton: false,
        onInput: (e: any) => {
          console.log(e);
        },
      },
      defaultValue: sn,
    },
    {
      field: 'cdk',
      component: 'NInput',
      label: '兑换码',
      componentProps: {
        placeholder: '请输入兑换码',
        showButton: false,
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'created_at',
      component: 'NDatePicker',
      label: '生成日期',
      componentProps: {
        type: 'datetimerange',
        clearable: true,
        shortcuts: defRangeShortcuts(),
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
  ];


  const params = ref({
    pageSize: 10,
    sn: sn,
  });

  const actionColumn = reactive({
    width: 150,
    title: '操作',
    key: 'action',
    // fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '兑换记录',
            onClick: handleConf.bind(null, record),
          },
          {
            label: '删除',
            onClick: handleDelete.bind(null, record),
          },
        ],
        select: (key) => {
          message.info(`您点击了，${key} 按钮`);
        },
      });
    },
  });

  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 80,
    schemas,
  });

  //删除
  function handleDelete(record: Recordable) {
    dialog.info({
      title: '提示',
      content: '再次确认？',
      positiveText: '确认',
      negativeText: '取消',
      maskClosable: false,
      onPositiveClick: () => {
        Del(record)
          .then((_res) => {
            console.log('_res:' + JSON.stringify(_res));
            message.success('操作成功');
            setTimeout(() => {
              reloadTable();
            });
          })
          .catch((_e: Error) => {
            /*            message.error(e.message ?? '操作失败');*/
          });
      },
      onNegativeClick: () => {
        // message.error('取消');
      },
    });
  }
  
  //兑换记录
  function handleConf(record: Recordable) {
    console.log('handleConf', record);
    openUserModal(record.id);
  }
  function openUserModal(id) {
    groupId.value = id;
    const { openDrawer } = RetrofitModalRef.value;
    openDrawer();
  }

  const loadDataTable = async (res) => {
    console.log('loadDataTable res：' + JSON.stringify(res));
    console.log('loadDataTable params：' + JSON.stringify(params.value));
    return await RedeemCodeList({ ...params.value, ...res });
  };

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

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

  function handleSubmit(values: Recordable) {
    params.value = values;
    console.log('handleSubmit values：' + JSON.stringify(values));
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
    params.value = values;
    reloadTable();
  }
</script>

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