import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { SysSelects } from '@/components/SysSelects';
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.Common.Admins.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      wrapperCol={{ span: 24 }}
      width="500px"
      form={form}
      trigger={<MyButtons.Create title={`添加${props.title}`} />}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({
            roles_id: [props?.item?.id],
          });
        }
      }}
      onFinish={async (values) =>
        Apis.Common.Admins.Store(values)
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'username',
          title: '用户名',
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'password',
          title: '密码',
          valueType: 'password',
          formItemProps: { ...rulesHelper.text },
        },
        SysSelects.SysRoles(),
      ]}
    />
  );
}
