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

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

#include "TAS0020MF.h"
#include "TAS00201F.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 resource "*.dfm"

TTAS0020M *TAS0020M = NULL;
//---------------------------------------------------------------------------
__fastcall TTAS0020M::TTAS0020M(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->LoadFromDb();
}
//---------------------------------------------------------------------------

void __fastcall TTAS0020M::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 TTAS0020M::FormShow(TObject *Sender)
{
    Refresh();
    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TTAS0020M::FormInit()
{
    CbAtrd->Properties->Items->Clear();
    try
    {
        MyItsAtrdManager->FNameLists.Lock();
        FOR_STL(TItsAtrdName*, pObj, MyItsAtrdManager->FNameLists)
        {
            CbAtrd->Properties->Items->AddObject(pObj->Name, (TObject*)pObj);
        }
    }
    __finally
    {
        MyItsAtrdManager->FNameLists.UnLock();
        CbAtrd->ItemIndex = 0;
    }
#if 0
    try
    {
        MyItsAtrdManager->FLists.Lock();
        FOR_STL(TItsAtrd*, pObj, MyItsAtrdManager->FLists)
        {
            if (pObj->DEL_YN == "Y") continue;

            String sDir = (pObj->DRCT_CD.Trim() == "0") ? FrmLang->lblUp->Caption : FrmLang->lblDown->Caption;String("상행") : String("하행");
            String sAtr = pObj->ATRD_NM;
            CbAtrd->Properties->Items->AddObject(sAtr + " [" + sDir + "]", (TObject*)pObj);
        }
    }
    __finally
    {
        MyItsAtrdManager->FLists.UnLock();

        CbAtrd->ItemIndex = 0;
    }
#endif

	TItsCode *pCode = ItsCodeManager->FLists.Find("DTW");
	if (pCode)
	{
        CbPatternWeek->Properties->Items->Clear();
		FOR_STL(TItsSubCode *, pSubCode, pCode->FSubLists)
		{
			CbPatternWeek->Properties->Items->Add("[" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
	CbPatternWeek->ItemIndex = 0;

    DtBaseMon->Date = Now();// - 30;
}
//---------------------------------------------------------------------------

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

void __fastcall TTAS0020M::RefreshData()
{
    // 검색조건에 의한 링크 데이터 조회
    try
    {
        //SelListData();
	}
	__finally
	{
	}
}
//---------------------------------------------------------------------------

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

    String sTitle;

	FQryDay = DtBaseMon->Date.FormatString("yyyymm");
    FPtrnWeek = ITSUtil_GetCode(CbPatternWeek->Text);
    FPtrnWeekName = CbPatternWeek->Text;
    FAtrdId = CbAtrd->Text.Trim();
    FAtrdNm = CbAtrd->Text.Trim();
#if 0
    if (CbAtrd->ItemIndex == 0)
    {
        FAtrdId = "";
        FAtrdNm = FrmLang->lblChartTotal->Caption;//"전체";
    }
#endif

	sTitle.printf(L"%s(%s) %s  ", FQryDay.c_str(), CbPatternWeek->Text.t_str(), FAtrdNm.c_str());

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

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

        TAS00201 = new TTAS00201(this, Handle, FAtrdId, FAtrdNm, FQryDay, FPtrnWeek, FPtrnWeekName);
        TAS00201->Parent = pSheet;
        TAS00201->Show();
        PgTab->ActivePage = pSheet;
        pSheet = PgTab->ActivePage;
        m_pFormList->Add(TAS00201);
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

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

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

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

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

