#include "Maintain.h"
#include "Runtime.h"

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

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

const char cWeek[7][8] = {
	"星期一",
	"星期二",
	"星期三",
	"星期四",
	"星期五",
	"星期六",
	"星期天",
};

void TMaintain::Init()
{
	Tag.Reset();
	SaveSta = 0;
	IsPermission =0;
}

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

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

void TMaintain::DrawSelf(void)
{
	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(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "工程->维保提醒");
		TextRender_string24(Lv.Grid.Left[0] + 6, Lv.Grid.Top[0] +8, clNearWhite,  "工程名称");
		TextRender_string24(Lv.Grid.Left[0] + 520, Lv.Grid.Top[0] +8, clNearWhite,  "运行时长:");
		TextRender_string24(Lv.Grid.Left[0] + 698, Lv.Grid.Top[0] +8, clNearWhite,  "天");
		TextRender_string24(Lv.Grid.Left[0] + 746, Lv.Grid.Top[0] +8, clNearWhite,  "小时");
		TextRender_string24(Lv.Grid.Left[1] + 6, Lv.Grid.Top[1] +10, clNearWhite,  "维保提醒");
		TextRender_string24(Lv.Grid.Left[1] + 46, Lv.Grid.Top[1] +50, clNearWhite,  "维保间隔时间");
		TextRender_string24(Lv.Grid.Left[1] + 46, Lv.Grid.Top[1] +90, clNearWhite,  "定时维保");
		TextRender_string24(Lv.Grid.Left[1] + 200, Lv.Grid.Top[1] +90, clNearWhite,  "月");
		TextRender_string24(Lv.Grid.Left[1] + 260, Lv.Grid.Top[1] +90, clNearWhite,  "日");
		TextRender_string24(Lv.Grid.Left[1] + 350, Lv.Grid.Top[1] +90, clNearWhite,  "月");
		TextRender_string24(Lv.Grid.Left[1] + 410, Lv.Grid.Top[1] +90, clNearWhite,  "日");
		TextRender_string24(Lv.Grid.Left[1] + 500, Lv.Grid.Top[1] +90, clNearWhite,  "月");
		TextRender_string24(Lv.Grid.Left[1] + 560, Lv.Grid.Top[1] +90, clNearWhite,  "日");
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 6, 120, 30, 7, clNearWhite, "完成维保", 14, clNearBlack);
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "Project->Maintenance Reminder");
		TextRender_string24(Lv.Grid.Left[0] + 6, Lv.Grid.Top[0] +8, clNearWhite,  "PrjName");
		TextRender_string24(Lv.Grid.Left[0] + 520, Lv.Grid.Top[0] +8, clNearWhite,  "Duration:");
		TextRender_string24(Lv.Grid.Left[0] + 698, Lv.Grid.Top[0] +8, clNearWhite,  "D");
		TextRender_string24(Lv.Grid.Left[0] + 746, Lv.Grid.Top[0] +8, clNearWhite,  "Hour");
		TextRender_string24(Lv.Grid.Left[1] + 6, Lv.Grid.Top[1] +10, clNearWhite,  "Reminder");
		TextRender_string24(Lv.Grid.Left[1] + 46, Lv.Grid.Top[1] +50, clNearWhite,  "TimeInterval");
		TextRender_string24(Lv.Grid.Left[1] + 46, Lv.Grid.Top[1] +90, clNearWhite,  "On Time ");
		TextRender_string24(Lv.Grid.Left[1] + 200, Lv.Grid.Top[1] +90, clNearWhite,  "M");
		TextRender_string24(Lv.Grid.Left[1] + 260, Lv.Grid.Top[1] +90, clNearWhite,  "D");
		TextRender_string24(Lv.Grid.Left[1] + 350, Lv.Grid.Top[1] +90, clNearWhite,  "M");
		TextRender_string24(Lv.Grid.Left[1] + 410, Lv.Grid.Top[1] +90, clNearWhite,  "D");
		TextRender_string24(Lv.Grid.Left[1] + 500, Lv.Grid.Top[1] +90, clNearWhite,  "M");
		TextRender_string24(Lv.Grid.Left[1] + 560, Lv.Grid.Top[1] +90, clNearWhite,  "D");
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 6, 120, 30, 7, clNearWhite, "Complete", 14, clNearBlack);
	}
	TextDigitRender5Right24(Lv.Grid.Left[0] + 636, Lv.Grid.Top[0] +8, clNearWhite, TimeSecHasRun / 24);
	TextDigitRender2Right24(Lv.Grid.Left[0] + 722, Lv.Grid.Top[0] +8, clNearWhite, TimeSecHasRun % 24);	
	TTriangleLeftRight::sDrawRightByHight(Lv.Grid.Left[1] + 252, Lv.Grid.Top[1] +52, 20, clNearWhite);
	
	ShowLastMaintain();
	aPanel->ReDrawItems();
	ShowIfCustomTime();
}

