//---------------------------------------------------------------------------

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSLangTransF.h"
#include "CDSCrossF.h"
#pragma hdrstop

#include "FrmCrsTrafVolStaF.h"

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxHint"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSpinEdit"
#pragma link "cxTextEdit"
#pragma link "dxBar"
#pragma link "dxCustomHint"
#pragma link "dxRibbon"
#pragma link "dxRibbonGallery"
#pragma link "dxRibbonSkins"
#pragma link "dxRibbonStatusBar"
#pragma link "dxScreenTip"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinChooserGallery"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinsdxBarPainter"
#pragma link "dxSkinsdxRibbonPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinsForm"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "dxStatusBar"
#pragma link "dxTabbedMDI"
#pragma link "cxButtons"
#pragma link "cxCalendar"
#pragma link "cxDropDownEdit"
#pragma link "cxGroupBox"
#pragma link "cxSplitter"
#pragma link "cxTimeEdit"
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxFilter"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxStyles"
#pragma resource "*.dfm"

TFrmCrsTrafVolSta *FrmCrsTrafVolSta = NULL;

//---------------------------------------------------------------------------

__fastcall TFrmCrsTrafVolSta::TFrmCrsTrafVolSta(TComponent* Owner)
    : TForm(Owner)//TdxCustomRibbonForm(Owner)//TForm(Owner)
{
    DoubleBuffered = true;

    g_AppCfg.lMainWinHandle = (long)Handle;

    LangTrans->Translate(this, ITSDb_GetConnection());

    CMM_SetFormPosition(g_AppCfg.bSaveForm);

    ITSSkin_Initialize(Application);
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

#if 0
    String sCaption = "";
    ReadConfigInfo(g_AppCfg.sConfigFile, "APPLICATION", "CAPTION", sCaption);
    ITSSkin_Caption(sCaption == "1" ? true : false);
#endif

    SetColorScheme(g_AppCfg.sSkinName);

    FFormList = new TList();
    TsList01->TabVisible = false;

    CrossManager = new TCrossManager();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsTrafVolSta::FormCreate(TObject *Sender)
{
    Application->ShowMainForm = false;

    try {
        Application->Icon->LoadFromResourceName(((unsigned int)HInstance), "MAINICON");
    } catch(...) { ShowMessage("LoadFromResourceName failed"); }

    Application->ShowMainForm = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsTrafVolSta::FormShow(TObject *Sender)
{
    Application->ProcessMessages();
    FormInit();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsTrafVolSta::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    Action = caFree;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsTrafVolSta::CommClose()
{
    try
    {
        for (int idx = FFormList->Count-1; idx >= 0; idx--)
        {
            TFrmCrsTrafVolStaSub *pLogForm = (TFrmCrsTrafVolStaSub*)FFormList->Items[idx];
            delete pLogForm;
            FFormList->Delete(idx);
        }
        delete FFormList;

        ITSSkin_Term();
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsTrafVolSta::SetColorScheme(String ASkinName)
{
    dxSkinController1->SkinName = ASkinName;
    dxSkinController1->Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsTrafVolSta::BtnCloseClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsTrafVolSta::FormInit()
{
	CbStatType->ItemIndex = 0;

	TDateTime dtStart = Now()-1;
	TDateTime dtEnd = Now();

    DtStDate->Date = dtStart;
    DtEdDate->Date = dtEnd;

    DtStTime->Date = dtStart;
    DtEdTime->Date = dtEnd;

    CrossManager->LoadFromDb();
    FillList();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsTrafVolSta::FillList()
{
    CbCross->Properties->OnChange = NULL;
    CbCross->Properties->Items->Clear();

    CrossManager->FLists.Lock();
    try
    {
        FOR_STL(TCross*, pObj, CrossManager->FLists)
        {
            CbCross->Properties->Items->AddObject("[" + pObj->IXR_ID + "] " + pObj->IXR_NM, (TObject*)pObj);
        }
    }
    __finally
    {
        CrossManager->FLists.UnLock();
    }
    CbCross->ItemIndex = 0;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsTrafVolSta::OnSubFormClose(TMessage Msg)
{
    int nActiveIdx = PgTab->ActivePageIndex;
    if (nActiveIdx <= 0) return;

    LockWindowUpdate(Handle);

    TcxTabSheet *pSheet = PgTab->ActivePage;
    if (pSheet)
    {
        delete pSheet;
    }
    nActiveIdx--;   // 첫번째 탭은 화면에 숨겨져 있다.
    FFormList->Delete(nActiveIdx);

    LockWindowUpdate(0);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsTrafVolSta::PgTabDblClick(TObject *Sender)
{
    TPoint APoint;
    APoint = PgTab->MouseDownPos;
    if (PgTab->IndexOfTabAt(APoint.x, APoint.y) != -1)
        POST_MSG(Handle, WM_SUBFORM_CLOSE, 0, 0);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsTrafVolSta::BtnSearchClick(TObject *Sender)
{
    TCross *pObj = (TCross*)CbCross->Properties->Items->Objects[CbCross->ItemIndex];
    if (!pObj) {
        Application->MessageBox(L"선택한 교차로정보를 찾을 수 없습니다. 프로그램을 재실행 한 후 확인 하십시요.",
                                Caption.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = CbCross;
        return;
    }

    String sCamNm  = "";
    String sCamIds = "";

    String sStTime, sEdTime, sTemp;
    switch(CbStatType->ItemIndex)
    {
    case 0: //15분
    case 1: //1시간
        sStTime = DtStDate->Date.FormatString("yyyyMMdd") + DtStTime->Time.FormatString("hh");
        sEdTime = DtEdDate->Date.FormatString("yyyyMMdd") + DtEdTime->Time.FormatString("hh");
        FStrTime = sStTime + "0000";
        FEndTime = sEdTime + "5959";
        break;
    }

    if (sStTime > sEdTime)
    {
        Application->MessageBox(L"검색 시작시각이 검색 종료시각 보다 큽니다.",
                                Caption.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = DtStDate;
        return;
    }


    FCrossId = pObj->IXR_ID;
    FCamId = sCamIds;

    String sTitle;
	sTitle.printf(L"%s~%s ", sStTime.c_str(), sEdTime.c_str());

    try
    {
        Application->ProcessMessages();
        LockWindowUpdate(Handle);

        TcxTabSheet *pSheet = CMM_AddTabSheet(PgTab, sTitle);
        if (!pSheet) return;

        TFrmCrsTrafVolStaSub *pLogForm = new TFrmCrsTrafVolStaSub(this, Handle, FStrTime, FEndTime, FCrossId, FCamId, CbStatType->ItemIndex, false, pObj);
        pLogForm->Caption= Caption;
        pLogForm->Parent = pSheet;
        pLogForm->Show();
        PgTab->ActivePage = pSheet;
        pSheet = PgTab->ActivePage;
        FFormList->Add(pLogForm);
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsTrafVolSta::PgTabCanCloseEx(TObject *Sender, int ATabIndex, bool &ACanClose)
{
    int nActiveIdx = ATabIndex;
    if (nActiveIdx <= 0) return;

    nActiveIdx--;   // 첫번째 탭은 화면에 숨겨져 있다.
    FFormList->Delete(nActiveIdx);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsTrafVolSta::CbStatTypePropertiesChange(TObject *Sender)
{
    switch(CbStatType->ItemIndex)
    {
    case 0: //15분
    case 1: //1시간
        DtStDate->Format = "  yyyy년 MM월 dd일";
        DtEdDate->Format = "  yyyy년 MM월 dd일";
        DtStTime->Enabled = true;
        DtEdTime->Enabled = true;
        break;
    }
    DtStDate->SetFocus();
}
//---------------------------------------------------------------------------

