/**
 * [OElove] (C)2010-2099 oelove.com Inc.biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * $ Last update 2020/11/30 by OE $
*/
$(function(){
    //上传图片...
    $(document).on("change", "[f='but_upload_image']", function(){
		$_file_id = $(this).attr("id");
		$_module = $(this).attr("data-mod");
		if (typeof($_module) == "undefined") {
			$_module = "";
		}
		$_thumb = $(this).attr("data-thumb");
		if (typeof($_thumb) == "undefined") {
			$_thumb = "";
		}
		$_forbidcom = $(this).attr("data-forbidcom");
		if (typeof($_forbidcom) == "undefined") {
			$_forbidcom = "";
		}
		$_forbidwater = $(this).attr("data-forbidwater");
		if (typeof($_forbidwater) == "undefined") {
			$_forbidwater = "";
		}
		$_file_url = _OECPFILE+"&c=index&a=uploadimg&module="+$_module+"&thumbfiles="+$_thumb+"&forbidwater="+$_forbidwater+"&forbidcom="+$_forbidcom;
		_oeloveUploadImg($_file_id, $_file_url);
	});
    
    //上传excel 8.12.20
	$(document).on("change", "[f='but_upload_excel']", function(){
		$_file_id = $(this).attr("id");
		$_mod = $(this).attr("data-mod");
		if (typeof($_mod) == "undefined") {
			$_mod = "excel";
		}
		$_file_url = _OECPFILE+"&c=index&a=uploadimg&module="+$_mod;
		_oeloveUploadExcel($_file_id, $_file_url);
	});
    
    //上传txt文本 9.07.30
    $(document).on("change", "[f='but_upload_txt']", function(){
		$_file_id = $(this).attr("id");
		$_mod = $(this).attr("data-mod");
		if (typeof($_mod) == "undefined") {
			$_mod = "attfile";
		}
		$_file_url = _OECPFILE+"&c=index&a=uploadatt&module="+$_mod;
		_oeloveUploadTxt($_file_id, $_file_url);
	});


});

//后台通用上传图片...
function _oeloveUploadImg($file, $url, callBack) {
	callBack = callBack || function(){};
	if (_oeloveCheckImgType($file) == true) {
		$obj_loading = $("#"+$file+"_loading");
		if ($obj_loading.length > 0) {
			$obj_loading.show();
		}
		$.ajaxFileUpload({
			"url": $url,
			"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;
				$attid = $json.attid;
				if (typeof($attid) == "undefined") {
					$attid = "";
				}
				if ($response == "1") {
                    //同步云存储 9.07.19
                    _oeloveFileSynYun($attid);
                    
					if ($("#"+$file+"_thumb").length > 0) {
						$("#"+$file+"_thumb").val($imgthumb);
					}
					if ($("#"+$file+"_url").length > 0) {
						$("#"+$file+"_url").val($imgurl);
					}
					if ($("#"+$file+"_preview").length > 0) {
						$img = _ROOT_PATH+$imgurl+"?r="+get_rndnum(8);
						$("#"+$file+"_preview").attr("src", $img);
						$("#"+$file+"_preview").show();
					}
					if ($obj_loading.length > 0) {
						$obj_loading.hide();
					}
				}
				else {
					if ($obj_loading.length > 0) {
						$obj_loading.hide();
					}
					if ($result.length > 0 ) {
						layer.msg($result);
					}
					else {
						layer.msg("上传失败，请检查图片。");
					}
				}

				//callback js
				if (typeof(callBack) == "function") {
					callBack();
				}

			},
			error: function ($data, e, b) {
				if ($obj_loading.length > 0) {
					$obj_loading.hide();
				}
				layer.msg("系统繁忙，请稍后再试！");
			}
		});

	}
}
//判断图片格式
function _oeloveCheckImgType(filepart) {
	$res = true;
	$oe_file_obj = $("#"+filepart);
	$oe_file_val = $oe_file_obj.val();
	$oe_file_size = 0;
	if ($oe_file_val.length == 0) {
        layer.msg("请选择要上传的图片");
		$res = false;
	}
	if(!/.(gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG)$/.test($oe_file_val)){ 
        layer.msg("图片格式不正确");
		$res = false;
	}
	return $res;
}

