var Epro_I_Page = "";
var MC_1_Page = "";
var Ep2_Page = "";
var Recheck_page = "";
var Epj_Page = "";
var Epe_page = "";
var EPRO_I_PAGE_ID = 1;
var MC_1_PAGE_ID = 2;
var EPRO2_PAGE_ID = 3;
var RECHECK_PAGE_ID = 4;
var EPROJ_PAGE_ID = 5;
var EPE_PAGE_ID = 6;
var CHECKER_PAGE_ID = 7;
var ViewPage_ID = 3;
var ViewPage_InitFlag = 0;
var commport_cfg = {
	comm_port:"",
	comm_baud:"",
	comm_parity:-1,
	comm_stopbit:-1,
};
var qchannel = null;
var qwebchannel = null;
var plugin_func;
var eshot_qthandle = null;
function Start_Comm()
{
	switch(ViewPage_ID){
		case MC_1_PAGE_ID:MC_1_Start_Comm();break;
		case EPRO2_PAGE_ID:Ep2_Start_Comm();break;
		case EPRO_I_PAGE_ID:Ep1_Start_Comm();break;
		case RECHECK_PAGE_ID:ReCheck_Start_Comm();break;
		case EPROJ_PAGE_ID:Epj_Start_Comm();break;
		case EPE_PAGE_ID:Epe_Start_Comm(); break;
		case CHECKER_PAGE_ID:Checker_Start_Comm(); break;
		
	}
}

function Main_Open_CommPort()
{
	commport_cfg.comm_port =$("#main_serial_comport option:selected").text();
	commport_cfg.comm_baud =parseInt($("#main_serial_baud option:selected").text(),10);
	commport_cfg.comm_parity =$("#main_serial_parity option:selected").val();
	commport_cfg.comm_stopbit =$("#main_serial_stopbit option:selected").val();
	switch(ViewPage_ID){
		case MC_1_PAGE_ID:MC_1_Open_CommPort(commport_cfg);break;
		case EPRO2_PAGE_ID: Ep2_Open_CommPort(commport_cfg);break;
		case EPRO_I_PAGE_ID:Ep1_Open_CommPort(commport_cfg);break;
		case RECHECK_PAGE_ID:ReCheck_Open_CommPort(commport_cfg);break;
		case EPROJ_PAGE_ID:Epj_Open_CommPort(commport_cfg);break;
		case EPE_PAGE_ID:Epe_Open_CommPort(commport_cfg);break;
		case CHECKER_PAGE_ID:Checker_Open_CommPort(commport_cfg);break;
	}
}
function Main_Close_CommPort_Select()
{
	if(Main_Select_Serial > 0)
	{
		layer.close(Main_Select_Serial);
	}
}
var Main_Select_Serial = 0;
function Main_ShowCommport(json_str)
{
	
	var index = 0;
	json_str = JSON.parse(json_str);
	console.log(json_str);
	var content = '<div style="padding: 5px; line-height: 25px; background-color: #393D49; color:#cdd4d3; ">';  
	content += '<div class="layui-form-item"><label class="layui-form-label" style = "width:60px;padding:5px 10px ;text-align:left;">端口</label> ';
	content += '<div class="layui-input-block"><select  id="main_serial_comport" lay-verify="required" style = "width:100px;height:25px">';
	for(index = 0; index < json_str.length;index++)
	{
		content +='<option value="'+index+'" class="layui-this"  selected>'+json_str[index]+'</option>';
	}
    content += ' <option value="'+index+'">关闭</option>';
	content += ' </select></div></div>'
	content += '<div class="layui-form-item"><label class="layui-form-label" style = "width:60px;padding:5px 10px; text-align:left;">波特率</label> ';
	content += '<div class="layui-input-block"><select id="main_serial_baud" lay-verify="required"  style = "width:100px;height:25px">';
	content +='<option value="0" class="layui-this"  selected>115200</option> <option value="1">57600</option> <option value="1">9600</option> <option value="2">38400</option>';
	content += ' </select></div></div>'
	content += '<div class="layui-form-item"><label class="layui-form-label" style = "width:60px;padding:5px 10px;text-align:left;">校验</label>';
	content += '<div class="layui-input-block"><select id="main_serial_parity" lay-verify="required" style = "width:100px;height:25px">';
	content +='<option value="0" class="layui-this"  selected>无</option> <option value="1">奇校验</option> <option value="2">偶校验</option>';
	content += ' </select></div></div>'
	content += '<div class="layui-form-item"><label class="layui-form-label" style = "width:60px;padding:5px 10px; text-align:left;">停止位</label>';
	content += '<div class="layui-input-block"><select id="main_serial_stopbit" lay-verify="required" style = "width:100px;height:25px">';
	content +='<option value="0" class="layui-this"  selected>1</option> <option value="1">1.5</option> <option value="2">2</option>';
	content += ' </select></div></div>'
	
	content += '<div class="layui-row"> <div class="layui-col-xs4"  >&nbsp </div> ';
	content += '<div class="layui-col-xs4"  >  <button type="button" class="layui-btn" onclick = "Main_Open_CommPort()">确定</button></div>';
	content += '<div class="layui-col-xs4"  > &nbsp</div> </div>';
	
	content += '</div>';
	
	if(Main_Select_Serial > 0)
	{
		layer.close(Main_Select_Serial);
	}
	Main_Select_Serial = layer.open({
	type: 1, 
	content:content,
	title: "端口设置",
	shade:0.6,
	moveType: 1,
	btnAlign: 'c',
	maxHeight:550,
	minWidth:350,
	});
	
}

