"use strict";
var lvid = getParameter("lvid");
var toLvid = getParameter("tolvid");
var sign = getParameter("sign");
var groupid = getParameter("groupid");
var selfHandimg = (lvid == toLvid);
var isfriend = false;
var userOut;
var birthyear, birthmonth, birthday;
var province, city, district, userName, remark;;
var s_countries, s_city, s_county;
var birsDateTime;
var date = new Date();
var Thisyear = date.getFullYear();
var closeWindowHook = false; //关闭窗口hook
var initStatus = false;
var isclose = false;
var entGroup = false;
var signatureT;
var teamName;
$(document).on('click', '#supergroupList dd', function() {
	var upGroupid = $(this).find('span').attr('groupid');
	$('#supergroupList').hide();
	$('#supergroup').text($(this).find('span').text());
	if($('#supergroup').text() == teamName) {
		return;
	}
	if(upGroupid == -2) {
		var params = '{command:9006,params:{"cat":3,"title":"用户提示", "content":"你确定将(' + toLvid + ')移至黑名单吗？"}}';
		var resultS = $.parseJSON(sendCommand(params));
		if(resultS['errorid'] == 1) {
			var params = '{command:1012,params:{"userid":' + toLvid + ',"teamid": ' + upGroupid + ' }}';
			var result = $.parseJSON(sendCommand(params));
		}
		if(resultS['errorid'] == 2) {
			loadUserInfo();
			return;
		}
		return;
	}
	var params = '{command:1012,params:{"userid":' + toLvid + ',"teamid": ' + upGroupid + ' }}';
	var result = $.parseJSON(sendCommand(params));
	if(result['errorid'] != 0) {
		return;
	}
	loadUserInfo();
}) 

$(document).ready(function(){
	if(groupid){
		loadGroupInfo();
		$('.datPriInfo').hide();
		$('.gourpCont').show();
		$('.datPriMain').css({'width':'328px','height':'348px'});
	}else{
		loadUserInfo();
		$('.gourpCont').hide();
		$('.datPriMain').css({'width':'240px','height':'440px'});
	}
})
function loadGroupInfo() {
	var params = {
		'lvid': lvid,
		'groupid': groupid,
		'sign': sign
	};
	$.getJSON(requestUrlMap['cloudGroupInfo']['url'], params, searchGroupCallBack);
}
function loadUserInfo() {
	var params = {
		'lvid': lvid,
		'tolvid': toLvid == lvid ? '' : toLvid,
		'sign': sign
	};
	$.getJSON(requestUrlMap['userInfo']['url'], params, searchUserCallBack);
}
//提交的回调函数

