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 Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Activity.Activities.Update>
      {...MyModalFormProps.props}
      title={`编辑活动内容`}
      trigger={<MyButtons.Edit />}
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      labelAlign="left"
      width="1000px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({
            ...props.item,
            activity_time: [
              props.item?.start_time || '',
              props.item?.end_time || '',
            ],
            project_ids: props?.item?.activity_projects?.map(
              (res: any) => res?.asset_projects_id,
            ),
          });
          // form.setFieldValue('project_ids', props.item?.project_ids[0]);
          // form.setFieldValue('activity_time', [
          //   props.item?.start_time || '',
          //   props.item?.end_time || '',
          // ]);
          // form.setFieldValue('content', props.item?.content);
        }
      }}
      onFinish={async (values) =>
        Apis.Activity.Activities.Update({
          ...values,
          id: props.item?.id ?? 0,
          // project_ids: values?.project_ids
          //   ? [values?.project_ids]
          //   : [props.item?.id],
        })
          .then(() => {
            props.reload?.();
            message.success('编辑公告内容成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.Companies({
          key: 'companies_id',
          title: '选择机构',
          colProps: { span: 24 },
          required: true,
        }),
        Selects?.AssetProjects({
          key: 'project_ids',
          title: '关联项目',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.array },
          fieldProps: {
            showSearch: true,
            mode: 'multiple',
            fieldNames: {
              label: 'label',
              value: 'value',
            },
            labelRender: (res: any) => {
              if (res?.label) {
                return res?.label;
              } else {
                return props?.item?.activity_projects?.map((i: any) => {
                  if (res?.value === i?.asset_projects_id) {
                    return i?.asset_project?.name;
                  }
                });
              }
            },
          },
        }),
        {
          key: 'title',
          title: '活动标题',
          colProps: { span: 16 },
          required: true,
          formItemProps: {
            labelCol: { span: 6 },
            wrapperCol: { span: 16 },
            required: true,
          },
        },
        {
          key: 'is_enroll',
          title: '活动报名',
          colProps: { span: 8 },
          valueType: 'switch',
          formItemProps: {
            labelCol: { span: 8 },
            wrapperCol: { span: 14 },
            ...rulesHelper.number,
          },
          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,
          required: true,
          formItemProps: {
            labelCol: { span: 12 },
            wrapperCol: { span: 12 },
          },
        }),
        {
          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 },
          renderFormItem: () => <MyEditor />,
          initialValue: props.item?.content,
        },
        MyFormItems.UploadImages({
          key: 'cover_image',
          title: '活动封面',
          tooltip: '建议图片比例 16:9',
          fieldProps: {
            style: {
              width: 160,
              height: 90,
            },
          },
          max: 1,
        }),
      ]}
    />
  );
}
