/**
 * [OEmarry] (C)2010-2099 OEMARRY.COM Inc.
 * Email: service@phpcoo.com, phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2016-08-16 by OEdev $
*/
/**
 * 获取提交参数
 * @return:: string/false
*/
function _oemarryGetField(){
    $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) {
				ToastShow($_data_prompt);
				return false; //跳出当前循环
			}
			else {
				ToastShow("请填写完整信息");
				return false; //跳出当前循环
			}
		}
	}
	else {
		return $data;
	}
}

/**
 * 发送ajax post
 * @param:: string $postjson JSON格式
 * @param:: null
*/
function _oemarryAjaxPost($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 {
								ToastShow($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 {
								ToastShow($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 {
								ToastShow($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 {
								ToastShow($success_text);
							}
						}
						else {
							if ($tips_box.length > 0) {
								$("#"+$tips_box).html("<font color='"+$success_color+"'>操作成功</font>");
							}
							else {
								ToastShow("操作成功");
							}
						}
					}
					
					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 {
							ToastShow($fail_text);
						}
					}

				}
			},

			error: function() {

			}
		});
	}
}

function _oemarryAjaxGetContent($_url, $_tpname){
    if (typeof($_url) == "undefined") {
        $_url = "";
    }
    if (typeof($_tpname) == "undefined") {
        $_tpname = "ajax_get_content";
    }
    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;
                $result = $json.result;
                if ($response == "1") {
                    $("#"+$_tpname).html($result);
                }
            },
            error: function() {

            }
        });
    }
}


$(function(){
	
	//点击加载下一页 通用
	$("[f='but_loadmore_page']").bind("click", function(){
		$_url = $(this).attr("data-url"); //请求页面
		if (typeof($_url) == "undefined") {
			$_url = "";
		}
		$_page = $("#page").val(); //请求页数
		if (typeof($_page) == "undefined") {
			$_page = 2;
		}
		$_result_box = $(this).attr("data-box"); //请求结果 存放框
		if (typeof($_result_box) == "undefined") {
			$_result_box = "json_data";
		}

		$s_orderby = $("#s_orderby").val(); //排序 备用
		if (typeof($s_orderby) == "undefined") {
			$s_orderby = "";
		}

		if ($_url.length > 0) {
			if ($s_orderby.length > 0) {
				$_url = $_url+"&s_orderby="+$s_orderby; //兼容排序
			}
			$(this).html("加载中...");
			$.ajax({
				type: "GET",
				url: $_url,
				cache: false,
				data: {page:$_page, r:get_rndnum(8)},
				dataType: "json",
				success: function($data) {
					$json = eval($data);
					$response = $json.response;
					$nextpage = $json.nextpage;
					$result = $json.result;
					$("#page").val($nextpage); //重新赋值
					if ($response == "1") {
						if ($result.length > 0) {
							if ($nextpage > 0) {
								$("[f='but_loadmore_page']").show();
							}
							else {
								$("[f='but_loadmore_page']").hide();
							}
							//加入数据
							$("#"+$_result_box).append($result);
							$("[f='but_loadmore_page']").html("点击加载下一页");
						}
					}
					
				},
				error: function() {
				
				}
			});
		}

	});

	//点击事件 AJAX载入第一页数据
	$("[f='but_loadfirst_data']").bind("click", function(){
		$_url = $(this).attr("data-url"); //请求页面
		if (typeof($_url) == "undefined") {
			$_url = "";
		}
		$_result_box = $(this).attr("data-box"); //请求结果 存放框
		if (typeof($_result_box) == "undefined") {
			$_result_box = "json_data";
		}

		if ($_url.length > 0) {
			$("#"+$_result_box).html("<p style='padding:10px;text-align:center;'>数据加载中...</p>");

			$but_loadmore_page = $("[f='but_loadmore_page']");
			$more_btn = $("#more_btn");

			if ($but_loadmore_page.length > 0) {
				$but_loadmore_page.hide();
			}
			if ($more_btn.length > 0) {
				$more_btn.hide();
			}
			$.ajax({
				type: "GET",
				url: $_url,
				cache: false,
				data: {r:get_rndnum(8)},
				dataType: "json",
				success: function($data) {
					$json = eval($data);
					$response = $json.response;
					$nextpage = $json.nextpage;
					$result = $json.result;
					$("#page").val($nextpage); //重新赋值

					if ($response == "1") {
						if ($nextpage > 0) {
							if ($but_loadmore_page.length > 0) {
								$but_loadmore_page.show();
							}
							if ($more_btn.length > 0) {
								$more_btn.show();
							}
						}
						else {
							if ($but_loadmore_page.length > 0) {
								$but_loadmore_page.hide();
							}
							if ($more_btn.length > 0) {
								$more_btn.hide();
							}
						}
						//加入数据
						$("#"+$_result_box).html($result);
						if ($but_loadmore_page.length > 0) {
							$but_loadmore_page.html("点击加载下一页");
						}
						if ($more_btn.length > 0) {
							$more_btn.html("点击加载下一页");
						}

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

	//删除单条记录
	$(document).on("click", "[f='but_del_one']", function(){
		$_type = $(this).attr("data-type");		
		$_mod = $(this).attr("data-mod");
		$_a = $(this).attr("data-a");
		$_id = $(this).attr("data-id");

		if (typeof($_type) == "undefined") {
			$_type = "shopbespoke";
		}
		if (typeof($_mod) == "undefined") {
			$_mod = "";
		}
		if (typeof($_a) == "undefined") {
			$_a = "del";
		}
		$.ajax({
			type: "POST",
			url: WAP_APPFILE,
			cache: false,
			data: {c:$_type, a:$_a, id:$_id, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					ToastShow("删除成功");
					if ($_mod.length > 0) {
						$("#"+$_mod+"_"+$_id).remove();
					}
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("删除失败");
					}
				}
			},
			error: function() {
			
			}
		});
	});

});



//page_more_btn处理
function _oeGetListData($url, $firstpage, $databox) {
	if (typeof($url) == "undefined") {
		$url = "";
	}
	if (typeof($firstpage) == "undefined") {
		$firstpage = "";
		
	}
	if ($firstpage == "1") {
		$page = 1;
	}
	else {
		$page = $("#page").val();
	}
	if (typeof($databox) == "undefined") {
		$databox = "json_data";
	}

	if ($url.length == 0) {
		$url = $("#reqs_url").val();
	}
	$more_obj = $("#page_more_btn");
	$more_obj.html("");
	if ($page == 1) {
		$("#"+$databox).html("<p style='text-align:center;padding:10px;'>数据加载中</p>");
	}
	else {
		$more_obj.html("加载中...");
	}
	if ($url.length > 0) {

		$.ajax({
			type: "GET",
			url: $url,
			cache: false,
			data: {page:$page, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$nextpage = $json.nextpage;
				$result = $json.result;
				$("#page").val($nextpage);
				if ($response == "1") {
					if ($nextpage > 0) {
						$more_obj.show();
					}
					else {
						$more_obj.hide();
					}
					if ($firstpage == "1") {
						//加入数据
						$("#"+$databox).html($result);
					}
					else {
						//叠加数据
						$("#"+$databox).append($result);
					}
					$more_obj.html("点击加载下一页");
				}
			},
			error: function() {}
		});

	}
}