function searchUserCallBack(data) {
	userOut = data.userinfo;
	if(userOut.lvid != lvid) {
		if(userOut['companyid'] == 0) {
			$('.remark,.groupL').show();
		}
	}
	var isFriend = data.isfriend;
	var imghead = userOut.headurl ? transferHeadSize(userOut.headurl,80) : userOut.sex == 0 ? '../images/classic/HEAD-gg-80.png' : '../images/classic/HEAD-mm-80.png'
	$('.headimg img').attr('src',imghead);
	$(".loading-popup").hide();
	$("#lvid").html(userOut.lvid);
	$("#nickName").html(userOut.nickname).attr('title',userOut.nickname);
	$("#usersex").html(userOut.sex == 0 ? "男" : "女");
	userOut.sex == 0 ? $('.sex dd').eq(0).click() : $('.sex dd').eq(1).click();
	$("#userage").html(userOut.age + '岁'); //年龄 
	$("#usercompany").text(userOut.company).css('cursor', 'default').attr('title', userOut.company);
	userName = userOut.nickname.replaceAll('<', '&lt').replaceAll('>', '&gt').replaceAll(' ', '&nbsp;');
	$("#usernick").html(userName + "(" + userOut.lvid + ")").css('cursor', 'default').attr('title', userOut.nickname);
	province = userOut['province'] ? userOut['province'] : "";
	city = userOut['city'] ? userOut['city'] : "";
	district = userOut['district'] ? userOut['district'] : "";
	$("#userprovince").html(province + " " + city + " " + district);
	$("#userprofession").text(userOut.profession == "" ? '-' : userOut.profession);
	$("#company").html(!userOut.company ? '-' : userOut.company);
	$("#useremail").html(!userOut.email ? '-' : userOut.email);
	$("#userphone").html(!userOut.phone ? '-' : userOut.phone);
	birthyear = userOut.birthyear;
	birthmonth = userOut.birthmonth;
	birthday = userOut.birthday;
	$("#year").html(birthyear + '年');
	$("#month").html(birthmonth + '月');
	$("#day").html(birthday + '日');
	$("#pBirthday").html(birthyear + "年" + birthmonth + "月" + birthday + "日");
	$("#companyedit").val(userOut.company);
	$("#signature").val(userOut.signature);
	signatureT = userOut.signature;
	$("#nickedit").val(userOut.nickname);
	$("#supergroupList").html('');
	$('#remarkN').text(userOut['remark'].trim() == '' ? '-' : userOut['remark']);
	if($('#remarkN').text() == '-') {
		$('.markNamebtn').text('添加备注');
	} else {
		$('.markNamebtn').text('修改备注');
	}
	var html = "";
	if(isFriend) {
		var team = data.userinfo.team;
		if(team.length > 4) {
			$("#supergroupList").css('overflow-y', 'scroll');
		}
		for(var i = 0; i < team.length; i++) {
			if(team[i].id == userOut.FteamId) {
				teamName = team[i].teamName;
			}
			$("#supergroupList").append("<dd> <span class=\"currency_type\" groupid=" + team[i].id + ">" + team[i].teamName + "</span> </dd>");
		}
		$('.remark,.groupL').show();
		$('#supergroup').text(teamName).attr('groupid', userOut.FteamId);
	}else{
		$('.groupL,.remark').hide();
	}
}

function markName(){
	var params = "{command:1010, params: { lvid:" + lvid + ", tolvid:" + toLvid + "}}";
	var result = $.parseJSON(sendCommand(params));
	if(result['errorid'] != 0) {
		return;
	}
	$('#remarkN').text(result['newname'].trim() == '' ? '-' : result['newname']);
	if($('#remarkN').text() == '-') {
		$('.markNamebtn').text('添加备注');
	} else {
		$('.markNamebtn').text('修改备注');
	}
}
//私聊
function privateChat() {
	var userStr = $.toJSON(userOut);
	var params = "{command:1011, params:" + userStr + "}";
	var resultS = sendCommand(params);
} 
$(function() {
	//打开或关闭下拉菜单
	$(".open_select").each(function() {
		$(this).click(function(event) {
			event.stopPropagation(); //阻止事件冒泡
			event.cancelBubble = false; //针对ie阻止冒泡
			$(this).parent(".open_select_box").next(".select_list").toggle();
		});
	});
	//离开选择区域后展开的下拉列表关闭
	$(document).click(function(event) {
		var eo = $(event.target);
		if($(".open_select").is(":visible") && eo.attr("class") != "select_list" && !eo.parent(".select_list").length) {
			$(".select_list").hide();
			$('#supergroup').css('border', '1px solid #fff');
			$('.year-open').removeClass('yearact');
			$('.day-open').removeClass('dayact');
			$('.month-open').removeClass('monthact');
			$('.countryBox').removeClass('countriesact');
			$('.provinceBox').removeClass('provicesact');
			$('.cityBox,.UpdataList span').removeClass('citiesact');
			$('.yearBox .open_select_box,.monthBox .open_select_box,.dayBox .open_select_box,.UpdataList .open_select').removeClass('actSelect');
			$('.countryBox .open_select_box,.provinceBox .open_select_box,.cityBox .open_select_box,.UpdataList .open_select').removeClass('actSelect');
			$('.ageList .open_select_box').removeClass('actSelect');
			$('.sexList .open_select_box').removeClass('actSelect');
			$('.ageList .open_select').removeClass('yearact');
			$('.sexList .open_select').removeClass('yearact');
			$('.inputBox').css('border', '1px solid #E8E8E8');

		}
	});
	//选择市
	$(".cities-open").click(function() {
		if($(this).attr('id') == 'supergroup') {
			$('#supergroup').toggleClass('actSelect').parent('.select_box').siblings().find('.open_select_box').removeClass('actSelect');
			$('#supergroup').css({
				'border': '1px solid #ccc',
				'border-radius': '2px'
			});
			return;
		}
		$(this).addClass('citiesact')
		$(".countries").hide();
		$(".provices").hide();
		$('.cityBox .open_select_box').toggleClass('actSelect').parent('.select_box').siblings().find('.open_select_box').removeClass('actSelect')
		$(".countries-open").removeClass('countriesact');
		$(".provices-open").removeClass('provicesact');
		$('.birBox a.inputBox').css('border-color', '#E8E8E8');
	});  
});

