import { MyBetaModalFormProps, MyButtons, MyModalFormProps } 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 Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Common.Admins.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Edit />}
      wrapperCol={{ span: 24 }}
      width="500px"
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({
            ...props.item,
            roles_id: props.item?.roles?.map((item: any) => item.value),
          });
        }
      }}
      onFinish={async (values) =>
        Apis.Common.Admins.Update({ ...values, id: props.item?.id ?? 0 })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'username',
          title: '用户名',
          readonly: true,
        },
        {
          key: 'password',
          title: '密码',
          valueType: 'password',
        },
        SysSelects.SysRoles(),
      ]}
    />
  );
}
