import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { WxCpActivityApi } from '#/api/wxcp/activity';

import {
  ACTIVITY_STATUS_OPTIONS,
  PUSH_SCOPE_OPTIONS,
  PUSH_STATUS_OPTIONS,
  SIGNUP_STATUS_OPTIONS,
} from '../data';

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

const DATETIME_PROPS = {
  showTime: true,
  valueFormat: 'YYYY-MM-DD HH:mm:ss',
  format: 'YYYY-MM-DD HH:mm:ss',
} as const;

/** 活动表单 */
export function useActivityFormSchema(
  formType: ActivityFormType,
): VbenFormSchema[] {
  const readonly = formType === 'detail';
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'title',
      label: '活动标题',
      component: 'Input',
      componentProps: {
        placeholder: '请输入活动标题',
        disabled: readonly,
      },
      rules: 'required',
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'location',
      label: '活动地点',
      component: 'Input',
      componentProps: { placeholder: '如：公司大会议室', disabled: readonly },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'startTime',
      label: '活动开始',
      component: 'DatePicker',
      componentProps: { ...DATETIME_PROPS, disabled: readonly },
    },
    {
      fieldName: 'endTime',
      label: '活动结束',
      component: 'DatePicker',
      componentProps: { ...DATETIME_PROPS, disabled: readonly },
    },
    {
      fieldName: 'signupStartTime',
      label: '报名开始',
      component: 'DatePicker',
      componentProps: { ...DATETIME_PROPS, disabled: readonly },
    },
    {
      fieldName: 'signupEndTime',
      label: '报名截止',
      component: 'DatePicker',
      componentProps: { ...DATETIME_PROPS, disabled: readonly },
    },
    {
      fieldName: 'signupLimit',
      label: '报名上限',
      component: 'InputNumber',
      componentProps: {
        placeholder: '不限请填 0',
        min: 0,
        class: 'w-full',
        disabled: readonly,
      },
      rules: 'required',
      help: '填 0 表示不限名额',
    },
    {
      fieldName: 'coverUrl',
      label: '封面图',
      component: 'Input',
      componentProps: { placeholder: '封面图地址（选填）', disabled: readonly },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'content',
      label: '活动详情',
      component: 'Textarea',
      componentProps: {
        rows: 4,
        placeholder: '活动介绍、注意事项等（报名页按纯文本展示）',
        disabled: readonly,
      },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: { rows: 2, placeholder: '内部备注，不展示给成员', disabled: readonly },
      formItemClass: 'col-span-2',
    },
  ];
}

/** 活动搜索表单 */
export function useActivitySearchFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'title',
      label: '活动标题',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入活动标题' },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: ACTIVITY_STATUS_OPTIONS,
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'pushStatus',
      label: '推送状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: PUSH_STATUS_OPTIONS,
        placeholder: '请选择推送状态',
      },
    },
  ];
}

/** 活动列表字段 */
export function useActivityGridColumns(): VxeTableGridOptions<WxCpActivityApi.Activity>['columns'] {
  return [
    {
      field: 'title',
      title: '活动标题',
      minWidth: 200,
      slots: { default: 'title' },
    },
    { field: 'location', title: '活动地点', minWidth: 140 },
    { field: 'startTime', title: '活动开始', width: 170 },
    { field: 'endTime', title: '活动结束', width: 170 },
    {
      field: 'signupCount',
      title: '报名/上限',
      width: 110,
      slots: { default: 'signupCount' },
    },
    {
      field: 'status',
      title: '状态',
      width: 90,
      slots: { default: 'status' },
    },
    {
      field: 'pushStatus',
      title: '推送',
      width: 90,
      slots: { default: 'pushStatus' },
    },
    { field: 'createTime', title: '创建时间', width: 170 },
    { title: '操作', width: 300, fixed: 'right', slots: { default: 'actions' } },
  ];
}

/** 报名记录搜索表单 */
export function useSignupSearchFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: '姓名',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入姓名' },
    },
    {
      fieldName: 'mobile',
      label: '手机号',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入手机号' },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: SIGNUP_STATUS_OPTIONS,
        placeholder: '请选择状态',
      },
    },
  ];
}

/** 报名记录列表字段 */
export function useSignupGridColumns(): VxeTableGridOptions<WxCpActivityApi.Signup>['columns'] {
  return [
    { field: 'name', title: '姓名', minWidth: 100 },
    { field: 'mobile', title: '手机号', width: 130 },
    { field: 'deptName', title: '部门', minWidth: 120 },
    { field: 'wxUserId', title: '企业微信账号', minWidth: 110 },
    {
      field: 'content',
      title: '参与说明',
      minWidth: 160,
      slots: { default: 'content' },
    },
    {
      field: 'attachment',
      title: '附件',
      width: 80,
      slots: { default: 'attachment' },
    },
    {
      field: 'status',
      title: '状态',
      width: 90,
      slots: { default: 'status' },
    },
    { field: 'signupTime', title: '报名时间', width: 170 },
    { field: 'checkInTime', title: '签到时间', width: 170 },
    { title: '操作', width: 150, fixed: 'right', slots: { default: 'actions' } },
  ];
}