void TMaintain::Show(void)
{	
	RenderBackGround();
	DrawSelf();
}

void TMaintain::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}else
	if(Prm == 1){
		//RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], ContentColor);
		RectFillRender(Lv.Grid.Left[3] +200 -10, Lv.Grid.Top[3] +16 -10, Lv.Grid.Left[3] +200 -10 + 420, Lv.Grid.Top[3] +16 -10 + 150, ContentColor);
		DrawSelf();
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}


//______________________________________________________________________________________________________________________________
void TMaintain::ClearSelected(void)
{
	unsigned int i;
	for(i=1; i<8;i++)aPanel->Edit[i].Selected = 0;
	if(Tag.Group == 0){
		switch(Tag.Inx0){
			case 0:
				TRoundSquare::sSelected(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 52, dWWidth, dWHeigh, 4, ItemColor);
				break;
			case 1:
				aPanel->Edit[1].Show();
				break;
			default:

				break;
		}	
	}else
	if(Tag.Group == 1){
		switch(Tag.Inx1){
			case 0:
				TRoundSquare::sSelected(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 92, dWWidth, dWHeigh, 4, ItemColor);
				break;
			case 1:
				aPanel->Edit[2].Show();
				break;
			case 2:
				aPanel->Edit[3].Show();
				break;
			case 3:
				aPanel->Edit[4].Show();
				break;
			case 4:
				aPanel->Edit[5].Show();
				break;
			case 5:
				aPanel->Edit[6].Show();
				break;
			case 6:
				aPanel->Edit[7].Show();
				break;
		}
	}else
	if(Tag.Group == 2){
		if(LanguageEnCn==0)TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 6, 120, 30, 7, clNearWhite, "完成维保", 14, clNearBlack);
		else TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 6, 120, 30, 7, clNearWhite, "Complete", 14, clNearBlack);
	}
}


void TMaintain::Check4Selected(void)
{
	unsigned int i;
	for(i=1; i<8;i++)aPanel->Edit[i].Selected = 0;
	if(Tag.Group == 0){
		switch(Tag.Inx0){
			case 0:
				TRoundSquare::sSelected(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 52, dWWidth, dWHeigh, 4, clNearWhite);
				break;
			case 1:
				aPanel->Edit[1].Selected = 1;
				if(LanguageEnCn==0){
					if(Setting.IsHalfYear)aPanel->Edit[1].Str.FromStr("半年");
					else aPanel->Edit[1].Str.FromStr("一年");
				}else{
					if(Setting.IsHalfYear)aPanel->Edit[1].Str.FromStr("Half");
					else aPanel->Edit[1].Str.FromStr("Year");
				}
				aPanel->Edit[1].Show();
				break;
			default:

				break;
		}	
	}else
	if(Tag.Group == 1){
		switch(Tag.Inx1){
			case 0:
				TRoundSquare::sSelected(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 92, dWWidth, dWHeigh, 4, clNearWhite);
				break;
			case 1:
				aPanel->Edit[2].Selected = 1;
				aPanel->Edit[2].SetPstTail();
				aPanel->Edit[2].Show();
				break;
			case 2:
				aPanel->Edit[3].Selected = 1;
				aPanel->Edit[3].SetPstTail();
				aPanel->Edit[3].Show();
				break;
			case 3:
				aPanel->Edit[4].Selected = 1;
				aPanel->Edit[4].SetPstTail();
				aPanel->Edit[4].Show();
				break;
			case 4:
				aPanel->Edit[5].Selected = 1;
				aPanel->Edit[5].SetPstTail();
				aPanel->Edit[5].Show();
				break;
			case 5:
				aPanel->Edit[6].Selected = 1;
				aPanel->Edit[6].SetPstTail();
				aPanel->Edit[6].Show();
				break;
			case 6:
				
				aPanel->Edit[7].Selected = 1;
				aPanel->Edit[7].SetPstTail();
				aPanel->Edit[7].Show();
				break;	
		}
	}else
	if(Tag.Group == 2){
		if(LanguageEnCn==0)TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 6, 120, 30, 7, clBlue, "完成维保", 14, clNearWhite);
		else TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 6, 120, 30, 7, clBlue, "Complete", 14, clNearWhite);
	}
}

