import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { FinLedgerApi } from '#/api/erp/finance/ledger';

import { DICT_TYPE } from '@vben/constants';

import { z } from '#/adapter/form';
import { getAccountSubjectList } from '#/api/erp/finance/subject';

import dayjs from 'dayjs';

/** 行类型 -> 展示文案 */
export const LEDGER_ROW_TYPE_LABEL: Record<string, string> = {
  opening: '期初余额',
  entry: '明细',
  monthly_total: '本月合计',
  yearly_total: '本年累计',
};

/** 行类型 -> Tag 颜色 */
export const LEDGER_ROW_TYPE_COLOR: Record<string, string> = {
  opening: 'blue',
  entry: 'default',
  monthly_total: 'orange',
  yearly_total: 'purple',
};

/**
 * 账页查询表单
 *
 * 说明：账页只统计「已过账」的凭证，未过账、已作废的凭证不计入。
 */
export function useLedgerFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'subjectCode',
      label: '会计科目',
      component: 'ApiTreeSelect',
      componentProps: {
        api: getAccountSubjectList,
        labelField: 'subjectName',
        valueField: 'subjectCode',
        childrenField: 'children',
        placeholder: '请选择会计科目',
        treeDefaultExpandAll: true,
        showSearch: true,
        treeNodeFilterProp: 'subjectName',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'startMonth',
      label: '起始月份',
      component: 'DatePicker',
      componentProps: {
        picker: 'month',
        allowClear: true,
        valueFormat: 'YYYY-MM',
        format: 'YYYY-MM',
        style: { width: '100%' },
        placeholder: '选择起始月份',
      },
      defaultValue: dayjs().startOf('year').format('YYYY-MM'),
    },
    {
      fieldName: 'endMonth',
      label: '结束月份',
      component: 'DatePicker',
      componentProps: {
        picker: 'month',
        allowClear: true,
        valueFormat: 'YYYY-MM',
        format: 'YYYY-MM',
        style: { width: '100%' },
        placeholder: '选择结束月份',
      },
      defaultValue: dayjs().format('YYYY-MM'),
    },
    {
      fieldName: 'includeChildren',
      label: '包含下级科目',
      component: 'Switch',
      componentProps: {
        checkedChildren: '是',
        unCheckedChildren: '否',
      },
      defaultValue: false,
      rules: z.boolean().default(false).optional(),
      help: '开启后，所选科目及其全部下级科目的发生额与余额合并统计',
    },
  ];
}

/** 总账列：期初 + 逐月合计 + 本年累计 */
export function useGeneralColumns(): VxeTableGridOptions<FinLedgerApi.LedgerRow>['columns'] {
  return [
    {
      field: 'rowType',
      title: '行类型',
      width: 110,
      fixed: 'left',
      slots: { default: 'rowType' },
    },
    {
      field: 'period',
      title: '期间',
      width: 110,
    },
    {
      field: 'subjectCode',
      title: '科目编码',
      minWidth: 120,
    },
    {
      field: 'subjectName',
      title: '科目名称',
      minWidth: 160,
    },
    {
      field: 'debit',
      title: '借方发生额',
      minWidth: 140,
      align: 'right',
      formatter: 'formatAmount2',
    },
    {
      field: 'credit',
      title: '贷方发生额',
      minWidth: 140,
      align: 'right',
      formatter: 'formatAmount2',
    },
    {
      field: 'direction',
      title: '方向',
      width: 80,
      align: 'center',
      slots: { default: 'direction' },
    },
    {
      field: 'balance',
      title: '期末余额',
      minWidth: 140,
      align: 'right',
      formatter: 'formatAmount2',
    },
  ];
}

/** 明细账列：期初 + 逐笔分录 + 结转合计 */
export function useDetailColumns(): VxeTableGridOptions<FinLedgerApi.LedgerRow>['columns'] {
  return [
    {
      field: 'rowType',
      title: '行类型',
      width: 110,
      fixed: 'left',
      slots: { default: 'rowType' },
    },
    {
      field: 'date',
      title: '日期',
      width: 120,
      formatter: 'formatDate',
    },
    {
      field: 'voucherNo',
      title: '凭证字号',
      minWidth: 140,
    },
    {
      field: 'subjectCode',
      title: '科目编码',
      minWidth: 120,
    },
    {
      field: 'summary',
      title: '摘要',
      minWidth: 200,
      showOverflow: 'tooltip',
    },
    {
      field: 'debit',
      title: '借方金额',
      minWidth: 140,
      align: 'right',
      formatter: 'formatAmount2',
    },
    {
      field: 'credit',
      title: '贷方金额',
      minWidth: 140,
      align: 'right',
      formatter: 'formatAmount2',
    },
    {
      field: 'direction',
      title: '方向',
      width: 80,
      align: 'center',
      slots: { default: 'direction' },
    },
    {
      field: 'balance',
      title: '余额',
      minWidth: 140,
      align: 'right',
      formatter: 'formatAmount2',
    },
  ];
}

/** 余额方向字典类型，供页面渲染方向标签使用 */
export const LEDGER_DIRECTION_DICT = DICT_TYPE.FINANCE_BALANCE_DIRECTION;
