/**
 * [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-04-23 by OEdev $
*/

$(function(){ 
	
	//通用 单个确认操作 2016.04.23
	$("[f='oneconfirm']").live("click", function(){
		$_url = $(this).attr("data-url");
		$_text = $(this).attr("data-text");
		if (typeof($_text) == "undefined") {
			$_text = "确定要执行该操作吗？";
		}
		oePopConfirm($_text, $_url, "url");
	});


	//改变tr错综样式代码 页面底部加载 table增加样式名 table_cs
	$oe_table_obj = $(".table_cs tr");
	$oe_tr_len = $oe_table_obj.length;
	if ($oe_tr_len > 0) {
		for (i=0; i<$oe_tr_len; i++){
			if (i > 0) {
				if (i%2 == 0) {
					$($oe_table_obj).eq(i).addClass("tr_b");
				}
			}
		}
	}

	//店铺装修位置选择器 <label class="label_pos" f="pos" data-css="label_pos" data-target="input name" data-value="1"></label>
	$("label[f='pos']").live("click", function(){
		$label_id = ($(this).attr("data-css"));
		$("."+$label_id).removeClass("_td_label_curr");
		$(this).addClass("_td_label_curr");

		$input_name = $(this).attr("data-target");
		$input_value = $(this).attr("data-value");
		$("#"+$input_name).val($input_value);
	});

	//店铺装修清除颜色
	$("a[f='delzxcolor']").live("click", function(){
		$tag_name = $(this).attr("data-target");
		$("#"+$tag_name).val("");
		$("#btn_"+$tag_name).css("backgroundColor", "");
	});

	//店铺装修删除图片
	$("a[f='delzximg']").live("click", function(){
		$tag_name = $(this).attr("data-target");
		$tag_type = $(this).attr("data-type");
		
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?m=shop",
			cache: false,
			data: {c:"diy", a:"delimg", type:$tag_type, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					ToastShow("删除成功");
					if ($tag_name.length > 0) {
						$("#"+$tag_name+"_preview").attr("src", "tpl/static/images/waitimg_s.jpg");
						$("#"+$tag_name+"_url").val("");
					}
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("删除失败");
					}
					
				}

			},
			error: function() {
				ToastShow("删除失败，请检查网络！");
			}
		});

	});

	//预览图片(通用)
	$("img[f='view']").live("click", function(){
		$img_url = $(this).attr("src");
		if ($img_url.length > 0) {
			if ($img_url.substr(0, 1) == "/") {
			
			}
			else {
				$img_url = _ROOT_PATH+$img_url;
			}
			window.open($img_url);
		}
	});

	//移除相册/商品图片集合
	$("span[f='removeimg']").live("click", function(){
		$img_target = $(this).attr("data-target");
		$("#"+$img_target+"_preview").attr("src", "tpl/static/images/waitimg.jpg");
		$("#"+$img_target+"_thumb").val("");
		$("#"+$img_target+"_url").val("");
	});


	//检测店铺名称、证件号码等..AJAX
	$("input[f='ajaxcheck']").live("blur", function(){
		$target_name = $(this).attr("id");
		$yes_msg = "";
		$no_msg = "";
		if ($(this).val().length > 0) {
			$tips_obj = $("#tips_"+$target_name);
			if ($target_name == "shopname") { //店铺名
				$url = _ROOT_PATH+"index.php?m=shop&c=rz&a=checkshopname&name="+$(this).val();
				$yes_msg = "店铺名称可用";
				$no_msg = "店铺名不可用";
			}
			else if ($target_name == "idnumber") { //证件号码
				$url = _ROOT_PATH+"index.php?m=shop&c=rz&a=checkidnumber&name="+$(this).val();
				$yes_msg = "证件号码通过";
				$no_msg = "证件号码不可用";
			}
			$.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") {
						$tips_obj.html("<font color='green'>"+$yes_msg+"</font>");
					}
					else {
						if ($result.length > 0) {
							$tips_obj.html("<font color='red'>"+$result+"</font>");
						}
						else {
							$tips_obj.html("<font color='red'>"+$no_msg+"</font>");
						}
					}
				},
				error: function() {
					ToastShow("请求失败，请检查网络！");
				}
			});

		}
	});

	
	//店铺升级
	$("a[f='buyshopvip']").live("click", function(){
		$mylevelid = $(this).attr("mylevel"); //我的等级
		$levelid = $(this).attr("lvid"); //等级ID
		$levelname = $(this).attr("lvname"); //等级名称
		
		$("#vipid").val($levelid);
		//alert($levelid);
		oePopConfirm("确定要升级【"+$levelname+"】吗？", "oemarryBuyShopVip", "func");
	});


	//阅读站内消息
	$("a[f='readmsg']").live("click", function(){
		$tag_value = $(this).attr("data-value");
		
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?m=shop",
			cache: false,
			data: {c:"msg", a:"read", id:$tag_value, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					$("#msg_status_"+$tag_value).html("<font color='green'>已阅</font>");
					$("#msg_btn_"+$tag_value).html("~~");
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("操作失败");
					}
					
				}

			},
			error: function() {
				ToastShow("操作失败，请检查网络！");
			}
		});

	});


	$("[f='addshopreply']").live("click", function(){ //添加店铺留言回复
		$commid = $(this).attr("data-id");
		oe_varpop_cp_addshopreply($commid);
	});
	$("[f='editshopreply']").live("click", function(){ //编辑店铺留言回复
		$replyid = $(this).attr("data-id");
		oe_varpop_cp_editshopreply($replyid);
	});
	$("[f='delshopreply']").live("click", function(){ //删除店铺留言回复
		$replyid = $(this).attr("data-id");
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?m=shop",
			cache: false,
			data: {c:"shopcomment", a:"delreply", id:$replyid, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					ToastShow("删除失败");
					setTimeout(function(){
						window.location.reload();
					}, 800);
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("删除失败");
					}
					
				}

			},
			error: function() {
				ToastShow("删除失败，请检查网络！");
			}
		});
	});

	
	$("[f='shopwordsreply']").live("click", function(){ //回复店铺留言
		$wordid = $(this).attr("data-id");
		oe_varpop_cp_replyshopwords($wordid);
	});

	//浏览内容
	$("[f='detail']").live("click", function(){
		$_mod = $(this).attr("data-mod");
		$_id = $(this).attr("data-id");
		$_url = _ROOT_PATH+"index.php?c="+$_mod+"&a=detail&id="+$_id;
		window.open($_url);
	});


	//通用 批量删除(form 提交方式)
	$("[f='multidel']").live("click", function(){
		$_id_name = $(this).attr("data-idname");
		$_a = $(this).attr("data-a");
		$_form = $(this).attr("data-form");

		$_value = document.getElementsByName($_id_name);;
		$_valueArr = $_value.length;
		$_str = "";
		$_num = 0;
		for ($_i=0; $_i<$_valueArr; $_i++) {
			if($_value[$_i].checked == true) {
				$_str += $_value[$_i].value+",";
				$_num++;
		   }
		}
		if ($_num < 1) {
			ToastShow("请选择要删除的信息");
			return false;
		}
		if ($_a != "") {
			$("#a").val($_a);
		}
		$("#oeform").val($_form);
		oePopConfirm("确定要删除吗？", "oemarryShopCallBackDelForm");
	});


	//检测标题长度
	$("[f='checktitle']").live("blur", function(){

		$_value = $(this).val();
		$_tips = $("#"+$(this).attr("data-tips"));
		if ($_value.length > 0) {
		
			$.ajax({
				type: "POST",
				url: _ROOT_PATH+"index.php",
				cache: false,
				data: {m:"shop", c:"index", a:"checktitle", value:$_value, r:get_rndnum(8)},
				dataType: "json",
				success: function($data) {
					$json = eval($data);
					$response = $json.response;
					$result = $json.result;
					if ($response == "1") {
						$_tips.html("<font color='green'>标题可用</font>");
					}
					else {
						if ($result.length > 0) {
							$_tips.html("<font color='red'>"+$result+"</font>");
						}
						else {
							$_tips.html("<font color='red'>标题超过30个字</font>");
						}
					}
				},
				error: function() {
					ToastShow("请求失败，请检查网络！");
				}
			});

		}

	});


});