void TMaintain::ShowIfCustomTime()
{
	if(Setting.IsCustomTime){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] + 52, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] + 92, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}else{
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] + 52, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] + 92, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}
}

void TMaintain::ShowMth()
{
	unsigned char x;
	auto show = [&x]() {
		x =0;
	};
	show();
}

void TMaintain::ShowVision()
{

}

void TMaintain::ShowSound()
{

}

void TMaintain::DecIndex()
{

}

void TMaintain::IncIndex()
{

}

void TMaintain::PermissionCmdReturnDraw()
{
	Check4Selected();
	RectFillRender(Lv.Grid.Left[3] +200 -10, Lv.Grid.Top[3] +16 -10, Lv.Grid.Left[3] +200 -10 + 420, Lv.Grid.Top[3] +16 -10 + 150, ContentColor);
}

//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TMaintain::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();
		}
	}else{
		switch(aKey){
			case VK_DELETE: 
			case  VK_0:	
			case  VK_1:
			case  VK_2:
			case  VK_3:
			case  VK_4:
			case  VK_5:
			case  VK_6:	
			case  VK_7:
			case  VK_8:
			case  VK_9:
			case  VK_LEFT:
			case  VK_RIGHT:
				if(Tag.Group == 0){
					if(Tag.Inx0 == 0){
						if(aKey == VK_RIGHT){
							ClearSelected();
							Tag.Inx0 = 1;
							Check4Selected();
						}else{
							if(Setting.IsCustomTime) Setting.IsCustomTime = 0; else Setting.IsCustomTime = 1;
							ShowIfCustomTime();
						}
					}else
					if(Tag.Inx0 == 1){
						if(aKey == VK_LEFT){
							ClearSelected();
							Tag.Inx0 = 0;
							Check4Selected();
						}else{
							if(Setting.IsHalfYear)Setting.IsHalfYear = 0; else Setting.IsHalfYear =1;
							if(LanguageEnCn==0){
								if(Setting.IsHalfYear)aPanel->Edit[1].Str.FromStr("半年");
								else aPanel->Edit[1].Str.FromStr("一年");
							}else{
								if(Setting.IsHalfYear)aPanel->Edit[1].Str.FromStr("Half");
								else aPanel->Edit[1].Str.FromStr("Year");	
							}
							aPanel->Edit[1].Show();
						}
					}
				}else
				if(Tag.Group == 1){
					if(Tag.Inx1 == 0){
						if(VK_RIGHT == aKey){
							ClearSelected();
							Tag.Inx1 = 1;
							Check4Selected();
						}else{
							if(Setting.IsCustomTime) Setting.IsCustomTime = 0; else Setting.IsCustomTime = 1;
							ShowIfCustomTime();
						}
					}else{
						if(Tag.Inx1 == 1){
							if(VK_LEFT == aKey){
								if(aPanel->Edit[2].Pst < 0){
									ClearSelected();
									Tag.Inx1 = 0;
									Check4Selected();
								}else{
									aPanel->Edit[2].KeyIn(aKey);
									aPanel->Edit[2].Show();
									aPanel->Edit[2].ShowCursor();
								}
							}else
							if(VK_RIGHT == aKey){
								if(aPanel->Edit[2].Pst > 0){
									ClearSelected();
									Tag.Inx1 = 2;
									Check4Selected();
								}else{
									aPanel->Edit[2].KeyIn(aKey);
									aPanel->Edit[2].Show();
									aPanel->Edit[2].ShowCursor();
								}
							}else{
								aPanel->Edit[2].KeyIn(aKey);
								aPanel->Edit[2].Show();
								aPanel->Edit[2].ShowCursor();
							}
						}else
						if(Tag.Inx1 < 7){
							if(VK_LEFT == aKey){
								if(aPanel->Edit[Tag.Inx1 +1].Pst < 0){
									ClearSelected();
									Tag.Inx1 --;
									Check4Selected();
								}else{
									aPanel->Edit[Tag.Inx1 +1].KeyIn(aKey);
									aPanel->Edit[Tag.Inx1 +1].Show();
									aPanel->Edit[Tag.Inx1 +1].ShowCursor();
								}
							}else
							if(VK_RIGHT == aKey){
								if( (aPanel->Edit[Tag.Inx1 +1].Pst > 0) && (Tag.Inx1 < 6) ){
									ClearSelected();
									Tag.Inx1 ++;
									Check4Selected();
								}else{
									aPanel->Edit[Tag.Inx1 +1].KeyIn(aKey);
									aPanel->Edit[Tag.Inx1 +1].Show();
									aPanel->Edit[Tag.Inx1 +1].ShowCursor();
								}
							}else{
								aPanel->Edit[Tag.Inx1 +1].KeyIn(aKey);
								aPanel->Edit[Tag.Inx1 +1].Show();
								aPanel->Edit[Tag.Inx1 +1].ShowCursor();
							}
						}
					}
				}
				break;
			case VK_UP:
				if(Tag.Group == 1){
					ClearSelected();
					Tag.Group =0;
					Check4Selected();
				}else
				if(Tag.Group == 2){
					ClearSelected();
					Tag.Group =1;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(Tag.Group == 0){
					ClearSelected();
					Tag.Group =1;
					Check4Selected();
				}else
				if(Tag.Group == 1){
					ClearSelected();
					Tag.Group =2;
					Check4Selected();
				}
				break;
			case VK_F1:
				if(Tag.Group == 0){
					if(Tag.Inx0 == 0){
						if(Setting.IsCustomTime) Setting.IsCustomTime = 0; else Setting.IsCustomTime = 1;
						ShowIfCustomTime();
					}else
					if(Tag.Inx0 == 1){
						if(Setting.IsHalfYear)Setting.IsHalfYear = 0; else Setting.IsHalfYear =1;
						if(LanguageEnCn==0){
							if(Setting.IsHalfYear)aPanel->Edit[1].Str.FromStr("半年");
							else aPanel->Edit[1].Str.FromStr("一年");
						}else{
							if(Setting.IsHalfYear)aPanel->Edit[1].Str.FromStr("Half");
							else aPanel->Edit[1].Str.FromStr("Year");	
						}
						aPanel->Edit[1].Show();
					}
				}else
				if(Tag.Group == 1){
					if(Tag.Inx1 == 0){
						if(Setting.IsCustomTime) Setting.IsCustomTime = 0; else Setting.IsCustomTime = 1;
						ShowIfCustomTime();
					}
				}
				break;
			case VK_EXECUTE:
				//Save Data Here
				if(Tag.Group < 2){
					if(SystemPermission >1){
						PermissionCmd = 0;
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						PermissionCmd = 0;
						PmsIntervene4Gui.Show4Gui(Lv.Grid.Left[3] +200, Lv.Grid.Top[3] +16, 400, 130, 2);
					}
				}else
				if(Tag.Group == 2){
					if(SystemPermission >1){
						PermissionCmd = 1;
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						PermissionCmd = 1;
						PmsIntervene4Gui.Show4Gui(Lv.Grid.Left[3] +200, Lv.Grid.Top[3] +16, 400, 130, 2);
					}
				}
				break;
			case VK_RETURN:
				aMsg = guiMsgReturn;
				break;
		}
		if( (aKey != VK_EXECUTE) && (aKey != VK_RETURN) ){
			ShowSta(0);
		}
	}
	
	return aMsg;	
}

