import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import MyEditor from '@/common/components/Editor';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { ActivitiesPublishTypeEnum } from '@/gen/Enums';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Activity.Activities.Store>
      {...MyModalFormProps.props}
      title={`添加活动`}
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      labelAlign="left"
      width="950px"
      trigger={<MyButtons.Create title={`添加活动`} />}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) =>
        Apis.Activity.Activities.Store({
          ...values,
          project_ids: values?.project_ids || [props.item?.id],
        })
          .then(() => {
            props.reload?.();
            message.success('添加活动成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        ...(props?.item?.id
          ? []
          : [
              Selects?.AssetProjects({
                key: 'project_ids',
                title: '关联项目',
                colProps: { span: 24 },
                formItemProps: { ...rulesHelper.array },
                fieldProps: {
                  showSearch: true,
                  mode: 'multiple',
                  fieldNames: {
                    label: 'label',
                    value: 'value',
                  },
                },
              }),
            ]),
        {
          key: 'title',
          title: '活动标题',
          colProps: { span: 16 },
          required: true,
          formItemProps: {
            labelCol: { span: 6 },
            wrapperCol: { span: 16 },
            ...rulesHelper.text,
          },
        },
        {
          key: 'is_enroll',
          title: '活动报名',
          colProps: { span: 8 },
          valueType: 'switch',
          required: true,
          formItemProps: {
            labelCol: { span: 8 },
            wrapperCol: { span: 14 },
            required: true,
          },
          fieldProps: {
            checkedChildren: '开启',
            unCheckedChildren: '关闭',
          },
        },
        {
          key: 'activity_time',
          title: '活动时间',
          valueType: 'dateTimeRange',
          colProps: { span: 24 },
          fieldProps: {
            style: { width: '50%' },
            showTime: true,
            format: 'YYYY-MM-DD HH:mm',
          },
          formItemProps: { ...rulesHelper.text },
          transform: (value: any) => {
            return {
              start_time: value?.[0],
              end_time: value?.[1],
            };
          },
        },
        MyFormItems.EnumRadio({
          key: 'publish_type',
          title: '发布方式',
          colProps: { span: 8 },
          valueEnum: ActivitiesPublishTypeEnum,
          formItemProps: {
            labelCol: { span: 12 },
            wrapperCol: { span: 12 },
            ...rulesHelper.text,
          },
        }),
        {
          name: ['publish_type'],
          valueType: 'dependency',
          columns: ({ publish_type }) => {
            return publish_type === ActivitiesPublishTypeEnum.Schedule.value
              ? [
                  {
                    key: 'schedule_publish_at',
                    // title: '定时发布时间',
                    valueType: 'dateTime',
                    colProps: { span: 16 },
                    fieldProps: {
                      style: { width: '40%' },
                      format: 'YYYY-MM-DD HH:mm',
                    },
                    formItemProps: {
                      ...rulesHelper.text,
                      wrapperCol: { span: 24 },
                      placeholder: '请设置定时发布时间',
                    },
                  },
                ]
              : [];
          },
        },
        {
          key: 'content',
          title: '活动内容',
          colProps: { span: 24 },
          fieldProps: {
            mode: 'simple',
          },
          renderFormItem: () => <MyEditor />,
        },
        MyFormItems.UploadImages({
          key: 'cover_image',
          title: '活动封面',
          tooltip: '建议图片比例 16:9',
          required: true,
          fieldProps: {
            style: {
              width: 160,
              height: 90,
            },
          },
          max: 1,
        }),
      ]}
    />
  );
}
