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

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

#include "TAS0060MF.h"
#include "TAS00601F.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"

TTAS0060M *TAS0060M = NULL;
//---------------------------------------------------------------------------
__fastcall TTAS0060M::TTAS0060M(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;

    MyItsAtrdManager = new TItsAtrdManager();
    MyItsAtrdManager->LoadFromDbAtrdName();
}
//---------------------------------------------------------------------------

void __fastcall TTAS0060M::CommClose()
{
    try
    {
        if (MyItsAtrdManager)
        {
            delete MyItsAtrdManager;
            MyItsAtrdManager = 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 TTAS0060M::FormShow(TObject *Sender)
{
    Refresh();
    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TTAS0060M::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 TTAS0060M::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 TTAS0060M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
}
//---------------------------------------------------------------------------

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

void __fastcall TTAS0060M::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 = "";
    if (ChkWeek7->Checked &&
        ChkWeek1->Checked &&
        ChkWeek2->Checked &&
        ChkWeek3->Checked &&
        ChkWeek4->Checked &&
        ChkWeek5->Checked &&
        ChkWeek6->Checked)
    {
        FWeek = "";
    }
    else
    if (!ChkWeek7->Checked &&
        !ChkWeek1->Checked &&
        !ChkWeek2->Checked &&
        !ChkWeek3->Checked &&
        !ChkWeek4->Checked &&
        !ChkWeek5->Checked &&
        !ChkWeek6->Checked)
    {
        FWeek = "";
    }
    else
    {
        if (ChkWeek7->Checked) FWeek += "'DTW7',";
        if (ChkWeek1->Checked) FWeek += "'DTW1',";
        if (ChkWeek2->Checked) FWeek += "'DTW2',";
        if (ChkWeek3->Checked) FWeek += "'DTW3',";
        if (ChkWeek4->Checked) FWeek += "'DTW4',";
        if (ChkWeek5->Checked) FWeek += "'DTW5',";
        if (ChkWeek6->Checked) FWeek += "'DTW6',";
        FWeek.Delete(FWeek.Length(), 1);
    }


    FDir = "";
    if (ChkUp->Checked)
    {
        FDir = "'0'";
    }
    if (ChkDn->Checked)
    {
        if (FDir == "") FDir = "'1'";
        else            FDir = "";
    }

    FAtrdNm = "";
    if (MyItsAtrdManager->FCDSNameSelCnt != 0 && MyItsAtrdManager->FCDSNameSelCnt != MyItsAtrdManager->FCDSNameLists.Size())
    {
        FOR_STL(TCDSAtrdName*, pObj, MyItsAtrdManager->FCDSNameLists)
        {
            if (pObj->IsSelected)
            {
                FAtrdNm += "'" + pObj->ATRD_NM +"',";
            }
        }
        FAtrdNm.Delete(FAtrdNm.Length(), 1);
    }

    FTable  = CbTable->ItemIndex;
    FQryType = 0;

    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());

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

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

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

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

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

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

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

void __fastcall TTAS0060M::EdAtrdPropertiesButtonClick(TObject *Sender, int AButtonIndex)
{
    TITSSEL4M *pSelForm = NULL;
    try
    {
        try {
            pSelForm = new TITSSEL4M(this, MyItsAtrdManager);
            pSelForm->ShowModal();
            if (pSelForm->m_bSelected)
            {
                if (MyItsAtrdManager->FCDSNameSelCnt == MyItsAtrdManager->FCDSNameLists.Size())
                {
                    EdAtrd->Text = "[전체]";
                }
                else
                {
                    String ATRD_NM = "";
                    FOR_STL(TCDSAtrdName*, pObj, MyItsAtrdManager->FCDSNameLists)
                    {
                        if (pObj->IsSelected)
                        {
                            ATRD_NM = pObj->ATRD_NM;
                            break;
                        }
                    }
                    if (MyItsAtrdManager->FCDSNameSelCnt == 1)
                    {
                        EdAtrd->Text = ATRD_NM;
                    }
                    else
                    {
                        EdAtrd->Text = "[" + ATRD_NM + "] 외" + String(MyItsAtrdManager->FCDSNameSelCnt-1) + " 개 도로";
                    }
                }
            }
        } catch(...) { }
    }
    __finally
    {
        if (pSelForm) delete pSelForm;
    }
}
//---------------------------------------------------------------------------

