import type { VxeGridProps } from '#/adapter/vxe-table';

import {
  getSettlementItemsList,
  getSettlementLedgerLogList,
} from '#/views/finance/withdrawal/api/settlement';

interface RowType {
  id: string;
  nick_name: string;
  email: string;
  avatar: string;
  roles: { name: string }[];
  open_id: string;
  code: string;
  platform_id: string;
  phone: string;
  desc: string;
  created_at: string;
}

export const gridOptions2: VxeGridProps<RowType> = {
  checkboxConfig: {
    highlight: true,
    labelField: '',
  },
  columnConfig: {
    useKey: true,
  },
  rowConfig: {
    useKey: true,
  },
  columns: [
    { type: 'checkbox', width: 60 },
    { field: 'id', align: 'left', title: 'ID', width: 100 },
    { field: 'order.order_no', title: '订单号' },
    { field: 'user_id', title: '名称', slots: { default: 'user_id' } },
    { field: 'money', title: '分账金额' },
    { field: 'fee_type_txt', title: '费用类型' },
    { field: 'status_txt', title: '结算状态' },
    { field: 'created_at', title: '创建时间' },
    { type: 'html', title: '操作', slots: { default: 'action' } },
  ],
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      // 请求后端接口方法
      query: async ({ page }, formValues) => {
        return await getSettlementItemsList({
          page: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
      },
    },
  },
  // height: 'auto',
  border: false,
  toolbarConfig: {
    // 是否显示搜索表单控制按钮
    // @ts-ignore 正式环境时有完整的类型声明
    search: true,
    refresh: true, // 刷新
    print: false, // 打印
    export: false, // 导出
    // custom: true, // 自定义列
    zoom: true, // 最大化最小化
    slots: {
      buttons: 'toolbar-buttons',
    },
    custom: {
      // 自定义列-图标
      icon: 'vxe-icon-menu',
    },
  },
  showOverflow: false,
};

function normalizeMergeByOrder(value: unknown): 0 | 1 {
  if (value === false || value === 0 || value === '0') {
    return 0;
  }
  return 1;
}

export const gridOptions3: VxeGridProps<RowType> = {
  checkboxConfig: {
    highlight: true,
    labelField: '',
  },
  columnConfig: {
    useKey: true,
  },
  rowConfig: {
    keyField: 'id',
    useKey: true,
  },
  columns: [
    { field: 'order_no', title: '订单号' },
    { field: 'order_type_txt', title: '订单类型' },
    { field: 'fee_type_txt', title: '费用类型' },
    { field: 'amount', title: '金额' },
    { field: 'type_txt', title: '类型' },
    { field: 'content', title: '说明' },
    { field: 'created_at', title: '结算时间' },
    { type: 'html', title: '操作', slots: { default: 'action' } },
  ],
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues) => {
        const { merge_by_order, ...rest } = formValues || {};
        return await getSettlementLedgerLogList({
          page: page.currentPage,
          pageSize: page.pageSize,
          merge_by_order: normalizeMergeByOrder(merge_by_order),
          exclude_zero_amount: 1,
          ...rest,
        });
      },
    },
  },
  // height: 'auto',
  border: false,
  toolbarConfig: {
    // 是否显示搜索表单控制按钮
    // @ts-ignore 正式环境时有完整的类型声明
    search: true,
    refresh: true, // 刷新
    print: false, // 打印
    export: false, // 导出
    // custom: true, // 自定义列
    zoom: true, // 最大化最小化
    slots: {
      buttons: 'toolbar-buttons',
    },
    custom: {
      // 自定义列-图标
      icon: 'vxe-icon-menu',
    },
  },
  showOverflow: false,
};
