import { Apis } from '@/gen/Apis';
import { ModalForm, ProFormText } from '@ant-design/pro-components';
import { history } from '@umijs/max';
import { message } from 'antd';
import { stateActions } from '../../libs/valtio/actions';
export default function ChangePassword({
  open,
  setOpen,
}: {
  open: boolean;
  setOpen: (open: boolean) => void;
}) {
  return (
    <ModalForm<ApiTypes.Common.Auth.ChangePassword>
      open={open}
      wrapperCol={{ span: 24 }}
      width="500px"
      title="请修改初始密码"
      onFinish={async (values) => {
        return Apis.Common.Auth.ChangePassword(values)
          .then(() => {
            message.success('修改密码成功');
            Apis.Common.Auth.Logout().then(() => {
              stateActions.setLogout();
              sessionStorage.removeItem('loginUserInfo');
              history.push('/login');
              setOpen(false);
            });
          })
          .catch(() => false);
      }}
      modalProps={{
        onCancel: () => setOpen(false),
      }}
    >
      <ProFormText.Password name="old_password" label="原密码" required />
      <ProFormText.Password
        name="new_password"
        label="新密码"
        tooltip="必须超6位数，包含数字、大小字母、#-_@&*特殊字符"
        required
      />
      <ProFormText.Password
        name="re_new_password"
        tooltip="必须超6位数，包含数字、大小字母、#-_@&*特殊字符"
        label="重复新密码"
        required
      />
    </ModalForm>
  );
}
