var CheckerPlanTest_flag = false;
var Checker_En_CmdTest = true;
function Checker_Start_Comm()
{
	console.log("Checker_Start_Comm ");
	if (typeof(epe_qthandle) != "undefined" && epe_qthandle != null) {
		epe_qthandle.SearchCommport();
		console.log("SearchCommport ");
	}
	
}

function Checker_Open_CommPort(main_serial) {
	var epe_oper = {};
	epe_oper.comm_port = main_serial.comm_port;
	epe_oper.comm_baud = main_serial.comm_baud;
	epe_oper.comm_parity = main_serial.comm_parity;
	epe_oper.comm_stopbit = main_serial.comm_stopbit;

	Main_Close_CommPort_Select();
	var json_str = JSON.stringify(epe_oper);
	console.log(json_str);
	if (typeof(epe_qthandle) != "undefined" && epe_qthandle != null) {
		epe_qthandle.OpenCommPort(json_str);
	}
}

function CheckerShowMes_autoClose(showtitle,mes,time_out)
{
	layer.alert(mes, {
			title:showtitle,
			time:time_out,
	      });
}
var MesBox_CmdTestRe_ID = -1;
var Checker_CmdMes_Scroll_flag = true;
function CheckerShowMes_CmdTestDialog()
{
	if(MesBox_CmdTestRe_ID < 0)
	{
		$("#checker_commCmdRes").unbind('dblclick').bind('dblclick', function(){
			Checker_CmdMes_Scroll_flag = Checker_CmdMes_Scroll_flag? false:true;
			
		});
		MesBox_CmdTestRe_ID = layer.open({
        type: 1,
		title:'串口功能测试',
        shade: false, // 不显示遮罩
        content: $('#checker_cmdTest_Mes'), // 捕获的元素
		btn: ['清空'],
		btnAlign: 'r', // 右边
		//area: 'auto',
		maxHeight:600,
        end: function(){
          MesBox_CmdTestRe_ID = -1;
		  CheckerPlanTest_flag = 0;
        },
		btn1:function(){
			$("#checker_commCmdRes")[0].innerHTML = "";
			return false
			
			// var obj_ = $("#checker_commCmdRes")[0];
			// obj_.innerHTML += "<br>sdfjhuih快递收发u返回文化意义粉红色丢分回复撒回复恢复hi发水u分化为回复哈随风还是低";
			// if(Checker_CmdMes_Scroll_flag)
			// {
			// 	obj_.scrollTop  = obj_.scrollHeight;
			// }
			
		},
      });
	}
}
function CheckerShowMes_CmdTestRe(mes)
{
	var obj_ = $("#checker_commCmdRes")[0];
	if(obj_.innerHTML.length < 1)
	{
		obj_.innerHTML = mes;
	}else{
		obj_.innerHTML  += "<br>"+mes;
	}
	
	if(Checker_CmdMes_Scroll_flag)
	{
		obj_.scrollTop  = obj_.scrollHeight;
	}
	CheckerShowMes_CmdTestDialog();
}
function Checker_CmdTest(taskindex)
{
	Checker_Export_Plan = Checker_GetCfgInfo();
	var TaskArray =Checker_Export_Plan.TaskArray;
	var param = "";
	var task_id = -1;
	for(var index = 0;  index < TaskArray.length; index++)
	{
		if(taskindex == TaskArray[index].TaskIndex)
		{
			task_id = TaskArray[index].TaskID;
			var paramList = TaskArray[index].ParamVal;
			for(var index2 = 0; index2 < paramList.length;index2++)
			{
				if(index2 != 0)
				{
					param+= " ";
				}
				param += paramList[index2];
			}
			
		}
	}
	if(task_id < 0)
	{
		return ;
	}
	var cmd_test = "";
	if(CheckerChipID == 0)
	{
		cmd_test = "JCH "
	}else if(CheckerChipID == 1)
	{
	    cmd_test = "XCH "
	}else{
		return;
	}
	for(var i = 0; i < CheckerTask_Info.length; i++)
	{
		if(task_id == CheckerTask_Info[i].TaskID)
		{
			cmd_test += CheckerTask_Info[i].CommTestCmd + " ";
		}
	}
	cmd_test += param;
	CheckerShowMes_CmdTestRe(cmd_test);
	//CheckerShowMes_CmdTestDialog();
	console.log("Checker_CmdTest  "+cmd_test);
	cmd_test += "\r\n";
	CheckerPlanTest_flag = false;
	Checker_CommSendStr(cmd_test);
	
}

