#include "Frm06.h"

Frm06::Frm06(HmiApp* app, STTWidget *par, const string& id, const string& desc) : STTPage(app->getRuntime(), par, id, desc) {
	hmiApp = app;
}

Frm06::~Frm06() {
}

void Frm06::onInit() {

}

void Frm06::createParts() {
	setMainArg(0, 0, 1024, 420);
	setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 0, 0xc0c0c0);

	/*----------------------------- Panel : wMPanel5                     -----------------------------*/
	wMPanel5 = new STTPanel(p_rt, this, "wMPanel5");
	wMPanel5->setMainArg(729, 0, 294, 420, 0);
	wMPanel5->setMainStyle(0, 0x8080c0, 0xffffff, GradDir::NONE, 255, 1, 0xc0c0c0);

	/*----------------------------- Button : wMButton12                  -----------------------------*/
	wMButton12 = new STTButton(p_rt, wMPanel5, "wMButton12", 0);
	wMButton12->setMainArg(62, 219, 172, 41, false);
	wMButton12->setPressedArg("查询", 0x0, "文泉驿微米黑", 20, 0);
	wMButton12->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton12->setReleasedArg("查询", 0x0, "文泉驿微米黑", 20, 0);
	wMButton12->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton12->onEventHandler(Event::CLICKED, this, (EHandler) &Frm06::wMButton12_clk_cb);

	/*----------------------------- Button : wMButton13                  -----------------------------*/
	wMButton13 = new STTButton(p_rt, wMPanel5, "wMButton13", 0);
	wMButton13->setMainArg(62, 354, 172, 43, false);
	wMButton13->setPressedArg("导出", 0x0, "文泉驿微米黑", 20, 0);
	wMButton13->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton13->setReleasedArg("导出", 0x0, "文泉驿微米黑", 20, 0);
	wMButton13->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton13->onEventHandler(Event::CLICKED, this, (EHandler) &Frm06::wMButton13_clk_cb);

	/*----------------------------- Button : wMButton14                  -----------------------------*/
	wMButton14 = new STTButton(p_rt, wMPanel5, "wMButton14", 0);
	wMButton14->setMainArg(62, 285, 172, 46, false);
	wMButton14->setPressedArg("清除记录", 0x0, "文泉驿微米黑", 20, 0);
	wMButton14->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton14->setReleasedArg("清除记录", 0x0, "文泉驿微米黑", 20, 0);
	wMButton14->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton14->onEventHandler(Event::CLICKED, this, (EHandler) &Frm06::wMButton14_clk_cb);

	/*----------------------------- Label : wMLabel2                     -----------------------------*/
	wMLabel2 = new STTLabel(p_rt, wMPanel5, "wMLabel2");
	wMLabel2->setMainArg(98, 78, 102, 39, "起始时间", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel2->setFonts(0x0, 25, "文泉驿微米黑", 0);
	wMLabel2->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel2->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Label : wMLabel3                     -----------------------------*/
	wMLabel3 = new STTLabel(p_rt, wMPanel5, "wMLabel3");
	wMLabel3->setMainArg(98, 180, 102, 39, "结束时间", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel3->setFonts(0x0, 25, "文泉驿微米黑", 0);
	wMLabel3->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel3->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- TextNumber : wMTextNumber3           -----------------------------*/
	wMTextNumber3 = new STTTextArea(p_rt, wMPanel5, "wMTextNumber3");
	wMTextNumber3->setMainArg(42, 19, 202, 54, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMTextNumber3->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 14, 0, 4, 4, 255);
	wMTextNumber3->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMTextNumber3->setFont(0x0, 22, "文泉驿微米黑", 0);
	wMTextNumber3->bindData(_V("历史记录起始时间"));

	/*----------------------------- TextNumber : wMTextNumber4           -----------------------------*/
	wMTextNumber4 = new STTTextArea(p_rt, wMPanel5, "wMTextNumber4");
	wMTextNumber4->setMainArg(42, 116, 202, 57, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMTextNumber4->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 16, 0, 4, 4, 255);
	wMTextNumber4->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMTextNumber4->setFont(0x0, 22, "文泉驿微米黑", 0);
	wMTextNumber4->bindData(_V("历史记录结束时间"));

	/*----------------------------- Table : wMTable2                     -----------------------------*/
	wMTable2 = new STTTable(p_rt, this, "wMTable2");
	wMTable2->setMainArg(0, 0, 730, 420, "SID,序号,82;STIME,时间,221;D1,样气压力,100;D2,载气压力,100;D3,CO2浓度,98;D4,可燃气体浓度,142;D5,进样压力,100;D6,环境温度,100;D7,环境湿度,99", 10, false, 1);
	wMTable2->setHeadCellStyle(0xf0f0f0, 0xffffff, 0, 1, 0xd0d0d0, 5, 5, 0, 0, 0x0, 16, "文泉驿微米黑", 0);
	wMTable2->setSelCellStyle(0x1a2b3, 0xffffff, 0, 1, 0xd0d0d0, 5, 5, 0, 0, 0x0, 16, "文泉驿微米黑", 0);
	wMTable2->setDataCellStyle(0xffffff, 0xffffff, 0, 1, 0xd0d0d0, 5, 5, 0, 0, 0x0, 16, "文泉驿微米黑", 0);
	wMTable2->setDataSource("1", "", true);
	wMTable2->bindPageCnt(vm->get("历史记录关联总页码"));
	wMTable2->bindPageIndex(vm->get("历史记录关联页码"));
	wMTable2->setDateMode(2);

	/*----------------------------- Button : wMButton15                  -----------------------------*/
	wMButton15 = new STTButton(p_rt, this, "wMButton15", 0);
	wMButton15->setMainArg(27, 348, 129, 40, false);
	wMButton15->setPressedArg("最前页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton15->setPressedStyle(8, 255, 0x808080, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton15->setReleasedArg("最前页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton15->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton15->onEventHandler(Event::CLICKED, this, (EHandler) &Frm06::wMButton15_clk_cb);

	/*----------------------------- Button : wMButton16                  -----------------------------*/
	wMButton16 = new STTButton(p_rt, this, "wMButton16", 0);
	wMButton16->setMainArg(209, 348, 129, 40, false);
	wMButton16->setPressedArg("前一页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton16->setPressedStyle(8, 255, 0x808080, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton16->setReleasedArg("前一页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton16->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton16->onEventHandler(Event::CLICKED, this, (EHandler) &Frm06::wMButton16_clk_cb);

	/*----------------------------- Button : wMButton17                  -----------------------------*/
	wMButton17 = new STTButton(p_rt, this, "wMButton17", 0);
	wMButton17->setMainArg(391, 348, 129, 40, false);
	wMButton17->setPressedArg("后一页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton17->setPressedStyle(8, 255, 0x808080, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton17->setReleasedArg("后一页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton17->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton17->onEventHandler(Event::CLICKED, this, (EHandler) &Frm06::wMButton17_clk_cb);

	/*----------------------------- Button : wMButton18                  -----------------------------*/
	wMButton18 = new STTButton(p_rt, this, "wMButton18", 0);
	wMButton18->setMainArg(573, 348, 129, 40, false);
	wMButton18->setPressedArg("最后页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton18->setPressedStyle(8, 255, 0x808080, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton18->setReleasedArg("最后页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton18->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton18->onEventHandler(Event::CLICKED, this, (EHandler) &Frm06::wMButton18_clk_cb);

	/*called after widget created.*/

	return; //createParts END
}

void Frm06::onLoad() {

}

bool Frm06::onClosing() {
	return true;
}

void Frm06::onDispose() {

}

void Frm06::wMButton15_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton15（最前页）的点击事件*/
	wMTable2->navFirst();
}

void Frm06::wMButton16_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton16（前一页）的点击事件*/
	wMTable2->navBack();
}

void Frm06::wMButton17_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton17（后一页）的点击事件*/
	wMTable2->navNext();
}

void Frm06::wMButton18_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton18（最后页）的点击事件*/
	wMTable2->navLast();
}

void Frm06::wMButton12_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton12（查询）的点击事件*/
	// 格式为yyyyMMdd
	int startDate=0,endDate=0;
	vm->getInt("历史记录起始时间", startDate);
	vm->getInt("历史记录结束时间", endDate);
	STTTable* widget = wMTable2;
	if (startDate == 0 && endDate == 0){
		// 可以约定做判断 这里默认0 (默认值)为未选择查询 其余状况默认为正确的日期数据格式
		refineSample(widget,0);
	}else if (startDate != 0 && endDate == 0){
		// 修改 yyyyMMdd数据为时间戳字符串传入
		string start = to_string(DateUtil::getMsTimeStamp2(startDate, 0, 0, 0, 0));
		refineSample(widget,1,start);
	}else if (endDate == 0 && endDate != 0){
		// 修改 yyyyMMdd数据为时间戳字符串传入
		string end = to_string(DateUtil::getMsTimeStamp2(endDate, 0, 0, 0, 0));
		refineSample(widget,2,"",end);
	}else{
		string start = to_string(DateUtil::getMsTimeStamp2(startDate, 0, 0, 0, 0));
		string end = to_string(DateUtil::getMsTimeStamp2(endDate, 0, 0, 0, 0));
		refineSample(widget,3,start,end);
	}
}

