(function(w){
    var api = {
        get:function(url,data,callback){
            this.ajax(url,data,callback,'get');
        },
        post:function(url,data,callback){
            this.ajax(url,data,callback,'post');
        },
        ajax:function(url,data,callback,method){
            method = method || 'get' ;
            data = data || '' ;
            $.ajax({
                url:url,
                data:data,
                type:method,
                // contentType:' application/json',
                dataType:'json',
                beforeSend:function(request){
                    request.setRequestHeader('X-CSRF-TOKEN' , $('meta[name="csrf-token"]').attr('content'))
                },
                complete:function(data){

                    /*$.post('/refreshToken',function(data){
                        $('meta[name="csrf-token"]').attr('content',data.data);
                    },'json')*/
                    refreshToken(function(){Ladda.stopAll();});
                },
                success:function(data){
                    console.log(data);
                    if( data.code != 0 ){
                        //$toast.error(data.data);
                        $alert.error(data.data);
                    }
                    if( callback ){
                        callback(data)
                    }else{
                        if( data.code == 0 ){
                            $toast.success('操作成功');
                        }
                    }
                    //form.ajaxSubmitCallback && form.ajaxSubmitCallback(data) ;
                },
                error:function(data){
                    var errorText = '' ;
                    if( data.status == 401 ){
                        $alert.error('登录状态已过期').then(function(){
                            parent.location.reload();
                        })
                        return ;
                    }

                    switch(data.status){
                        case 404:
                            errorText = '请求地址不存在';
                            break;
                        case 500:
                            if( 'responseJSON' in data && data.responseJSON.message ){
                                errorText = data.responseJSON.message;
                            }else{
                                errorText = '请刷新重试';
                            }
                            break;
                        default:
                            errorText = '请刷新重试2';
                            break;
                    }
                    $alert.error(errorText);
                    console.log(data);
                }
            });
        }
    }

    var form = {
        ajaxSubmit :function(){
            /*console.log( $(this).serializeArray());
            console.log( $(this).serialize());*/

           /* var sourceData = $(this).serializeArray() ;
            var d = {} ;
            $.map(sourceData,function(n,i){
                d[n['name']] = n['value'] ;
            });
            if(  'editors' in w ){
                for(var i in editors ){
                    d[i] = editors[i].getData();
                }
            }

            d = JSON.stringify(d);
            console.log(d);*/
            var d =  $(this).serialize() ;
            var editorData = [] ;
            if(  'editors' in w ){
                for(var i in editors ){
                    //editorData.push({[i] :editors[i].getData()});
                    d+= encodeURI('&'+[i]+'='+editors[i].getData());
                }

            }
            console.log(d);
            api.ajax($(this).attr('action'),
                d,
                $f.ajaxSubmitCallback,
                $(this).attr('method'));
            /*$.ajax({
                url:$(this).attr('action'),
                data:$(this).serialize(),
                type:$(this).attr('method'),
                dataType:'json',
                beforeSend:function(request){
                    request.setRequestHeader('X-CSRF-TOKEN' , $('meta[name="csrf-token"]').attr('content'))
                },
                complete:function(data){
                    Ladda.stopAll();
                    $.post('/refreshToken',function(data){
                        $('meta[name="csrf-token"]').attr('content',data.data);
                    },'json')
                },
                success:function(data){
                    console.log(data);
                    if( data.code != 0 ){
                        //$toast.error(data.data);
                        $alert.error(data.data);
                    }
                    if( form.ajaxSubmitCallback ){
                        form.ajaxSubmitCallback(data)
                    }else{
                      if( data.code == 0 ){
                          $toast.success('保存成功');
                      }
                    }
                    //form.ajaxSubmitCallback && form.ajaxSubmitCallback(data) ;
                },
                error:function(data){
                    var errorText = '' ;
                    if( data.status == 401 ){
                        $alert.error('登录状态已过期').then(function(){
                            parent.location.reload();
                        })
                        return ;
                    }
                    $alert.error('请刷新重试');
                    console.log(data);
                }
            });*/
            return false ;
        },
        ajaxSubmitCallback : null
    };

    w.$f = form ;
    w.$api = api ;

}(window));

function refreshToken(callback){
    $.post('/refreshToken',function(data){
        $('meta[name="csrf-token"]').attr('content',data.data);
        callback && callback();
    },'json')
}

var rootUrl = window.location.pathname;
$(function(){
    Ladda.bind('button[type=submit]');
    Ladda.bind('.ladda-button');
    $("form.form").submit($f.ajaxSubmit);
});

