/**
 * [OElove] (C)2010-2099 oelove.com Webapp
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2020/03/12 by CL $
*/
$(function () {

    function OEProfile() {
        this.api_url = WAP_APPFILE+'&c=profile';
        this._this_dialog = null;
        this.slide_time = 300;
        
        this.obj_id = '';
        this.wake();
        this.refreshHeadImage();
        
    }
    OEProfile.prototype = {
        //触发
        wake:function() {
            var _this = this;
            
            //弹出
            $(document).on("click", "[f='open_profile_dialog']", function() {
                _this._this_dialog = OEUI.Dialog({
                    el: "#data_dialog",
                    animated: 'slideRightIn',
                    time:_this.slide_time
                });
            });
            //关闭 
            $(document).on("click", "[f='close_profile_dialog']", function() {
                _check_profile_mygr();
                if (_this._this_dialog) {
                    _this._this_dialog.hide();
                }
            });  
        },
        
        refreshHeadImage:function() {
            var _this = this;
            setTimeout(function(){
                
                var $window_url = window.location.href;
                if ($window_url != '') {
                    var $refresh = $window_url.indexOf('refresh=avatar');
                    if (typeof($refresh) == 'undefined') {
                        $refresh = '-1';
                    }
                    if ($refresh != '-1' && $refresh != '') {
                        if ($("#headimg_preview").length > 0) {
                            var $src = $("#headimg_preview").attr("src");
                            $("#headimg_preview").attr("src", $src+"?spm="+get_rndnum(6));
                        }
                    }
                }
                
            }, 200);
        }
    }
    new OEProfile();

});

//picker自动保存回调
function _auto_save(data, elm) {
    if (typeof($(data)) == 'undefined') {
        data = null;
    }
    
    var $api_url = WAP_APPFILE+'&c=profile';
    
    if (data && elm) {
        var $item = $(elm).attr("data-item");
        if (typeof($item) == 'undefined') {
            $item = '';
        }
        var $name = $(elm).attr("data-name");
        if (typeof($name) == 'undefined') {
            $name = '';
        }
        
        if ($item != '' && $name != '') {
            var $send_data = '';
            if ($item == 'area') {
                var $dist1 = $("#"+$name+"1").val();
                var $dist2 = $("#"+$name+"2").val();
                var $dist3 = $("#"+$name+"3").val();
                var $dist4 = $("#"+$name+"4").val();
                if (typeof($dist4) == 'undefined') {
                    $dist4 = 0;
                }
                $api_url += "&a=savearea";
                $send_data = {
                    'dist1':$dist1, 'dist2':$dist2, 'dist3':$dist3, 'dist4':$dist4
                };
                
            }
            else if ($item == 'hometown') {
                var $home1 = $("#"+$name+"1").val();
                var $home2 = $("#"+$name+"2").val();
                var $home3 = $("#"+$name+"3").val();
                if (typeof($home3) == 'undefined') {
                    $home3 = 0;
                }
                $api_url += "&a=savehometown";
                $send_data = {
                    'home1':$home1, 'home2':$home2, 'home3':$home3
                };
            }
            else if ($item == 'weixin') {
                var $val = $("#"+$name).val();
                $api_url += "&a=saveweixin";
                $send_data = {
                    'weixin':$val
                };
            }
            else if ($item == 'school') {
                var $areaid = $("#"+$name+"area1").val();
                var $schid = $("#"+$name+"id").val();
                $api_url += "&a=saveschool";
                $send_data = {
                    'scharea1':$areaid, 'schid':$schid
                };
            }
            else {
                var $val = $("#"+$name).val();
                $api_url += "&a=autosave";
                var $type = 'radio';
                if ($item == 'interest' || $item == 'language') {
                    $type = 'checkbox';
                }
                
                $send_data = {
                    'item':$item, 'type':$type, value:$val
                };
            }
            
            $.ajax({
                type: "POST",
                url: $api_url,
                cache: false,
                data:$send_data,
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    
                    if ($response == "1") {
                        /*
                        OEUI.toast({
                            type: 'bottom',
                            text: '保存成功'
                        });
                        */
                    }
                    else {
                        /*
                        if ($result != '') {
                            OEUI.toast({
                                type: 'bottom',
                                text: $result
                            });
                        }
                        */
                    }
                },
                error: function() {
                }
            });
            
        }
        
    }
}

//检测资料完善度
function _check_profile_mygr() {
    $.ajax({
        type: "POST",
        url: WAP_APPFILE + "&c=profile",
        cache: false,
        data: {
            a:"mygr", datatype:"json"
        },
        dataType: "json",
        success: function($data) {
            var $json = eval($data);
            var $response = $json.response;
            var $result = $json.result;
            if ($response == "1") {
                var $gr_data = parseInt($result.data);
                var $gr_cond = parseInt($result.cond);
                var $gr_dubai = parseInt($result.dubai);
                
                if ($gr_data >= 20) {
                    if ($("#gr_data_tips").length > 0) {
                        $("#gr_data_tips").hide();
                    }
                }
                if ($gr_cond >= 10) {
                    if ($("#gr_cond_tips").length > 0) {
                        $("#gr_cond_tips").hide();
                    }
                }
                if ($gr_dubai >= 10) {
                    if ($("#gr_dubai_tips").length > 0) {
                        $("#gr_dubai_tips").hide();
                    }
                }
            }
        },
        error: function() {

        }
    }); 
}