import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AftersalesCancellationApi } from '#/api/aftersales/cancellation';

/** 表单类型 */
export type CancellationFormType = 'create' | 'update';

/** 受理类型选项（对齐后端 AfterSaleCancellationDO.type） */
export const CANCELLATION_TYPE_OPTIONS = [
  { label: '客户退货', value: 'RETURN' },
  { label: '合同变更', value: 'CONTRACT_CHANGE' },
  { label: '合同终止', value: 'CONTRACT_TERMINATE' },
];

/** 状态选项 */
export const CANCELLATION_STATUS_OPTIONS = [
  { label: '草稿', value: 0 },
  { label: '待审批', value: 1 },
  { label: '已通过', value: 2 },
  { label: '已驳回', value: 3 },
  { label: '已取消', value: 4 },
];

/** 退货类型选项 */
export const RETURN_TYPE_OPTIONS = [
  { label: '退款退货', value: 1 },
  { label: '换货', value: 2 },
  { label: '仅退款', value: 3 },
];

/** 类型文案 */
export const CANCELLATION_TYPE_MAP: Record<string, string> =
  Object.fromEntries(
    CANCELLATION_TYPE_OPTIONS.map((o) => [o.value, o.label]),
  );

/** 状态文案 */
export const CANCELLATION_STATUS_MAP: Record<number, string> =
  Object.fromEntries(
    CANCELLATION_STATUS_OPTIONS.map((o) => [o.value, o.label]),
  );

/** 状态颜色 */
export const CANCELLATION_STATUS_COLOR: Record<number, string> = {
  0: 'default',
  1: 'processing',
  2: 'success',
  3: 'error',
  4: 'default',
};

/** 是否合同类受理 */
export function isContractType(type?: string) {
  return type === 'CONTRACT_CHANGE' || type === 'CONTRACT_TERMINATE';
}

/** 搜索表单 */
export function useCancellationSearchSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '受理单号',
      component: 'Input',
      componentProps: { placeholder: '请输入受理单号' },
    },
    {
      fieldName: 'type',
      label: '受理类型',
      component: 'Select',
      componentProps: {
        options: CANCELLATION_TYPE_OPTIONS,
        placeholder: '请选择受理类型',
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: CANCELLATION_STATUS_OPTIONS,
        placeholder: '请选择状态',
        allowClear: true,
      },
    },
  ];
}

/** 列表列 */
export function useCancellationGridColumns(): VxeTableGridOptions<AftersalesCancellationApi.Cancellation>['columns'] {
  return [
    { field: 'code', title: '受理单号', width: 170 },
    {
      field: 'type',
      title: '受理类型',
      width: 110,
      formatter: ({ cellValue }) =>
        CANCELLATION_TYPE_MAP[cellValue as string] ?? '-',
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      slots: { default: 'status' },
    },
    { field: 'reason', title: '申请原因', minWidth: 200, showOverflow: 'tooltip' },
    { field: 'customerId', title: '客户编号', width: 100 },
    { field: 'contractId', title: '合同编号', width: 100 },
    {
      field: 'expectedAmount',
      title: '预计金额',
      width: 120,
      align: 'right',
    },
    {
      field: 'executed',
      title: '下游执行',
      width: 200,
      slots: { default: 'executed' },
    },
    { field: 'creator', title: '创建人', width: 110 },
    { field: 'createTime', title: '创建时间', width: 170 },
    { field: 'operation', title: '操作', width: 220, fixed: 'right', slots: { default: 'operation' } },
  ];
}

/** 表单 Schema */
export function useCancellationFormSchema(
  formType: CancellationFormType,
): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: { show: () => false, triggerFields: [''] },
    },
    {
      fieldName: 'type',
      label: '受理类型',
      component: 'Select',
      componentProps: {
        options: CANCELLATION_TYPE_OPTIONS,
        placeholder: '请选择受理类型',
        // 已创建的受理单不允许改类型，避免下游动作错配
        disabled: formType === 'update',
      },
      rules: 'required',
    },
    {
      fieldName: 'ticketId',
      label: '售后工单',
      component: 'InputNumber',
      componentProps: {
        placeholder: '退货类型必填，填工单编号',
        class: 'w-full',
        min: 1,
      },
      dependencies: {
        triggerFields: ['type'],
        show: (values) => values.type === 'RETURN',
      },
    },
    {
      fieldName: 'returnType',
      label: '退货类型',
      component: 'Select',
      componentProps: {
        options: RETURN_TYPE_OPTIONS,
        placeholder: '请选择退货类型',
      },
      dependencies: {
        triggerFields: ['type'],
        show: (values) => values.type === 'RETURN',
      },
    },
    {
      fieldName: 'contractId',
      label: 'CRM 合同',
      component: 'InputNumber',
      componentProps: {
        placeholder: '合同变更/终止必填，填合同编号',
        class: 'w-full',
        min: 1,
      },
      dependencies: {
        triggerFields: ['type'],
        show: (values) => isContractType(values.type),
      },
    },
    {
      fieldName: 'customerId',
      label: '客户编号',
      component: 'InputNumber',
      componentProps: {
        placeholder: '退货类型必填',
        class: 'w-full',
        min: 1,
      },
      dependencies: {
        triggerFields: ['type'],
        show: (values) => values.type === 'RETURN',
      },
    },
    {
      fieldName: 'saleOrderNo',
      label: '销售订单号',
      component: 'Input',
      componentProps: { placeholder: '选填' },
    },
    {
      fieldName: 'expectedAmount',
      label: '预计金额',
      component: 'InputNumber',
      componentProps: { placeholder: '选填', class: 'w-full', min: 0 },
    },
    {
      fieldName: 'reason',
      label: '申请原因',
      component: 'Textarea',
      componentProps: {
        placeholder: '请填写申请原因',
        maxlength: 500,
        showCount: true,
        rows: 3,
      },
      rules: 'required',
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '选填',
        maxlength: 500,
        showCount: true,
        rows: 2,
      },
    },
  ];
}
