import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import MyEditor from '@/common/components/Editor';
import { Apis } from '@/gen/Apis';
import {
  MsgPropertyAnnouncementsObjectEnum,
  MsgPropertyAnnouncementsPublishTypeEnum,
} 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.Msg.MsgPropertyAnnouncements.Update>
      {...MyModalFormProps.props}
      title={`编辑公告内容`}
      trigger={<MyButtons.Edit />}
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      labelAlign="left"
      width="950px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values) =>
        Apis.Msg.MsgPropertyAnnouncements.Update({
          ...values,
          id: props.item?.id ?? 0,
          asset_projects_id: props.item?.asset_projects_id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success('编辑公告内容成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'title',
          title: '公告标题',
          colProps: { span: 24 },
          required: true,
          formItemProps: {
            labelCol: { span: 4 },
            wrapperCol: { span: 20 },
            ...rulesHelper.text,
          },
        },
        MyFormItems.EnumRadio({
          key: 'object',
          title: '发布对象',
          colProps: { span: 8 },
          valueEnum: MsgPropertyAnnouncementsObjectEnum,
          formItemProps: {
            labelCol: { span: 12 },
            wrapperCol: { span: 12 },
            ...rulesHelper.text,
          },
        }),
        MyFormItems.EnumRadio({
          key: 'publish_type',
          title: '发布方式',
          colProps: { span: 8 },
          valueEnum: MsgPropertyAnnouncementsPublishTypeEnum,
          formItemProps: {
            labelCol: { span: 8 },
            wrapperCol: { span: 16 },
            ...rulesHelper.text,
          },
        }),

        {
          name: ['publish_type'],
          valueType: 'dependency',
          columns: ({ publish_type }) => {
            return publish_type ===
              MsgPropertyAnnouncementsPublishTypeEnum.Schedule.value
              ? [
                  {
                    key: 'schedule_publish_at',
                    // title: '定时发布时间',
                    valueType: 'dateTime',
                    colProps: { span: 16 },
                    fieldProps: {
                      style: { width: '100%' },
                      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,
        },
        {
          key: 'publish_at',
          title: '落款日期',
          valueType: 'date',
          colProps: { span: 24 },
          fieldProps: {
            style: { width: '40%' },
          },
          formItemProps: { ...rulesHelper.text },
        },
      ]}
    />
  );
}