var setGroupinfo = [];
function searchGroupCallBack(data) {
	$("#editBut,.loading-popup").hide();
	if(data.errorid != 0) {
		$("#groupmess").html("非法请求");
		return;
	}
	var groupOut = data.groupinfo;
	var creatorInfo = data['creatorinfo'];
	if(data.admininfos.length == 0) {
		$('#leadImgDiv i').hide();
	}
	if(groupOut.grouptype == 3) {
		entGroup = true;
		$('.titinvitation').hide();
	}
	var url;
	var repName = groupOut.cloudgroupname.replaceAll('&lt', '<').replaceAll('&gt', '>');
	$('#groupUrl').attr('src',transferHeadSize(groupOut.iconurl,80));
	if(!groupOut.iconurl){
		$('#groupUrl').attr('src','../images/classic/HEAD-Group-80.png');
	}
	$('#groupid').text(data.groupid)
	$("#creatday").text(groupOut.createtimestr);
	$("#peo").text(groupOut.num + '/500');
	$("#groupCall").text(repName);
	$("#gongGaoSpan").text(groupOut.noticedata ? groupOut.noticedata : '组长很懒，什么都没有留下');
	$("#gongGaoText").text(groupOut.noticedata);
	var creatorHeadUrl;
	if(!creatorInfo['headurl']) {
		creatorHeadUrl = creatorInfo['sex'] == 0 ? '../images/classic/HEAD-gg-32.png' : '../images/classic/HEAD-mm-32.png';
	} else {
		creatorHeadUrl = transferHeadSize(creatorInfo['headurl'], 32);
	}
	$("#creatorHeadImg").attr({
		"src": transferHeadSize(creatorHeadUrl, 32),
		'creatId': creatorInfo.lvid,
		'title': creatorInfo.nickname
	});
	var defaultHeadUrl, params, resultS, headUrl, adminUrl = '';
	for(var i = 0; i < data.admininfos.length; i++) {
		setGroupinfo.push(data.admininfos[i].lvid);
		defaultHeadUrl = data.admininfos[i].sex == 0 ? '../images/classic/HEAD-gg-32.png' : '../images/classic/HEAD-mm-32.png';
		params = "{command:1016,params:{lvid:" + data.admininfos[i].lvid + ", size:32}}";
		resultS = $.parseJSON(sendCommand(params));
		if(resultS.errorid == 0) {
			headUrl = resultS['url'] || defaultHeadUrl;
		}
		adminUrl += '<img src="' + headUrl + '" title=' + data.admininfos[i].nickname + ' creatId=' + data.admininfos[i].lvid + '>';
		$('.Administrators').html(adminUrl);
	}
	$("#leadImgDiv img").click(function() {
		var toId = $(this).attr('creatId');
		sendCommand("{command:1003,params:{userid:" + creatorid + ",lvid:" + toId + "}}");
	}); 
}
function privateChatGrounp() {
	var params = "{command:3009,params:{groupid:" + groupid + "}}"
	var resultS = sendCommand(params);
}