//下载adobe地址
//http://ardownload.adobe.com/pub/adobe/reader/win/9.x/9.3/chs/AdbeRdr930_zh_CN.exe
window.onload = function () {
	var checkAdobeReader = Check_AdobeReader();
	if(checkAdobeReader != 'Acrobat Version: 6.0'){
		alert("对不起,您还没有安装PDF阅读器软件呢,为了方便预览PDF文档,请选择安装!");
		location = path + "/static/js/font/AdbeRdr60_chs_full.exe";
	}
}
//树操作
/*var data = [
	{ id:1,level:1, "text":"Root node","newName":"2" ,
		"state" : { "opened" : true },"children" : [
			{id:2,"newName":"2", "text" : "Child node 1" ,level:2,"children" : [
					{id:3,"newName":"2", level:3,"text" : "Child node 3" },
					{ id:4,"newName":"2",level:3,"text" : "Child node 4" }
				]},
			{id:5,"newName":"2", "text" : "Child node 2", level:2,"children" : [
					{id:6,"newName":"2", level:3,"text" : "Child node 5" },
					{ id:7,"newName":"2",level:3,"text" : "Child node 6" }
				] }
		]}
];*/
var tree = $('#data').data('jstree', false).empty().jstree({
	'core': {
		"data" : function (obj, callback){
			$.ajax({
				type : "post",
				url : path+'/font/getFontBloodRecordTree?patientId='+$("#patientId").val(),
				dataType : "json",
				success : function(data) {
					var assortList = data[0].children;
					var assortIds = '';
					var sources = '';
					var scanPages = '';
					if(assortList != '') {
						for (var i = 0; i < assortList.length; i++) {
							var picList = assortList[i].children;
							for (var j = 0; j < picList.length; j++) {
								assortIds += picList[j].newName + ",";
								sources += picList[j].source + ",";
							}
						}
						if(assortIds != ''){
							assortIds = assortIds.substring(0,assortIds.length-1);
						}
						if(sources != ''){
							sources = sources.substring(0,sources.length-1);
						}
						$("#assortIds").val(assortIds);
						$("#sources").val(sources);
						onloadPdf();
					}else{
						$("#jstree").html("暂无数据！");
					}
					if(data){
						callback.call(this, data);
					}
				}
			});
		},
		"check_callback": true
	},

	plugins: ["checkbox"],
	"checkbox": {
		"keep_selected_style": false,//是否默认选中
		"three_state": true,//父子级别级联选择
		"tie_selection": false
	},
});
function Check_AdobeReader() {
	var displayString;
	var acrobat = new Object();
	acrobat.installed = false;
	acrobat.version = "0.0";
	if (navigator.plugins && navigator.plugins.length) {
		for (x = 0; x < navigator.plugins.length; x++) {
			if (navigator.plugins[x].description.indexOf("Adobe Acrobat") != -1) {
				acrobat.version = parseFloat(navigator.plugins[x].description.split("Version ")[1]);
				if (acrobat.version.toString().length == 1) acrobat.version += ".0";
				acrobat.installed = true;
				displayString = "Acrobat Version: " + acrobat.version;
				break;
			} else if (navigator.plugins[x].description.indexOf("Adobe PDF Plug-In") != -1) {
				acrobat.installed = true;
				acrobat.version = "8+";
				displayString = "Acrobat Version 8 or Above";
			}
		}
	} else if (window.ActiveXObject) {
		for (x = 2; x < 10; x++) {
			try {
				oAcro = eval("new ActiveXObject('PDF.PdfCtrl." + x + "');");
				if (oAcro) {
					acrobat.installed = true;
					acrobat.version = x + ".0";
					displayString = "Acrobat Version: " + acrobat.version;
				}
			}
			catch (e) { }
		}
		try {
			oAcro4 = new ActiveXObject("PDF.PdfCtrl.1");
			if (oAcro4) {
				acrobat.installed = true;
				acrobat.version = '4.0';
				displayString = 'Acrobat Version: ' + acrobat.version;
			}
		}
		catch (e) { }
		try {
			oAcro7 = new ActiveXObject('AcroPDF.PDF.1');
			if (oAcro7) {
				acrobat.installed = true;
				acrobat.version = '7.0';
				displayString = 'Acrobat Version: ' + acrobat.version;
			}
		}
		catch (e) { }
	}
	return displayString;
}
//checkBox选中、取消选中逻辑
function selectAssortIds(){
	var instance = $('#data').jstree(true);//获取jstree对象
	var nodes = instance.get_checked(true);
	var assortIds = '';
	var sources = '';
	for(var i = 0;i < nodes.length;i++){
		if(nodes[i].children == ''){
			assortIds += nodes[i].original.newName + ",";
			sources += nodes[i].original.source + ",";
		}
	}
	if(assortIds != ''){
		assortIds = assortIds.substring(0,assortIds.length - 1);
	}
	if(sources != ''){
		sources = sources.substring(0,sources.length - 1);
	}
	$("#assortIds").val(assortIds);
	$("#sources").val(sources);
}
//checkbox 选中事件
$('#data').on("check_node.jstree", function (node, data, event) {
	selectAssortIds();
});
tree.on('uncheck_node.jstree', function(node, data, event) {
	selectAssortIds();
});
//加载pdf按钮功能
function onloadPdf(scanPages){
	$("#loading1").css("background","url("+path+"/static/img/load.gif) no-repeat 10px 50%");
	var patientId = $("#patientId").val();
	var assortIds = $("#assortIds").val();
	var sources = $("#sources").val();
	var flag = $("#flag").val();
	var rootPaths = parent.$("#rootPaths").val();
	var url = path+"/font/showRecordContentBloodFont?flag="+flag+"&patientId="+patientId+"&scanPages="+assortIds+"&sources="+sources;
	$("#iframe1").prop("src",url);
}

//记录打印记录
function printInfoBlood(){
	var patientId = "'"+$("#patientId").val()+"'";
	var assortIds = $("#assortIds").val();
	var typeId = $("#typeId").val();
	if(typeId == undefined){
		typeId = '';
	}
	var flag = $("#flag").val();
	post(path+'/template/printInfoBlood',{"patientIds":patientId,"assortIds":assortIds,"typeId":typeId,"flag":flag});
}

