/**
 * [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 2018/08/31 by CL $
*/
/**
 * 获取提交参数
 * @return:: string/false
*/
function _oeloveGetField(){
    $data = "";
    $_name = $_id = ""; //ID元素
    $_data_prompt = ""; //校验文本提示
	$_type = ""; //元素的类型
	$_tips_box = ""; //文本提示框
	$_color = ""; //文本提示颜色
	$_value = ""; //值
	$need_tips = 0; //是否触发弹窗提醒

    $("[f='data-post']").each(function(){
		$_data_flag = 0;
		$_id = $(this).attr("id");
        $_name = $(this).attr("name");
        $_type = $(this).attr("type");
        $_data_flag = $(this).attr("data-flag");
        $_data_prompt = $(this).attr("data-prompt");
		$_tips_box = $(this).attr("data-box");
		$_color = $(this).attr("data-color");

		if (typeof($_name) == "undefined") {
			$_name = "";
		}
		if (typeof($_type) == "undefined") {
			$_type = "";
		}
		if (typeof($_data_flag) == "undefined") {
			$_data_flag = 0;
		}
		if (typeof($_data_prompt) == "undefined") {
			$_data_prompt = "";
		}
		if (typeof($_tips_box) == "undefined") {
			$_tips_box = "";
		}
		if (typeof($_color) == "undefined") {
			$_color = "red";
		}
		
		if ($_type == "radio") { //单选
			$_value = "";
			if ($_name.length > 0) {
				$_value = $("input[name='"+$_name+"']:checked").val();
				if (typeof($_value) == "undefined") {
					$_value = "";
				}
				
			}
		}
		else if ($_type == "checkbox") { //多选
			$_value = "";
			$_checkbox = document.getElementsByName($_name);
			$_valueArr = $_checkbox.length;
			for ($_i=0; $_i<$_valueArr; $_i++) {
				if ($_checkbox[$_i].checked == true) {
					$_value += $_checkbox[$_i].value+",";
				}
			}
		}
		else if ($_type == "select") { //下拉选择
			$_value = $("#"+$_id).find("option:selected").val();
		}
		else { //其他 text/hidden
			$_value = $("#"+$_id).val();
		}

		if ($_data_flag == 1) { //校验
			if ($_value.length == 0) {
				$need_tips = 1;
				return false; //跳出当前循环
			}
		}
		$data = $data+"&"+$_name+"="+encodeURIComponent($_value);

    });
	
	if ($need_tips == 1) {
		if ($_tips_box.length > 0) {
			if ($_data_prompt.length > 0) {
				$("#"+$_tips_box).html("<font color='"+$_color+"'>"+$_data_prompt+"</font>");
				return false; //跳出当前循环
			}
			else {
				$("#"+$_tips_box).html("<font color='"+$_color+"'>请填写完整信息</font>");
				return false; //跳出当前循环
			}
		}
		else {
			if ($_data_prompt.length > 0) {
				ToastTips($_data_prompt);
				return false; //跳出当前循环
			}
			else {
				ToastTips("请填写完整信息");
				return false; //跳出当前循环
			}
		}
	}
	else {
		return $data;
	}
}

