#include "AreaCfg.h"
#include "Runtime.h"

extern "C"{
	#include "user_norflash.h"
	#include "user_eeprom.h"
	#include "uart_key_drv.h"
	#include "task.h"
}

//#define dFlash4AreaChip			(0)
//#define dFlash4Area			(0x00BB'0000)

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

#define dWWidth	24
#define dWHeigh	24
#define dRoundR	7

const int xNew = 10;
const int xModify = 100;
const int xDelete = 100;
const int xSave = 10;

static const char CharBcdText[20]= {"0123456789*------\0"};

void TAreaCfg::Init()
{
	CtlIndex = 0;
	IsShow = 0;
	ListPage = 0;
	ItemInx = 0;
	Sta =0;
	
	IsPermission =0;
	IsEdittingDescp =0;
	IsEdittingShortName =0;
}

void TAreaCfg::sDelayMs(unsigned int aMs)
{
	volatile unsigned int mTick;
	volatile unsigned int Tick;
	for(Tick=0; Tick<aMs; Tick++){
		for(mTick=0; mTick<160000; mTick++);
		sys_error_led_task();
	}
}

void TAreaCfg::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

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

void TAreaCfg::DrawFixText()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "联动->分区编辑");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "分区号");
		TextRender_string24(Lv.Grid.Left[0] + 186, Lv.Grid.Top[0] +6, clNearWhite, "机器号");
		TextRender_string24(Lv.Grid.Left[0] + 286, Lv.Grid.Top[0] +6, clNearWhite, "本机");
		TextRender_string24(Lv.Grid.Left[0] + 400, Lv.Grid.Top[0] +6, clNearWhite, "分区数量:");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "简称");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +40, clNearWhite, "描述");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +114, clNearWhite, "二次码");
	
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "操作");
		TRoundRect::sDrawBySize(xNew, 	Lv.Grid.Top[0] + 40, 76, 30, 7, clNearWhite, "新建", 14, clNearBlack);
		//TRoundRect::sDrawBySize(xModify, 	Lv.Grid.Top[0] + 40, 76, 30, 7, clNearWhite, "修改", 14, clNearBlack);
		TRoundRect::sDrawBySize(xDelete, 	Lv.Grid.Top[0] + 40, 76, 30, 7, clNearWhite, "删除", 14, clNearBlack);
		TRoundRect::sDrawBySize(xSave, 	Lv.Grid.Top[2] + 40, 76, 30, 7, clNearWhite, "保存", 14, clNearBlack);
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Linkage->Zone Setting");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "ZoneID");
		TextRender_string24(Lv.Grid.Left[0] + 186, Lv.Grid.Top[0] +6, clNearWhite, "CtlID");
		TextRender_string24(Lv.Grid.Left[0] + 286, Lv.Grid.Top[0] +6, clNearWhite, " Local");
		TextRender_string24(Lv.Grid.Left[0] + 400, Lv.Grid.Top[0] +6, clNearWhite, "Amout:");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "Brie");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +40, clNearWhite, "Desc");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +114, clNearWhite, "Aux ID");
	
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "Operation");
		TRoundRect::sDrawBySize(xNew, 	Lv.Grid.Top[0] + 40, 76, 30, 7, clNearWhite, "New ", 14, clNearBlack);
		//TRoundRect::sDrawBySize(xModify, 	Lv.Grid.Top[0] + 40, 76, 30, 7, clNearWhite, "修改", 14, clNearBlack);
		TRoundRect::sDrawBySize(xDelete, 	Lv.Grid.Top[0] + 40, 76, 30, 7, clNearWhite, "Del ", 14, clNearBlack);
		TRoundRect::sDrawBySize(xSave, 	Lv.Grid.Top[2] + 40, 76, 30, 7, clNearWhite, "Save", 14, clNearBlack);
	}
}

