#include "fNetworkRegManual.h"
#include "runtime.h"

#define dDescpOffSet	0x1000

void TfNetworkRegManual::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	(429,		Top,		370,   CH, 0, 0, clWhite, clMaroon);
	
	Content.Set(Left, 	Top+CH, 	Right, 	Bottom);
	
	Color = aColor;
	#undef CH

	Caption.Border.Color = clGray;

	IsShowing = 0;
	IsViewList = 0;
	CtlIndex = 1;
	Done = 0;
}

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

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

void TfNetworkRegManual::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);
	
	RenderBackGround();
}


void TfNetworkRegManual::DrawCaption2(void)
{	
	//TMyString::sFromStr("TAB键在面板与机器列表之间切换", SText.Text);
	TMyString::sFromStr(" ", SText.Text);
	SText.Show();
}

void TfNetworkRegManual::Show(void)
{	
	IsShowing = 1;
	RenderBackGround();
	DrawSelf();
	ShowCaption();
	DrawCaption2();	
	
	ShowTip();
	JustShowSlaveListNum();
}

void TfNetworkRegManual::ShowActionResult()
{
	if(Sta == 0){
		TextRender_string24(20,200,clNearBlack,Color,"                    ");
	}else
	if(Sta == 1){
		TextRender_string24(20,200,clNearBlack,Color,"添加成功            ");
	}else
	if(Sta == -1){
		TextRender_string24(20,200,clNearBlack,Color,"删除成功            ");
	}else
	if(Sta == -2){
		TextRender_string24(20,200,clNearBlack,Color,"机器号不在列表      ");
	}else
	if(Sta == -3){
		TextRender_string24(20,200,clNearBlack,Color,"机器号超出范围      ");
	}else
	if(Sta == -4){
		TextRender_string24(20,200,clNearBlack,Color,"区域机已存在于列表内");
	}else
	if(Sta == -5){
		TextRender_string24(20,200,clNearBlack,Color,"区域机数量超出范围  ");
	}else
	if(Sta == -6){
		TextRender_string24(20,200,clNearBlack,Color,"区域机不在列表范围  ");
	}else
	if(Sta == -7){
		TextRender_string24(20,200,clNearBlack,Color,"添加失败, 记忆出错  ");
	}else
	if(Sta == -8){
		TextRender_string24(20,200,clNearBlack,Color,"删除失败, 记忆出错  ");
	}
}


void TfNetworkRegManual::JustShowSlaveListNum()
{
	int i, x;
	if(MainCtl.fData.Split.NetworkMode == 1){
		TextRender_string24(20,296,clNearBlack, Color, "区域机列表:");
		x = 60;
		for(i=0; i<ProtoC2C.SlaveCount; i++){
			TextDigitRender2Right24(x + (i*48),332,clNearBlack, Color, ProtoC2C.SlaveList[i]);
			TextRender_string24(x + (i*48 + 24),332,clNearBlack,Color," ");
		}
		for(; i<dNETWORK_MACHINE_COUNT;i++){
			TextRender_string24(x + (i*48),332,clNearBlack, Color, "   ");
		}
	}else
	if(MainCtl.fData.Split.NetworkMode == 2){
		TextRender_string24(20,296,clNearBlack, Color, "已挂载的集中机号:");
		x = 60;
		TextRender_string24    (x ,332,clNearBlack, Color, "   ");
		if( (ProtoC2C.MasterNum > 0) && (ProtoC2C.MasterNum <=dNETWORK_MACHINE_COUNT) )
			TextDigitRender2Right24(x ,332,clNearBlack, Color, ProtoC2C.MasterNum);
	}
}


void TfNetworkRegManual::ShowTip()
{
	if(MainCtl.fData.Split.NetworkMode == 1){
		TextRender_string24(20,240,clNearBlack, Color, "本机为集中机                  ");
	}else
	if(MainCtl.fData.Split.NetworkMode == 2){
		TextRender_string24(20,240,clNearBlack, Color, "本机为区域机                  ");
	}else
	if(MainCtl.fData.Split.NetworkMode == 0){
		TextRender_string24(20,240,clNearBlack, Color, "本机为单机                    ");
	}else{
		TextRender_string24(20,240,clNearBlack, Color, "本机网络设定不正确, 请重新设定");
	}
	
	TextRender_string24(20,370,clNearBlack,"1.设置键向左选择,查询键向右选择,上下键在添加栏与删除栏之间切换");
	TextRender_string24(20,406,clNearBlack,"2.机器号范围: 1~32");
}

