#ifndef __HCM4A_CalcFS4A_ctrlCalc_h_
#define __HCM4A_CalcFS4A_ctrlCalc_h_

#include <Core/Hash.h>
#include <GridCtrl/GridCtrl.h>

#include "dtCalc.h"
#include "ctrlHcmPack.h"
#include "ctrlGIS.h"

using namespace Upp;




struct VSinglCalcCtrl{
	typedef VSinglCalcCtrl CLASSNAME;
	WithLayCalcResV<ParentCtrl> layCalcRes;	// layout to place the control
	GridCtrl grCalcRes;
	void Clear();
	void Setup();
	void Load(const TblSingleRec& tblSingleRec);
	int GetCurSelID();
	VSinglCalcCtrl();
};
VSinglCalcCtrl::VSinglCalcCtrl(){
	CtrlLayout(layCalcRes);
	layCalcRes.Add(grCalcRes.SizePos());
	Setup();
}

void VSinglCalcCtrl::Setup(){
	grCalcRes.Clear(true);
	grCalcRes.Header(false);
	grCalcRes.ColoringMode(1);
	grCalcRes.OddColor().ColoringMode(1);
	grCalcRes.EvenColor().ColoringMode(2);
	grCalcRes.Indicator(); //---------------
	grCalcRes.AddColumn("T_NAME", String("")).Min(250).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).HeaderAlignCenter().SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("T_VALUE", String("")).Min(250).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).HeaderAlignCenter().SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.RejectNullRow();
	grCalcRes.Navigating();
	grCalcRes.SetToolBar();
	grCalcRes.Clipboard();
	grCalcRes.Absolute();
	grCalcRes.SetRowHeight(0, Single<ItemGrid>().headFont.GetHeight()+10); // header height = GridCtrl.height + 10
	// grCalcRes.WhenLeftDouble
}

void VSinglCalcCtrl::Load(const TblSingleRec& tblSingleRec){
	grCalcRes.Clear();
	for(int i=0; i<tblSingleRec.GetNameValList().GetCount(); i++){
		grCalcRes.Add();
		grCalcRes("T_NAME") = tblSingleRec.GetNameValList()[i].name;
		grCalcRes("T_VALUE") = tblSingleRec.GetNameValList()[i].val;
		grCalcRes.SetRowHeight(grCalcRes.GetRowCount(), Single<ItemGrid>().itemFont.GetHeight()*1.5);// row height = GridCtrl.height * 1.5
	}
}
int VSinglCalcCtrl::GetCurSelID(){
	Value npp = grCalcRes("T_No");
	if(!npp.IsNull()){
		return int(npp);
	}
	return -1;
}
void VSinglCalcCtrl::Clear(){
	grCalcRes.Clear();
}
//!---------------------------------------------------------------------------------------


struct HSinglCalcCtrl{
	typedef HSinglCalcCtrl CLASSNAME;
	WithLayCalcResH<ParentCtrl> layCalcRes;	// layout to place the control
	GridCtrl grCalcRes;
	Callback fcbExport;
	Callback fcbReport;
	void ExportAs();
	void Report();
	void GridPopMenu(Bar& bar);
	
	void Clear();
	void Setup();
	void Load(const TblSingleRec& tblSingleRec);
	void Serialize(Stream& s);
	int GetCurSelID();
	HSinglCalcCtrl();
};
HSinglCalcCtrl::HSinglCalcCtrl(){
	CtrlLayout(layCalcRes);
	layCalcRes.Add(grCalcRes.SizePos());
	Setup();
}

