/**
 * [OEUI] (C)2010-2099 oelove.com Inc. biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2020/11/11 by CL $
*/
var boundx, boundy, flip_obj;
$(function(){
    
    function OEAvatar() {
        this.obj_id = '';
        this.clickUpload();
        this.clickCut(); 
    }
    
    OEAvatar.prototype = {
        
        //初始化上传头像 
        clickUpload:function() {
            var _this = this;
            
            $("[f='but_upload_avatar']").each(function(index, e){
                var up_id = $(e).attr('id');
                var module = $(e).attr('data-mod');
                if (typeof(module) == 'undefined') {
                    module = 'tmp';
                }
                var thumb = $(e).attr('data-thumb');
                if (typeof(thumb) == 'undefined') {
                    thumb = '';
                }
                var forbidcom = $(e).attr('data-forbidcom');
                if (typeof(forbidcom) == 'undefined') {
                    forbidcom = '';
                }
                var forbidwater = $(e).attr('data-forbidwater');
                if (typeof(forbidwater) == 'undefined') {
                    forbidwater = '1';
                }
                var url = $(e).attr('data-url') || '';
                //实例化上传控件
                var upload = new Uploader({
                    el: $("#"+up_id)[0],
                    data:{
                        'uploadpart':'file', 'module':module, 
                        'thumb':thumb, 'forbidcom':forbidcom, 'forbidwater':forbidwater
                    },
                    url: url ? url : _OE_FILE+'&c=index&a=upload'
                });
                
                upload.on("success", function(data){
                    var response = data.response;
                    var result = data.result;
                    var imgthumb = data.thumbimg;
                    var imgurl = data.uploadimg;
                    var base64img = data.base64img;
                    if (typeof(base64img) == '') {
                        base64img = '';
                    }
                    var attid = data.attid;
                    if (typeof(attid) == 'undefined') {
                        attid = '';
                    }
                    if (response == '1') {
                        //弹出裁剪框
                        window.uploadAvatar = OEUI.modal.dialog({
                            type: 'iframe',
                            title: '裁剪头像',
                            width: '650',
                            height: '500',
                            content: _ROOT_PATH+ 'index.php?m=user&c=avatar&imgcode='+encodeURIComponent(base64img)
                        });
                        
                    }
                    else {
                        if (result == '') {
                            result = '上传失败，请检查';
                        }
                        OEUI.message.msg({
                            text: result
                        });
                    }
                    
                });
            });
        },
        
        //裁剪头像
        clickCut:function() {
            var _this = this;
            
            $(document).on("click", "[f='but_confirm_cut']", function(){
                var obj_id = $(this).attr("id");
                
                if (!$("#"+obj_id).hasClass("forbid_submit")) {
                
                    var imgcode = $("#imgcode").val();
                    var x1 = $("#x1").val();
                    var y1 = $("#y1").val();
                    var x2 = $("#x2").val();
                    var y2 = $("#y2").val();
                    var w = $("#w").val();
                    var h = $("#h").val();
                    var boundwidth = boundx; //压缩比例宽
                    var boundheight = boundy; //压缩比例高
                    if (w.length == '') {
                        OEUI.message.msg({
                            text: '请拖动设置白色裁剪框'
                        });
                        return false;
                    }
                    if (h == '') {
                        OEUI.message.msg({
                            text: '请拖动设置白色裁剪框'
                        });
                        return false;
                    }
                    
                    $("#"+obj_id).addClass("forbid_submit");
                    $("#"+obj_id).find("span").show();
                    $("#cut_loading").show();
                    
                    var come_from = $("#from").val();
                    if (typeof(come_from) == 'undefined') {
                        come_from = '';
                    }
                    var forward = $("#forward").val();
                    
                    $.ajax({
                        type: "POST",
                        url: _ROOT_PATH+"index.php?m=user&c=avatar",
                        cache: false,
                        data: {
                            a:"cropper", imgcode:imgcode, x1:x1, y1:y1, x2:x2, y2:y2, 
                            w:w, h:h, boundwidth:boundwidth, boundheight:boundheight
                        },
                        dataType: "json",
                        success: function(data) {
                            var response = data.response;
                            var result = data.result;
                            var attid = data.attid;
                            if (typeof(attid) == "undefined") {
                                attid = "";
                            }
                            if (response == "1") {
                                OEUI.message.msg({
                                    text: '裁剪成功'
                                });
                                _oeloveFileSynYun(attid); //同步云存储
                                
                                if (come_from == "reg") {
                                    //来源注册流程
                                    if (forward == '') {
                                        forward = _ROOT_PATH+"index.php?c=passport&a=success";
                                    }
                                    setTimeout(function(){
                                        window.parent.location.href = forward;
                                    }, 600);
                                }
                                else if (come_from == "cp") {
                                    if (forward == '') {
                                        forward = _ROOT_PATH+"index.php?m=user";
                                    }
                                    setTimeout(function(){
                                        parent.layer.closeAll();
                                        window.parent.location.href = forward;
                                    }, 600);
                                }
                                else {
                                    setTimeout(function(){
                                        window.parent.location.reload();
                                    }, 600);
                                }
                            }
                            else {
                                if (result == '') {
                                    result = '裁剪失败，请检查';
                                }
                                OEUI.message.msg({
                                    text: result
                                });
                                $("#"+$obj_id).removeClass("forbid_submit");
                                $("#"+$obj_id).find("span").hide();
                                $("#cut_loading").hide();
                                
                            }
                        },
                        error: function() {
                            OEUI.message.msg({
                                text: '系统繁忙，请稍后再试'
                            });
                            $("#"+$obj_id).removeClass("forbid_submit");
                            $("#"+$obj_id).find("span").hide();
                            $("#cut_loading").hide();
                        }
                    });
                
                }

            }); 
        }
    }
    
    new OEAvatar();
});