import type { LeaseBusinessDetailRow } from '../types';

export type DetailEditFieldType = 'text' | 'number' | 'date';

export interface DetailEditFieldSpec {
  key: keyof LeaseBusinessDetailRow;
  label: string;
  type: DetailEditFieldType;
}

/** 编辑弹窗可改字段（对齐导入模板手工列） */
export const DETAIL_EDIT_FIELDS: DetailEditFieldSpec[] = [
  { key: 'year', label: '年份', type: 'number' },
  { key: 'month', label: '月份', type: 'number' },
  { key: 'paymentDate', label: '付款日期', type: 'date' },
  { key: 'plateNo', label: '车牌号码', type: 'text' },
  { key: 'customerName', label: '客户名称', type: 'text' },
  { key: 'valueAddedService', label: '增值服务', type: 'text' },
  { key: 'exemptionPolicy', label: '享免政策', type: 'text' },
  { key: 'pickupDate', label: '提车日期', type: 'date' },
  { key: 'contractStartDate', label: '合同生效日期', type: 'date' },
  { key: 'contractEndDate', label: '合同到期日期', type: 'date' },
  { key: 'periodStartDate', label: '起始日期', type: 'date' },
  { key: 'returnDate', label: '退车日期', type: 'date' },
  { key: 'avgDays', label: '平均天数', type: 'number' },
  { key: 'deposit', label: '押金', type: 'text' },
  { key: 'contractRent', label: '合同标的租金', type: 'number' },
  { key: 'receivableRent', label: '应收租金', type: 'number' },
  { key: 'maintenanceIncome', label: '维保包干收入', type: 'number' },
  { key: 'insuranceSurcharge', label: '保险上浮费', type: 'number' },
  { key: 'opsFeeIncome', label: '运维费（收入）', type: 'number' },
  { key: 'otherIncome', label: '其他收入', type: 'number' },
  { key: 'mileageDeduction', label: '里程减免金额', type: 'number' },
  { key: 'otherDeduction', label: '其他减免金额', type: 'number' },
  { key: 'receivedAmount', label: '实收金额', type: 'number' },
  { key: 'invoiceDate', label: '开票日期', type: 'date' },
  { key: 'actualPaymentDate', label: '实际付款日期', type: 'date' },
  { key: 'paymentMethod', label: '付款方式', type: 'text' },
  { key: 'vehicleStandardCost', label: '车辆标准成本', type: 'number' },
  { key: 'insuranceFee', label: '保险费', type: 'number' },
  { key: 'opsFeeCost', label: '运维费（成本）', type: 'number' },
  { key: 'brokerageFee', label: '居间费', type: 'number' },
  { key: 'otherCost', label: '其他', type: 'number' },
  { key: 'gpsMileage', label: 'GPS里程', type: 'text' },
  { key: 'otherDeductionDetail', label: '其他减免金额明细内容', type: 'text' },
  { key: 'assetOwner', label: '资产归属', type: 'text' },
  { key: 'signingCompany', label: '签约公司', type: 'text' },
  { key: 'remark', label: '备注', type: 'text' },
];

export function draftFromRow(row: LeaseBusinessDetailRow): Record<string, string | number> {
  const draft: Record<string, string | number> = {};
  DETAIL_EDIT_FIELDS.forEach((field) => {
    const value = row[field.key];
    if (field.key === 'deposit') {
      draft[field.key] = value === '/' ? '/' : String(value ?? '');
      return;
    }
    if (typeof value === 'number') {
      draft[field.key] = value;
      return;
    }
    draft[field.key] = String(value ?? '');
  });
  return draft;
}

export function parseDraftToPatch(
  draft: Record<string, string | number>,
): Partial<LeaseBusinessDetailRow> {
  const patch: Partial<LeaseBusinessDetailRow> = {};
  DETAIL_EDIT_FIELDS.forEach((field) => {
    const raw = draft[field.key];
    if (field.key === 'deposit') {
      const text = String(raw ?? '').trim();
      patch.deposit = text === '/' || text === '' ? '/' : Number(text);
      return;
    }
    if (field.type === 'number') {
      const num = Number(raw);
      patch[field.key] = Number.isFinite(num) ? num : 0;
      return;
    }
    patch[field.key] = String(raw ?? '');
  });
  return patch;
}