void HSinglCalcCtrl::Setup(){
	grCalcRes.Clear(true);
	grCalcRes.ColoringMode(1);
	grCalcRes.OddColor().ColoringMode(1);
	grCalcRes.EvenColor().ColoringMode(2);
	grCalcRes.Indicator(); //---------------
	grCalcRes.AddColumn("T_No", String(t_("№"))).Min(50).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("TX_NAME", String(t_("txName"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("PRX_NAME", String(t_("prxName"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("PTX_NAME", String(t_("ptxName"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("RX_NAME", String(t_("rxName"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("T_TD", String(t_("TD"))).Min(150).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("TX_COUNTRY", String(t_("txCountry"))).Min(50).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("RX_COUNTRY", String(t_("rxCountry"))).Min(50).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("I_I", String(t_("I"))).Min(150).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("T_O_AREA", String(t_("O_AREA"))).Min(150).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("T_NFD", String(t_("NFD"))).Min(50).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("T_MD", String(t_("MD"))).Min(100).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("T_A_TOT", String(t_("A_TOT"))).Min(100).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("T_Error", String(t_("Error"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.RejectNullRow();
	grCalcRes.Navigating();
	grCalcRes.SetToolBar();
	grCalcRes.Clipboard();
	grCalcRes.Absolute();
	grCalcRes.SetRowHeight(0, Single<ItemGrid>().headFont.GetHeight()+10); // header height = GridCtrl.height + 10
	grCalcRes.WhenMenuBar = THISBACK(GridPopMenu);
}

void HSinglCalcCtrl::Load(const TblSingleRec& tblSingleRec){
	grCalcRes.Clear();
	grCalcRes.Add();
	grCalcRes("T_No") = 1;
	grCalcRes("TX_NAME") = tblSingleRec.ts_txname;
	grCalcRes("PRX_NAME") = tblSingleRec.ts_prxname;
	grCalcRes("PTX_NAME") = tblSingleRec.ts_ptxname;
	grCalcRes("RX_NAME") = tblSingleRec.ts_rxname;
	grCalcRes("TX_COUNTRY") = tblSingleRec.ts_txcountry;
	grCalcRes("RX_COUNTRY") = tblSingleRec.ts_rxcountry;
	grCalcRes("I_I") = Format("%.4f", tblSingleRec.ts_i_i);
	grCalcRes("T_O_AREA") = Format("%.4f", tblSingleRec.ts_i_o_area);
	grCalcRes("T_NFD") = Format("%.4f", tblSingleRec.ts_i_nfd);
	grCalcRes("T_MD") = Format("%.4f", tblSingleRec.ts_i_md);
	grCalcRes("T_A_TOT") = Format("%.4f", tblSingleRec.ts_i_a_tot);
	grCalcRes("T_TD") = Format("%.4f", tblSingleRec.ts_i_td);
	grCalcRes("T_Error") = appErrorMap.Get(tblSingleRec.ts_i_error).ToString(); // String() << tblSingleRec.ts_i_error << ": " << tblSingleRec.ts_msgerror;
	grCalcRes.SetRowHeight(grCalcRes.GetRowCount(), Single<ItemGrid>().itemFont.GetHeight()*1.5);// row height = GridCtrl.height * 1.5
	grCalcRes.WhenLeftDouble = THISBACK(Report);
}
int HSinglCalcCtrl::GetCurSelID(){
	Value npp = grCalcRes("T_No");
	if(!npp.IsNull()){
		return int(npp);
	}
	return -1;
}
void HSinglCalcCtrl::Clear(){
	grCalcRes.Clear();
}
void HSinglCalcCtrl::Serialize(Stream& s){
	int version = 1;
	s / version;
	grCalcRes.Serialize(s);
}
void HSinglCalcCtrl::ExportAs(){
	if(fcbExport != Null){
		fcbExport();
	}
}
void HSinglCalcCtrl::Report(){
	if(fcbReport != Null){
		fcbReport();
	}
}
void HSinglCalcCtrl::GridPopMenu(Bar& bar){
	bar.Add(t_("Report .."), Images::REPORT(), THISBACK(Report));
	bar.Separator();
	bar.Add(t_("Export to XLS .."), Images::EXPORT_XLS(), THISBACK(ExportAs));
}
//!---------------------------------------------------------------------------------------


struct HMultCalcCtrl{
	typedef HMultCalcCtrl CLASSNAME;
	WithLayCalcResH<ParentCtrl> layCalcRes;	// layout to place the control
	GridCtrl grCalcRes;
	Callback fcbExport;
	Callback fcbReport;
	void ExportAs();
	void Report();
	void GridPopMenu(Bar& bar);
	void Clear();
	void Setup();
	void Load(const Vector<TblMultRec>& multList);
	void Serialize(Stream& s);
	void ShowRecNo();
	int curRecNum;
	int GetCurSelID();
	HMultCalcCtrl();
};
HMultCalcCtrl::HMultCalcCtrl(){
	curRecNum = 0;
	CtrlLayout(layCalcRes);
	layCalcRes.Add(grCalcRes.SizePos());
	Setup();
}

void HMultCalcCtrl::Setup(){
	grCalcRes.Clear(true);
	grCalcRes.ColoringMode(1);
	grCalcRes.OddColor().ColoringMode(1);
	grCalcRes.EvenColor().ColoringMode(2);
	grCalcRes.Indicator(); //---------------
	grCalcRes.AddColumn("T_No", String(t_("№"))).Min(50).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("TX_TEST_NAME", String(t_("TX_TEST"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("PRX_TEST_NAME", String(t_("PRX_TEST"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("PTX_TEST_NAME", String(t_("PTX_TEST"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("RX_TEST_NAME", String(t_("RX_TEST"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("TX_REF_NAME", String(t_("TX_REF"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("PRX_REF_NAME", String(t_("PRX_REF"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("PTX_REF_NAME", String(t_("PTX_REF"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("RX_REF_NAME", String(t_("RX_REF"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("TX_COUNTRY", String(t_("TX_COUNTRY"))).Min(50).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("PX_COUNTRY", String(t_("PX_COUNTRY"))).Min(50).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("RX_COUNTRY", String(t_("RX_COUNTRY"))).Min(50).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("T_TD", String(t_("TD"))).Min(150).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("I_I", String(t_("I"))).Min(150).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("T_O_AREA", String(t_("O_AREA"))).Min(150).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("T_NFD", String(t_("NFD"))).Min(50).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("T_MD", String(t_("MD"))).Min(100).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("T_A_TOT", String(t_("A_TOT"))).Min(100).HeaderAlignCenter().AlignCenter().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("T_Error", String(t_("Error"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.RejectNullRow();
	grCalcRes.Navigating();
	grCalcRes.SetToolBar();
	grCalcRes.Clipboard();
	grCalcRes.Absolute();
	grCalcRes.SetRowHeight(0, Single<ItemGrid>().headFont.GetHeight()+10); // header height = GridCtrl.height + 10
	grCalcRes.WhenLeftDouble = THISBACK(Report);
	grCalcRes.WhenMenuBar = THISBACK(GridPopMenu);
}

void HMultCalcCtrl::Load(const Vector<TblMultRec>& multList){
	curRecNum = 0;
	grCalcRes.Clear();
	int cnt = 0;
	for(auto v: multList){
		cnt++;
		grCalcRes.Add();
		grCalcRes("T_No") = cnt;
	    grCalcRes("TX_TEST_NAME") = v.tl_txtestname;
	    grCalcRes("PRX_TEST_NAME") = v.tl_prxtestname;
	    grCalcRes("PTX_TEST_NAME") = v.tl_ptxtestname;
	    grCalcRes("RX_TEST_NAME") = v.tl_rxtestname;
	    grCalcRes("TX_COUNTRY") = v.tl_txcountry;
	    grCalcRes("PX_COUNTRY") = v.tl_pxcountry;
	    grCalcRes("RX_COUNTRY") = v.tl_rxcountry;
	    grCalcRes("TX_REF_NAME") = v.tl_txrefname;
	    grCalcRes("PRX_REF_NAME") = v.tl_prxrefname;
	    grCalcRes("PTX_REF_NAME") = v.tl_ptxrefname;
	    grCalcRes("RX_REF_NAME") = v.tl_rxrefname;
		grCalcRes("I_I") = Format("%.4f", v.tl_i_i);
		grCalcRes("T_O_AREA") = Format("%.4f", v.tl_i_o_area);
		grCalcRes("T_NFD") = Format("%.4f", v.tl_i_nfd);
		grCalcRes("T_MD") = Format("%.4f", v.tl_i_md);
		grCalcRes("T_A_TOT") = Format("%.4f", v.tl_i_a_tot);
		grCalcRes("T_TD") = Format("%.4f", v.tl_i_td);
		grCalcRes("T_Error") = appErrorMap.Get(v.tl_i_error).ToString();
		grCalcRes.SetRowHeight(grCalcRes.GetRowCount(), Single<ItemGrid>().itemFont.GetHeight()*1.5);// row height = GridCtrl.height * 1.5
	}
}

void HMultCalcCtrl::ShowRecNo(){
	PromptOK(String() << GetCurSelID());
}

int HMultCalcCtrl::GetCurSelID(){
	Value npp = grCalcRes("T_No");
	if(!npp.IsNull()){
		curRecNum = npp; curRecNum--;
		return curRecNum;
	}
	return -1;
}
void HMultCalcCtrl::Clear(){
	grCalcRes.Clear();
}
void HMultCalcCtrl::Serialize(Stream& s){
	int version = 1;
	s / version;
	grCalcRes.Serialize(s);
}
void HMultCalcCtrl::ExportAs(){
	if(fcbExport != Null){
		fcbExport();
	}
}
void HMultCalcCtrl::Report(){
	if(fcbReport != Null){
		fcbReport();
	}
}
void HMultCalcCtrl::GridPopMenu(Bar& bar){
	bar.Add(t_("Report .."), Images::REPORT(), THISBACK(Report));
	bar.Separator();
	bar.Add(t_("Export to XLS .."), Images::EXPORT_XLS(), THISBACK(ExportAs));
}
//!---------------------------------------------------------------------------------------

struct HTrigCalcCtrl{
	typedef HTrigCalcCtrl CLASSNAME;
	WithLayCalcTrig<ParentCtrl> layCalcRes;	// layout to place the control
	FormMap formMap;
	GridCtrl grCalcRes;
	void Clear();
	void Setup();
	void Load(const Vector<TrigerRec>& multList, const String& file);
	int GetCurSelID();
	void FillStationList(const bool isRef, const Vector<TrigerRec>& multList, Vector<StationRec>& list)const;
	void ShowMap();
	void SaveSingleCalcToXLS();
	HTrigCalcCtrl();
protected:
	String fileName;
	Vector<TrigerRec> trigList;
};
HTrigCalcCtrl::HTrigCalcCtrl(){
	CtrlLayout(layCalcRes);
	layCalcRes.mainCtrl.Add(grCalcRes.SizePos());
	Setup();
}
void HTrigCalcCtrl::Setup(){
	grCalcRes.Clear(true);
	grCalcRes.ColoringMode(1);
	grCalcRes.OddColor().ColoringMode(1);
	grCalcRes.EvenColor().ColoringMode(2);
	grCalcRes.Indicator(); //---------------
	grCalcRes.AddColumn("T_No", String(t_("№"))).Min(50).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("NAME", String(t_("NAME"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("REF", String(t_("REFERENCE"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("COUNTRIES", String(t_("COUNTRIES"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.AddColumn("Error", String(t_("Error"))).Min(150).HeaderAlignCenter().AlignLeft().SetDisplay(Single<ItemGrid>()).SetHeaderFont(Single<ItemGrid>().headFont);
	grCalcRes.RejectNullRow();
	grCalcRes.Navigating();
	grCalcRes.SetToolBar();
	grCalcRes.Clipboard();
	grCalcRes.Absolute();
	grCalcRes.SetRowHeight(0, Single<ItemGrid>().headFont.GetHeight()+10); // header height = GridCtrl.height + 10
}

/// for GisMap
// DblCoordRec(double longitude, double latitude)
void HTrigCalcCtrl::FillStationList(const bool isRef, const Vector<TrigerRec>& multList, Vector<StationRec>& list)const{
	for(int i=0; i< multList.GetCount(); i++){
		DblCoordRec coordRec = DblCoordRec(multList[i].longitude, multList[i].latitude);// StrToDblCoordRec(hcmMap[i].GetField("4C").GetStr());
		StationRec rec(
				multList[i].name,
				coordRec.longitude,
				coordRec.latitude,
				multList[i].azimuth,
				multList[i].h_asl, // TODO: "4Z" + "9Y" ???
				0.0,
				isRef);
		list.Add(rec);
	}
}

void HTrigCalcCtrl::Load(const Vector<TrigerRec>& multList, const String& file){
	fileName = file;
	trigList = clone(multList);
	grCalcRes.Clear();
	int cnt = 0;
	for(auto v: multList){
		cnt++;
		grCalcRes.Add();
		grCalcRes("T_No") = cnt;
	    grCalcRes("NAME") = v.name;
	    grCalcRes("REF") = v.reference;
	    grCalcRes("COUNTRIES") = v.countries;
	    grCalcRes("Error") = appErrorMap.Get(v.error).ToString();
		grCalcRes.SetRowHeight(grCalcRes.GetRowCount(), Single<ItemGrid>().itemFont.GetHeight()*1.5);// row height = GridCtrl.height * 1.5
	}

	formMap.layGisMap.stationsList.Clear();
	FillStationList(false, multList, formMap.layGisMap.stationsList);
	formMap.layGisMap.RefreshMapTree();
	formMap.layGisMap.Refresh();
}

int HTrigCalcCtrl::GetCurSelID(){
	Value npp = grCalcRes("T_No");
	if(!npp.IsNull()){
		return int(npp);
	}
	return -1;
}
void HTrigCalcCtrl::Clear(){
	grCalcRes.Clear();
}
void HTrigCalcCtrl::ShowMap(){
	formMap.Title("");
	if(formMap.isShow){
		//ActiveFocus(formMap);
	}
	else{
		//formMap.layGisMap.ChangeOptions();
		formMap.layGisMap.RefreshMapSettings();
		formMap.OpenMain();	// for non-modal form calls
		formMap.isShow = true;
	}
}
void HTrigCalcCtrl::SaveSingleCalcToXLS(){
	FileSel fs;
	fs.Set(String() << GetFileTitle(fileName));
	fs.ReadOnlyOption(true);
	fs.MkDirOption(false);
	fs.ActiveDir(GetExeFolder());
	fs.EditFileName(true);
	fs.Types("Excel files\t*.xlsx");
	if(fs.ExecuteSaveAs()){
		const String file = fs;
		xlnt::workbook wbOut;
	    xlnt::worksheet wsOut = wbOut.active_sheet();
	    wsOut.title("Trigger for Co-ordination");
	    Vector<String> title;
	    TrigerRec().FillTitle(title);
	    for(int i=0; i<title.GetCount(); i++){
			wsOut.cell(xlnt::cell_reference(i+1, 1)).value(title[i].ToStd());
		}
		Vector<Vector<String>> dataLst;
	    for(auto t: trigList){
		    Vector<String> row;
			t.FillData(row);
			dataLst.Add(pick(row));
	    }
	    for(int i=0; i<dataLst.GetCount(); i++){
	        for(int j=0; j<dataLst[i].GetCount(); j++){
	            wsOut.cell(xlnt::cell_reference(j+1, i+2)).value(dataLst[i][j].ToStd());
	        }
	    }
		wbOut.save(file.ToStd());
	}
}
//!---------------------------------------------------------------------------------------


class FormHTrigCalc : public WithLayCalcTrig<TopWindow> {
public:
	typedef FormHTrigCalc CLASSNAME;
	HTrigCalcCtrl frmCtrl;
	void ShowMap();
	void SaveAs();
	bool isShow;
	void OnFrmClose();
	void Serialize(Stream& s);
	FormHTrigCalc();
};
FormHTrigCalc::FormHTrigCalc(){
	CtrlLayout(*this, String(t_("")));
	Sizeable().NoZoomable();
	Add(frmCtrl.layCalcRes.SizePos());
	WhenClose = THISBACK(OnFrmClose);

	frmCtrl.layCalcRes.btn_SaveAs.SetImage(Images::SAVE_AS());
	frmCtrl.layCalcRes.btn_SaveAs.Tip(t_("Save as"));
	frmCtrl.layCalcRes.btn_SaveAs <<= THISBACK(SaveAs);
	frmCtrl.layCalcRes.btn_Map.SetImage(Images::MAP());
	frmCtrl.layCalcRes.btn_Map.Tip(t_("Show Map"));
	frmCtrl.layCalcRes.btn_Map <<= THISBACK(ShowMap);
	frmCtrl.layCalcRes.btn_Exit.SetImage(Images::EXIT());
	frmCtrl.layCalcRes.btn_Exit.Tip("Exit");
	frmCtrl.layCalcRes.btn_Exit <<= THISBACK(OnFrmClose);

	isShow = false;
}
void FormHTrigCalc::Serialize(Stream& s){
	int version = 1;
	s / version;
	SerializePlacement(s);
	frmCtrl.formMap.Serialize(s);
	frmCtrl.grCalcRes.Serialize(s);
}
void FormHTrigCalc::OnFrmClose(){
	isShow = false;
	if(frmCtrl.formMap.isShow){frmCtrl.formMap.isShow = false; frmCtrl.formMap.Close();};
	Close();
}
void FormHTrigCalc::ShowMap(){
	frmCtrl.ShowMap();
}
void FormHTrigCalc::SaveAs(){
	frmCtrl.SaveSingleCalcToXLS();
}
//!---------------------------------------------------------------------------------------


struct CalcParamDlg: public WithFrmParamCalc<TopWindow> {
	typedef CalcParamDlg CLASSNAME;
	void Serialize(Stream& s);
	void ToCtrl();
	void FromCtrl();
	void Init();
	void Save();
	void Load();
	CalcParamDlg(CalcParamData& calcParam);
protected:
	CalcParamData& calcParam;
};
CalcParamDlg::CalcParamDlg(CalcParamData& calcParam):calcParam(calcParam){
	CtrlLayoutOKCancel(*this, t_("Set the calculation parameters"));
	Sizeable().Zoomable();
	Init();
	Load();
}
void CalcParamDlg::Serialize(Stream& s){
	int version = 1;
	s / version;
	SerializePlacement(s);
}
void CalcParamDlg::Init(){
	dl_Plz.SetDisplay(Single<ItemDisplay>());
	ed_MinTD.SetFont(Single<ItemGrid>().headFont);
}
void CalcParamDlg::ToCtrl(){
	PlzToDropList(dl_Plz);
	dl_Plz.SetIndex(0);
	ed_TimePercent.SetData(calcParam.inCalcRec.time_percentage);
	ed_Press.SetData(calcParam.inCalcRec.press);
	ed_Temp.SetData(calcParam.inCalcRec.temp);

	ed_MinTD.SetData(calcParam.minTD);
	opt_ShowUnCalc.Set(calcParam.isShowUnCalc);
}
void CalcParamDlg::FromCtrl(){
	calcParam.inCalcRec.plz = PlzToInt(StrToPlz(dl_Plz.GetData().ToString()));
	calcParam.inCalcRec.time_percentage = ed_TimePercent.GetData();
	calcParam.inCalcRec.press = ed_Press.GetData();
	calcParam.inCalcRec.temp = ed_Temp.GetData();
	calcParam.minTD = ed_MinTD.GetData();
	calcParam.isShowUnCalc = opt_ShowUnCalc.Get();
	calcParam.inCalcRec.dN = 0.0;
	calcParam.inCalcRec.n0 = 0.0;
}
void CalcParamDlg::Load(){
	calcParam.Load();
	if(calcParam.IsEmpty()) calcParam.inCalcRec.OnDefault();
	calcParam.minTD = Single<AppCFG>().cfgCalcRec.coMinTD;
	ToCtrl();
}
void CalcParamDlg::Save(){
	FromCtrl();
	calcParam.Save();
}
//!---------------------------------------------------------------------------------------------

//	formCalcRes.layResCo.CloseHcmPack();
//	formCalcRes.layResCo.OpenHcmPack(testFile);


class LayHcmPackCoEdit : public WithLayHcmPackEdit<ParentCtrl> {
public:
	typedef LayHcmPackCoEdit CLASSNAME;

	LayHeadV layHcmHeadEd = {true, CveCoHeaderList().GetList()};
	LayViewEntrisH layHcmEntris;

	WithLayGridCaption<ParentCtrl> layCaption;
	FrameTop<WithLayGridCaption<ParentCtrl>> frameCaption;
	Label  lbCaption;
	Splitter spl;

	void GrEntrisPopMenu(Bar& bar);

	void ToCtrl();
	//void ToCtrl(const HcmPack& hPack);
	
	void EditEntry();

	const HcmPack& GetHcmPack()const{return hcmPack;};
	void SetCoStatus(const int id, const String& f13Y, const String& f13Z){hcmPack.SetCoStatus(id, f13Y, f13Z);}
	void SetCoStatus(const int id, const String& f13Y){hcmPack.SetCoStatus(id, f13Y);}
	void SetCoStatus(const String& f13Y){hcmPack.SetCoStatus(f13Y);}

	void OpenHcmPack();
	bool OpenHcmPack(const String& fileName);

	void CloseHcmPack();
	void SaveHcmPackAs();

	void SetCaption(const String& str){lbCaption.SetText(str);};

	bool IsModified()const{return isModified;}
	void Clear();
	void Serialize(Stream& s);
	LayHcmPackCoEdit();
protected:
	bool isOpen;
	bool isModified;
	HcmPack hcmPack;
	VectorMap<String, String> seriaMap;
	void Setup();
};
LayHcmPackCoEdit::LayHcmPackCoEdit(){
	CtrlLayout(*this);
	Add(spl.Vert(layHcmHeadEd.SizePos(), layHcmEntris.SizePos()));
	lbCaption.SetFrame(BlackFrame());
	layCaption.Add(lbCaption.SizePos());
	frameCaption.Add(layCaption.SizePos());
	frameCaption.Height(Single<ItemGrid>().headFont.GetHeight()*2); // !!! Высота фрейма заголовка = высота шрифта + 3 !!!
	AddFrame(frameCaption);
	lbCaption.SetFont(Single<ItemGrid>().headFont);
	lbCaption.SetText(t_(" Fill in a responding country contacts"));
	isOpen = false;
	Setup();
	//layHcmHeadEd.SetDescr("LayHeader");
}
void LayHcmPackCoEdit::Setup(){
	layHcmEntris.layEntris.grid.WhenMenuBar = THISBACK(GrEntrisPopMenu);
	layHcmEntris.layEntris.grid.WhenLeftDouble = [=] { EditEntry(); };
}
void LayHcmPackCoEdit::Serialize(Stream& s){
	spl.Serialize(s);
	layHcmHeadEd.Serialize(s);
	layHcmEntris.Serialize(s);
	seriaMap.Serialize(s);
}
void LayHcmPackCoEdit::GrEntrisPopMenu(Bar& bar){
	bar.Add(t_("Edit .."), Images::EDIT(),  THISBACK(EditEntry));
}
void LayHcmPackCoEdit::Clear(){
	layHcmHeadEd.Clear();
	layHcmEntris.Clear();
}
void LayHcmPackCoEdit::ToCtrl(){
	layHcmHeadEd.ToCtrl(hcmPack.GetHeader());
	layHcmEntris.ToCtrl(hcmPack);
	layHcmEntris.SetFirstRec();
}
/*
void LayHcmPackCoEdit::ToCtrl(const HcmPack& hPack){
	layHcmHeadEd.ToCtrl(hPack.GetHeader());
	layHcmEntris.ToCtrl(hPack);
	layHcmEntris.SetFirstRec();
}
*/
bool LayHcmPackCoEdit::OpenHcmPack(const String& fileName){
	CloseHcmPack();
	String ext = GetFileExt(fileName);
	isOpen = false;
	if(ext == ".json"){
		isOpen = hcmPack.FromJson(fileName);
	}else if(ext == ".xlsx"){
		isOpen = hcmPack.FromXLS(fileName);
	}else if(ext == ".txt"){
		isOpen = hcmPack.FromCSV(fileName);
	}
	if(isOpen){
		hcmPack.GetHeader().EditVal("H03", "A");
		ToCtrl();
	}
	return isOpen;
}
void LayHcmPackCoEdit::OpenHcmPack(){
	FileSel fs;
	fs.ReadOnlyOption(true);
	fs.MkDirOption(false);
	String wDir = Single<AppCFG>().cfgPathRec.GetEdWorkDir();
	if(wDir.IsEmpty() || !DirectoryExists(wDir))
		wDir = GetExeFolder();
	fs.ActiveDir(wDir);
	fs.Types("Text files\t*.txt\nExcel files\t*.xlsx\nJson files\t*.json");
	String fileName = "";
	if(fs.ExecuteOpen()){
		fileName = fs;
		if(!fileName.IsEmpty()){
			CloseHcmPack();
			Single<AppCFG>().cfgPathRec.SetEdWorkDir(GetFileFolder(fileName));
			isOpen = false;
			if(fs.GetActiveType()==0){
				isOpen = hcmPack.FromCSV(fileName);
			}
			if(fs.GetActiveType()==1){
				isOpen = hcmPack.FromXLS(fileName);
			}
			if(fs.GetActiveType()==2){
				isOpen = hcmPack.FromJson(fileName);
			}
			if(!isOpen){
				Clear();
				Exclamation(DeQtf(Format("Error opening file: \"%s\"\nThe selected file is not an hcm data file", fileName)));
				return;
			}
			if(isOpen){
				hcmPack.GetHeader().EditVal("H03", "A");
				ToCtrl();
			}
		}
	}
}
void LayHcmPackCoEdit::SaveHcmPackAs(){
	if(hcmPack.IsEmpty()) return;
	layHcmHeadEd.FromCtrl(hcmPack.GetHeader());
	FileSel fs;
	fs.Set(String() << GetFileTitle(hcmPack.GetPackFile()) << "_Answer");
	fs.ReadOnlyOption(true);
	fs.MkDirOption(false);
	fs.ActiveDir(GetExeFolder());
	fs.EditFileName(true);
	fs.Types("Text files\t*.txt\nExcel files\t*.xlsx\nJson files\t*.json");
	if(fs.ExecuteSaveAs()){
		layHcmHeadEd.ClearModify();
		const String fileName = fs;
		if(fs.GetActiveType()==0){
			isModified = !hcmPack.ToCSV(fileName);
		}
		if(fs.GetActiveType()==1){
			isModified = !hcmPack.ToXLS(fileName);
		}
		if(fs.GetActiveType()==2){
			isModified = !hcmPack.ToJson(fileName);
		}
	}
}
void LayHcmPackCoEdit::CloseHcmPack(){
	if(hcmPack.IsEmpty()){
		return;
	}
    hcmPack.Clear();
    hcmPack.GetHeader().Clear();
    layHcmHeadEd.Clear();
    layHcmEntris.Clear();
}
void LayHcmPackCoEdit::EditEntry(){
	CoEntryEditDlg dlg(true);
	dlg.BeforeFrmShow("EditEntryDlg", seriaMap);
	int idx =  layHcmEntris.GetCurSelID();
	if(idx != -1){
		dlg.ToCtrl(hcmPack.GetHcmIN(idx));
		if(dlg.Execute() == IDOK){
			dlg.FromCtrl(hcmPack.GetHcmIN(idx));
			layHcmEntris.ToCtrl(hcmPack);
			isModified = dlg.IsModified();
		}
		else{
			// isModified = false;
		}
	}
	dlg.BeforeFrmClose("EditEntryDlg", seriaMap);
}
//!---------------------------------------------------------------------------------------------



class FormCalcRes : public WithFrmCalcRes<TopWindow> {
public:
	typedef FormCalcRes CLASSNAME;
	ParentCtrl ctrlCalc;
	LayHcmPackCoEdit layResCo;
	bool isShow;
	void SavePackAs();
	void CoEditPack(const TblMultData& multHcmData, const CalcParamData& calcParam);
	void OnClose();
	void Clear(){layResCo.Clear();}
	void Serialize(Stream& s);
	FormCalcRes();
};
FormCalcRes::FormCalcRes(){
	CtrlLayout(*this, String(t_("")));
	Sizeable().MinimizeBox(false).MaximizeBox(false);
	tab.Add(ctrlCalc.SizePos(), t_("\1[ [ [3*!Arial! Calculation report]]]"));
	tab.Add(layResCo.SizePos(), t_("\1[ [ [3*!Arial! Result of coordination]]]"));

	WhenClose = THISBACK(OnClose);
	isShow = false;
	btn_SaveAs.SetImage(Images::SAVE_AS());
	btn_SaveAs.Tip(t_("Save HMC FS file as"));
	btn_SaveAs <<= THISBACK(SavePackAs);
	btn_Exit.SetImage(Images::EXIT());
	btn_Exit.Tip("Exit");
	btn_Exit <<= THISBACK(OnClose);
}
void FormCalcRes::Serialize(Stream& s){
	int version = 3;
	s / version;
	SerializePlacement(s);
	layResCo.Serialize(s);
}
void FormCalcRes::OnClose(){
	layResCo.CloseHcmPack();
	isShow = false;
	Close();
}
void FormCalcRes::SavePackAs(){
	layResCo.SaveHcmPackAs();
}
void FormCalcRes::CoEditPack(const TblMultData& multHcmData, const CalcParamData& calcParam){
	const HcmLstRec& lst = Single<HcmLstData>().GetLst("13Y");
	String s13Z = lst.GetLstValue("C");
	layResCo.SetCoStatus(s13Z);
	for(int i=0; i< multHcmData.multCalcLst.GetCount(); i++){
		for(int j=0; j<layResCo.GetHcmPack().GetMap().GetCount(); j++){
			if(multHcmData.multCalcLst[i].tl_hash == layResCo.GetHcmPack().GetMap()[j].GetHash()){
				// if(multHcmData.multCalcLst[i].tl_i_td >= Single<AppCFG>().cfgCalcRec.coMinTD){
				if(multHcmData.multCalcLst[i].tl_i_td >= calcParam.minTD){
					if(layResCo.GetHcmPack().GetMap()[j].GetEntity() == HcmEntity::TX || layResCo.GetHcmPack().GetMap()[j].GetEntity() == HcmEntity::PTX){
						String s13Z = lst.GetLstValue("Z");
						layResCo.SetCoStatus(j, s13Z, Format("Interference to protected Rx. Max TD = %.1f, dB", multHcmData.multCalcLst[i].tl_i_td));
					}
					else if(layResCo.GetHcmPack().GetMap()[j].GetEntity() == HcmEntity::RX || layResCo.GetHcmPack().GetMap()[j].GetEntity() == HcmEntity::PRX){
						String s13Z = lst.GetLstValue("G");
						layResCo.SetCoStatus(j, s13Z, Format("Interference from recognized Tx. Max TD = %.1f, dB", multHcmData.multCalcLst[i].tl_i_td));
					}
				}
/*
				else{
					String s13Z = lst.GetLstValue("C");
					layResCo.SetCoStatus(j, s13Z);
				}
*/
			}
		}
	}
}




//!---------------------------------------------------------------------------------------



#endif
