<script lang="ts" setup>
import { h, useTemplateRef } from 'vue';
import { useDateFormat } from '@vueuse/core';
import { NDatePicker, useDialog, useMessage } from 'naive-ui';
import { client, safeClient } from '@/service/api';
import type { TableBaseColumns, TableFetchData, TableFilterColumns, TableInst } from '@/components/table';
import { RwaStatusEnum } from '@/enum';
import Add from './components/add.vue';

const dialog = useDialog();
const message = useMessage();
const tableInst = useTemplateRef<TableInst>('tableInst');

const fetchData: TableFetchData = ({ pagination, filter }) => {
  return safeClient(() =>
    client.api.admin.rwa.issuance.products.get({
      query: {
        ...pagination,
        ...filter
      }
    })
  );
};

const columns: TableBaseColumns = [
  {
    key: 'selection',
    title: '序号',
    type: 'selection',
    width: 60
  },
  {
    title: '产品代码',
    key: 'code'
  },
  {
    title: '产品名称',
    key: 'name'
  },
  {
    title: '产品估值',
    key: 'estimatedValue'
  },
  {
    title: '产品分类',
    key: 'categoryId'
  },
  {
    title: '创建人ID',
    key: 'createdBy'
  },
  {
    title: '创建时间',
    key: 'createdAt',
    render: (row: any) => {
      return useDateFormat(row.createdAt, 'YYYY-MM-DD HH:mm').value;
    }
  },
  {
    title: '状态',
    key: 'status',
    render: row => {
      return RwaStatusEnum[row.status as keyof typeof RwaStatusEnum];
    }
  },
  {
    title: '描述',
    key: 'description'
  },
  {
    title: '操作',
    fixed: 'right',
    key: 'operation',
    width: 140,
    operations: (row: any) => [
      {
        contentText: '批准',
        size: 'small',
        visible: row.status === 'under_review',
        onClick: async () => {
          await safeClient(() =>
            client.api.admin.rwa.issuance.approve.post({
              productId: row.id as string,
              publishFirstEdition: true
            })
          );
          tableInst.value?.reload();
        }
      },
      {
        contentText: '拒绝',
        size: 'small',
        visible: row.status === 'under_review',
        onClick: async () => {
          await safeClient(() =>
            client.api.admin.rwa.issuance.reject.post({
              productId: row.id as string,
              rejectionReason: '不符合要求'
            })
          );
          tableInst.value?.reload();
        }
      }
      // {
      //   contentText: '编辑',
      //   size: 'small',
      //   onClick: () => {
      //     tableInst.value?.reload();
      //   }
      // }
    ]
  }
];

const filterColumns: TableFilterColumns = [
  {
    title: '产品名称',
    key: 'name'
  },
  {
    title: '产品Code',
    key: 'Code'
  },
  {
    title: '创建时间',
    key: 'createdAt',
    component: NDatePicker
  }
];

function handleAdd() {
  const dialogInstance = dialog.create({
    title: '添加产品',
    content: () =>
      h(Add, {
        onClose: () => {
          dialogInstance.destroy();
          tableInst.value?.reload();
        }
      }),
    style: { width: '600px' },
    showIcon: false,
    onPositiveClick: () => {
      message.success('添加成功');
      tableInst.value?.reload();
    }
  });
}
</script>

<template>
  <TableBase
    ref="tableInst"
    show-header-operation
    :columns="columns"
    :filter-columns="filterColumns"
    :fetch-data="fetchData"
    @add="handleAdd"
  />
</template>

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