function Checker_PlanTest()
{
	var cmdtest = "";
	if(CheckerChipID == 0)
	{
		cmd_test = "JCH ExcuePlan\r\n"
	}else if(CheckerChipID == 1)
	{
	    cmd_test = "XCH ExcuePlan\r\n"
	}else{
		return;
	}
	CheckerPlanTest_flag = true;
	Checker_CommSendStr(cmd_test);
	//Checker_ShowReadStr("");
}

var MesBox_InputTestResult_ID = -1;
function CheckerShowMes_InputResult()
{

	if(MesBox_InputTestResult_ID < 0)
	{
	
		MesBox_InputTestResult_ID = layer.open({
        type: 1,
		title:'数据检测验证，确认与方案匹配',
        shade: false, // 不显示遮罩
        content: $('#checker_testData_Check'), // 捕获的元素
		btn: ['导入'],
		btnAlign: 'r', // 右边
		//area: 'auto',
		maxHeight:600,
        end: function(){
          MesBox_InputTestResult_ID = -1;
        },
		btn1:function(){
			
			var result = $("#checker_testData_input").val();
			Checker_ShowTestResult(result);
		},
      });
	}
}

function Checker_ShowTestResult(result)
{
	var mesLine = result.split(",");
	var resultdata = [];
	for(var i = 0; i < mesLine.length;i++)
	{
		resultdata[i] = parseInt(mesLine[i]);
	}
	var task_info_list = $(".checker_cfig_div").children();
	var TaskPlan = Checker_GetCfgInfo();
	var taskInfoArray = TaskPlan.TaskArray;
	var resultCount = 0;
	for(var index = 0; index < taskInfoArray.length;index++)
	{
		if(taskInfoArray[index].ReturnCount == 0)
		{
			continue;
		}
		var Taskindex = taskInfoArray[index].TaskIndex;
		var itemObj = task_info_list[Taskindex+1];
		var viewItemObj = $(itemObj).find(".checker_test_cmd_item");
		
		var resultInputList = $(viewItemObj).find("input");
		var rtv_error_flag = 0;
		for(var index2 = 0; index2 < resultInputList.length && index2 < taskInfoArray[index].TestStandard.length ; index2++)
		{
			$(resultInputList[index2]).val(resultdata[resultCount]);
			
			if(resultdata[resultCount] > taskInfoArray[index].TestStandard[index2].Max 
			||resultdata[resultCount] < taskInfoArray[index].TestStandard[index2].Min )
			{
				rtv_error_flag = 1;
				$(resultInputList[index2]).css("color","#dd0000");
			}else{
				$(resultInputList[index2]).css("color","#030000");
			}
			resultCount++;
		}
		if(rtv_error_flag != 0)
		{
			var viewItemObj = $(task_info_list[Taskindex+1]).find(".layui-colla-title");
			$(viewItemObj).css("background-color","#c10205");
			$(viewItemObj).css("color","#fffaf7");
		}else{
			var viewItemObj = $(task_info_list[Taskindex+1]).find(".layui-colla-title");
			$(viewItemObj).css("background-color","#71dfda");
			$(viewItemObj).css("color","#030202");
		}
	}
	
}