void TMaintain::InitPanel(void)
{
	int i;
	aPanel->Init(400,400,10,10,0,0,clFrmFace, bvRaised);
	
	aPanel->Edit[0].Init(Lv.Grid.Left[0] +104, Lv.Grid.Top[0] + 2, 400, 	32, 	0,0,	1,ItemColor);
	
	aPanel->Edit[1].Init(Lv.Grid.Left[1] +194, Lv.Grid.Top[1] + 46,54,	30,	0,0,	1,ItemColor);
	
	aPanel->Edit[2].Init(Lv.Grid.Left[1] +164,Lv.Grid.Top[1] + 86,32,30,0,0,1,ItemColor);
	aPanel->Edit[3].Init(Lv.Grid.Left[1] +224,Lv.Grid.Top[1] + 86,32,30,0,0,1,ItemColor);
	aPanel->Edit[4].Init(Lv.Grid.Left[1] +314,Lv.Grid.Top[1] + 86,32,30,0,0,1,ItemColor);
	aPanel->Edit[5].Init(Lv.Grid.Left[1] +374,Lv.Grid.Top[1] + 86,32,30,0,0,1,ItemColor);
	aPanel->Edit[6].Init(Lv.Grid.Left[1] +464,Lv.Grid.Top[1] + 86,32,30,0,0,1,ItemColor);
	aPanel->Edit[7].Init(Lv.Grid.Left[1] +524,Lv.Grid.Top[1] + 86,32,30,0,0,1,ItemColor);
	
	for(i=0; i<1; i++){
		aPanel->Edit[i].IsShowCursor = 0;
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
	}
	for(i=1; i<2; i++){
		aPanel->Edit[i].IsShowCursor = 0;
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
	}
	for(i=2; i<8; i++){
		aPanel->Edit[i].IsShowCursor = 1;
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
	}
	for(i=1; i<8;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(2);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.FromStr("20");
		aPanel->Edit[2].SetPstTail();
	}
}

