/**
 * [OElove] (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 2019/10/22 by OE $
*/
$(function(){

	//上传头像
	$(document).on("change", "[f='upload_img']", function(){
        
        //9.10.22
        $from = $("#from").val();
        if (typeof($from) == "undefined") {
            $from = "reg";
        }
        if ($from == "") {
            $from = "cp";
        }
        
        $forward = $("#forward").val();
        
		$file = $(this).attr("id");
		$url = $(this).attr("data-url");

		$obj_tips = $("#"+$file+"_tips");
		if ($obj_tips.length > 0) {
			$obj_tips.html("");
		}

		$obj_loading = $("#"+$file+"_loading");
		if ($obj_loading.length > 0) {
			$obj_loading.show();
		}

        
		$.ajaxFileUpload({
			"url": _ROOT_PATH+"index.php?=index&a=upload&module=tmp&forbidwater=1&r="+get_rndnum(8),
			"secureuri": false,
			"fileElementId": $file,
			"dataType": "json",
			"data": {
				uploadpart: $file
			},
			async: this.async,
			beforeSend: function ($xmlobj) {},
			complete: function ($data) {},
			success: function ($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				$imgthumb = $json.thumbimg;
				$imgurl = $json.uploadimg;
				$base64img = $json.base64img;
				//成功
				if ($response == "1") {
					if ($obj_loading.length > 0) {
						$obj_loading.hide();
					}
                    $("#"+$file+"_preview").attr("src", $imgurl);
                    layer.open({
                        title:"裁剪头像",
                        type: 2,
                        area: ['650px', '490px'],
                        shadeClose: false, //9.10.22
                        resize: false,
                        scrollbar: false,
                        content: _ROOT_PATH+"index.php?m=user&from="+$from+"&forward="+encodeURIComponent($forward)+"&c=avatar&imgcode="+encodeURIComponent($base64img)+"&r="+get_rndnum(8)
                    });
				}
				//失败
				else {
					if ($obj_loading.length > 0) {
						$obj_loading.hide();
					}
					$error = "上传失败，请检查图片大小和格式！";
					if ($result.length > 0 ) {
						$error = $result;
					}
					if ($obj_tips.length > 0) {
						$obj_tips.html("<font color='red'>"+$error+"</font>");
					}
				}

			},
			error: function ($data, e, b) {
				if ($obj_tips.length > 0) {
					$obj_tips.html("<font color='red'>上传失败，请检查图片大小和格式！</font>");
				}

			}
		});

	});

});