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

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

/** 问题类型选项（对齐 AfterSaleIssueTypeEnum） */
export const ISSUE_TYPE_OPTIONS = [
  { label: '一般问题', value: 1 },
  { label: '维修问题', value: 2 },
  { label: '退货问题', value: 3 },
];

/** 严重程度选项 */
export const SEVERITY_LEVEL_OPTIONS = [
  { label: '轻微', value: 0 },
  { label: '一般', value: 1 },
  { label: '严重', value: 2 },
  { label: '致命', value: 3 },
];

/** 根因分类选项（对齐 AfterSaleRootCauseCategoryEnum） */
export const ROOT_CAUSE_CATEGORY_OPTIONS = [
  { label: '设计缺陷', value: 1 },
  { label: '物料缺陷', value: 2 },
  { label: '工艺缺陷', value: 3 },
  { label: '装配缺陷', value: 4 },
  { label: '运输损坏', value: 5 },
  { label: '包装缺陷', value: 6 },
  { label: '使用不当', value: 7 },
  { label: '软件缺陷', value: 8 },
  { label: '其他', value: 9 },
];

/** 问题状态选项（对齐 AfterSaleProblemStatusEnum） */
export const PROBLEM_STATUS_OPTIONS = [
  { label: '草稿', value: 0 },
  { label: '已发布', value: 1 },
  { label: '已停用', value: 2 },
];

/** 问题库表单 */
export function useProblemFormSchema(
  formType: ProblemFormType,
): VbenFormSchema[] {
  const readonly = formType === 'detail';
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'title',
      label: '问题标题',
      component: 'Input',
      componentProps: { placeholder: '请输入问题标题' },
      rules: 'required',
    },
    {
      fieldName: 'issueType',
      label: '问题类型',
      component: 'Select',
      componentProps: {
        options: ISSUE_TYPE_OPTIONS,
        placeholder: '请选择问题类型',
        disabled: readonly,
      },
    },
    {
      fieldName: 'issueCategory',
      label: '问题分类',
      component: 'Input',
      componentProps: {
        placeholder: '如功能故障、外观缺陷',
        disabled: readonly,
      },
    },
    {
      fieldName: 'severityLevel',
      label: '严重程度',
      component: 'Select',
      componentProps: {
        options: SEVERITY_LEVEL_OPTIONS,
        placeholder: '请选择',
        disabled: readonly,
      },
    },
    {
      fieldName: 'rootCauseCategory',
      label: '根因分类',
      component: 'Select',
      componentProps: {
        options: ROOT_CAUSE_CATEGORY_OPTIONS,
        placeholder: '请选择根因分类',
        disabled: readonly,
      },
    },
    {
      fieldName: 'itemCode',
      label: '物料编码',
      component: 'Input',
      componentProps: { placeholder: '来源于工单明细', disabled: readonly },
    },
    {
      fieldName: 'itemName',
      label: '物料名称',
      component: 'Input',
      componentProps: { placeholder: '来源于工单明细', disabled: readonly },
    },
    {
      fieldName: 'batchCode',
      label: '批次号',
      component: 'Input',
      componentProps: { placeholder: '来源于工单明细', disabled: readonly },
    },
    {
      fieldName: 'description',
      label: '问题描述',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        rows: 3,
        placeholder: '问题现象与背景',
        disabled: readonly,
      },
    },
    {
      fieldName: 'rootCause',
      label: '根因分析',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        rows: 3,
        placeholder: '缺陷产生的根本原因',
        disabled: readonly,
      },
    },
    {
      fieldName: 'solution',
      label: '解决方案',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        rows: 3,
        placeholder: '处理措施/SOP，供后续工单参考',
        disabled: readonly,
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: { rows: 2, placeholder: '备注', disabled: readonly },
    },
    {
      fieldName: 'blobIds',
      label: '附件',
      component: 'FileUpload',
      componentProps: {
        valueKey: 'id',
        maxNumber: 10,
        multiple: true,
        disabled: readonly,
      },
      formItemClass: 'col-span-2',
    },
  ];
}

/** 问题库搜索表单 */
export function useProblemSearchFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'keyword',
      label: '关键字',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '单号/标题/描述/方案',
      },
    },
    {
      fieldName: 'itemCode',
      label: '物料编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入物料编码',
      },
    },
    {
      fieldName: 'issueCategory',
      label: '问题分类',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入问题分类',
      },
    },
    {
      fieldName: 'issueType',
      label: '问题类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: ISSUE_TYPE_OPTIONS,
        placeholder: '请选择',
      },
    },
    {
      fieldName: 'rootCauseCategory',
      label: '根因分类',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: ROOT_CAUSE_CATEGORY_OPTIONS,
        placeholder: '请选择',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: PROBLEM_STATUS_OPTIONS,
        placeholder: '请选择状态',
      },
    },
  ];
}

/** 问题库列表字段 */
export function useProblemGridColumns(): VxeTableGridOptions<AftersalesProblemApi.Problem>['columns'] {
  return [
    { field: 'no', title: '问题单号', minWidth: 170, slots: { default: 'no' } },
    { field: 'title', title: '问题标题', minWidth: 180 },
    { field: 'sourceTicketNo', title: '来源工单', minWidth: 150 },
    { field: 'itemName', title: '物料名称', minWidth: 120 },
    { field: 'itemCode', title: '物料编码', minWidth: 120 },
    { field: 'issueType', title: '问题类型', width: 100, slots: { default: 'issueType' } },
    { field: 'severityLevel', title: '严重程度', width: 100, slots: { default: 'severityLevel' } },
    { field: 'rootCauseCategory', title: '根因分类', width: 100, slots: { default: 'rootCauseCategory' } },
    { field: 'status', title: '状态', width: 90, slots: { default: 'status' } },
    { field: 'ownerUserName', title: '沉淀人', minWidth: 100 },
    { field: 'updateTime', title: '更新时间', width: 170, formatter: 'formatDate' },
    { title: '操作', width: 220, fixed: 'right', slots: { default: 'actions' } },
  ];
}
