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 { MsgPropertyAnnouncementsPublishTypeEnum } 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.Msg.MsgPropertyAnnouncements.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(); // 清空表单数据
          form.setFieldValue('is_publish', 1);
        }
      }}
      onFinish={async (values) =>
        Apis.Msg.MsgPropertyAnnouncements.Store({
          ...values,
          asset_projects_id: values?.asset_projects_id || props.item?.id,
        })
          .then(() => {
            props.reload?.();
            message.success('添加公告内容成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        ...(props?.item?.id
          ? []
          : [
              Selects?.AssetProjects({
                key: 'asset_projects_id',
                title: '关联项目',
                colProps: { span: 24 },
                required: true,
              }),
            ]),

        {
          key: 'title',
          title: '公告标题',
          colProps: { span: 24 },
          required: true,
          formItemProps: {
            labelCol: { span: 4 },
            wrapperCol: { span: 20 },
            ...rulesHelper.text,
          },
        },
        MyFormItems.EnumRadio({
          key: 'publish_type',
          title: '发布方式',
          colProps: { span: 8 },
          valueEnum: MsgPropertyAnnouncementsPublishTypeEnum,
          formItemProps: {
            labelCol: { span: 12 },
            wrapperCol: { span: 12 },
            ...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: '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 />,
        },
        {
          key: 'publish_at',
          title: '落款日期',
          valueType: 'date',
          colProps: { span: 24 },
          fieldProps: {
            style: { width: '40%' },
          },
          formItemProps: { ...rulesHelper.text },
        },
        // {
        //   key: 'sort',
        //   title: '排序',
        //   valueType: 'digit',
        //   colProps: { span: 12 },
        //   tooltip: '数值越大越靠前',
        //   fieldProps: {
        //     placeholder: '数值越大越靠前',
        //     min: 0,
        //     style: { width: '100%' },
        //   },
        //   initialValue: 0,
        // },
      ]}
    />
  );
}
