import $ from 'jquery'
import {withRouter,Link} from 'react-router-dom';
import {Button,Modal,Table,Icon} from 'antd';
import '../../../mock';
//const BASE_URI = "/eIBS/service/"
export const BASE_HOST = "/psbcfront/"
//export const BASE_URI=BASE_HOST+"service/";
export const BASE_URI="/psbcfront/service/";


export default class Api 
{
    static get(url,data)
    {

    }
	static logout(){
        console.log('asdasdasdasdasd')
        window.location.href="#/login";
    }
    static _post(url,postdata)
    {
    
        let token = window.sessionStorage._token_
        let userId =  window.sessionStorage._nam_
        return new Promise( (resolve, reject) =>{
            $.ajax(url,{
                data:JSON.stringify(postdata),
                dataType:"json",
                contentType:"application/json;charset=utf-8",
                error:function(xhttp,textStatus,erroThrown){
                    switch (textStatus){
                        case "timeout":
                           
                            
                        case "error":
                           
                        case "parsererror":
                            resolve({retcod:'500',retmsg:textStatus});
                            break;
                        default:
                            resolve({retcod:"500"});
                            break;
                    }
                },
                success:data=>{
                    if(data && data.retcod != SUCCESS)
                    {
                        console.log("错误码："+data.retcod+" 错误信息："+data.retmsg)
                    }
                    if(data && data.retcod == 'L0003'){
                        //自动重新登录
                        // Api.autoLogin("yanshi","1").then(data=>{
                        //     if(data.retcod == SUCCESS) //二次请求
                        //     {
                        //         Api._post(url,postdata).then(data=>resolve(data));
                        //     }
                        // })
                        // return ;
						 Modal.warning({
                            title:'会话超时',
                            content:'即将退出，请重新登录',
							onOk:()=>{
                                window.sessionStorage.removeItem("token")
                                window.sessionStorage.removeItem("userId")
                                if(window.sessionStorage.lang)
                                    // window.location.href= "/?#/"+window.sessionStorage.i18n+"/login"
                                    window.location.href= "/E-Credit/?#/"+window.sessionStorage.i18n+"/login"
                                else
                                    // window.location.href= "/?#/"+"login"
                                    window.location.href= "/E-Credit/?#/"+"login"
                            }
                        })
                        return ;
                    }
                    resolve(data);
                },
                beforeSend: function (XMLHttpRequest) {
                    
         		   if(url!= `${BASE_URI}login` && token)
                        XMLHttpRequest.setRequestHeader("token", token);
                    XMLHttpRequest.setRequestHeader("userId", userId);         
                    
                     XMLHttpRequest.setRequestHeader("terminalType", 'WEB');
                },
                type:"POST"
            })
        })
    }
    static _get(url,data)
    {
    
        let token = window.sessionStorage.token
        let userId =  window.sessionStorage.userId
        return new Promise( (resolve, reject) =>{
            $.ajax(url,{
                data:data,
                dataType:"json",
                contentType:"application/json;charset=utf-8",
                error:function(xhttp,textStatus,erroThrown){
                    switch (textStatus){
                        case "timeout":
                           
                            
                        case "error":
                           
                        case "parsererror":
                            resolve({retcod:'500',retmsg:textStatus});
                            break;
                        default:
                            resolve({retcod:"500"});
                            break;
                    }
                },
                success:data=>{
                    if(data && data.retcod == 'L0003'){
						
						 Modal.warning({
                            title:'会话超时',
                            content:'即将退出，请重新登录',
							onOk:()=>{
                                window.sessionStorage.removeItem("token")
                                window.sessionStorage.removeItem("userId")
                                if(window.sessionStorage.lang)
                                    window.location.href= "/?#/"+window.sessionStorage.i18n+"/login"
                                    // window.location.href= "/E-Credit/?#/"+window.sessionStorage.i18n+"/login"
                                else
                                    window.location.href= "/?#/"+"login"
                                    // window.location.href= "/E-Credit/?#/"+"login"
                            }
                        })
                        return ;
                    }
                    resolve(data);
                },
                beforeSend: function (XMLHttpRequest) {
                    
         		   if(url!= `${BASE_URI}login` && token)
                        XMLHttpRequest.setRequestHeader("token", token);
                    XMLHttpRequest.setRequestHeader("userId", userId);         
                    
                     XMLHttpRequest.setRequestHeader("terminalType", 'WEB');
                },
                type:"GET"
            })
        })
    }
    static post(url,data)
    {
       return this._post(BASE_URI+url,data)
    }

    static get(url,data)
    {
       return this._get(BASE_HOST+url,data)
    }

    static async autoLogin(username,password)
    {
       var args = {username,password}
       let rtnmsg = await Api.post('login',{data:args})
       if(rtnmsg.retcod == SUCCESS)
       {
            const {data} = rtnmsg
            Storage.setUser(username)
            Storage.setToken(data.token)
            Storage.setMenu(tdata.menu)
       }  
    }
}