void TAreaCfg::DrawSelf(void)
{
	RenderBackGround();
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);

	for(int i=0; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);

	DrawFixText();
	TextDigitRender2_24(Lv.Grid.Left[0] + 260, Lv.Grid.Top[0] +6, clNearWhite, MainCtl.fData.Split.MyNum);
	
	IList->Edit[0].Init(Lv.Grid.Left[0] + 86, Lv.Grid.Top[0] +4, 60, 30, 0,0,1,clNearWhite);
	IList->Edit[0].Str.Clear();
	IList->Edit[0].SetMaxLen(4);
	IList->Edit[0].Enable =1;
	
	IList->Edit[1].Init(Lv.Grid.Left[1] + 60, Lv.Grid.Top[1] +4, 		226, 30, 0,0,1,clNearWhite);
	IList->Edit[1].Str.Clear();
	IList->Edit[1].Enable =1;
	IList->Edit[1].IsShowCursor =0;
	mlEdit.Init(Lv.Grid.Left[1] +60, Lv.Grid.Top[1] +40, 				226, 64, 0,0,1,clNearWhite);
	mlEdit.SetLineSpacing(4);
	mlEdit.Str.Clear();
	mlEdit.Enable =1;
	mlEdit.IsShowCursor =0;
	
	IList->EditUserCode.Init(Lv.Grid.Left[1] + 86, Lv.Grid.Top[1] +110, 	200, 30, 0,0,1,clNearWhite);
	IList->EditUserCode.Str.Clear();
	IList->EditUserCode.SetMaxLen(8);
	IList->EditUserCode.Enable =1;
	
	IList->Edit[0].Show();
	mlEdit.Show();
	IList->Edit[1].Show();
	IList->EditUserCode.Show();
}

void TAreaCfg::Redraw4EditText()
{
	RectFillRender(9, 239, 790, 431, ContentColor);
	
	for(int i=1; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	
	DrawFixText();

	IList->Edit[0].Show();
	IList->Edit[1].Show();
	mlEdit.Show();
	IList->EditUserCode.Show();
	
	Check4Selected();
}

void TAreaCfg::Show(void)
{	
	IsShow = 1;
	RenderBackGround();
	DrawSelf();
	DrawList();
}

void TAreaCfg::ShowSta()
{
	if(LanguageEnCn==0){
		if(Sta == 0){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "          ");
		}else
		if(Sta == 1){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "保存成功  ");
		}else
		if(Sta == -1){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "保存失败  ");
		}else
		if(Sta == 2){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "删除成功  ");
		}else
		if(Sta == -2){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "删除失败  ");
		}
	}else{
		if(Sta == 0){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "          ");
		}else
		if(Sta == 1){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "Saved OK  ");
		}else
		if(Sta == -1){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "Save Err  ");
		}else
		if(Sta == 2){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "Deleted OK");
		}else
		if(Sta == -2){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "Failed    ");
		}
	}
}

void TAreaCfg::CheckCount()
{
	int i;
	ItemCount = AreaSetting.Count;
}

void TAreaCfg::DrawCount()
{
	int i=0;
	
	TextDigitRender4_24(Lv.Grid.Left[0] + 400 + 110, Lv.Grid.Top[0] +6, clNearWhite, ItemColor, ItemCount);
}

void TAreaCfg::ShowSta(int aSta)
{
	Sta = aSta;
	ShowSta();
}

void TAreaCfg::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
		ShowSta();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], ContentColor);
		for(int i=0; i<2; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
		if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
		DrawFixText();
		TextDigitRender2_24(Lv.Grid.Left[0] + 260, Lv.Grid.Top[0] +6, clNearWhite, MainCtl.fData.Split.MyNum);

		IList->Edit[0].Show();
		mlEdit.Show();
		IList->Edit[1].Show();
		IList->EditUserCode.Show();
		if(IsList == 0)Check4Selected();
		DrawList();
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}


