#include "RuntimeData.h"
#include "BoardState.h"

//Not Using This since 2024 04 20

#define TextTopOffSet	6
#define ListLineCnt	10

#define ipdWidth0  	400
#define ipdWidth1		390

static const unsigned char stCmd[8] = {221,222,223,224,225,226,227,228};
static const unsigned char stPrm[8] = {0,1,2,3,4,5,6,7};

void TBoardState::Init(int x1, int y1, int aW, int aH, int OwnerX, int OwnerY, u32 aColor)
{
	#define CH	30	//Caption Height
	unsigned short Left,Right, Top, Bottom, Width, Height, i;
	Bound.SetBySize(OwnerX + x1, OwnerY + y1, aW, aH);
	Left = Bound.Left +3;
	Top = Bound.Top+3;
	Width = aW - 7;
	Height = aH - 7;
	Right = Bound.Right -4;
	Bottom = Bound.Bottom -4;
	Caption.Init(Left,Top,Width, CH, 0, 0, clWhite, clMaroon);
	SText.Init(Left+358,Top,434, CH, 0, 0, clWhite, clMaroon);
	Content.Set(Left, Top+CH, Right, Bottom);
	RectPanel.Set(205,78,595,400);
	Color = aColor;
	#undef CH
	
	Caption.Border.Color = clGray;
	TMyString::sFromStr("板卡运行状态-本机", Caption.Text);
	
	TextColor = clNearBlack;
	SelectedColor = clBlue;
	SelectedTextColor = clNearWhite;
	
	CtlIndex = 0;
	Target = 0;
	IsShowing = 0;
	
	for(i=0; i<12; i++){
		RecData[i] = 0;
		RecDataUpdateFlag[i] =0;
	}	
}

void TBoardState::RenderBackGround(void)
{
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);
}

void TBoardState::RenderBackGround(unsigned int aClr)
{
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, aClr);
}

void TBoardState::DrawSelf(void)
{
	unsigned int x, y, x2, y2, w, h;
	
	VertLineRender(Bound.Left,		Bound.Top, 			Bound.Height -1,			0xFFFFFFFF);
	VertLineRender(Bound.Right -1,	Bound.Top +1,		Bound.Height -2,			0xFF808080);
	VertLineRender(Bound.Right,		Bound.Top,			Bound.Height,			0xFF404040);
	
	HorizLineRender(Bound.Left, 		Bound.Top, 			Bound.Width - 1, 			0xFFFFFFFF);
	HorizLineRender(Bound.Left +1, 	Bound.Bottom -1,  	Bound.Width - 2, 			0xFF808080);
	HorizLineRender(Bound.Left,		Bound.Bottom,		Bound.Width,			0xFF404040);
	
	x = Bound.Left +2 ;	
	x2 = Bound.Right -2;
	y = Bound.Top +2;
	y2 = Bound.Bottom - 2;
	w = Bound.Width -4;
	h = Bound.Height - 4;
	VertLineRender	(x,		y, 		h-1,	0xFF808080);
	HorizLineRender	(x,		y, 		w-1,	0xFF808080);
		
	VertLineRender	(x+1,		y+1, 		h-2,	0xFF404040);
	HorizLineRender	(x+1,		y+1, 		w-2,	0xFF404040);
	
	VertLineRender	(x2-1,	y+1, 		h-2,	0xFFD4D0C8);
	HorizLineRender	(x+1,		y2-1, 	w-2,	0xFFD4D0C8);

	VertLineRender	(x2,		y, 	  	h,	0xFFFFFFFF);
	HorizLineRender	(x,		y2, 		w,	0xFFFFFFFF);
	
	TextRender_string24(RectPanel.Left, Bound.Bottom -32 ,clNearBlack, "选中板卡类型时,按TAB键切换类型;");
}


void TBoardState::DrawCaption2(void)
{	
	TMyString::sFromStr("xxxxxxx", SText.Text);
	//SText.Show();
}

void TBoardState::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	Caption.Show();
	DrawCaption2();
}

void TBoardState::SetAndShowCaption(unsigned char aType)
{
	TMyString::sFromStr(" zzzzzzz",  Caption.Text);
}

//______________________________________________________________________________________________________________________________