void TMaintain::ExtLoadData()
{
	ExtLoadSetting.IsCustomTime = MaintainData.fDa.IsCustomTime;
	ExtLoadSetting.IsHalfYear = MaintainData.fDa.IsHalfYear;
	ExtLoadSetting.Month[0] = MaintainData.fDa.Month0;
	ExtLoadSetting.Month[1] = MaintainData.fDa.Month1;
	ExtLoadSetting.Month[2] = MaintainData.fDa.Month2;
	ExtLoadSetting.Month[3] = MaintainData.fDa.Month3;
	ExtLoadSetting.Day[0] = MaintainData.fDa.Day0;
	ExtLoadSetting.Day[1] = MaintainData.fDa.Day1;
	ExtLoadSetting.Day[2] = MaintainData.fDa.Day2;
	ExtLoadSetting.Day[3] = MaintainData.fDa.Day3;	
}

void TMaintain::LoadData()
{
	Setting.IsCustomTime = MaintainData.fDa.IsCustomTime;
	Setting.IsHalfYear = MaintainData.fDa.IsHalfYear;
	Setting.Month[0] = MaintainData.fDa.Month0;
	Setting.Month[1] = MaintainData.fDa.Month1;
	Setting.Month[2] = MaintainData.fDa.Month2;
	Setting.Month[3] = MaintainData.fDa.Month3;
	Setting.Day[0] = MaintainData.fDa.Day0;
	Setting.Day[1] = MaintainData.fDa.Day1;
	Setting.Day[2] = MaintainData.fDa.Day2;
	Setting.Day[3] = MaintainData.fDa.Day3;
	
	for(int i=0; i<32; i++){
		aPanel->Edit[0].Str.Text[i] = PrjName[i];
	}
	aPanel->Edit[0].Str.Text[32] = '\0';
	aPanel->Edit[0].Str.Text[33] = '\0';
	
	if(LanguageEnCn==0){
		if(Setting.IsHalfYear)aPanel->Edit[1].Str.FromStr("半年");
		else aPanel->Edit[1].Str.FromStr("一年");
	}else{
		if(Setting.IsHalfYear)aPanel->Edit[1].Str.FromStr("Half");
		else aPanel->Edit[1].Str.FromStr("Year");
	}
	
	aPanel->Edit[2].Str.FromUInt2Dg(Setting.Month[0]);
	aPanel->Edit[4].Str.FromUInt2Dg(Setting.Month[1]);
	aPanel->Edit[6].Str.FromUInt2Dg(Setting.Month[2]);
	
	aPanel->Edit[3].Str.FromUInt2Dg(Setting.Day[0]);
	aPanel->Edit[5].Str.FromUInt2Dg(Setting.Day[1]);
	aPanel->Edit[7].Str.FromUInt2Dg(Setting.Day[2]);

	ShowIfCustomTime();
	aPanel->ReDrawItems();
	
	Check4Selected();
}

