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

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

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

const columns: TableBaseColumns = [
  {
    title: '银行名称',
    key: 'nameCn'
  },
  {
    title: '银行编号',
    key: 'bankCode'
  },
  {
    title: '是否启用',
    key: 'isActive',
    width: 100,
    render(row: any) {
      return row.isActive ? '是' : '否';
    }
  },
  {
    title: '排序',
    key: 'sortOrder',
    width: 80
  },
  {
    title: '创建时间',
    key: 'createdAt',
    render(row: any) {
      return useDateFormat(row.createdAt, 'YYYY-MM-DD HH:mm:ss').value;
    }
  },
  {
    title: '操作',
    fixed: 'right',
    key: 'operation',
    width: 160,
    operations: (row: any) => [
      {
        contentText: '编辑',
        type: 'primary',
        ghost: true,
        size: 'small',
        onClick: () => {
          handleEdit(row);
        }
      },
      {
        contentText: '删除',
        type: 'error',
        ghost: true,
        size: 'small',
        onClick: async () => {
          dialog.create({
            title: '提示',
            positiveText: '是',
            negativeText: '否',
            content: '确认删除该银行信息？',
            onPositiveClick: async () => {
              await safeClient(() => client.api.admin.bank_account.banks({ id: row.id }).delete());
              message.success('删除成功');
              tableInst.value?.reload();
            }
          });
        }
      }
    ]
  }
];

const filterColumns: TableFilterColumns = [
  {
    title: '银行名称',
    key: 'nameCn'
  },
  {
    title: '银行编号',
    key: 'bankCode'
  }
];

function handleAdd() {
  const dialogInstance = dialog.create({
    title: '创建银行',
    content: () =>
      h(Add, {
        onClose: () => {
          dialogInstance.destroy();
          tableInst.value?.reload();
        }
      }),
    style: { width: '600px' },
    showIcon: false
  });
}
function handleEdit(row) {
  const dialogInstance = dialog.create({
    title: '编辑银行',
    content: () =>
      h(Edit, {
        data: row,
        onClose: () => {
          dialogInstance.destroy();
          tableInst.value?.reload();
        }
      }),
    style: { width: '600px' },
    showIcon: false
  });
}
</script>

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

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