void TBoardState::Check4Selected(void)
{
	unsigned int i;
	aPanel->Btn[0].Selected = 0;
	aPanel->Edit[0].Selected = 0;
	aPanel->Edit[1].Selected = 0;
	switch(CtlIndex){
		case 0:
			aPanel->Edit[0].Selected = 1;
			break;
		case 1:
			aPanel->Edit[1].Selected = 1;
			break;
		case 2:
			aPanel->Btn[0].Selected = 1;
			break;
		default:

			break;
	}		
}

void TBoardState::FillStateName()
{
	if(Target == 0){
		aPanel->Edit[0].Str.FromStr("总线盘");
		aPanel->Edit[0].ReDraw();
		TMyString::sFromStr("按键数", aPanel->SText[0].Text);
		TMyString::sFromStr("允许数", aPanel->SText[1].Text);
		TMyString::sFromStr("电压", aPanel->SText[2].Text);
		TMyString::sFromStr("电流", aPanel->SText[3].Text);
		TMyString::sFromStr("手动允许", aPanel->SText[4].Text);
		TMyString::sFromStr("状态值6", aPanel->SText[5].Text);
	}else
	if(Target ==1){
		aPanel->Edit[0].Str.FromStr("多线盘");
		aPanel->Edit[0].ReDraw();
		TMyString::sFromStr("按键数", aPanel->SText[0].Text);
		TMyString::sFromStr("允许数", aPanel->SText[1].Text);
		TMyString::sFromStr("电压", aPanel->SText[2].Text);
		TMyString::sFromStr("电流", aPanel->SText[3].Text);
		TMyString::sFromStr("手动允许", aPanel->SText[4].Text);
		TMyString::sFromStr("状态值6", aPanel->SText[5].Text);
	}else
	if(Target ==2){
		aPanel->Edit[0].Str.FromStr("回路母板");
		aPanel->Edit[0].ReDraw();
		TMyString::sFromStr("最大挂载", aPanel->SText[0].Text);
		TMyString::sFromStr("已挂载", aPanel->SText[1].Text);
		TMyString::sFromStr("电压", aPanel->SText[2].Text);
		TMyString::sFromStr("电流", aPanel->SText[3].Text);
		TMyString::sFromStr("联动托管", aPanel->SText[4].Text);
		TMyString::sFromStr("状态6", aPanel->SText[5].Text);
	}else
	if(Target ==3){
		aPanel->Edit[0].Str.FromStr("组网通讯板");
		aPanel->Edit[0].ReDraw();
		TMyString::sFromStr("波特率", aPanel->SText[0].Text);
		TMyString::sFromStr("连接", aPanel->SText[1].Text);
		TMyString::sFromStr("电压", aPanel->SText[2].Text);
		TMyString::sFromStr("电流", aPanel->SText[3].Text);
		TMyString::sFromStr("状态5", aPanel->SText[4].Text);
		TMyString::sFromStr("状态6", aPanel->SText[5].Text);
	}else
	if(Target ==4){
		aPanel->Edit[0].Str.FromStr("CRT连接板");
		aPanel->Edit[0].ReDraw();
		TMyString::sFromStr("波特率", aPanel->SText[0].Text);
		TMyString::sFromStr("心跳时间", aPanel->SText[1].Text);
		TMyString::sFromStr("电压", aPanel->SText[2].Text);
		TMyString::sFromStr("电流", aPanel->SText[3].Text);
		TMyString::sFromStr("状态5", aPanel->SText[4].Text);
		TMyString::sFromStr("状态6", aPanel->SText[5].Text);
	}
}


