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

$(function(){ 
	
	//改变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");
				}
			}
		}
	}

	//通用 单个确认操作 2016.04.23
	$("[f='oneconfirm']").bind("click", function(){
		$_url = $(this).attr("data-url");
		$_text = $(this).attr("data-text");
		if (typeof($_text) == "undefined") {
			$_text = "确定要执行该操作吗？";
		}
		oePopConfirm($_text, $_url, "url");
	});
	
	//预览图片
	$("[f='viewimg']").live("click", function(){
		$img_url = $(this).attr("data-img"); //预览大图
		if (typeof($img_url) == "undefined") {
			$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);
		}
	});

	//清除图片
	$("[f='removeimg']").live("click", function(){
		$_img_data_preview = $(this).attr("data-preview"); //预览
		$_img_data_input = $(this).attr("data-input"); //默认input
		$_img_data_thumbimg = $(this).attr("data-thumb"); //缩略图input
		$_img_data_drawimg = $(this).attr("data-draw"); //原图input
		
		
		if (typeof($_img_data_input) != "undefined") {
			$("#"+$_img_data_input).val("");
		}
		if (typeof($_img_data_thumbimg) != "undefined") {
			$("#"+$_img_data_thumbimg).val("");
		}
		if (typeof($_img_data_drawimg) != "undefined") {
			$("#"+$_img_data_drawimg).val("");
		}
		if (typeof($_img_data_preview) != "undefined") {
			$("#"+$_img_data_preview).attr("src", _ROOT_PATH+"tpl/static/images/waitimg.jpg");
		}
	});

	//跳转到页面 2016.03.15
	$("[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);
	});

});



//URL方式删除
function oemarryDialogUrlDel(url, text, back) {
	if (typeof(url) == "undefined") {url = "";}
	if (typeof(text) == "undefined") {text = "确定要删除吗？一旦执行不可恢复！";}
	if (typeof(back) == "undefined") {back = "";}
	$.dialog({
		lock:true,
		title: "温馨提示",
		content: text, 
		icon: "warning",
		button: [{
				name: '确定',
				callback: function () {
					if (url.length > 0) {
						window.location.href = url;
					}
					if (typeof(back) == "function") {
						back.call(this);
					}
					return true;
				}
			}, 
			{
				name: '取消'
			}]
	});
}

//Form方式删除
function oemarryDialogFormDel(form, text, back) {
	if (typeof(form) == "undefined") {form = "";}
	if (typeof(text) == "undefined") {text = "确定要删除吗？一旦执行不可恢复！";}
	if (typeof(back) == "undefined") {back = "";}
	$.dialog({
		lock:true,
		title: "温馨提示",
		content: text, 
		icon: "warning",
		button: [{
				name: '确定',
				callback: function () {
					if (form.length > 0) {
						$("#"+form).submit();
					}
					if (typeof(back) == "function") {
						back.call(this);
					}
					return true;
				}
			}, 
			{
				name: '取消'
			}]
	});
}


//URL方式确定对话框2016.03.25
function oemarryDialogUrlConfirm(url, text, back) {
	if (typeof(url) == "undefined") {url = "";}
	if (typeof(text) == "undefined") {text = "确定要执行该操作吗？一旦执行不可恢复！";}
	if (typeof(back) == "undefined") {back = "";}
	$.dialog({
		lock:true,
		title: "温馨提示",
		content: text, 
		icon: "warning",
		button: [{
				name: '确定',
				callback: function () {
					if (url.length > 0) {
						window.location.href = url;
					}
					if (typeof(back) == "function") {
						back.call(this);
					}
					return true;
				}
			}, 
			{
				name: '取消'
			}]
	});
}

//Form方式确定对话框2016.03.25
function oemarryDialogFormConfirm(form, text, back) {
	if (typeof(form) == "undefined") {form = "";}
	if (typeof(text) == "undefined") {text = "确定要执行该操作吗？一旦执行不可恢复！";}
	if (typeof(back) == "undefined") {back = "";}
	$.dialog({
		lock:true,
		title: "温馨提示",
		content: text, 
		icon: "warning",
		button: [{
				name: '确定',
				callback: function () {
					if (form.length > 0) {
						$("#"+form).submit();
					}
					if (typeof(back) == "function") {
						back.call(this);
					}
					return true;
				}
			}, 
			{
				name: '取消'
			}]
	});
}

//检测通过
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);
	}
}

//AJAX更新状态...
function oemarryUpdateFlag($imgid, $id){
	$requesturl = document.URL;
	$url = $requesturl.split("&")[0]+"&"+$requesturl.split("&")[1];
	$img_on  = _ROOT_PATH + "tpl/static/images/img_open.gif";
	$img_off = _ROOT_PATH + "tpl/static/images/img_close.gif";
    $type  = $("#attr_"+$imgid+$id).val();
	if ($type == $imgid+"close") {
		$("#"+$imgid+$id).attr("src",$img_off);
		$("#attr_"+$imgid+$id).val($imgid+"open");
		$.ajax({
			type: "POST",
			url: $url,
			data: "a=update&type="+$type+"&id="+$id+"&rnd="+new Date().toString(),
	        dataType : "text",
			success: function(data) {
			}
		});
	}
	else {
		$("#"+$imgid+$id).attr("src",$img_on);
		$("#attr_"+$imgid+$id).val($imgid+"close");
		$.ajax({
			type: "POST",
			url: $url,
			data: "a=update&type="+$type+"&id="+$id+"&rnd="+new Date().toString(),
	        dataType : "text",
			success: function(data) {
			}
		});
	}
}

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