/**
 * 上传Excel表格...8.12.20
 * @param:: string $file input file name
 * @param:: string $url post url
 * @param:: string $callback callback js name
 * @return::
*/
function _oeloveUploadExcel($file, $url) {
	if (_oeloveCheckExcelType($file) == true) {
		$obj_loading = $("#"+$file+"_loading");
		if ($obj_loading.length > 0) {
			$obj_loading.show();
		}
		$.ajaxFileUpload({
			"url": $url,
			"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;
				$dataurl = $json.uploadimg;
				$attid = $json.attid;
				if (typeof($attid) == "undefined") {
					$attid = "";
				}
				if ($response == "1") {
                    //同步云存储 9.07.19
                    _oeloveFileSynYun($attid);
                    
					if ($("#"+$file+"_url").length > 0) {
						$("#"+$file+"_url").val($dataurl);
					}
                    if ($("#"+$file+"_preview").length > 0) {
						$("#"+$file+"_preview").html($dataurl);
					}
					if ($obj_loading.length > 0) {
						$obj_loading.hide();
					}
				}
				else {
					if ($obj_loading.length > 0) {
						$obj_loading.hide();
					}
					if ($result.length > 0 ) {
						layer.msg($result);
					}
					else {
						layer.msg("上传失败，请检查");
					}
				}

			},
			error: function ($data, e, b) {
				if ($obj_loading.length > 0) {
					$obj_loading.hide();
				}
				layer.msg("系统繁忙，请稍后再试！");
			}
		});

	}
}
//判断Excel格式 8.09.01
function _oeloveCheckExcelType(filepart) {
	$res = true;
	$oe_file_obj = $("#"+filepart);
	$oe_file_val = $oe_file_obj.val();
	$oe_file_size = 0;
	if ($oe_file_val.length == 0) {
		layer.msg("请选择要上传的文件");
		$res = false;
	}
	if(!/.(xls|XLS|xlsx|XLSX)$/.test($oe_file_val)){ 
		layer.msg("Excel格式不正确");
		$res = false;
	}
	return $res;
}


//上传txt文本... 9.07.30
function _oeloveUploadTxt($file, $url, callBack) {
	callBack = callBack || function(){};
	if (_oeloveCheckTxtType($file) == true) {
		$obj_loading = $("#"+$file+"_loading");
		if ($obj_loading.length > 0) {
			$obj_loading.show();
		}
		$.ajaxFileUpload({
			"url": $url,
			"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;
				$atturl = $json.atturl;
				$attname = $json.attname;
                if (typeof($attid) == "undefined") {
					$attid = "";
				}
				if ($response == "1") {
                    //同步云存储 9.07.19
                    _oeloveFileSynYun($attid);
                    
					if ($("#"+$file+"_url").length > 0) {
						$("#"+$file+"_url").val($atturl);
					}
                    if ($("#"+$file+"_name").length > 0) {
						$("#"+$file+"_name").val($attname);
					}
					if ($("#"+$file+"_preview").length > 0) {
                        $("#"+$file+"_preview").html("上传成功："+$attname);
						$("#"+$file+"_preview").show();
					}
					if ($obj_loading.length > 0) {
						$obj_loading.hide();
					}
				}
				else {
					if ($obj_loading.length > 0) {
						$obj_loading.hide();
					}
					if ($result.length > 0 ) {
						layer.msg($result);
					}
					else {
						layer.msg("上传失败，请检查附件");
					}
				}

				//callback js
				if (typeof(callBack) == "function") {
					callBack();
				}

			},
			error: function ($data, e, b) {
				if ($obj_loading.length > 0) {
					$obj_loading.hide();
				}
				layer.msg("系统繁忙，请稍后再试！");
			}
		});

	}
}
//判断文本格式 9.07.30
function _oeloveCheckTxtType(filepart) {
	$res = true;
	$oe_file_obj = $("#"+filepart);
	$oe_file_val = $oe_file_obj.val();
	$oe_file_size = 0;
	if ($oe_file_val.length == 0) {
        layer.msg("请选择要上传的文本");
		$res = false;
	}
	if (!/.(txt|TXT)$/.test($oe_file_val)) { 
        layer.msg("只能上传txt文本");
		$res = false;
	}
	return $res;
}