//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TBoardState::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	int iVal;
	
	if( 	(aKey == VK_DELETE) || 
		(aKey == VK_0) ||	
		(aKey == VK_1) ||
		(aKey == VK_2) ||
		(aKey == VK_3) ||
		(aKey == VK_4) ||
		(aKey == VK_5) ||
		(aKey == VK_6) ||	
		(aKey == VK_7) ||
		(aKey == VK_8) ||
		(aKey == VK_9) ){
		if(aPanel->Edit[1].Selected){
			aPanel->Edit[1].KeyIn(aKey);
			aPanel->Edit[1].ReDraw();
		}
	}
	
	if(  (aKey == VK_UP) || (aKey == VK_DOWN) ){
		if(aPanel->Edit[0].Selected){
			if(  aKey == VK_UP  ){
				Target--;
				if(Target < 0){
					Target = 4;
				}
				FillStateName();
				aPanel->ReDrawItems();
			}
			if(  aKey == VK_DOWN  ){
				Target++;
				if(Target > 4){
					Target = 0;
				}
				FillStateName();
				aPanel->ReDrawItems();
			}
		}else
		if(aPanel->Edit[1].Selected){
			iVal = aPanel->Edit[1].Str.ToInteger();
			if((aKey == VK_UP)){
				iVal++;
				if(iVal > 40)iVal =40;
			}
			if((aKey == VK_DOWN)){
				iVal--;
				if(iVal <1)iVal =1;
			}
			aPanel->Edit[1].Str.FromInt(iVal);
			aPanel->Edit[1].SetPstTail();
			aPanel->Edit[1].Show();
		}
	}
	
	if(aKey == VK_LEFT){
		if(CtlIndex == 1){
			if(aPanel->Edit[1].Pst < 0){
				CtlIndex--;
				Check4Selected();
				aPanel->ReDrawItems();
				PanelCanvasOut();
			}else{
				aPanel->Edit[1].KeyIn(aKey);
				aPanel->Edit[1].Show();
			}
		}else
		if(CtlIndex == 2){
			CtlIndex--;
			Check4Selected();
			aPanel->ReDrawItems();
			PanelCanvasOut();
		}
	}else
	if(aKey == VK_RIGHT){
		if(CtlIndex == 1){
			if(aPanel->Edit[1].Pst > 0){
				CtlIndex = 2;
				Check4Selected();
				aPanel->ReDrawItems();
				PanelCanvasOut();
			}else{
				aPanel->Edit[1].KeyIn(aKey);
				aPanel->Edit[1].Show();
			}
		}else
		if(CtlIndex == 0){
			CtlIndex++;
			Check4Selected();
			aPanel->ReDrawItems();
			PanelCanvasOut();
		}
	}else
	if(aKey == VK_TAB){
		if(aPanel->Edit[0].Selected){
			Target++;
			if(Target > 4){
				Target = 0;
			}
			FillStateName();
			aPanel->ReDrawItems();
		}
	}
	if(aKey == VK_EXECUTE){
		//Save Data Here
		SendCmd();
	}else
	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;
	}
	
	return aMsg;	
}

void TBoardState::PanelCanvasOut()
{
	HorizLineRender(RectPanel.Left +6, 		317, 		RectPanel.Width - 12, 			clSilver);
}

