//注册
import {JSONResponse, Method, request} from "./interface";
import {API} from "./api";
import {RegisterRes, RegisterTransform} from "./result";
import {Button} from "react-bootstrap";
import * as React from "react";


export function register(that:React.Component<any,any>,managerId:string,password:string,onSuccess?:Function){
    request(API.account.register,Method.POST, {
        managerId:managerId,
        password:password
    },new RegisterTransform(),function (res:JSONResponse<RegisterRes>) {
        switch (res.customResult) {
            case RegisterRes.ok:
                that.setState({
                    tipContent:<div><h3 className="text-center text-info">注册成功</h3>
                        {that.props.toLogin?<Button className={"col-3 ml-auto mr-auto"} block={true} onClick={()=>that.props.toLogin()}>返回登录</Button>:null}</div>
    })
                if(onSuccess){
                    onSuccess()
                }
        break
    case RegisterRes.fail:
        that.setState({
            tipContent:<h3 className="text-center text-danger">注册失败,请联系管理员</h3>
        })
        break
    case RegisterRes.user_repeat:
        that.setState({
            tipContent:<h3 className="text-center text-danger">注册失败，账号：{that.state.managerId}已存在</h3>
    })
        break
    }
    })
}