//弹出确认框回调删除信息函数结合f=multidel使用
function oemarryShopCallBackDelForm() {
	eval($("#oeform").val()).submit();
}

//购买VIP店铺
function oemarryBuyShopVip() {
	$vipid = $("#vipid").val();
	if ($vipid > 1) {
		$url = _ROOT_PATH+"index.php";
		$.ajax({
			type: "POST",
			url: $url,
			cache: false,
			data: {m:"shop", c:"vip", a:"buy", vipid:$vipid, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					//$tips_obj.html("<font color='green'>"+$yes_msg+"</font>");
					ToastShow("购买成功");
					setTimeout(function(){
						window.location.href = _ROOT_PATH+"index.php?m=shop";
					}, 800);
				}
				else if ($response == "2") {
					window.location.href = _ROOT_PATH+"index.php?c=pay&paynum="+$result;
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("购买失败，请联系网站！");
					}
				}
			},
			error: function() {
				ToastShow("请求失败，请检查网络！");
			}
		});
	}

}

//检测通过
function oemarryCheckOk(id, text) {
	if (typeof(text) == "undefined") {
		text = "";
	}
	$obj = $("#"+id);
	$obj.next("span.oe_trap_ok").remove();
	$obj.next("span.oe_trap_error").remove();
	$result = "<span class='oe_trap_ok'><label></label>";
	if (text.length > 0) {
		$result += "<em>"+text+"</em>";
	}
	$result += "</span>";
	$obj.after($result);
}
//检测不通过
function oemarryCheckError(id, text) {
	if (typeof(text) == "undefined") {
		text = "";
	}
	$obj = $("#"+id);
	$obj.next("span.oe_trap_ok").remove();
	$obj.next("span.oe_trap_error").remove();
	$result = "<span class='oe_trap_error'><label></label>";
	if (text.length > 0) {
		$result += "<em>"+text+"</em>";
	}
	$result += "</span>";
	$obj.after($result);
}