function loadPage(href)
{
	var href_url = href;
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET", href, false);
	xmlhttp.send();
	return xmlhttp.responseText;
}
function RemoveJsPlugin(js_filename,filetype)
{
	var targetelement=(filetype=="js")? "script" :(filetype=="css")? "link" : "none"
	
	var targetattr=(filetype=="js")?"src" : (filetype=="css")? "href" :"none"
	
	var allsuspects=document.getElementsByTagName(targetelement)
	
	for (var i=allsuspects.length; i>=0;i--){
	
	if (allsuspects[i] &&allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(js_filename)!=-1)
	
	  allsuspects[i].parentNode.removeChild(allsuspects[i])
	
	}

}
function logJsPlugin(js_filename,plugin)
{
	plugin_func = null;
   console.log(js_filename);
    console.log(plugin);
	$.getScript(js_filename,function (response,status){
		if(status == "success")
		{
			
			plugin_func = plugin;
			ShowCommMes("插件更新成功",MesgType.MESG_TYPE_INFO);
			runJsPlugin();
		}else{
			ShowCommMes("插件加载失败",MesgType.MESG_TYPE_ERROR);
		}
		
	});

}

function runJsPlugin()
{
	if(plugin_func != null && typeof(plugin_func) != "undefined" )
	{
		eval(plugin_func+"();");
	}
}
var test_flag = 0;
function BtLoadPulgin()
{
	if(eshot_qthandle == null || typeof(eshot_qthandle) == "undefined")
	{
		logJsPlugin("./plugin/Energy_Echar.js","Energy_Echar");
	}else{
		
		eshot_qthandle.OpenJsPlugin();
	}
	
	

	
}
function FirstPage_Prapare(page)
{
	console.log("FirstPage_Prapare " + page);
	var obj = $("#Oper_View")[0];
	if(page != 7 )
	{
		ViewPage_ID = page;
		if(window.location.pathname.indexOf("index.html") < 0)
		{
			var  url = "./index.html?jumpto="+page;
			window.location.href=url;
			return;
		}
	}else if(page == 7)
	{
		window.location.href="./checker_ye_cfg.html";
		ViewPage_ID = 7;
		return;
	}
	if(ViewPage_InitFlag == 0)
	{
		page = getUrlParam("jumpto")
		if(typeof (page) == "undefined" || page == null)
		{
			page = ViewPage_ID;
		}
		page = parseInt(page);
		$(".title_nav_list").children().eq(page-1).addClass("layui-this");
		$(".title_nav_list").children().eq(6).removeClass("layui-this");
		ViewPage_InitFlag = 1;
		Epro_I_Page = "";
	}
	ViewPage_ID = page;
	switch(page)
	{
		case EPRO_I_PAGE_ID: if(Epro_I_Page.length < 10)
		{
			Epro_I_Page = loadPage("./Epro-I.html");
		}
		obj.innerHTML = Epro_I_Page;
		Ep1_Init();
		break;
		case MC_1_PAGE_ID: if(MC_1_Page.length < 10){
			MC_1_Page = loadPage("./mc_1.html");
		}
		obj.innerHTML = MC_1_Page;
		MC_1Init();
		break;
		case EPRO2_PAGE_ID: if(Ep2_Page.length < 10){
			Ep2_Page = loadPage("./Epro-II.html");
			//Ep2_Page = obj.innerHTML;
		} 
		obj.innerHTML = Ep2_Page;
		Ep2_Init();
		break;
		case RECHECK_PAGE_ID:if(Recheck_page.length < 10){
			Recheck_page =  loadPage("./Recheck.html");
		}
		obj.innerHTML = Recheck_page;
		ReCheck_Init();
		break;
		case EPROJ_PAGE_ID: if(Epj_Page.length < 10){
		Epj_Page =loadPage("./eproj-cmd.html");
		}
		obj.innerHTML = Epj_Page;
		Epj_Init();
		break;
		case EPE_PAGE_ID: if(Epe_page.length < 10){
		Epe_page =loadPage("./epe-cmd.html");
		}
		obj.innerHTML = Epe_page;
		Epe_Init();
		break;
		
		default: return;
	}
	
	
}


function getDateTime()
{
	var myDate = new Date();
	var log_time = IntFormat(2,(myDate.getMonth()+1));
	log_time += "-";
	log_time += IntFormat(2,(myDate.getDate()));
	log_time += " ";
	log_time += IntFormat(2,(myDate.getHours()));
	log_time += ":";
	log_time += IntFormat(2,(myDate.getMinutes()));
	log_time += ":";
	log_time += IntFormat(2,(myDate.getSeconds()));
	log_time += ":";
	log_time += IntFormat(3,(myDate.getMilliseconds()));
	return log_time;
}

 //获取url中的参数
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
	var r = window.location.search.substr(1).match(reg);  //匹配目标参数
	if (r != null) return unescape(r[2]); return null; //返回参数值
}

function MainInit()
{
	if(typeof(qt) != "undefined" &&  qt != null )
	{
		 var qwebchannel =  new QWebChannel(qt.webChannelTransport,function(channel){
			 
		 	  qchannel = channel;
              console.log("MainInit  ---");
              BTCommInit();
              FirstPage_Prapare(EPRO2_PAGE_ID);
			  //Ep1_Init();
			  //Epe_Init();
			 // Ep2_Init();
            });
		
	}else{
		BTCommInit();
        FirstPage_Prapare(EPRO2_PAGE_ID);
	}
	
	
}