void TMaintain::WriteData()
{
	Setting.Month[0] = aPanel->Edit[2].Str.ToInteger();
	Setting.Month[1] = aPanel->Edit[4].Str.ToInteger();
	Setting.Month[2] = aPanel->Edit[6].Str.ToInteger();
	Setting.Day[0] = aPanel->Edit[3].Str.ToInteger();
	Setting.Day[1] = aPanel->Edit[5].Str.ToInteger();
	Setting.Day[2] = aPanel->Edit[7].Str.ToInteger();
	
	Setting.Month[3] = 2;
	Setting.Day[3] = 2;
	
	ToWriteBytes[0] = Setting.IsCustomTime;
	ToWriteBytes[1] = Setting.IsHalfYear;
	ToWriteBytes[2] = 1;
	ToWriteBytes[3] = 1;
	ToWriteBytes[4] = Setting.Month[0];
	ToWriteBytes[5] = Setting.Day[0];
	ToWriteBytes[6] = Setting.Month[1];
	ToWriteBytes[7] = Setting.Day[1];
	ToWriteBytes[8] = Setting.Month[2];
	ToWriteBytes[9] = Setting.Day[2];
	ToWriteBytes[10] = Setting.Month[3];
	ToWriteBytes[11] = Setting.Day[3];
	
	Record.WriteMaintain(ToWriteBytes);
	Record.ReadMaintain();
	
	int WriteOk = 1;
	for(int i=0; i<12; i++){
		if(MaintainData.D8[i] != ToWriteBytes[i]){
			WriteOk = 0; 
			break;
		}
	}
	
	if(WriteOk){
		ShowSta(1);
	}else{
		ShowSta(-1);
	}
}

void TMaintain::sRecoverSetDefault()
{
	unsigned char tWb[20];
	tWb[0] = 0;	//Setting.IsCustomTime;
	tWb[1] = 1;	//Setting.IsHalfYear;
	tWb[2] = 1;
	tWb[3] = 1;
	tWb[4] = 1;	//Setting.Month[0];
	tWb[5] = 1;	//Setting.Day[0];
	tWb[6] = 1;	// Setting.Month[1];
	tWb[7] = 1;	//Setting.Day[1];
	tWb[8] = 1;	//Setting.Month[2];
	tWb[9] = 1;	//Setting.Day[2];
	tWb[10] = 1;	//Setting.Month[3];
	tWb[11] = 1;	//Setting.Day[3];
	tWb[12] = 0;	
	tWb[13] = 0;	
	tWb[14] = 0;	
	tWb[15] = 0;	
	tWb[16] = 0;	
	tWb[17] = 0;	
	
	Record.WriteMaintain(tWb);
	Record.ReadMaintain();
	
	LastMaintain.Day = 0x01;
	LastMaintain.Month = 0x01;
	LastMaintain.Year = 0x20;
	LastMaintain.Cent = 0x20;
	(void)Record.WriteLastMaintainTime();
	
	Check4MaintainToShow();
	if(IsSelfChecking == 0)
		StateBarReflashMaintain();
}

