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

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

#include "TAS0090MF.h"
#include "TAS00901F.h"
#include "CDSCodeF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxDropDownEdit"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "cxCheckBox"
#pragma link "cxButtonEdit"
#pragma resource "*.dfm"

TTAS0090M *TAS0090M = NULL;
//---------------------------------------------------------------------------
__fastcall TTAS0090M::TTAS0090M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FTitle = Caption;//"노선별속도분석";
    m_pFormList = new TList(); 
    TsList01->TabVisible = false;

    MyManager = new TVdsCtlrManager();
    MyManager->LoadFromDbVdsLink();
}
//---------------------------------------------------------------------------

void __fastcall TTAS0090M::CommClose()
{
    try
    {
        if (MyManager)
        {
            delete MyManager;
            MyManager = NULL;
        }

        for (int idx = m_pFormList->Count-1; idx >= 0; idx--)
        {
            m_pFormList->Delete(idx);
        }
        delete m_pFormList;

        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TTAS0090M::FormShow(TObject *Sender)
{
    Refresh();
    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TTAS0090M::FormInit()
{
    DtStDay->Date = Now() - 2;
    DtEdDay->Date = Now() - 1;

    int ii;
    String sHour;
    CbStHour->Properties->Items->Clear();
    for(ii = 0; ii < 24; ii++)
    {
        sHour.printf(L" %02d시", ii);
        CbStHour->Properties->Items->Add(sHour);
    }
    CbStHour->ItemIndex = 0;
}
//---------------------------------------------------------------------------

void __fastcall TTAS0090M::CbStHourPropertiesChange(TObject *Sender)
{
    int nStHour = CbStHour->Text.Trim().SubString(1,2).ToIntDef(0);

    int ii;
    String sHour;
    CbEdHour->Properties->Items->Clear();
    for(ii = nStHour; ii < 24; ii++)
    {
        sHour.printf(L" %02d시", ii);
        CbEdHour->Properties->Items->Add(sHour);
    }
    CbEdHour->ItemIndex = CbEdHour->Properties->Items->Count-1;
}
//---------------------------------------------------------------------------

void __fastcall TTAS0090M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TTAS0090M::RefreshData()
{
}
//---------------------------------------------------------------------------

void __fastcall TTAS0090M::BtnSearchClick(TObject *Sender)
{
    Application->ProcessMessages();
    TSqlCursor sqlCrs((TControl*)BtnSearch);

    String sTitle;

    FStDay = DtStDay->Date.FormatString("yyyymmdd");
    FEdDay = DtEdDay->Date.FormatString("yyyymmdd");
    FStHour = CbStHour->Text.Trim().SubString(1,2);
    FEdHour = CbEdHour->Text.Trim().SubString(1,2);

    FWeek = "";
    FDir = "";
    FAtrdNm = "";
    FOR_STL(TVdsLink*, pObj, MyManager->FLinkLists)
    {
        if (pObj->IsSelected)
        {
            FAtrdNm += "'" + pObj->LINK_ID +"',";
        }
    }
    FAtrdNm.Delete(FAtrdNm.Length(), 1);

    FTable  = 0;
    FQryType = 0;

    //FStDay = FStDay + FStHour;
    //FEdDay = FEdDay + FEdHour;
    if (FStDay > FEdDay)
    {
        Application->MessageBox(FrmLang->lblQryDtErr->Caption.c_str(),//L"검색 시작시각이 검색 종료시각 보다 큽니다.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = DtStDay;
        return;
    }

	sTitle.printf(L"%s ~ %s  ", FStDay.c_str(), FEdDay.c_str());

    FStDay = FStDay + "000000";
    FEdDay = FEdDay + "235959";

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

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

        TAS00901 = new TTAS00901(this, Handle, FStDay, FEdDay, FStHour, FEdHour, FWeek, FDir, FAtrdNm, FTable, FQryType, MyManager);
        TAS00901->Parent = pSheet;
        TAS00901->Show();
        PgTab->ActivePage = pSheet;
        pSheet = PgTab->ActivePage;
        m_pFormList->Add(TAS00901);
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

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

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

    LockWindowUpdate(Handle);

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

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

void __fastcall TTAS0090M::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 TTAS0090M::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    TAS0090M = NULL;
    Action = caFree;    
}
//---------------------------------------------------------------------------

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

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

void __fastcall TTAS0090M::EdAtrdPropertiesButtonClick(TObject *Sender, int AButtonIndex)
{
    TITSSEL5M *pSelForm = NULL;
    try
    {
        try {
            pSelForm = new TITSSEL5M(this, MyManager);
            pSelForm->ShowModal();
            if (pSelForm->m_bSelected)
            {
                if (MyManager->FLinkSelCnt == MyManager->FLinkLists.Size())
                {
                    EdAtrd->Text = "[전체]";
                }
                else
                {
                    String LINK_NM = "";
                    FOR_STL(TVdsLink*, pObj, MyManager->FLinkLists)
                    {
                        if (pObj->IsSelected)
                        {
                            LINK_NM = pObj->STR_NAME + " → " + pObj->END_NAME;
                            break;
                        }
                    }
                    if (MyManager->FLinkSelCnt == 1)
                    {
                        EdAtrd->Text = LINK_NM;
                    }
                    else
                    {
                        EdAtrd->Text = "[" + LINK_NM + "] 외" + String(MyManager->FLinkSelCnt-1) + " 개 구간";
                    }
                }
            }
        } catch(...) { }
    }
    __finally
    {
        if (pSelForm) delete pSelForm;
    }
}
//---------------------------------------------------------------------------