void Frm06::wMButton14_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton14（清除记录）的点击事件*/
	// 格式为yyyyMMdd
	int startDate=0,endDate=0;
	vm->getInt("历史记录起始时间", startDate);
	vm->getInt("历史记录结束时间", endDate);

	if (startDate == 0 || endDate == 0){
		SampleMgr::getInstance(p_rt)->deleteDB("", 20200101, 20301231);
	}else{
		// 删除指定日期数据
		SampleMgr::getInstance(p_rt)->deleteDB("", startDate, endDate);
	}
	TTableView* tv = (TTableView*)wMTable2->getData();
    tv->drawData(true);
}

void Frm06::wMButton13_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton13（导出）的点击事件*/
	// 格式为yyyyMMdd
	int startDate=0,endDate=0;
	vm->getInt("历史记录起始时间", startDate);
	vm->getInt("历史记录结束时间", endDate);

	if (startDate == 0 || endDate == 0){
		// 导出全部数据 到U盘sample目录 也可以是其他路径
		SampleMgr::getInstance(p_rt)->exportCSV("",0,99999999,"sample");
	}else{
		SampleMgr::getInstance(p_rt)->exportCSV("",startDate, endDate,"sample");
	}
}

void Frm06::refineSample(STTTable* widget,int mode,const string& startDate,const string& endDate) {
	string tbl = "";
	if (mode == 0){
		// 查询全部
		tbl = StringUtil::snprintf("(SELECT * FROM SAMPLE_1) TT");
	}else if (mode == 1){
		// 带起始日期(包括起始日期)
		tbl = StringUtil::snprintf("(SELECT * FROM SAMPLE_1 WHERE STIME>='%s') TT", startDate.c_str());
	}else if (mode == 2){
		// 带终止日期(包括终止日期)
		tbl = StringUtil::snprintf("(SELECT * FROM SAMPLE_1 WHERE STIME<='%s') TT", endDate.c_str());
	}else if (mode == 3){
		tbl = StringUtil::snprintf("(SELECT * FROM SAMPLE_1 WHERE STIME>='%s' AND STIME<'%s') TT", startDate.c_str(), endDate.c_str());
	}
    TTableView* tv = (TTableView*)widget->getData();
    tv->refine(tbl, "");
    tv->drawData(true);
}