void TfNetworkRegManual::ShowActionResult(int aSta)
{
	Sta = aSta;
	ShowActionResult();
}


void TfNetworkRegManual::SendNwList()
{
	//First Pkg     
	ProtoC2C.TxMsg.Data.CarryByte =255;
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Identification =0;
	ProtoC2C.TxMsg.Data.Priority = 1;
	
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 4;
	ProtoC2C.TxMsg.Data.Data[0] =0;
	ProtoC2C.TxMsg.Data.Data[1] = ProtoC2C.MasterNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = ProtoC2C.SlaveCount;
	
	ProtoC2C.oBufInsert();
	
	//First Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =1;
	ProtoC2C.TxMsg.Data.Data[1] = ProtoC2C.MasterNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = ProtoC2C.SlaveCount;
	ProtoC2C.TxMsg.Data.Data[4] = ProtoC2C.SlaveList[0];
	ProtoC2C.TxMsg.Data.Data[5] = ProtoC2C.SlaveList[1];
	ProtoC2C.TxMsg.Data.Data[6] = ProtoC2C.SlaveList[2];
	ProtoC2C.TxMsg.Data.Data[7] = ProtoC2C.SlaveList[3];
	
	ProtoC2C.oBufInsert();

	//First Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =2;
	ProtoC2C.TxMsg.Data.Data[1] = ProtoC2C.MasterNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = ProtoC2C.SlaveCount;
	ProtoC2C.TxMsg.Data.Data[4] = ProtoC2C.SlaveList[4];
	ProtoC2C.TxMsg.Data.Data[5] = ProtoC2C.SlaveList[5];
	ProtoC2C.TxMsg.Data.Data[6] = ProtoC2C.SlaveList[6];
	ProtoC2C.TxMsg.Data.Data[7] = ProtoC2C.SlaveList[7];
	
	ProtoC2C.oBufInsert();
	
	//First Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =3;
	ProtoC2C.TxMsg.Data.Data[1] = ProtoC2C.MasterNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = ProtoC2C.SlaveCount;
	ProtoC2C.TxMsg.Data.Data[4] = ProtoC2C.SlaveList[8];
	ProtoC2C.TxMsg.Data.Data[5] = ProtoC2C.SlaveList[9];
	ProtoC2C.TxMsg.Data.Data[6] = ProtoC2C.SlaveList[10];
	ProtoC2C.TxMsg.Data.Data[7] = ProtoC2C.SlaveList[11];
	
	ProtoC2C.oBufInsert();
	
	//First Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =4;
	ProtoC2C.TxMsg.Data.Data[1] = ProtoC2C.MasterNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = ProtoC2C.SlaveCount;
	ProtoC2C.TxMsg.Data.Data[4] = ProtoC2C.SlaveList[12];
	ProtoC2C.TxMsg.Data.Data[5] = ProtoC2C.SlaveList[13];
	ProtoC2C.TxMsg.Data.Data[6] = ProtoC2C.SlaveList[14];
	ProtoC2C.TxMsg.Data.Data[7] = ProtoC2C.SlaveList[15];
	
	ProtoC2C.oBufInsert();
}