/**
 * 根据角色ID获取帐户
 * @param:: string $departArgId 部门元素ID
 * @param:: string $selname 系统用户元素ID
 * @param:: tinyint $child 是否包含子部门
 * @param:: tinyint $text 内容提示
*/
function oemarrySelectRoleAdmin(roleArgId, selname, child, text) {
	if (typeof(child) == "undefined") {child = 1;}
	if (typeof(text) == "undefined") {text = '=请选择=';}
	$roleid = $("#"+roleArgId).val();
	if ($roleid.length == 0) {
		ToastShow("请选择角色");
		return false;
	}
	$option_html = "";
	//DATA
	$.getJSON(_OEMCPFILE +"&c=index&a=getroleadmin&roleid="+$roleid+"&child="+child+"&r="+get_rndnum(8),
		function($data){
			$.each($data, function($entryIndex, $obj){
				$option_html = $option_html + "<option value='"+$obj.adminid+"'>"+$obj.lastname+"("+$obj.loginname+")</option>";
			});
			//HTML
			$result = "";
			$result = "<select name='"+selname+"' id='"+selname+"'>";
			$result += "<option value='0'>"+text+"</option>";
			if ($option_html.length > 0) {
				$result += $option_html;
			}
			$result += "</select>";
			$("#json_"+selname).html($result);
		}
	);
}


/**
 * 获取我的下属 select
 * @param:: string $selname SELECT名称
 * @param:: int $inid 当前用户ID
 * @param:: tinyint $text 内容提示
*/
function oemarrySelectSubAdmin(selname, inid, text) {
	if (typeof(text) == "undefined") {text = "=全部=";}
	$option_html = "";
	//DATA
	$.getJSON(_OEMCPFILE+"&c=admin&a=jsonsubadmin&r="+get_rndnum(8),
		function($data){
			$.each($data, function($entryIndex, $obj){
				$option_html += "<option value='"+$obj.adminid+"'";
				if ($obj.adminid == inid) {
					$option_html += " selected";
				}
				$option_html += ">"+$obj.lastname+"-"+$obj.rolename+"</option>";
			});
			//HTML
			$result = "";
			$result = "<select name='"+selname+"' id='"+selname+"'>";
			$result += "<option value='0'>"+text+"</option>";
			if ($option_html.length > 0) {
				$result += $option_html;
			}
			$result += "</select>";
			$("#json_"+selname).html($result);
		}
	);
}

//添加价格体系
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);
}


/*------------------------------ 华丽分割线 Begin ---------------------------------*/
//选择模板
function oemarry_varpop_tpl(inputid, width, height) {
	if (typeof(width) == 'undefined') {width = 650;}
	if (typeof(height) == 'undefined') {height = 300;}
	$url = _OEMCPFILE+"&c=templet&a=select&inputid="+inputid+"&r="+get_rndnum(6);
	$.dialog.open($url, {title: '选择模板', width: width, height: height, drag: true});
}

//分配会员
function oemarry_varpop_allotuser(ids, width, height) {
	if (typeof(width) == 'undefined') {width = 500;}
	if (typeof(height) == 'undefined') {height = 180;}
	$url = _OEMCPFILE+"&c=allotuser&a=allot&ids="+ids+"&r="+get_rndnum(6);
	$.dialog.open($url, {title: '分配会员', width: width, height: height, drag: true});
}


//分配店铺
function oemarry_varpop_allotshop(ids, width, height) {
	if (typeof(width) == 'undefined') {width = 500;}
	if (typeof(height) == 'undefined') {height = 180;}
	$url = _OEMCPFILE+"&c=allotshop&a=allot&ids="+ids+"&r="+get_rndnum(6);
	$.dialog.open($url, {title: '分配店铺', width: width, height: height, drag: true});
}


//添加现金
function oemarry_varpop_money(uid, width, height) {
	if (typeof(width) == 'undefined') {width = 600;}
	if (typeof(height) == 'undefined') {height = 300;}
	$url = _OEMCPFILE+"&c=money&a=add&userid="+uid+"&r="+get_rndnum(6);
	$.dialog.open($url, {title: '操作余额', width: width, height: height, drag: true});
}


//选择会员
function oemarry_varpop_user(inputid, width, height) {
	if (typeof(width) == 'undefined') {width = 850;}
	if (typeof(height) == 'undefined') {height = 300;}
	$url = _OEMCPFILE+"&c=user&a=selectshop&inputid="+inputid+"&r="+get_rndnum(6);
	$.dialog.open($url, {title: '选择会员', width: width, height: height, drag: true});
}

//选择店铺
function oemarry_varpop_shop(inputid, width, height) {
	if (typeof(width) == 'undefined') {width = 850;}
	if (typeof(height) == 'undefined') {height = 300;}
	$url = _OEMCPFILE+"&c=shop&a=select&inputid="+inputid+"&r="+get_rndnum(6);
	$.dialog.open($url, {title: '选择店铺', width: width, height: height, drag: true});
}

//升级、操作店铺VIP等级
function oemarry_varpop_shopvip(shopid, width, height) {
	if (typeof(width) == 'undefined') {width = 750;}
	if (typeof(height) == 'undefined') {height = 500;}
	$url = _OEMCPFILE+"&c=shopid&c=shopvip&a=vip&shopid="+shopid+"&r="+get_rndnum(6);
	$.dialog.open($url, {title: "操作、升级店铺VIP等级", width: width, height: height, drag: true});
}

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



