var info={}, oAccount;

$(document).ready( onDocumentReady );

function onDocumentReady() {
	Origami.fastclick(document.body);
	$("#file_picture").change(picturePrepare);
	$("#img_headimg").click(pictureChoose);
	pageInit(showInfo);	
}

function onVersionResponse(res) {
	if (res.require_upgrade) {
		showConfirm("请下载更新APP至版本"+res.server_version+"<br>(当前版本"+res.client_version+")", "location.href='setting.html';");
	}
}

function showEmployee() {
	menuSwitch("employee");
	loadEmployee();
}

function loadEmployee() {
	jsonRequest("login", "load_employee_list", {}, onEmployeeResponse);
}

function onEmployeeResponse(res) {
	var html="";
	for (var i=0; i<res.employee_list.length; i++) {
		var t=res.employee_list[i];
		var action=t.allow_simulation?"confirmEmployeeSimulate('"+res.employee_list[i].uid+"')":"alertMessage('没有权限演示该账户')";
		html+=div("div_employee_"+(t.current_simulation?"on":"off"), 
			img("img_employee_headimg", t.headimg)+
			div("div_employee_name", t.name)+
			div("div_employee_code", t.code)+
			div("div_employee_title", t.title)+
			div("div_employee_level", t.level), action);
	}
	$("#div_employee_list").html(html);
}

function confirmEmployeeSimulate(uid) {
	var req={employee_uid:uid};
	jsonRequest("login", "do_employee_simulate", req, onEmployeeSimulateResponse);
}

function onEmployeeSimulateResponse(res) {
	alertMessage("已切换演示账户");
	sessionStorage.setItem("account", JSON.stringify(res.account));
	pageBack();
}	

function goLogin() {
	location.href="login.html";
}

function goAccount() {
	location.href="login.html?account=1";
}

function goDownload() {
	var iosURL='itms-services://?action=download-manifest&url=https%3A%2F%2Fwww.pgyer.com%2Fapiv1%2Fapp%2Fplist%3FaId%3D27d1c2fed38dbd7a2775d701901c348b%26_api_key%3D6eab3e0d9d8522c6efa56e1b8d28e342';
	var androidURL='http://www.pgyer.com/apiv1/app/install?aId=509769215fd4762fffe7778c67e8d21d&_api_key=6eab3e0d9d8522c6efa56e1b8d28e342';
	var downloadURL="";
	if (isAgent("android")) {
		downloadURL=androidURL;
	} else if (isAgent("iphone os") || isAgent("ipad")) {
		downloadURL=iosURL;
	} else {
		alertMessage("请在移动设备上下载APP");
		return;
	}

	if (window.device) {
		loadMessage("正在连接应用商店");
		window.open(downloadURL, '_system', 'location=yes');
	} else if (isAgent("micromessenger")) {
		weixinGuide();
	} else {
		if (isAgent("iphone os") || isAgent("ipad")) {
			$("#page_ios").fadeIn();
		}
		location.href=downloadURL;
	}	
}

function clearStorage() {
	localStorage.clear();
	sessionStorage.clear();
	alertMessage("手机存储参数已清除");
}

function toggleOption(name) {
	var s=localStorage.getItem(name);
	if (s) {
		localStorage.removeItem(name);
	} else {
		localStorage.setItem(name, "1");
	}
}

function showInfo() {
	menuSwitch("info");
	loadInfo();
}

function refreshInfo() {
	var html="";
	for (var i in info) {
		html+=div("div_property_title", info[i].title);
		for (var j in info[i].property) {
			html+=div("div_property_row",
				div("div_property_label", info[i].property[j].label)+div("div_property_content", info[i].property[j].content));
		}
	}
	html+=div("div_property_title", "系统选项");
	html+=div("div_property_row",
			div("div_property_label", "手机存储") + div("div_property_content", "清除"), "clearStorage(); loadInfo();");
	var visual=localStorage.getItem("visual_aid")=="1"?"已启用":"已关闭";
	html+=div("div_property_row",
			div("div_property_label", "点击效果") + div("div_property_content", visual), "toggleOption('visual_aid'); loadInfo(); ");
	var local=localStorage.getItem("local_server")=="1"?"本地调试服务器":"远程生产服务器";
	html+=div("div_property_row",
			div("div_property_label", "服务器") + div("div_property_content", local), "toggleOption('local_server'); loadInfo(); ");
	$("#div_info").html(html);
}