void TfNetworkRegManual::TryAddRemoveSlave(int IsAdd)
{
	int iVal,i, Found,x, IsWriteOK;
	if(IsAdd){
		iVal = aPanel->Edit[0].Str.ToInteger();
		if( (iVal >0) && (iVal <dNETWORK_MACHINE_COUNT ) ){
			ProtoC2C.MasterNum = MainCtl.fData.Split.MyNum;
			
			if(ProtoC2C.SlaveCount > dNETWORK_MACHINE_COUNT){
				ShowActionResult(-5);
				return;
			}
			for(i=0; i<ProtoC2C.SlaveCount; i++){
				if(ProtoC2C.SlaveList[i] == iVal){
					ShowActionResult(-4);
					return;
				}
			}
			IsWriteOK = 1;
			ProtoC2C.SlaveList[ProtoC2C.SlaveCount] = iVal;
			ProtoC2C.SlaveCount++;
			aMasterNum = ProtoC2C.MasterNum;
			aSlaveCount = ProtoC2C.SlaveCount;
			for(i=0; i<ProtoC2C.SlaveCount; i++)aNumList[i] = ProtoC2C.SlaveList[i];
			Record.SaveNwCfg(aMasterNum, aSlaveCount, aNumList);
			Record.ReadNwCfg();
			if( (aMasterNum == ProtoC2C.MasterNum) && (aSlaveCount == ProtoC2C.SlaveCount) ){
				for(i=0; i<ProtoC2C.SlaveCount; i++){
					if(aNumList[i] != ProtoC2C.SlaveList[i])IsWriteOK = 0;
					break;
				}
			}
			if(IsWriteOK){
				SendNwList();
				Record.OperateAdd_AddRemoveNwCtl(1, iVal);
				JustShowSlaveListNum();
				ShowActionResult(1);
			}else{
				ShowActionResult(-7);
			}
		}else{
			ShowActionResult(-3);
		}
	}else{
		iVal = aPanel->Edit[1].Str.ToInteger();
		if( (iVal >0) && (iVal <= dNETWORK_MACHINE_COUNT) ){
			ProtoC2C.MasterNum = MainCtl.fData.Split.MyNum;
			Found = 0;
			if(ProtoC2C.SlaveCount >dNETWORK_MACHINE_COUNT)ProtoC2C.SlaveCount =dNETWORK_MACHINE_COUNT;
			for(i=0; i<ProtoC2C.SlaveCount; i++){
				if(ProtoC2C.SlaveList[i] == iVal){
					ProtoC2C.SlaveList[i] = 0;
					Found = 1;
				}
			}
			if(Found){
				aSlaveCount = 0;
				for(i=0; i<dNETWORK_MACHINE_COUNT; i++)aNumList[i] = 0;
				for(i=0; i<ProtoC2C.SlaveCount; i++){
					if(ProtoC2C.SlaveList[i] != 0){
						aNumList[aSlaveCount] = ProtoC2C.SlaveList[i];
						aSlaveCount++;
					}
				}
				aMasterNum = ProtoC2C.MasterNum ;
				Record.SaveNwCfg(aMasterNum, aSlaveCount,  aNumList);
				Record.ReadNwCfg();
				IsWriteOK = 1;
				if( (aMasterNum == ProtoC2C.MasterNum) && (aSlaveCount == ProtoC2C.SlaveCount) ){
					for(i=0; i<ProtoC2C.SlaveCount; i++){
						if(aNumList[i] != ProtoC2C.SlaveList[i])IsWriteOK = 0;
						break;
					}
				}
				if(IsWriteOK){
					SendNwList();
					Record.OperateAdd_AddRemoveNwCtl(2, iVal);
					ShowActionResult(1);
					JustShowSlaveListNum();
				}else{
					ShowActionResult(-8);
				}
			}else{
				ShowActionResult(-3);
			}				
		}else{
			ShowActionResult(-3);
		}
	}
	ProtoC2C.SyncCheckOnline();
}

void TfNetworkRegManual::TryAddRemoveMaster(int IsAdd)
{
	int iVal, IsWriteOk,i;
	IsWriteOk = 1;
	if(IsAdd){
		iVal = aPanel->Edit[0].Str.ToInteger();
		if( (iVal >0) && (iVal <dNETWORK_MACHINE_COUNT) ){
			ProtoC2C.MasterNum = iVal;
			aMasterNum = ProtoC2C.MasterNum ;
			aSlaveCount = ProtoC2C.SlaveCount;
			for(i=0; i<aSlaveCount; i++)aNumList[i] = ProtoC2C.SlaveList[i];
			Record.SaveNwCfg(aMasterNum, aSlaveCount,  aNumList);
			Record.ReadNwCfg();
			if(  (aMasterNum == ProtoC2C.MasterNum) && (aSlaveCount == ProtoC2C.SlaveCount) ){
				Record.OperateAdd_AddRemoveNwCtl(3, iVal);
				ShowActionResult(1);
				JustShowSlaveListNum();
			}else{
				IsWriteOk = 0;
			}
			if(IsWriteOk == 0){
				ShowActionResult(-7);
			}
		}else{
			ShowActionResult(-2);
		}
	}else{
		ProtoC2C.MasterNum = 0;
		aMasterNum = 0;
		aSlaveCount = ProtoC2C.SlaveCount;
		for(i=0; i<aSlaveCount; i++)aNumList[i] = ProtoC2C.SlaveList[i];
		Record.SaveNwCfg(0, aSlaveCount, aNumList);
		Record.ReadNwCfg();
		if(  (aMasterNum == ProtoC2C.MasterNum) && (aSlaveCount == ProtoC2C.SlaveCount) ){
			Record.OperateAdd_AddRemoveNwCtl(4, ProtoC2C.MasterNum);
			ShowActionResult(1);
			JustShowSlaveListNum();
		}else{
			ShowActionResult(-8);
		}
	}
	ProtoC2C.SyncCheckOnline();
}

