import Content from '@/sheep/layouts/content.vue';
import { SELECT, CRUD } from '@/sheep/request/crud';
import { request } from '@/sheep/request';
import i18n from '@/locales';
import { getCurrentLanguage } from '@/locales';
const currentLanguage = getCurrentLanguage();

// 获取 t 函数
const { t } = i18n.global;

// 财务模块公共配置
const financeConfig = {
  // 币种配置
  currencies: {
    1: 'BDT',
  },

  // 状态配置
  status: {
    recharge: {
      1: () => t('modules.recharge.processing'),
      2: () => t('modules.recharge.success'),
      3: () => t('modules.recharge.failed'),
      4: () => t('modules.recharge.timeout'),
    },
    withdraw: {
      1: () => t('modules.withdraw.processing'),
      2: () => t('modules.withdraw.approved'),
      3: () => t('modules.withdraw.rejected'),
      4: () => t('modules.withdraw.success'),
      5: () => t('modules.withdraw.failed'),
      6: () => t('modules.withdraw.timeout'),
    },
  },

  // 账户类型配置
  accountTypes: {
    1: () => t('modules.finance.walletBalance'),
    2: () => t('modules.finance.accountEarnings'),
  },

  // 操作日志类型配置
  logTypes: {
    1: () => t('modules.finance.submitRechargeOrder'),
    2: () => t('modules.finance.rechargeCallThirdParty'),
    3: () => t('modules.finance.rechargeCallbackSuccess'),
    4: () => t('modules.finance.submitWithdrawApplication'),
    5: () => t('modules.finance.withdrawApproved'),
    6: () => t('modules.finance.withdrawRejected'),
    7: () => t('modules.finance.withdrawCallThirdParty'),
    8: () => t('modules.finance.withdrawCallbackSuccess'),
    9: () => t('modules.finance.withdrawCallbackFailed'),
  },
};

// 公共工具函数
const financeUtils = {
  // 获取币种文本
  getCurrencyText: (currency) => financeConfig.currencies[currency] || 'BDT',

  // 获取充值状态文本
  getRechargeStatusText: (status) => {
    const statusFn = financeConfig.status.recharge[status];
    return statusFn ? statusFn() : t('common.unknown');
  },

  // 获取提现状态文本
  getWithdrawStatusText: (status) => {
    const statusFn = financeConfig.status.withdraw[status];
    return statusFn ? statusFn() : t('common.unknown');
  },

  // 获取账户类型文本
  getAccountTypeText: (accountType) => {
    const typeFn = financeConfig.accountTypes[accountType];
    return typeFn ? typeFn() : t('common.unknown');
  },

  // 获取操作日志类型文本
  getLogTypeText: (type) => {
    const typeFn = financeConfig.logTypes[type];
    return typeFn ? typeFn() : t('modules.finance.unknownOperation');
  },

  // 获取状态类型（用于标签颜色）
  getStatusType: (status, type = 'withdraw') => {
    if (type === 'recharge') {
      const statusMap = {
        1: 'warning', // 处理中
        2: 'success', // 充值成功
        3: 'danger', // 充值失败
        4: 'info', // 超时取消
      };
      return statusMap[status] || 'info';
    } else {
      const statusMap = {
        1: 'warning', // 处理中
        2: 'success', // 审核通过
        3: 'danger', // 审核拒绝
        4: 'success', // 提现成功
        5: 'danger', // 提现失败
        6: 'info', // 超时取消
      };
      return statusMap[status] || 'info';
    }
  },

  // 获取渠道类型（用于标签颜色）
  getChannelType: (channel) => {
    const channelMap = {
      1: 'info', // TKPAY
      2: 'success', // 3QPAY
    };
    return channelMap[channel] || 'info';
  },

  // 佣金相关状态处理函数
  getCommissionStatusText: (status) => {
    const statusMap = {
      1: () => t('modules.finance.unsettled'),
      2: () => t('modules.finance.settled'),
    };
    const statusFn = statusMap[status];
    return statusFn ? statusFn() : t('common.unknown');
  },

  getCommissionStatusType: (status) => {
    const statusMap = {
      1: 'warning', // 未结算
      2: 'success', // 已结算
    };
    return statusMap[status] || 'info';
  },

  getBizTypeText: (bizType) => {
    const typeMap = {
      1001: () => t('modules.finance.recharge'),
      2001: () => t('modules.finance.withdraw'),
      3001: () => t('modules.finance.groupPayment'),
      3002: () => t('modules.finance.joinPayment'),
      4001: () => t('modules.finance.incompleteRefund'),
      4002: () => t('modules.finance.completeRefund'),
      5001: () => t('modules.finance.groupRedPacketEarnings'),
      5002: () => t('modules.finance.joinRedPacketEarnings'),
      5003: () => t('modules.finance.signInRedPacketEarnings'),
      5004: () => t('modules.finance.subordinateRedPacketCommission'),
      5005: () => t('modules.finance.subSubordinateRedPacketCommission'),
      6001: () => t('modules.finance.rechargeRebate'),
      7001: () => t('modules.finance.withdrawalFee'),
    };
    const typeFn = typeMap[bizType];
    return typeFn ? typeFn() : t('modules.finance.unknownType');
  },

  // 格式化金额，保留两位小数
  formatAmount: (amount) => {
    if (amount === null || amount === undefined) return '0.00';
    return Number(amount || 0).toFixed(2);
  },

  // 格式化金额并添加货币符号
  formatCurrency: (amount, currency = 'BDT') => {
    const formattedAmount = financeUtils.formatAmount(amount);
    return `৳${formattedAmount}`;
  },
};