void TBoardState::InitPanel(void)
{
	int x,y,x2,y2, aW,aH,i;
	x = RectPanel.Left;
	y = RectPanel.Top;
	x2 = RectPanel.Right;
	y2 = RectPanel.Bottom;
	aW = RectPanel.Width;
	aH = RectPanel.Height;
	
	aPanel->Init(x,y,aW,aH,0,0,clFrmFace, bvRaised);
	
	aPanel->Caption.Init(1,1,aW-3,32,x,y,clFrmFace,clFrmFace);
	aPanel->Caption.Text[0] = 0;
	
	aPanel->SText[0].Init(2,12,100,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(2,50,100,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(2,88,100,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[3].Init(2,126,100,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[4].Init(2,164,100,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[5].Init(2,202,100,30,x,y,clNearBlack,clFrmFace);
	
	aPanel->SText[10].Init(200,12,100,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[11].Init(200,50,100,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[12].Init(200,88,100,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[13].Init(200,126,100,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[14].Init(200,164,100,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[15].Init(200,202,100,30,x,y,clNearBlack,clFrmFace);
	
	
	aPanel->SText[6].Init(36,247,100,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[7].Init(180,247,60,30,x,y,clNearBlack,clFrmFace);
	//aPanel->SText[8].Init(205,250,60,30,x,y,clNearBlack,clFrmFace);
	//aPanel->SText[9].Init(290,250,60,30,x,y,clNearBlack,clFrmFace);
	
	
	TMyString::sFromStr("状态值1", aPanel->SText[0].Text);
	TMyString::sFromStr("状态值2", aPanel->SText[1].Text);
	TMyString::sFromStr("状态值3", aPanel->SText[2].Text);
	TMyString::sFromStr("状态值4", aPanel->SText[3].Text);
	TMyString::sFromStr("状态值5", aPanel->SText[4].Text);
	TMyString::sFromStr("状态值6", aPanel->SText[5].Text);
	
	TMyString::sFromStr("返回值1", aPanel->SText[10].Text);
	TMyString::sFromStr("返回值2", aPanel->SText[11].Text);
	TMyString::sFromStr("返回值3", aPanel->SText[12].Text);
	TMyString::sFromStr("返回值4", aPanel->SText[13].Text);
	TMyString::sFromStr("返回值5", aPanel->SText[14].Text);
	TMyString::sFromStr("返回值6", aPanel->SText[15].Text);
	
	TMyString::sFromStr("板卡类型", aPanel->SText[6].Text);
	TMyString::sFromStr("序号", aPanel->SText[7].Text);
	
	for(i=0; i<8;i++){
		aPanel->SText[i].Enable = 1;
		aPanel->SText[i].Visible = 1;
	}
	for(i=10; i<16;i++){
		aPanel->SText[i].Enable = 1;
		aPanel->SText[i].Visible = 1;
	}
	
	aPanel->Edit[4].Init(110,12,70,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[5].Init(110,50,70,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[6].Init(110,88,70,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[7].Init(110,126,70,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[8].Init(110,164,70,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[9].Init(110,202,70,30,x,y,clNearBlack,clNearWhite);
	
	aPanel->Edit[10].Init(306,12,70,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[11].Init(306,50,70,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[12].Init(306,88,70,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[13].Init(306,126,70,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[14].Init(306,164,70,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[15].Init(306,202,70,30,x,y,clNearBlack,clNearWhite);
	
	aPanel->Edit[0].Init(8,280,160,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[1].Init(180,280,56,30,x,y,clNearBlack,clNearWhite);

	for(i=0; i<2;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(8);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.Clear();
		aPanel->Edit[i].Pst = -1;
	}
	for(i=4; i<16;i++){
		aPanel->Edit[i].Enable = 0;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(5);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.Clear();
		aPanel->Edit[i].Pst = -1;
	}
	aPanel->Edit[0].SetMaxLen(16);
	aPanel->Edit[0].Str.FromStr("总线盘");
	aPanel->Edit[0].Pst = 1;
	aPanel->Edit[0].Color = clMoneyGreen;
	aPanel->Edit[1].SetMaxLen(2);
	aPanel->Edit[1].Str.FromStr("01");
	aPanel->Edit[1].Pst = 1;
	aPanel->Edit[0].IsShowCursor = 0;

	aPanel->Btn[0].Init(250,280,128,32,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("监测状态");
	
	for(i=0; i<1;i++){
		aPanel->Btn[i].Enable = 1;
		aPanel->Btn[i].Visible = 1;
		aPanel->Btn[i].SetCaptionLeftSpacing(2);
		aPanel->Btn[i].SelectedColor = clBlue;
	}
	
	aPanel->Caption.Visible = 1;
	
	aPanel->Show();
	Check4Selected();
	FillStateName();
	aPanel->ReDrawItems();
	PanelCanvasOut();
}

void TBoardState::SendCmd()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	if(CtlIndex == 10){
		Len = 5;
		
		Data[0] = 0;
		Data[1] = 1;
		
		//Path 0
		iVal = aPanel->Edit[6].Str.ToInteger();
		if(iVal>255)iVal=255;
		cVal = iVal;
		Data[2] = cVal;
		
		//Cmd 1
		iVal = aPanel->Edit[7].Str.ToInteger();
		if(iVal>255)iVal=255;
		cVal = iVal;
		aIde.BitF.Cmd = cVal;
		
		//Cmd 2
		iVal = aPanel->Edit[8].Str.ToInteger();
		if(iVal>255)iVal=255;
		cVal = iVal;
		Data[3] = cVal;
		
		//Prm 
		iVal = aPanel->Edit[9].Str.ToInteger();
		if(iVal>255)iVal=255;
		cVal = iVal;
		Data[4] = cVal;
		
		aIde.BitF.Pri = 1;
		aIde.BitF.sLvl = 1;
		aIde.BitF.dLvl = 7;
		aIde.BitF.sAddr = 0;
		aIde.BitF.dAddr = 127;
	
		InnerCan.Buf4GuiSend.Insert(aIde, Data, Len);
	}
}


int TBoardState::ExtRequst(unsigned char Prm)
{
	IsShowing = 0;
	return 0;
}






