
import { InputNumber, Space, message, Popconfirm } from 'antd';
import {
    PageContainer,
    ProForm,
    ProFormText,
} from '@ant-design/pro-components';

import { history, useModel } from '@umijs/max';
import { stringify } from 'querystring';
import { resetPwd } from '@/services/system/user';

export default () => {

    const handleCreate = async (fields) => {
        let data = JSON.parse(JSON.stringify(fields))
        const { success } = await resetPwd({ ...data });
        if (success) {
            message.success('修改成功,请重新登录')
            const { search, pathname } = window.location;
            const urlParams = new URL(window.location.href).searchParams;
            /** 此方法会跳转到 redirect 参数所在的位置 */
            const redirect = urlParams.get('redirect');
            // Note: There may be security issues, please note
            localStorage.removeItem('token')
            if (window.location.pathname !== '/user/login' && !redirect) {
                history.replace({
                    pathname: '/user/login',
                    search: stringify({
                        redirect: pathname + search,
                    }),
                });
            }
        }
    };


    return (
        <PageContainer
            header={{
                title: '重置密码',
            }}
        >
            <>
                <ProForm
                    onFinish={(values) => {
                        handleCreate(values)
                    }}
                >
                    <ProFormText
                        rules={[{ required: true, message: '请输入!' }]}
                        width={300}
                        name="oldPassword"
                        label="原密码"
                        placeholder="原密码"
                    />
                    <ProFormText
                        rules={[{ required: true, message: '请输入!' }]}
                        width={300}
                        name="newPassword"
                        label="新密码"
                        placeholder="新密码"
                    />
                    <ProFormText
                        rules={[{ required: true, message: '请输入!' }]}
                        width={300}
                        name="passwordSure"
                        label="确认密码"
                        placeholder="确认密码"
                    />
                </ProForm>
            </>
        </PageContainer>
    );
};
