import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
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={`添加公告`}
      wrapperCol={{ span: 24 }}
      width="600px"
      trigger={<MyButtons.Create title={`添加公告`} />}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) =>
        Apis.Msg.MsgPropertyAnnouncements.Store(values)
          .then(() => {
            props.reload?.();
            message.success('添加公告内容成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'title',
          title: '公告标题',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'content',
          title: '公告内容',
          valueType: 'textarea',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            autoSize: { minRows: 4, maxRows: 6 },
          },
        },
        {
          key: 'publish_at',
          title: '内容显示的发布日期',
          valueType: 'date',
          colProps: { span: 12 },
          fieldProps: {
            style: { width: '100%' },
          },
          formItemProps: { ...rulesHelper.text },
        },
        Selects?.AssetProjects({
          title: '请选择项目',
          key: 'asset_projects_id',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          key: 'sort',
          title: '排序',
          valueType: 'digit',
          colProps: { span: 12 },
          tooltip: '数值越大越靠前',
          fieldProps: {
            placeholder: '数值越大越靠前',
            min: 0,
            style: { width: '100%' },
          },
          initialValue: 0,
        },
      ]}
    />
  );
}