/**
 * 发送ajax post
 * @param:: string $postjson JSON格式
 * @param:: null
*/
function _oeloveAjaxPost($postjson) {
	$post = eval($postjson);
	$url = $post.url; //提交URL
	if (typeof($url) == "undefined") {$url = "";}

	$type = $post.type; //执行结果处理类型 reload/jump/jumpb/other/return/.
	if (typeof($type) == "undefined") {$type = "";}

	$jumpurl = $post.jumpurl; //执行成功 跳转地址
	if (typeof($jumpurl) == "undefined") {$jumpurl = "";}

	$success_text = $post.success_text; //成功 信息提示
	if (typeof($success_text) == "undefined") {$success_text = "";}
	$success_color = $post.success_color; //成功 信息提示 字体颜色
	if (typeof($success_color) == "undefined") {$success_color = "green";}

	$fail_text = $post.fail_text; //失败 信息提示
	if (typeof($fail_text) == "undefined") {$fail_text = "";}
	$fail_color = $post.fail_color; //失败 信息提示 字体颜色
	if (typeof($fail_color) == "undefined") {$fail_color = "red";}

	$tips_box = $post.tips_box; //信息提示框
	if (typeof($tips_box) == "undefined") {$tips_box = "";}

	$callback = $post.callback; //回调函数
	if (typeof($callback) == "undefined") {$callback = "";}

	$timeout = $post.timeout; //时间间隔
	if (typeof($timeout) == "undefined") {$timeout = 600;}
	
	if ($url.length > 0) {
		$.ajax({
			type: "POST",
			url: $url,
			cache: false,
			data: {r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response; //执行状态 1-成功，0-失败
				$result = $json.result;
				$url = $json.url;

				if ($response == "1") { //成功

					if ($type == "reload") { //刷新当前页面

						if ($result.length > 0) {
							$success_text = $result;
						}
						if ($success_text.length > 0) {
							if ($tips_box.length > 0) {
								$("#"+$tips_box).html("<font color='"+$success_color+"'>"+$success_text+"</font>");
							}
							else {
								ToastTips($success_text);
							}
						}
						setTimeout(function(){
							window.location.reload();
						}, $timeout);

					}

					else if ($type == "jump"){ //跳转url
						if ($result.length > 0) {
							$success_text = $result;
						}
						if ($success_text.length > 0) {
							if ($tips_box.length > 0) {
								$("#"+$tips_box).html("<font color='"+$success_color+"'>"+$success_text+"</font>");
							}
							else {
								ToastTips($success_text);
							}
							setTimeout(function(){
								window.location.href = $jumpurl;
							}, $timeout);
						}
						else {
							window.location.href = $jumpurl;
						}
					}

					else if($type == "jumpb"){ //跳转url并获取服务器参数$result

						if ($success_text.length > 0) {
							if ($tips_box.length > 0) {
								$("#"+$tips_box).html("<font color='"+$success_color+"'>"+$success_text+"</font>");
							}
							else {
								ToastTips($success_text);
							}
							setTimeout(function(){
								window.location.href = $jumpurl+$result;
							}, $timeout);

						}
						else {
							window.location.href = $jumpurl+$result;
						}
					}
					else if($type == "other") { //其他不做任何处理
					
					}
					else if ($type == "return") { //返回结果
						return $response;
					}

					else { //默认
						if ($success_text.length > 0) {
							if ($tips_box.length > 0) {
								$("#"+$tips_box).html("<font color='"+$success_color+"'>"+$success_text+"</font>");
							}
							else {
								ToastTips($success_text);
							}
						}
						else {
							if ($tips_box.length > 0) {
								$("#"+$tips_box).html("<font color='"+$success_color+"'>操作成功</font>");
							}
							else {
								ToastTips("操作成功");
							}
						}
					}
					
					if ($callback.length > 0) {
						$fun = eval($callback);
						if (typeof($fun) == "function") {
							$fun.call();
						}
					}
				}


				else { //失败
					
					if ($type == "return") {
						return $response;
					}
					else {
						if ($result.length > 0) {
							$fail_text = $result;
						}
						if ($fail_text.length == 0) {
							$fail_text = "系统繁忙，请稍后再试";
						}
						if ($tips_box.length > 0) {
							$("#"+$tips_box).html("<font color='"+$fail_color+"'>"+$fail_text+"</font>");
						}
						else {
							ToastTips($fail_text);
						}
					}

				}
			},

			error: function() {

			}
		});
	}
}

//同步附件云存储
function _oeloveFileSynYun($id) {
	if ($id != "") {
		$.ajax({
			type: "POST",
			url:  _ROOT_PATH+"index.php?c=ajax",
			cache: false,
			data: {a:"synyun", id:$id, r:get_rndnum(8)},
			dataType: "json",
			success: function($yundata) {
				$yunjson = eval($yundata);
				$yunresponse = $yunjson.response;
				$yunresult = $yunjson.result;
				if ($yunresponse == "1") {

				}
				else {

				}
			},
			error: function() {
			}
		});
	}

}