void TMaintain::ShowLastMaintain()
{
	if(LanguageEnCn==0)TextRender_string24(Lv.Grid.Left[2] +280, Lv.Grid.Top[2] + 6, clNearWhite, ItemColor, 	"上次维保时间:");
	else TextRender_string24(Lv.Grid.Left[2] +280, Lv.Grid.Top[2] + 6, clNearWhite, ItemColor, 			"   Last Time:");
	
	ToWriteBytes[0] = 2 + '0';
	ToWriteBytes[1] = 0 + '0';
	ToWriteBytes[2] = LastMaintain.Year / 16 % 10 + '0';
	ToWriteBytes[3] = LastMaintain.Year % 16 % 10 + '0';
	ToWriteBytes[4] = '/';
	ToWriteBytes[5] = LastMaintain.Month / 16 % 10 + '0';
	ToWriteBytes[6] = LastMaintain.Month % 16 % 10 + '0';
	ToWriteBytes[7] = '/';
	ToWriteBytes[8] = LastMaintain.Day / 16 % 10 + '0';
	ToWriteBytes[9] = LastMaintain.Day % 16 % 10 + '0';
	ToWriteBytes[10] = ' ';
	ToWriteBytes[11] = '\0';
	ToWriteBytes[12] = '\0';
	
	TextRender_string24(Lv.Grid.Left[2] +280 + 156, Lv.Grid.Top[2] + 6, clNearWhite, ItemColor, ToWriteBytes);
	
	if(LanguageEnCn!=0){
		TextRender_string24(Lv.Grid.Left[2] +580, Lv.Grid.Top[2] + 6, clNearWhite, ItemColor, "(Year/Month/Day)");
	}
}

int TMaintain::WriteLastDate()
{
	LastMaintain.Day = DateTime.Date.Split.Day;
	LastMaintain.Month = DateTime.Date.Split.Month;
	LastMaintain.Year = static_cast<unsigned char>(DateTime.Date.Split.Year & 0x00FF);
	LastMaintain.Cent = 0x20;
	
	if(0 == Record.WriteLastMaintainTime()){
		return 0;
	}else{
		//error here
		return -1;
	}
}

void TMaintain::ShowSta()
{
	if(SaveSta == 0){
		TextRender_string24(ResultSavePoint.x, ResultSavePoint.y, clNearWhite, ContentColor, "          ");
	}else
	if(SaveSta == 1){
		if(LanguageEnCn==0)TextRender_string24(ResultSavePoint.x, ResultSavePoint.y, clNearWhite, ContentColor, 	"保存成功      ");
		else TextRender_string24(ResultSavePoint.x, ResultSavePoint.y, clNearWhite, ContentColor, 			"Save OK       ");
	}else
	if(SaveSta == -1){
		if(LanguageEnCn==0)TextRender_string24(ResultSavePoint.x, ResultSavePoint.y, clNearWhite, ContentColor, 	"保存失败      ");
		else TextRender_string24(ResultSavePoint.x, ResultSavePoint.y, clNearWhite, ContentColor, 			"Failed To Save");
	}
}

void TMaintain::ShowSta(int aSta)
{
	SaveSta = aSta;
	ShowSta();
}

void TMaintain::StartUpInit()
{
	Seconeds =0;
}

void TMaintain::DoCheckPerSec()
{
	
}


void TMaintain::PermissionCmdGO()
{
	int aVal;
	unsigned char aBits;
	switch(PermissionCmd){
		case 0:
			WriteData();
			break;
		case 1:
			if(0 == WriteLastDate()){
				ShowLastMaintain();
				IsMaintainTip = 0;
				StateBarReflashMaintain();
			}else{
				ShowLastMaintain();
			}
			break;
	}
}