void TfNetworkRegManual::FullRedraw(int Prm)
{	
	if(Prm == 0){
		IsShowing = 1;
		RenderBackGround();
		DrawSelf();
		ShowCaption();
		DrawCaption2();
		ShowTip();
		
		aPanel->Show();
		//DrawList();
		JustShowSlaveListNum();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		ShowTip();
		
		aPanel->Show();
		//DrawList();
		JustShowSlaveListNum();
	}
}

void TfNetworkRegManual::ShowCaption()
{
	TMyString::sFromStr("系统设置->组网机器手动注册",  Caption.Text);
	Caption.Show();
}

void TfNetworkRegManual::SetPanelCaptionColor()
{
	if(IsViewList == 0){
		aPanel->Caption.Color = clDeepBlue;
		aPanel->Caption.TextColor = clNearWhite;
	}else{
		aPanel->Caption.Color = clMedGray;
		aPanel->Caption.TextColor = clNearWhite;
	}
}

//______________________________________________________________________________________________________________________________
void TfNetworkRegManual::InitPanel4Master(void)
{
	int i,x,y,x2,y2, aW,aH;
	unsigned int aClr, bClr;
	x = Content.Left;
	y = Content.Top;
	x2 = Content.Right;
	y2 = Content.Bottom;
	aW = Content.Width;
	aH = Content.Height;
	
	//ClearData();
	//LoadData();
	
	if(aPanel == nullptr)return;
	
	aPanel->Init(10,3,772,123,x,y,clFrmFace, bvRaised);
	TMyString::sFromStr("添加或删除组网机器", aPanel->Caption.Text);
	aPanel->Caption.Visible = 1;
	aPanel->Caption.Enable = 1;
	
	x = aPanel->Content.Left;
	y = aPanel->Content.Top;
	
	aPanel->SText[0].Init(4,6,80,30,x,y,clNearBlack,clFrmFace);
	TMyString::sFromStr("机器号", aPanel->SText[0].Text);
	aPanel->SText[0].TextColor = clNearBlack;
	
	aPanel->SText[1].Init(4,50,80,30,x,y,clNearBlack,clFrmFace);
	TMyString::sFromStr("机器号", aPanel->SText[1].Text);
	aPanel->SText[1].TextColor = clNearBlack;
	
	aPanel->Edit[0].Init(86,6,40,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[1].Init(130,6,400,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[2].Init(86,50,40,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[3].Init(130,50,400,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[0].Str.FromStr("00");
	aPanel->Edit[1].Str.FromStr("要添加的机器号");
	aPanel->Edit[2].Str.FromStr("00");
	aPanel->Edit[3].Str.FromStr("要删除的机器号");
	aPanel->Edit[0].Enable =1;
	aPanel->Edit[0].Visible =1;
	aPanel->Edit[1].Enable =0;
	aPanel->Edit[1].Visible =1;
	aPanel->Edit[2].Enable =1;
	aPanel->Edit[2].Visible =1;
	aPanel->Edit[3].Enable =0;
	aPanel->Edit[3].Visible =1;
	aPanel->Edit[0].SetMaxLen(2);
	aPanel->Edit[1].SetMaxLen(32);
	aPanel->Edit[2].SetMaxLen(2);
	aPanel->Edit[3].SetMaxLen(32);
	
	aPanel->Btn[0].Init(536,6,160,30,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[1].Init(704,6,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[2].Init(704,50,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("远程获取描述");
	aPanel->Btn[1].Caption.FromStr("添加");
	aPanel->Btn[2].Caption.FromStr("删除");
	
	aPanel->Btn[0].SetCaptionLeftSpacing(2);
	aPanel->Btn[1].SetCaptionLeftSpacing(2);
	aPanel->Btn[2].SetCaptionLeftSpacing(2);
	
	aPanel->Btn[0].Visible = 1;
	aPanel->Btn[1].Visible = 1;
	aPanel->Btn[2].Visible = 1;
	aPanel->Btn[0].Enable = 0;
	aPanel->Btn[1].Enable = 1;
	aPanel->Btn[2].Enable = 1;

	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
	
	RectFillRender(20,149,779,150,clGray);
	
	//DrawList();	
	
	JustShowSlaveListNum();
}

void TfNetworkRegManual::InitPanel4Slave(void)
{
	int i,x,y,x2,y2, aW,aH;
	unsigned int aClr, bClr;
	x = Content.Left;
	y = Content.Top;
	x2 = Content.Right;
	y2 = Content.Bottom;
	aW = Content.Width;
	aH = Content.Height;
	
	//ClearData();
	//LoadData();
	
	if(aPanel == nullptr)return;
	
	aPanel->Init(10,3,772,123,x,y,clFrmFace, bvRaised);
	TMyString::sFromStr("设定要连接的集中机号", aPanel->Caption.Text);
	aPanel->Caption.Visible = 1;
	aPanel->Caption.Enable = 1;
	
	x = aPanel->Content.Left;
	y = aPanel->Content.Top;
	
	aPanel->SText[0].Init(4,6,80,30,x,y,clNearBlack,clFrmFace);
	TMyString::sFromStr("机器号", aPanel->SText[0].Text);
	aPanel->SText[0].TextColor = clNearBlack;
	
	aPanel->Edit[0].Init(86,6,40,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[1].Init(130,6,400,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[2].Init(86,50,40,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[3].Init(130,50,400,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[0].Str.FromStr("00");
	aPanel->Edit[1].Str.FromStr("集中机号");
	aPanel->Edit[2].Str.FromStr(" ");
	aPanel->Edit[3].Str.FromStr(" ");
	aPanel->Edit[0].Enable =1;
	aPanel->Edit[0].Visible =1;
	aPanel->Edit[1].Enable =0;
	aPanel->Edit[1].Visible =1;
	aPanel->Edit[2].Enable =1;
	aPanel->Edit[2].Visible =0;
	aPanel->Edit[3].Enable =0;
	aPanel->Edit[3].Visible =0;
	aPanel->Edit[0].SetMaxLen(2);
	aPanel->Edit[1].SetMaxLen(32);
	aPanel->Edit[2].SetMaxLen(2);
	aPanel->Edit[3].SetMaxLen(32);
	
	aPanel->Btn[0].Init(536,6,160,30,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[1].Init(540,6,216,30,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[2].Init(540,50,216,30,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("远程获取描述");
	aPanel->Btn[1].Caption.FromStr("    集中机连接  ");
	aPanel->Btn[2].Caption.FromStr("  删除集中机连接");
	
	aPanel->Btn[0].SetCaptionLeftSpacing(2);
	aPanel->Btn[1].SetCaptionLeftSpacing(2);
	aPanel->Btn[2].SetCaptionLeftSpacing(2);
	
	aPanel->Btn[0].Visible = 0;
	aPanel->Btn[1].Visible = 1;
	aPanel->Btn[2].Visible = 1;
	aPanel->Btn[0].Enable = 0;
	aPanel->Btn[1].Enable = 1;
	aPanel->Btn[2].Enable = 1;

	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
	
	RectFillRender(20,149,779,150,clGray);
	
	//DrawList();	
	
	JustShowSlaveListNum();
}


void TfNetworkRegManual::InitPanel4None(void)
{
	int i,x,y,x2,y2, aW,aH;
	unsigned int aClr, bClr;
	x = Content.Left;
	y = Content.Top;
	x2 = Content.Right;
	y2 = Content.Bottom;
	aW = Content.Width;
	aH = Content.Height;
	
	//ClearData();
	//LoadData();
	
	if(aPanel == nullptr)return;
	
	aPanel->Init(10,3,772,123,x,y,clFrmFace, bvRaised);
	TMyString::sFromStr("本机为单机,请返回", aPanel->Caption.Text);
	aPanel->Caption.Visible = 1;
	aPanel->Caption.Enable = 1;
	
	x = aPanel->Content.Left;
	y = aPanel->Content.Top;
	
	aPanel->SText[0].Init(4,6,80,30,x,y,clNearBlack,clFrmFace);
	TMyString::sFromStr("请返回", aPanel->SText[0].Text);
	aPanel->SText[0].TextColor = clNearBlack;
	
	aPanel->Show();

	RectFillRender(20,149,779,150,clGray);
}

void TfNetworkRegManual::InitPanel(void) 
{
	if(MainCtl.fData.Split.NetworkMode == 1){
		InitPanel4Master();
	}else
	if(MainCtl.fData.Split.NetworkMode == 2){
		InitPanel4Slave();
	}else{
		InitPanel4None();
	}
}

//_____________________________________________________________________________________________________________________________

void TfNetworkRegManual::ClearData()
{
	int i,x;
	unsigned int aAddr;
	//Clear
	for(i=0; i<64; i++){
		aAddr = dAddrSdRam_Register  + i*4;	
		*(volatile unsigned int *)(aAddr) = 0;
	}
	for(i=0; i<64; i++){
		for(x=0; x<64; x++){
			aAddr = dAddrSdRam_Register  + dDescpOffSet + i*64 + x;	
			*(volatile unsigned int *)(aAddr) = 0;
		}
	}
}

void TfNetworkRegManual::LoadData()
{
	int i,x;
	unsigned int aAddr;
	//Load Here
	Count = 0;
	
	//Clac
	PageCount = Count / 10;
	if(Count % 10){
		PageCount++;
	}
	PageIndex = 0;
}

void TfNetworkRegManual::DrawList()
{
	int i, x, y, z;
	unsigned char aP0;
	unsigned int aClr, aAddr, wc;

	if(IsViewList)aClr = clBlue; else aClr = clDeepGray;

	RectFillRender		(13, 	239, 	786, 268, aClr);
	TextRender_string24	(18, 	241 ,	clNearWhite, 	"机器列表 数量:");
	TextDigitRender2Right24 (200,241 ,  clNearWhite, Count);
	
	RectFillRender(6,232,10,430,aClr);
	RectFillRender(6,232,792,236,aClr);
	RectFillRender(6,430,792,434,aClr);
	RectFillRender(788,232,792,434,aClr);
	
	RectFillRender(11,269,787,429,clMoneyGreen);
	
	i = PageIndex * 10;
	wc =0;
	for(; i<Count; i++){
		x = 10;
		y = wc * 32 + 196;
		
		aAddr = dAddrSdRam_Register  + i*4;	
		aP0 = *(volatile unsigned int *)(aAddr);
		
		TextDigitRender2_24 (x,	y, clBlack, i);
		TextRender_string24	(x+24,y, clBlack,".");
		TextDigitRender3_24	(x+36,y,clBlack, aP0);
		
		aAddr = dAddrSdRam_Register  + dDescpOffSet + i*64;
		for(z=0; z<64; z++){
			dText[z] = *(volatile unsigned char *)(aAddr);
			aAddr++;
		}
		TextRender_string24	(x+84,y,clBlack,dText);

		wc++;
		if(wc >= 10)break;
	}
	i = PageIndex * 10;
	TVScrollBar::sDrawByPoint(767, 270, 786, 428, aClr, clGray, Count, 14, i);
}

void TfNetworkRegManual::ClearOutText()
{
	TMyString::sClear(aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TfNetworkRegManual::Task1000Ms()
{
	if(IsShowing){

	}
}

void TfNetworkRegManual::Check4Selected()
{
	aPanel->Btn[0].Selected = 0;
	aPanel->Btn[1].Selected = 0;
	aPanel->Btn[2].Selected = 0;
	
	aPanel->Edit[0].Selected = 0;
	aPanel->Edit[1].Selected = 0;
	aPanel->Edit[2].Selected = 0;

	/*if(IsViewList ==0){
		if(CtlIndex == 0){
			aPanel->Edit[0].Selected = 1;
		}else
		if(CtlIndex == 1){
			aPanel->Edit[1].Selected = 1;
		}else
		if(CtlIndex == 2){
			aPanel->Btn[0].Selected = 1;
		}else
		if(CtlIndex == 3){
			aPanel->Btn[1].Selected = 1;
		}else
		if(CtlIndex == 4){
			aPanel->Edit[2].Selected = 1;
		}else
		if(CtlIndex == 5){
			aPanel->Btn[2].Selected = 1;
		}
	}*/
	
	if(IsViewList ==0){
		if(CtlIndex == 0){
			aPanel->Edit[0].Selected = 1;
		}else
		if(CtlIndex == 1){
			aPanel->Btn[1].Selected = 1;
		}else
		if(CtlIndex == 2){
			aPanel->Edit[2].Selected = 1;
		}else
		if(CtlIndex == 3){
			aPanel->Btn[2].Selected = 1;
		}
	}
}

void TfNetworkRegManual::Check4Selected4Slave()
{
	aPanel->Btn[0].Selected = 0;
	aPanel->Btn[1].Selected = 0;
	aPanel->Btn[2].Selected = 0;
	
	aPanel->Edit[0].Selected = 0;
	aPanel->Edit[1].Selected = 0;
	aPanel->Edit[2].Selected = 0;

	/*if(IsViewList ==0){
		if(CtlIndex == 0){
			aPanel->Edit[0].Selected = 1;
		}else
		if(CtlIndex == 1){
			aPanel->Edit[1].Selected = 1;
		}else
		if(CtlIndex == 2){
			aPanel->Btn[0].Selected = 1;
		}else
		if(CtlIndex == 3){
			aPanel->Btn[1].Selected = 1;
		}else
		if(CtlIndex == 4){
			aPanel->Edit[2].Selected = 1;
		}else
		if(CtlIndex == 5){
			aPanel->Btn[2].Selected = 1;
		}
	}*/
	
	if(IsViewList ==0){
		if(CtlIndex == 0){
			aPanel->Edit[0].Selected = 1;
		}else
		if(CtlIndex == 1){
			aPanel->Btn[1].Selected = 1;
		}else
		if(CtlIndex == 2){
			aPanel->Btn[2].Selected = 1;
		}
	}
}

TGuiMsgReturn TfNetworkRegManual::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Err;
	unsigned char x,y,aLen;
	TDMix aFullPath;

	//if(IsViewList){
	if(0){
		switch(aKey){
			case VK_UP:
				if(PageIndex >0){
					PageIndex--;
					DrawList();
				}
				break;
			case VK_DOWN:
				if(PageIndex <(PageCount-1)){
					PageIndex++;
					DrawList();
				}
				break;
			case VK_TAB:
				IsViewList = 0;
				DrawList();
				Check4Selected();
				SetPanelCaptionColor();
				aPanel->ReDrawItems();
				break;
			case VK_RETURN:
				IsShowing = 0;
				aMsg = guiMsgReturn;
				break;
		}		
	}else{
		if(MainCtl.fData.Split.NetworkMode == 1){
			switch(aKey){
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
				case VK_DELETE:
					if(CtlIndex == 0){
						aPanel->Edit[0].KeyIn(aKey);
						aPanel->Edit[0].Show();
					}else
					if(CtlIndex == 2){
						aPanel->Edit[2].KeyIn(aKey);
						aPanel->Edit[2].Show();
					}
					break;
				case VK_LEFT:
					if(CtlIndex == 0){
						aPanel->Edit[0].KeyIn(aKey);
						aPanel->Edit[0].Show();
					}else
					if(CtlIndex == 1){
						//if(aPanel->Edit[1].Pst <0){
						if(1){
							CtlIndex = 0;
							Check4Selected();
							SetPanelCaptionColor();
							aPanel->ReDrawItems();
						}else{
							aPanel->Edit[1].KeyIn(aKey);
							aPanel->Edit[1].Show();
						}
					}else
					if(CtlIndex == 2){
						aPanel->Edit[2].KeyIn(aKey);
						aPanel->Edit[2].Show();
						//CtlIndex--;
						//Check4Selected();
						//SetPanelCaptionColor();
						//aPanel->ReDrawItems();
					}else
					if(CtlIndex == 3){
						CtlIndex--;
						Check4Selected();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}else
					if(CtlIndex == 4){
						aPanel->Edit[2].KeyIn(aKey);
						aPanel->Edit[2].Show();
					}else
					if(CtlIndex == 5){
						CtlIndex--;
						Check4Selected();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}
					break;
				case VK_RIGHT:
					if(CtlIndex == 0){
						if(aPanel->Edit[0].Pst >0){
							CtlIndex++;
							Check4Selected();
							SetPanelCaptionColor();
							aPanel->ReDrawItems();
						}else{
							aPanel->Edit[0].KeyIn(aKey);
							aPanel->Edit[0].Show();
						}
					}else
					if(CtlIndex == 1){
						//aLen = aPanel->Edit[1].Str.GetEndPst();
						//if(aPanel->Edit[1].Pst >=aLen){
						//	CtlIndex++;
						//	Check4Selected();
						//	SetPanelCaptionColor();
						//	aPanel->ReDrawItems();
						//}else{
						//	aPanel->Edit[1].KeyIn(aKey);
						//	aPanel->Edit[1].Show();
						//}
					}else
					if(CtlIndex == 2){
						if(aPanel->Edit[2].Pst >0){
							CtlIndex++;
							Check4Selected();
							SetPanelCaptionColor();
							aPanel->ReDrawItems();
						}else{
							aPanel->Edit[2].KeyIn(aKey);
							aPanel->Edit[2].Show();
						}
						
						//CtlIndex++;
						//Check4Selected();
						//SetPanelCaptionColor();
						//aPanel->ReDrawItems();
					}else
					if(CtlIndex == 3){

					}else
					if(CtlIndex == 4){
						if(aPanel->Edit[2].Pst >0){
							CtlIndex++;
							Check4Selected();
							SetPanelCaptionColor();
							aPanel->ReDrawItems();
						}else{
							aPanel->Edit[2].KeyIn(aKey);
							aPanel->Edit[2].Show();
						}
					}
					break;
				case VK_UP:
					if(CtlIndex == 2){
						CtlIndex = 0;
						Check4Selected();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}else
					if(CtlIndex == 3){
						CtlIndex = 1;
						Check4Selected();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}
					break;
				case VK_DOWN:
					if(CtlIndex <1){
						CtlIndex = 2;
						Check4Selected();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}else
					if(CtlIndex <2){
						CtlIndex = 3;
						Check4Selected();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}
					break;
				case dfKEY_SETUP:
					if(CtlIndex <4){
						if(CtlIndex >0){
							CtlIndex--;
							Check4Selected();
							SetPanelCaptionColor();
							aPanel->ReDrawItems();
						}
					}else
					if(CtlIndex == 5){
						CtlIndex = 4;
						Check4Selected();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}
					break;
				case dfKEY_QUERY:
					/*if(CtlIndex <4){
						if(CtlIndex <3){
							CtlIndex++;
							Check4Selected();
							SetPanelCaptionColor();
							aPanel->ReDrawItems();
						}
					}else
					if(CtlIndex == 4){
						CtlIndex = 5;
						Check4Selected();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}*/
					break;
				case VK_TAB:
					//IsViewList = 1;
					//DrawList();
					//Check4Selected();
					//SetPanelCaptionColor();
					//aPanel->ReDrawItems();
					break;
				case VK_EXECUTE:
					if(CtlIndex == 1){
						TryAddRemoveSlave(1);
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
						JustShowSlaveListNum();
					}else
					if(CtlIndex == 3){
						TryAddRemoveSlave(0);
						JustShowSlaveListNum();
					}
					break;
				case VK_RETURN:
					IsShowing = 0;
					aMsg = guiMsgReturn;
					break;
			}
		}else
		if(MainCtl.fData.Split.NetworkMode == 2){
			switch(aKey){
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
				case VK_DELETE:
					if(CtlIndex == 0){
						aPanel->Edit[0].KeyIn(aKey);
						aPanel->Edit[0].Show();
					}
					break;
				case VK_LEFT:
					if(CtlIndex == 0){
						aPanel->Edit[0].KeyIn(aKey);
						aPanel->Edit[0].Show();
					}else
					if(CtlIndex == 1){
						//if(aPanel->Edit[1].Pst <0){
						if(1){
							CtlIndex = 0;
							Check4Selected4Slave();
							SetPanelCaptionColor();
							aPanel->ReDrawItems();
						}else{
							aPanel->Edit[1].KeyIn(aKey);
							aPanel->Edit[1].Show();
						}
					}else
					if(CtlIndex == 2){
						CtlIndex = 0;
						Check4Selected4Slave();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}
					break;
				case VK_RIGHT:
					if(CtlIndex == 0){
						if(aPanel->Edit[0].Pst >0){
							CtlIndex++;
							Check4Selected4Slave();
							SetPanelCaptionColor();
							aPanel->ReDrawItems();
						}else{
							aPanel->Edit[0].KeyIn(aKey);
							aPanel->Edit[0].Show();
						}
					}
					break;
				case VK_UP:
					if(CtlIndex == 2){
						CtlIndex = 1;
						Check4Selected4Slave();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}
					break;
				case VK_DOWN:
					if(CtlIndex <2){
						CtlIndex = 2;
						Check4Selected4Slave();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}
					break;
				case dfKEY_SETUP:
					break;
				case dfKEY_QUERY:
					break;
				case VK_TAB:
					break;
				case VK_EXECUTE:
					if(CtlIndex == 1){
						TryAddRemoveMaster(1);
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}else
					if(CtlIndex == 2){
						TryAddRemoveMaster(0);
					}
					break;
				case VK_RETURN:
					IsShowing = 0;
					aMsg = guiMsgReturn;
					break;
			}
		}else{
			switch(aKey){
				case VK_RETURN:
					IsShowing = 0;
					aMsg = guiMsgReturn;
					break;
			}
		}
	}
	return aMsg;	
}

void TfNetworkRegManual::SendfNetMsg(int Len)
{
	
}

void TfNetworkRegManual::PushfNetMsg(unsigned char aCubCmd, unsigned char *data, int Len)
{
	
}

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