function Checker_ShowPlanTest(messtr)
{
	var mesLine = messtr.split("\n");
	var showmes = "";
	for(var index = 0; index <mesLine.length; index++ )
	{
		if(mesLine[index].length < 2)
		{
			continue;
		}
		CheckerShowMes_CmdTestRe(mesLine[index]);
	}
	var objarray = [];
	objarray =  Checker_ResultMes2Obj(messtr);
	var task_info_list = $(".checker_cfig_div").children(); 
	Checker_Export_Plan = Checker_GetCfgInfo();
	var TaskArray =Checker_Export_Plan.TaskArray;
	var error_flag = 0;
	var rtv_error_flag = 0;
	for(var index = 0; index < objarray.length; index++)
	{
		var obj_ = objarray[index];
		if(obj_.TaskIndex >= (TaskArray.length))
		{
			error_flag = 1;
			break;
		}
		var viewTaskId = TaskArray[obj_.TaskIndex].TaskID;
		if(viewTaskId != obj_.TaskID)
		{
			
			error_flag = 1;
			break;
		}
		var viewItemObj = $(task_info_list[obj_.TaskIndex+1]).find(".checker_test_cmd_item");
		var resultInputList = $(viewItemObj).find("input");
		if(typeof(resultInputList) == "undefined" || resultInputList == null )
		{
			error_flag = 1;
			break;
		}
		rtv_error_flag = 0;
		for(var index2 = 0; index2 < obj_.ResultV.length; index2++)
		{
			$(resultInputList[index2]).val(obj_.ResultV[index2]);
			
			if(obj_.ResultV[index2] > TaskArray[obj_.TaskIndex].TestStandard[index2].Max 
			||obj_.ResultV[index2] < TaskArray[obj_.TaskIndex].TestStandard[index2].Min )
			{
				rtv_error_flag = 1;
				$(resultInputList[index2]).css("color","#dd0000");
			}else{
				$(resultInputList[index2]).css("color","#030000");
			}
		}
		if(obj_.ResultE != 0 || rtv_error_flag != 0)
		{
			var viewItemObj = $(task_info_list[obj_.TaskIndex+1]).find(".layui-colla-title");
			$(viewItemObj).css("background-color","#c10205");
			$(viewItemObj).css("color","#fffaf7");
		}else{
			var viewItemObj = $(task_info_list[obj_.TaskIndex+1]).find(".layui-colla-title");
			$(viewItemObj).css("background-color","#71dfda");
			$(viewItemObj).css("color","#030202");
		}
		
	}
	if(error_flag)
	{
		$("#checker_commCmdRes")[0].innerHTML += "<br>测试结果与方案不匹配<br>";
	}
	CheckerShowMes_CmdTestDialog();
	
	
}

function Checker_ResultMes2Obj(messtr)
{
	var objarray= [];
	var mesList = messtr.split("\n");
	var obj_count = 0;
	for(var index = 0; index < mesList.length; index++)
	{
		var mes_temp = "";
		var mes_src = mesList[index];
		var temp_count = 0;
		for(var index2 = 0;index2 < mes_src.length;index2++)
		{
			if( mes_src.charAt(index2) == ' ' 
			||  mes_src.charAt(index2) == ',' 
			||  mes_src.charAt(index2) == ';'
			||  mes_src.charAt(index2) == '\t')
			{
				if(mes_temp.length == 0)
				{
					continue;
				}
				if(mes_temp.charAt(mes_temp.length-1) == ' ')
				{
					continue;
				}
			}
			mes_temp += mes_src.charAt(index2);
		}
		if(mes_temp.length < 10)
		{
			continue;
		}
		
		mes_temp = mes_temp.trim();
		var resultItems = mes_temp.split(" ");
		if(resultItems.length < 6)
		{
			continue;
		}
		var obj={
			TaskIndex:parseInt(resultItems[1]),
			TaskID:parseInt(resultItems[3]),
			ResultE:parseInt(resultItems[5]),
			ResultV:[],
		}
		var result_array = [];
		for(var index2 = 6; index2 < resultItems.length; index2++)
		{
			var data =  parseInt(resultItems[index2]);
			if(isNaN(data))
			{
				data = -1;
			}
			result_array[index2-6] =data;
		}
		obj.ResultV = result_array;
		objarray[obj_count++] = obj;
	}
	return objarray;
}

function Checker_CommSendStr(str)
{
		if(epe_qthandle != null && typeof(epe_qthandle) != "undefined")
		{
			epe_qthandle.Epe_SendStr(str);
		}
	
}


function Checker_ShowReadStr(str)
{
	//str = "test taefCurM1ontiorofjiofjowfjoeifdslafjow\n"

	if(str.indexOf("CurMR") >= 0)
	{
		str = str.split("\t");
		CheckerShowCurEcharts(str[1]);
	}
	else if(CheckerPlanTest_flag == false)
	{
		str = str.replace("\r\n","\n");
		str = str.replace("\r","\n");
		str = str.split("\n");
		for(var index = 0; index < str.length;index++)
		{
			if(str[index].length > 5)
			{
				CheckerShowMes_CmdTestRe(str[index]);
			}
		}
		
	}
	else{
		
		Checker_ShowPlanTest(str);
	}
}