const route = {
  path: 'finance',
  name: 'shop.admin.finance',
  component: Content,
  meta: {
    title: '财务',
  },
  children: [
    {
      path: 'commission',
      name: 'shop.admin.finance.commission',
      component: () => import('@/app/shop/admin/finance/commission/index.vue'),
      meta: {
        title: '佣金管理',
      },
    },
    {
      path: 'recharge',
      name: 'shop.admin.finance.recharge',
      component: () => import('@/app/shop/admin/finance/recharge/index.vue'),
      meta: {
        title: '充值管理',
      },
    },
    {
      path: 'withdraw',
      name: 'shop.admin.finance.withdraw',
      component: () => import('@/app/shop/admin/finance/withdraw/index.vue'),
      meta: {
        title: '提现管理',
      },
    },
    {
      path: 'report',
      name: 'shop.admin.finance.report',
      component: () => import('@/app/shop/admin/finance/report/index.vue'),
      meta: {
        title: '财务报表',
      },
    },
  ],
};

const api = {
  // 佣金相关 API
  commission: {
    ...CRUD('/cif/red/envelope', ['list', 'detail', 'report']),
    // 获取佣金类型枚举
    getTypes: () =>
      request({
        url: '/cif/api/user/getEnum',
        method: 'GET',
        params: { id: 5, lang: currentLanguage },
      }),
  },

  // 充值相关 API
  recharge: {
    ...CRUD('cif/recharge/record', ['list', 'detail', 'report']),
    recordDetail: (id) =>
      request({
        url: `cif/recharge/record/detail`,
        method: 'GET',
        params: { id },
      }),
    select: (params) => SELECT('shop/admin/finance/recharge', params),
    confirm: (id) => ({
      error: 0,
      msg: t('modules.finance.confirmSuccess'),
      data: null,
    }),
    batchConfirm: (ids) => ({
      error: 0,
      msg: t('modules.finance.batchConfirmSuccess'),
      data: null,
    }),
  },

  // 提现相关 API
  withdraw: {
    ...CRUD('/cif/withdraw/record', ['list', 'detail', 'report']),
    withdrawDetail: (id) =>
      request({
        url: `cif/withdraw/record/detail`,
        method: 'GET',
        params: { id },
      }),
    // 审核接口
    review: (data) =>
      request({
        url: 'cif/withdraw/record/review',
        method: 'POST',
        data,
      }),
    batchApprove: (ids) => ({
      error: 0,
      msg: t('modules.finance.batchApproveSuccess'),
      data: null,
    }),
    batchReject: (ids, data) => ({
      error: 0,
      msg: t('modules.finance.batchRejectSuccess'),
      data: null,
    }),
  },

  // 财务报表相关 API
  report: {
    ...CRUD('/cif/subject', ['list', 'export'], { exportMethod: 'GET' }),
  },
};

export { route, api, financeConfig, financeUtils };