function addInfo(i, s, v) {
	if (v) {
		info[i].property.push({label:s, content:v});
	} else {
		info[i]={};
		info[i].title=s;
		info[i].property=[];
	}
}
			
function loadInfo() {
	addInfo("account", "账户信息");
	addInfo("employee", "员工信息");
	addInfo("app", "应用信息");
	addInfo("app", "发布日期", clientDate);
	addInfo("app", "当前版本", clientVersion);
	
	addInfo("device", "设备信息");
	var screen=$(window).width()+" x "+$(window).height();
	addInfo("device", "屏幕", screen);
	if (window.device) {
		addInfo("device", "系统", device.platform);
		addInfo("device", "版本", device.version);
		addInfo("device", "型号", device.model);
		addInfo("device", "序号", device.uuid);
	} else {
		addInfo("device", "系统", navigator.platform);
	}
	var agent=navigator.userAgent.replace(/\)/g, ")<br>");
	addInfo("device", "浏览器", agent);
	
	addInfo("network", "网络信息");
	var status=navigator.onLine?"已连接":"未连接";
	addInfo("network", "状态", status);
	if (navigator.connection) {
		addInfo("network", "类型", navigator.connection.type);
	}
	addInfo("network", "界面", location.href);
	addInfo("network", "服务器", jsonURL);
	var access="网页版";
	if (isAgent("micromessenger")) {
		access="微信版";
	} else if (window.device) {
		access="APP版";
	}
	addInfo("network", "访问方式", access);

	addInfo("location", "地理位置信息");
	addInfo("storage", "手机存储信息");
	for(var i=0; i<localStorage.length; i++){
		if (localStorage.key(i).substr(0,4)!="BMap") {
			addInfo("storage", "L." + localStorage.key(i), localStorage.getItem(localStorage.key(i)));
		}
	}
	for(var i=0; i<sessionStorage.length; i++){
		addInfo("storage", "S." + sessionStorage.key(i), sessionStorage.getItem(sessionStorage.key(i)));
	}
	refreshInfo();
	loadPosition(showPosition);

	jsonRequest("login", "load_info", {}, onInfoResponse);
}

function showPosition () {
	var pos=getPosition();
	addInfo("location", "经度", pos.longitude || "未知");
	addInfo("location", "纬度", pos.latitude || "未知");
	addInfo("location", "高度", pos.altitude || "未知");
	addInfo("location", "精度", pos.accuracy || "未知");
	refreshInfo();
}

function onInfoResponse(res) {
	addInfo("account", "手机号", res.phone);
	addInfo("account", "注册时间", res.register_time);
	addInfo("account", "登录次数", res.login_freq);
	addInfo("account", "最近登录", res.login_time);
	addInfo("account", "访问次数", res.visit_freq);
	addInfo("account", "最近访问", res.visit_time);
	addInfo("account", "来源地址", res.visit_addr);

	addInfo("employee", "员工代码", res.code);
	addInfo("employee", "员工姓名", res.name);
	addInfo("employee", "员工性别", res.gender==1?"男":"女");
	addInfo("employee", "员工职位", res.title);
	addInfo("employee", "职位级别", res.level);

	addInfo("app", "最新版本", res.server_version);
	refreshInfo();
}

function showAccountInfo() {
	menuSwitch("account");
	loadAccountInfo();
}

function loadAccountInfo() {
	jsonRequest("login", "load_account_info", {}, onAccountInfoResponse);
}

function onAccountInfoResponse(res) {
	oAccount=res;
	$("#img_headimg").attr("src", imgURL+res.headimg);
	$("#div_account_phone").html(res.phone);
	$("#input_account_name").val(res.name);
	$("#input_account_code").val(res.code);
	$("#input_account_title").val(res.title);
	setGender(res.gender);
	setLevel(res.level);
}

function confirmAccountUpdate() {
	oAccount.name=$("#input_account_name").val();	
	oAccount.code=$("#input_account_code").val();
	oAccount.title=$("#input_account_title").val();
	jsonRequest("login", "do_account_update", oAccount, onAccountUpdateResponse);
}	

function onAccountUpdateResponse() {
	alertMessage("成功更新用户信息");
	pageBack();
}

function setGender(i) {
	toggleSwitch("gender", i);
	oAccount.gender=i;
}

function setLevel(i) {
	toggleSwitch("level", i);
	oAccount.level=i;
}

function pictureReady() {
	oAccount.headimg=aPicture[aPicture.length-1].pre_name;
	$("#img_headimg").attr("src", imgURL+oAccount.headimg);
}