function getOwner(gid,callback){
	var url = "./?app=api&act=getOwnerList&gid="+gid;
	$.get(url,function(txt){
		var html = '<option value="">选择owner</option>';
		var j = JSON.parse(txt);
		
		j.sort(function(a,b){
			return (b.split(',')).length - (a.split(',')).length;
		});

		for(var i=0;i<j.length;i++){
			html += '<option value="'+ j[i] +'">'+ j[i] +'</option>';
		}
		//$('#ownlistselect').html(html);
		callback && callback(html);
	});
}
var loading = {
	show : function(){
		loading.hide();
		$('body').append('<div class="message-loading-overlay"  style="z-index:999999999 !important"><i class="icon-spin icon-spinner orange2 bigger-160"></i></div>');
	},
	hide : function(){
		$('.message-loading-overlay').remove();
	}
};

function checkChange(v){
	$('.checkid').each(function(){
		this.checked = v;
	});
}

function getCheckedID(){
	var ids = [];
	$('.checkid').each(function(){
		if(this.checked){
			ids.push(this.value);
		}
	});
	return ids;
}

//自定义命令
function selfOrder(callback){
	var form = $("<form class=''></form>");
	
	var uorder = {
		'查僵尸进程':'ps -A -o stat,ppid,pid,cmd | grep -e ^[Zz]',
		'查看config.py':'cat ./config.py',
		'重启mysql':'service mysqld restart',
		'重启mongo':'pkill -9 mongod;sleep 2;service mongodb start',
		'mongo日志转新':'echo > /var/log/mongod.log;/home/ciniao/mongodb-linux-x86_64-3.2.3/bin/mongo -u root -p iamciniao --authenticationDatabase=admin < /home/ciniao/mongoRotateLog.js',
		'列泄露句柄':'lsof | grep deleted',
		'清合区临时目录':'rm -rf /data/hequtmp/*',
		'获取内网ip':'wget -q -O /home/ciniao/localip.py  http://gametools.legu.cc/game/localip.py && python /home/ciniao/localip.py',
	};
	var op = "";
	for(var k in uorder){
		op += "<option value='"+ uorder[k] +"'>"+ k +"</option>";
	}

	form.append("<div class='space-1'></div><label for='form-field-mask-1'>输入命令</label>或选择 <select id='uorderselect' onchange='$(\"#form-field-8\").val(this.value)'><option value=''>常用</option>"+ op +"</select><textarea class='form-control' id='form-field-8' placeholder='' style='margin-top:10px'></textarea> ");
	
	var div = bootbox.dialog({
		message: form,
		buttons: {
			"onebyone" : {
				"label" : "顺序执行",
				"className" : "btn-sm btn-danger",
				"callback": function() {
					var order = $('#form-field-8').val();
					if(order.length==0)return;
					localStorage['selfordercont'] = order;
					if(callback){
						callback(order);
					}else{
						mutiCtrl('selforder',true,{
							order:order
						});
					}
				}
			},
			"onetime" : {
				"label" : "同时执行",
				"className" : "btn-sm btn-danger",
				"callback": function() {
					var order = $('#form-field-8').val();
					if(order.length==0)return;
					localStorage['selfordercont'] = order;
					if(callback){
						callback(order);
					}else{
						mutiCtrl('selforder1',true,{
							order:order
						});
					}
				}
			}
		}
	});

	if(localStorage['selfordercont']){
		$('#form-field-8').val(localStorage['selfordercont']);
	}



	form.on('submit', function(){
		div.modal("hide");
		return false;
	});
}
function mutiCtrl(act,ifconfirm,formData){
	var ids = getCheckedID();
	if(ids.length==0){
		alert('请先选择要操作的区服');
		return;
	}

	if(act=='bindserver' && ids.length!=2){
		alert('只能勾选2个区进行合区操作');
		return;
	}
	
	if(act=='bindserver'){
		if(prompt("确定要合并选中的区吗？确定请输入 合区 2字")!='合区'){
			return
		}
	}

	if(act=='resetopentime'){
		if(prompt("确定要重置开区时间吗？确定请输入 重置 2字")!='重置'){
			return
		}
	}

	

	if(ifconfirm==null || ifconfirm==false || (ifconfirm && confirm("确定要对选中的【"+ ids.length +"个区】进行【"+ act +"】操作?") )){
		
		var form = $('<form action="./?app=serverctrl&act='+ act +'" target="_blank" method="post"></form>')
		form.append("<input name='id' id='title' type='text' value='"+ ids.join(',') +"' />");
		if(formData){
			for(var k in formData){
				form.append("<textarea name='"+ k +"' id='"+ k +"'>"+ formData[k] +"</textarea>");
			}
		}
		form.submit();
	}
}
$(function(){
	$('[data-rel=tooltip]').tooltip({container:'body'});
});