//______________________________________________________________________________________________________________________________
void TAreaCfg::ClearSelected(void)
{
	char *tText;
	switch(CtlIndex){
		case 0:
			IList->Edit[0].Selected =0;
			IList->Edit[0].Show();
			break;
		case 1:
			if(LanguageEnCn==0)tText="新建";else tText="New ";
			TRoundRect::sDrawBySize(xNew, 	Lv.Grid.Top[0] + 40, 76, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 2:
			if(LanguageEnCn==0)tText="删除";else tText="Del ";
			TRoundRect::sDrawBySize(xDelete, 	Lv.Grid.Top[0] + 40, 76, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 3:
			IList->Edit[1].Selected =0;
			IList->Edit[1].Show();
			break;
		case 4:
			mlEdit.Selected =0;
			mlEdit.Show();
			break;
		case 5:
			IList->EditUserCode.Selected =0;
			IList->EditUserCode.Show();
			break;
		case 6:
			if(LanguageEnCn==0)tText="保存";else tText="Save";
			TRoundRect::sDrawBySize(xSave, 	Lv.Grid.Top[2] + 40, 76, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		default:

			break;
	}	
}

void TAreaCfg::Check4Selected(void)
{
	char *tText;
	switch(CtlIndex){
		case 0:
			IList->Edit[0].Selected =1;
			IList->Edit[0].SetPstTail();
			IList->Edit[0].Show();
			break;
		case 1:
			if(LanguageEnCn==0)tText="新建";else tText="New ";
			TRoundRect::sDrawBySize(xNew, 	Lv.Grid.Top[0] + 40, 76, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
		case 2:
			if(LanguageEnCn==0)tText="删除";else tText="Del ";
			TRoundRect::sDrawBySize(xDelete, 	Lv.Grid.Top[0] + 40, 76, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
		case 3:
			IList->Edit[1].Selected =1;
			IList->Edit[1].SetPstTail();
			IList->Edit[1].Show();
			break;
		case 4:
			mlEdit.Selected =1;
			mlEdit.Show();
			break;
		case 5:
			IList->EditUserCode.Selected =1;
			IList->EditUserCode.SetPstTail();
			IList->EditUserCode.Show();
			break;
		case 6:
			if(LanguageEnCn==0)tText="保存";else tText="Save";
			TRoundRect::sDrawBySize(xSave, 	Lv.Grid.Top[2] + 40, 76, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
		default:

			break;
	}	
}

void TAreaCfg::ShowCfg()
{
	IList->Edit[0].Show();
	IList->Edit[1].Show();
	mlEdit.Show();
	IList->EditUserCode.Show();
	DrawCount();
}

void TAreaCfg::Start()
{
	LoadData();
	Check4Selected();
	ShowCfg();
}

void TAreaCfg::PermissionCmdGO()
{
	if((PermissionCmd == 1) || (PermissionCmd == 2)){
		RestoreData();
		WriteEntryViaFlash();
		LoadData();
		DrawList();
	}
}

void TAreaCfg::PermissionCmdReturnDraw()
{
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	DrawList();
}

void TAreaCfg::RestoreShortName(char *p)
{
	unsigned int i;
	for(i=0; i<16; i++){
		IList->Edit[1].Str.Text[i] = *p;
		if(*p == 0){
			break;
		}
		p++;
	}
	IList->Edit[1].Str.Text[i] =0;
	IList->Edit[1].Str.Text[16] =0;
	IList->Edit[1].Str.Text[17] =0;
}

void TAreaCfg::RestoreDescp(char *p)
{
	unsigned int i;
	for(i=0; i<32; i++){
		mlEdit.Str.Text[i] = *p;
		if(*p == 0){
			break;
		}
		p++;
	}
	mlEdit.Str.Text[i] =0;
	mlEdit.Str.Text[32] =0;
	mlEdit.Str.Text[33] =0;
}

void TAreaCfg::InitPanel(char *pText)
{
	int x,y,x2,y2, aW,aH,i;

	x = 10;
	y = 240;
	x2 = 789;
	y2 = 430;
	aW = x2 - x +1;
	aH = y2 -y +1;
	
	aPanel->Init(x,y,aW,aH,0,0,clMoneyGreen, bvRaised);
	
	aPanel->Caption.Init(1,1,aW-3,32,x,y,clNearWhite,clBlue);
	if(LanguageEnCn==0)aPanel->Caption.SetText("按中/EN键切换输入法,设置键保存更改,返回键放弃更改并返回",24);
	else aPanel->Caption.SetText("Key 中/EN To Switch Input Method,Key Set To Save",24);
	aPanel->Caption.Enable = 1;
	aPanel->Caption.Visible = 1;

	aPanel->SText[0].Init(460,40,300,34,x,y,clNearBlack,clMoneyGreen);
	if(LanguageEnCn==0)TMyString::sFromStr("当前快捷输入数字", aPanel->SText[0].Text);
	else TMyString::sFromStr("Enter Numbers", aPanel->SText[0].Text);
	aPanel->SText[0].Enable = 1;
	aPanel->SText[0].Visible = 1;
	
	aPanel->Edit[ 0].Init(10,40,420,30,x,y,1,clNearBlack);
	aPanel->Edit[ 0].Str.FromStr(pText);
	
	for(i=0; i<1;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(32);
		aPanel->Edit[i].SetPstTail();
		aPanel->Edit[i].Selected = 1;
	}
	
	aPanel->Show();
	aPanel->Caption.Show();
	aPanel->SText[0].Show();
	aPanel->Edit[0].Show();
	aPanel->Edit[0].ShowCursor();
}

void TAreaCfg::NewEntry()
{
	int i, GotNewInx;
	unsigned int sAddr, Idf;
	GotNewInx =0;
	for(i=0; i<dAreaCountMax; i++){
		sAddr = dAddrSdRam_4Area + (i * 64);
		Idf = *(volatile unsigned int *)(sAddr);
		if(Idf != 0xA8A7A6A5){
			GotNewInx = 1;
			break;
		}
	}
	if(GotNewInx){
		ItemInx = i;
		LoadData();
		ClearSelected();
		CtlIndex = 3;
		Check4Selected();
	}
}

//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TAreaCfg::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg = guiMsgNone;
	int iVal;

	if(IsPermission){
		PmsRtnMsg = PmsIntervene4Gui.KeyIn(aKey);
		if(PmsRtnMsg == guiMsgOkAndReturn){
			//Enter And Run Go
			IsPermission = 0;
			PermissionCmdReturnDraw();
			PermissionCmdGO();
		}else
		if(PmsRtnMsg == guiMsgReturn){
			//Not Enter Redraw
			IsPermission = 0;
			PermissionCmdReturnDraw();
		}
		return aMsg;
	}else
	if( (IsEdittingDescp) || (IsEdittingShortName) ){
		const char *p;
		if(aKey == dfKEY_SETUP){
			//Restore
			if(IsEdittingDescp){
				RestoreDescp(this->aPanel->Edit[0].Str.Text);
			}else
			if(IsEdittingShortName){
				RestoreShortName(this->aPanel->Edit[0].Str.Text);
			}
			Redraw4EditText();
			DrawList();
			IsEdittingDescp = 0;
			IsEdittingShortName =0;
		}else
		if(IsExtInput){
			if(aKey == dfKEY_ENCN){
				CnInput.KeyIn(aKey);
				if(CnInput.InputMode == 0){
					IsExtInput = 0;
					CnInput.OverPrint(this->aPanel->Color);
					aPanel->Show();
					aPanel->Caption.Show();
					aPanel->SText[0].Show();
					aPanel->Edit[0].Show();
					aPanel->Edit[0].ShowCursor();
					if(LanguageEnCn==0)TMyString::sFromStr("当前快捷输入数字", aPanel->SText[0].Text);
					else TMyString::sFromStr("Enter Numbers", aPanel->SText[0].Text);
					aPanel->SText[0].Show();
				}
			}else
			if(aKey == VK_RETURN){
				//Just return
				Redraw4EditText();
				DrawList();
				IsEdittingDescp = 0;
				IsEdittingShortName =0;
			}else{
				if(  (aKey == VK_DELETE)  &&  (CnInput.sEdit.Pst == -1) ){
					aPanel->Edit[0].KeyIn(aKey);
					aPanel->Edit[0].Show();
					aPanel->Edit[0].ShowCursor();
				}else{
					p = CnInput.KeyIn(aKey);
					if(p != nullptr){
						if(CnInput.GotChar[0] != '\0'){
							aPanel->Edit[0].Pst = aPanel->Edit[0].Str.Insert(CnInput.GotChar, aPanel->Edit[0].Pst);
							aPanel->Edit[0].Str.BrokeFixLen(32);
							aPanel->Edit[0].CorrectPst();
							aPanel->Edit[0].Show();
						}
					}
				}
			}
		}else{
			switch(aKey){
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
				case VK_LEFT:
				case VK_RIGHT:
				case VK_DELETE:
					aPanel->Edit[0].KeyIn(aKey);
					aPanel->Edit[0].Str.BrokeFixLen(32);
					aPanel->Edit[0].CorrectPst();
					aPanel->Edit[0].Show();
					break;
				case dfKEY_ENCN:
					if(IsExtInput == 0){
						IsExtInput = 1;
						CnInput.Init(aPanel->Bound.Left + 10, aPanel->Bound.Top +80, 760, 90, 0, 0, clGray);
						CnInput.Show();
						if(LanguageEnCn==0)TMyString::sFromStr("当前根据输入法输入", aPanel->SText[0].Text);
						else TMyString::sFromStr("Pay Attention To The Tip", aPanel->SText[0].Text);
						aPanel->SText[0].Show();
					}
					break;
				case VK_EXECUTE:
					
					break;
				case VK_RETURN:
					Redraw4EditText();
					DrawList();
					IsEdittingDescp = 0;
					IsEdittingShortName =0;
					break;
			}
		}
	}else
	if(IsList){
		int PageCountMax = dAreaCountMax / 8;
		switch(aKey){
			case VK_LEFT:
				if(ListPage >0){
					ListPage -=8;
					if(ListPage <0)ListPage = 0;
				}
				DrawList();
				break;
			case VK_RIGHT:
				if(ListPage < PageCountMax){
					ListPage +=8;
					if(ListPage > (PageCountMax-1))ListPage = PageCountMax-1;
				}
				DrawList();
				break;
			case VK_UP:
				if(ListPage >0){
					ListPage--;
					if(ListPage <0)ListPage = 0;
				}
				DrawList();
				break;
			case VK_DOWN:
				if(ListPage < PageCountMax){
					ListPage ++;
					if(ListPage > (PageCountMax-1))ListPage = PageCountMax-1;
				}
				DrawList();
				break;
			case VK_TAB:
				IsList = 0;
				DrawList();
				Check4Selected();
				break;
			case VK_EXECUTE:

				break;
			case VK_RETURN:
				aMsg = guiMsgReturn;
				break;
			default :
				break;
		}			
	}else{
		switch(aKey){
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case VK_LEFT:
			case VK_RIGHT:
			case VK_DELETE:
				if(CtlIndex == 0){
					IList->Edit[0].KeyIn(aKey);
					int iVal;
					iVal = IList->Edit[0].Str.ToInteger();
					if( (iVal <1) || (iVal > dAreaCountMax) ){
						IList->Edit[0].SelectedColor = clRed;
					}else{
						IList->Edit[0].SelectedColor = clBlue;
						iVal--;
						if(iVal != ItemInx){
							ItemInx = iVal;
							LoadUcBcdText();
							LoadShortName();
							LoadDescp();
							ShowCfg();
							Check4Selected();
						}
					}
					IList->Edit[0].Show();
				}else
				if(CtlIndex == 1){
					if(aKey == VK_RIGHT){
						ClearSelected();
						CtlIndex++;
						Check4Selected();
					}
				}else
				if(CtlIndex == 2){
					if(aKey == VK_LEFT){
						ClearSelected();
						CtlIndex--;
						Check4Selected();
					}
				}else
				if(CtlIndex == 3){
					IList->Edit[1].Selected = 0;
					IList->Edit[1].Show();
					InitPanel(IList->Edit[1].Str.Text);
					IsEdittingShortName = 1;
					IsExtInput = 0;
				}else
				if(CtlIndex == 4){
					mlEdit.Selected = 0;
					mlEdit.Show();
					InitPanel(mlEdit.Str.Text);
					IsEdittingDescp = 1;
					IsExtInput = 0;
				}else
				if(CtlIndex == 5){
					IList->EditUserCode.KeyIn(aKey);
					IList->EditUserCode.Show();
				}
				break;
			case dfKEY_ASTERISK:
					if(CtlIndex == 5){
						IList->EditUserCode.KeyIn(aKey);
						IList->EditUserCode.Show();
					}
				break;
			case VK_UP:
				if(CtlIndex >0){
					ClearSelected();
					CtlIndex--;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <6){
					ClearSelected();
					CtlIndex++;
					Check4Selected();
				}
				break;

			case VK_TAB:
				IsList = 1;
				DrawList();
				ClearSelected();
				break;				
			case dfKEY_SETUP:		//0x70		//  Key value for SETUP

				//ShowCfg();
				break;
			case VK_EXECUTE:
				//Save Data Here
				if(CtlIndex == 1){
					NewEntry();
				}else
				if(CtlIndex == 2){
					PermissionCmd = 2;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}else
				if(CtlIndex == 3){
					IList->Edit[1].Selected = 0;
					IList->Edit[1].Show();
					InitPanel(IList->Edit[1].Str.Text);
					IsEdittingShortName = 1;
					IsExtInput = 0;
				}else
				if(CtlIndex == 4){
					mlEdit.Selected = 0;
					mlEdit.Show();
					InitPanel(mlEdit.Str.Text);
					IsEdittingDescp = 1;
					IsExtInput = 0;
				}else
				if(CtlIndex == 6){
					PermissionCmd = 1;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -36 , 140, 3);
					}
				}
				break;
			case VK_RETURN:
				aMsg = guiMsgReturn;
				break;
			default :
				break;
		}
		
		if(VK_EXECUTE != aKey){
			if( (IsEdittingDescp == 0) && (IsEdittingShortName == 0) ){
				ShowSta(0);
			}
		}
	}
	
	return aMsg;	
}

void TAreaCfg::LoadUcBcdText()
{
	int i;
	unsigned int Idf, aOrd, sAddr;
	
	if(ItemInx >= dAreaCountMax ){
		IList->EditUserCode.Str.Text[0] = '\0';
		IList->EditUserCode.Str.Text[1] = '\0';
		return;
	}
	
	sAddr = dAddrSdRam_4Area + (ItemInx * 64);
	Idf = *(volatile unsigned int *)(sAddr);
	if(Idf == 0xA8A7A6A5){
		sAddr = dAddrSdRam_4Area + (ItemInx * 64) + 4;
		for(i=0; i<8; i++){
			IList->EditUserCode.Str.Text[i] = *(volatile unsigned char *)(sAddr +i);
		}
		IList->EditUserCode.Str.Text[8] = '\0';
	}else{
		IList->EditUserCode.Str.Text[0] = '\0';
		IList->EditUserCode.Str.Text[1] = '\0';
	}
}

void TAreaCfg::LoadShortName()
{
	unsigned int Idf, sAddr;
	
	if(ItemInx >= dAreaCountMax ){
		IList->Edit[1].Str.Text[0] = '\0';
		IList->Edit[1].Str.Text[1] = '\0';
		return;
	}
	
	sAddr = dAddrSdRam_4Area + (ItemInx * 64);
	Idf = *(volatile unsigned int *)(sAddr);
	if(Idf == 0xA8A7A6A5){
		sAddr = dAddrSdRam_4Area + (ItemInx * 64) + 16;
		for(int i=0; i<16; i++){
			IList->Edit[1].Str.Text[i] = *(volatile unsigned char *)(sAddr +i);
		}
		IList->Edit[1].Str.Text[16] = '\0';
	}else{
		IList->Edit[1].Str.Text[0] = '\0';
		IList->Edit[1].Str.Text[1] = '\0';
	}
}

void TAreaCfg::LoadDescp()
{
	unsigned int Idf, sAddr;
	
	if(ItemInx >= dAreaCountMax ){
		mlEdit.Str.Text[0] ='\0';
		mlEdit.Str.Text[1] ='\0';
		return;
	}
	
	sAddr = dAddrSdRam_4Area + (ItemInx * 64);
	Idf = *(volatile unsigned int *)(sAddr);
	if(Idf == 0xA8A7A6A5){
		sAddr = dAddrSdRam_4Area + (ItemInx * 64) + 32;
		for(int i=0; i<32; i++){
			 mlEdit.Str.Text[i] = *(volatile unsigned char *)(sAddr +i);;
		}
		mlEdit.Str.Text[32] = '\0';
	}else{
		mlEdit.Str.Text[0] ='\0';
		mlEdit.Str.Text[1] ='\0';
	}
}

void TAreaCfg::RestoreUcBcdText()
{
	for(int i=0; i<8; i++){
		RawData[i+4] = IList->EditUserCode.Str.Text[i];
	}
}

void TAreaCfg::RestoreShortName()
{
	for(int i=0; i<16; i++){
		RawData[i+16] = IList->Edit[1].Str.Text[i];
	}
}

void TAreaCfg::RestoreDescp()
{
	for(int i=0; i<32; i++){
		RawData[i+32] = mlEdit.Str.Text[i];
	}
}

void TAreaCfg::LoadData()
{
	CheckCount();
	IList->Edit[0].Str.FromUInt4Dg(ItemInx +1);
	
	LoadUcBcdText();
	LoadShortName();
	LoadDescp();
	ShowCfg();
	Check4Selected();
}

void TAreaCfg::RestoreData()
{
	int i;
	if(PermissionCmd == 1){
		RestoreUcBcdText();
		RestoreShortName();
		RestoreDescp();
		//Save
		for(i=0; i<4; i++)RawData[i] =0xA5 +i;
	}else
	if(PermissionCmd == 2){
		//Erase
		for(i=0; i<64; i++)RawData[i] =0;
	}
}

void TAreaCfg::ReadEntryViaFlash()
{

}

void TAreaCfg::WriteEntryViaFlash()
{
	unsigned int fAddr, sAddr, GetSta;
	unsigned int BlockX, PageX, OffSet;
	
	BlockX = ItemInx * 64 / 4096;
	do{
		sDelayMs(3);
		GetSta = nor_get_busy_state(dFlash4AreaChip);
	}while(GetSta);
	fAddr = dFlash4Area + (BlockX * 4096);
	nor_erase_sector_4k(dFlash4AreaChip,fAddr);
	sDelayMs(100);
	do{
		sDelayMs(3);
		GetSta = nor_get_busy_state(dFlash4AreaChip);
	}while(GetSta);
	
	sAddr = dAddrSdRam_4Area + (ItemInx * 64);
	for(int x=0; x<64; x++){
		*(volatile unsigned char *)(sAddr + x) = RawData[x];
	}
	PageX = ItemInx / (256 / 64);
	for(int i=0; i<16; i++){
		IList->RealtimeData.Reset();
		sAddr = dAddrSdRam_4Area + ( (BlockX * 16 +i) * 256);
		do{
			sDelayMs(10);
			GetSta = nor_get_busy_state(dFlash4AreaChip);
		}while(GetSta);
		for(int x=0;x<256;x++){
			IList->RealtimeData.Data[0][x] = *(volatile unsigned char *)(sAddr + x);
		}
		fAddr = dFlash4Area + ( (BlockX * 16 + i) * 256);
		nor_write_page(dFlash4AreaChip, fAddr, IList->RealtimeData.Data[0]);
		do{
			sDelayMs(5);
			GetSta = nor_get_busy_state(dFlash4AreaChip);
		}while(GetSta);
	}
	
	do{
		sDelayMs(5);
		GetSta = nor_get_busy_state(dFlash4AreaChip);
	}while(GetSta);
	Record.Read4Area();
	GetSta =0;
	sAddr = dAddrSdRam_4Area + (ItemInx * 64);
	for(int i=0; i<64; i++){
		if(RawData[i] != *(volatile unsigned char *)(sAddr +i)){
			GetSta=1;
			break;
		}
	}
	
	if(GetSta){
		//Write Err
		if(PermissionCmd == 1){
			//Save Err
			ShowSta(-1);
		}else
		if(PermissionCmd == 2){
			//Delete Err
			ShowSta(-2);
		}
	}else{
		//Write OK
		if(PermissionCmd == 1){
			//Save OK
			ShowSta(1);
		}else
		if(PermissionCmd == 2){
			//Delete OK
			ShowSta(2);
		}
	}	
}

void TAreaCfg::DrawList()
{
	int i, z, x, y;
	unsigned int aClr, TopInx, sAddr, Idf;
	IsList ? aClr=clBlue : aClr=clMedGray;
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Bottom,PanelColor);
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Left +4,Lv.Panel.Bottom,aClr);
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Top +4,aClr);
	RectFillRender(Lv.Panel.Right-4,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Bottom,aClr);
	RectFillRender(Lv.Panel.Left,Lv.Panel.Bottom -4,Lv.Panel.Right,Lv.Panel.Bottom,aClr);
	
	const unsigned int PageCount = dAreaCountMax / 8;
	if(ListPage >= PageCount) ListPage = 0;

	x = Lv.Panel.Left+6;
	y = Lv.Panel.Top +6;
	if(LanguageEnCn==0)TextRender_string24(x, y, clNearBlack, PanelColor, 	"区域号   配置     简称           二次码");
	else TextRender_string24(x, y, clNearBlack, PanelColor, 			"ZoneID   Config   Brief          Aux ID");
	TopInx = ListPage * 8;
	for(i=0,z=TopInx; (i<8) && (z<dAreaCountMax); i++, z++){
		aClr = clNearWhite;
		x = Lv.Panel.Left+6;
		y+=28;
		TextDigitRender4Right24(x, y, aClr, PanelColor, z+1);
		x+=96;
		
		sAddr = dAddrSdRam_4Area + (z * 64);
		Idf = *(volatile unsigned int *)(sAddr);
		if(Idf == 0xA8A7A6A5){
			if(LanguageEnCn==0)TextRender_string24(x, y, clNearBlack, PanelColor, 	"已配置");
			else TextRender_string24(x, y, clNearBlack, PanelColor, 			" Yes  ");
			
			//Show ShortName
			for(int g=0; g<20;g++)ListText[g] =0;
			sAddr = dAddrSdRam_4Area + (z * 64) + 16;
			for(int g=0; g<16; g++){
				ListText[g] = *(volatile unsigned char *)(sAddr +g);
			}
			x+=96;
			TextRender_string24(x, y, clNearBlack, PanelColor, ListText);
			
			//Show UserCode
			for(int g=0; g<20;g++)ListText[g] =0;
			sAddr = dAddrSdRam_4Area + (z * 64) + 4;
			for(int g=0; g<8; g++){
				ListText[g] = *(volatile unsigned char *)(sAddr +g);
			}
			x+=200;
			TextRender_string24(x, y, clNearBlack, PanelColor, ListText);
		}else{
			TextRender_string24(x, y, clNearBlack, PanelColor, " ----");
		}
	}	
}

void TAreaCfg::sRecoverSetDefault()
{
	unsigned int fAddr, sAddr, GetSta;
	unsigned int BlockX, PageX, OffSet;
	
	SysLoopTick = 0;
	do{
		sDelayMs(3);
		GetSta = nor_get_busy_state(dFlash4AreaChip);
	}while(GetSta);
	fAddr = dFlash4Area;
	nor_erase_sector_64k(dFlash4AreaChip,fAddr);
	sDelayMs(400);
	
	SysLoopTick = 0;
	do{
		sDelayMs(3);
		GetSta = nor_get_busy_state(dFlash4AreaChip);
	}while(GetSta);

	Record.Read4Area();
}