//全选/反选
function oemarrySelectAll(obj, cls) {
	if (typeof(cls) == "undefined") {
		cls = "selall";
	}
	//alert($(obj).attr("class").indexOf("icon-checkbox-unchecked"));
	if ($(obj).hasClass("icon-checkbox-unchecked")) { //执行全选
		$(obj).removeClass("icon-checkbox-unchecked");
		$(obj).addClass("icon-checkbox-checked");
		$("."+cls+":checkbox").attr("checked", true);

	}
	else { //执行反选
		$(obj).removeClass("icon-checkbox-checked");
		$(obj).addClass("icon-checkbox-unchecked");
		$("."+cls+":checkbox").attr("checked", false);
	}
}


/**
 * 弹出添加留言回复
 * @param:: int $commid 店铺ID
 * @param:: int $width 页面宽
 * @param:: int $height iframe高
 * @return:: null
*/
function oe_varpop_cp_addshopreply($commid, $width, $height) {
	if (typeof($width) == "undefined") {$width = 580;}
	if (typeof($height) == "undefined") {$height = 300;}
	$_url = _ROOT_PATH+"index.php?m=shop&c=shopcomment&a=addreply&id="+$commid;
	oePopDialog("oe_js_pop_shop_addreply", "回复点评", $_url, "ajax", $width, $height);
}
/**
 * 弹出编辑留言回复
 * @param:: int $replyid 回复ID
 * @param:: int $width 页面宽
 * @param:: int $height iframe高
 * @return:: null
*/
function oe_varpop_cp_editshopreply($replyid, $width, $height) {
	if (typeof($width) == "undefined") {$width = 580;}
	if (typeof($height) == "undefined") {$height = 300;}
	$_url = _ROOT_PATH+"index.php?m=shop&c=shopcomment&a=editreply&id="+$replyid;
	oePopDialog("oe_js_pop_shop_editreply", "编辑点评回复", $_url, "ajax", $width, $height);
}

/**
 * 弹出回复店铺留言
 * @param:: int $wordsid 留言ID
 * @param:: int $width 页面宽
 * @param:: int $height iframe高
 * @return:: null
*/
function oe_varpop_cp_replyshopwords($wordsid, $width, $height) {
	if (typeof($width) == "undefined") {$width = 580;}
	if (typeof($height) == "undefined") {$height = 300;}
	$_url = _ROOT_PATH+"index.php?m=shop&c=shopwords&a=edit&id="+$wordsid;
	oePopDialog("oe_js_pop_shop_replyshopwords", "回复留言", $_url, "ajax", $width, $height);
}

/*------------------------------ 华丽分割线  ---------------------------------*/

//添加价格体系
function oemarryAddGoodsPriceItem(objid, numid) {	
	$obj = $("#"+objid);
	$obj_num = parseInt($("#"+numid).val());
	$current_id = parseInt($obj_num+1);
	$html_tr = "<tr>"+
				"<td><input type='text' name='priceid_"+$current_id+"' id='priceid_"+$current_id+"' class='input-s' /></td>"+
				"<td><input type='text' name='name_"+$current_id+"' id='name_"+$current_id+"' class='input-150' /></td>"+
				"<td><input type='text' name='price_"+$current_id+"' id='price_"+$current_id+"' class='input-100' /></td>"+
				"<td><a href='javascript:;' onclick=\"oemarryDelTrItem(this);\">移除</a></td>";
	$obj.find("tr").last().after($html_tr);
	//重新赋值
	$("#"+numid).val($current_id);
}
//删除价格体系
function oemarryDelTrItem(obj) {
	$(obj).parent('td').parent('tr').remove();
}


//添加属性体系
function oemarryAddGoodsAttrItem(objid, numid) {	
	$obj = $("#"+objid);
	$obj_num = parseInt($("#"+numid).val());
	$current_id = parseInt($obj_num+1);
	$html_tr = "<tr>"+
				"<td><input type='text' name='attrid_"+$current_id+"' id='attrid_"+$current_id+"' class='input-s' /></td>"+
				"<td><input type='text' name='attrvalue_"+$current_id+"' id='attrvalue_"+$current_id+"' class='input-150' /></td>"+
				"<td><a href='javascript:;' onclick=\"oemarryDelTrItem(this);\">移除</a></td>";
	$obj.find("tr").last().after($html_tr);
	//重新赋值
	$("#"+numid).val($current_id);
}