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

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

#include "IST0070MF.h"
#include "IST00701F.h"
#include "IST00702F.h"
#include "IST00703F.h"
#include "CDSRoadF.h"
#include "CDSIfscF.h"
#include "CDSLinkF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_LinkListF"
#pragma link "FRAME_RoadListF"
#pragma link "cxButtons"
#pragma link "cxCalendar"
#pragma link "cxCheckBox"
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxDropDownEdit"
#pragma link "cxEdit"
#pragma link "cxFilter"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSplitter"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "FRAME_IfscListF"
#pragma resource "*.dfm"

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

    ColumnSel->Options->Filtering = false;
    FTitle = Caption;//"간선도로 소통정보 통계";
    m_pFormList = new TList(); 
    TsList01->TabVisible = false;

    MyItsAtrdManager = new TItsAtrdManager();
    MyItsAtrdManager->LoadFromDb();

    FRAMELinkList1->TvList->OptionsView->GroupByBox = false;
    FRAMELinkList1->PnlTop->Visible = false;
    FRAMELinkList1->PnlBottom->Visible = false;
    FRAMELinkList1->ColumnSeq->Visible = true;

    FRAMEIfscList1->TvList->OptionsView->GroupByBox = false;
    FRAMEIfscList1->PnlTop->Visible = false;
    FRAMEIfscList1->PnlBottom->Visible = false;
    FRAMEIfscList1->ColumnSeq->Visible = true;
    FRAMEIfscList1->Column02->Visible = false;
    FRAMEIfscList1->Column03->Visible = false;
    FRAMEIfscList1->Column09->Visible = false;
    FRAMEIfscList1->Column10->Visible = false;
    FRAMEIfscList1->Column08->Visible = false;
    FRAMEIfscList1->Column04->Visible = false;
    FRAMEIfscList1->Column05->Visible = false;

    FRAMERoadList1->TvList->OptionsView->GroupByBox = false;
    FRAMERoadList1->PnlTop->Visible = false;
    FRAMERoadList1->PnlBottom->Visible = false;
    FRAMERoadList1->ColumnSeq->Visible = true;
    FRAMERoadList1->Column02->Visible = false;
    FRAMERoadList1->Column03->Visible = false;

    PgLink->ActivePageIndex = 0;
    PgQry->ActivePageIndex = 0;
    PnlQry->Height = 190;

    FAtrdId = "";

    CbAtrd->Properties->Items->Clear();
    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;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIST0070M::CommClose()
{
    try
    {
        for (int idx = m_pFormList->Count-1; idx >= 0; idx--)
        {
            m_pFormList->Delete(idx);
        }
        delete m_pFormList;

        if (MyItsAtrdManager)
        {
            delete MyItsAtrdManager;
            MyItsAtrdManager = NULL;
        }

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

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

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

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

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

    DtStTime->Date = dtStart;
    DtEdTime->Date = dtEnd;
    //DtStTime->Time = StrToDateTime("00:00");
    //DtEdTime->Time = StrToDateTime("23:59");

    //시간대 검색
	CbStatType1->ItemIndex = 0;
    dtStrDate1->DateTime = Now()-1;
    dtEndDate1->DateTime = Now();//-1;
    CbStrTm1->ItemIndex = 7;
    CbEndTm1->ItemIndex = 7;

    //지정시각 검색
    CbStrTm2->ItemIndex = 7;
    CbEndTm2->ItemIndex = 7;
    InitQryList();
}
//---------------------------------------------------------------------------

void __fastcall TIST0070M::InitQryList()
{
    CMM_ClearGridTableView(TvList);

    int nRow = 0;
    try
    {
        TcxDataController *pGDC = TvList->DataController;;
        TvList->BeginUpdate();

        for (int ii = 0; ii < MAX_QRY_LIST; ii++)
        {
            nRow = pGDC->AppendRecord();

            pGDC->Values[nRow][ColumnSel->Index] = false;                 //선택
            pGDC->Values[nRow][Column00->Index]  = "-";                   //편집
            pGDC->Values[nRow][Column01->Index]  = (Now() - (MAX_QRY_LIST-ii)).FormatString("yyyy-MM-dd");
            pGDC->Values[nRow][Column02->Index]  = "07";
            pGDC->Values[nRow][Column03->Index]  = "07";
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

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

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

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

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

void __fastcall TIST0070M::CbStatTypePropertiesChange(TObject *Sender)
{
    switch(CbStatType->ItemIndex)
    {
    case 0: //15분 통계
    case 1: //시간 통계(1시간)
        DtStDate->Format = "  yyyy년 MM월 dd일";
        DtEdDate->Format = "  yyyy년 MM월 dd일";
        DtStTime->Visible = true;
        DtEdTime->Visible = true;
        break;
    case 2: //일 통계(1일)
        DtStDate->Format = "  yyyy년 MM월 dd일";
        DtEdDate->Format = "  yyyy년 MM월 dd일";
        DtStTime->Visible = false;
        DtEdTime->Visible = false;
        break;
    case 3: //월 통계(1개월)
        DtStDate->Format = "     yyyy년 MM월";
        DtEdDate->Format = "     yyyy년 MM월";
        DtStTime->Visible = false;
        DtEdTime->Visible = false;
        break;
    case 4: //년 통계(1년)
        DtStDate->Format = "         yyyy년";
        DtEdDate->Format = "         yyyy년";
        DtStTime->Visible = false;
        DtEdTime->Visible = false;
        break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIST0070M::PgQryPageChanging(TObject *Sender, TcxTabSheet *NewPage,
          bool &AllowChange)
{
    switch(NewPage->PageIndex)
    {
    case 0 :
        PnlQry->Height = 190;
        break;
    case 1 :
        PnlQry->Height = 220;
        break;
    case 2 :
        PnlQry->Height = 300;
        break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIST0070M::SearchTermLink(bool ALink/*=false*/)
{
    TcxGrid          *pCxList = NULL;
    TcxGridTableView *pTvList = NULL;
    int               nIdCol  = -1;

    if (PgLink->Properties->ActivePage->PageIndex == 0)
    {
        pCxList = FRAMELinkList1->CxList;
        pTvList = FRAMELinkList1->TvList;
        nIdCol  = FRAMELinkList1->Column01->Index;
        FLinkLevel = 0;
    }
    else
    if (PgLink->Properties->ActivePage->PageIndex == 1)
    {
        pCxList = FRAMEIfscList1->CxList;
        pTvList = FRAMEIfscList1->TvList;
        nIdCol  = FRAMEIfscList1->Column01->Index;
        FLinkLevel = 1;
    }
    else
    if (PgLink->Properties->ActivePage->PageIndex == 2)
    {
        pCxList = FRAMERoadList1->CxList;
        pTvList = FRAMERoadList1->TvList;
        nIdCol  = FRAMERoadList1->Column01->Index;
        FLinkLevel = 2;
    }
    if (!pTvList) return;

    if (ALink)
    {
        TcxDataController *pGDC = pTvList->DataController;
        int nIndex = pGDC->FocusedRecordIndex;
        if( nIndex < 0 )
        {
            Application->MessageBox(FrmLang->lblSelErr->Caption.c_str(),//L"링크를 먼저 선택 하세요.",
                                    FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
            ActiveControl = pCxList;
            return;
        }

        FLinkId = VarToStr(pGDC->Values[nIndex][nIdCol]);
    }
    else
    {
        if (FAtrdId == "")
        {
            Application->MessageBox(FrmLang->lblSelErr->Caption.c_str(),//L"간선도로를 먼저 선택 하세요.",
                                    FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
            ActiveControl = CbAtrd;
            return;
        }
        FLinkId = FAtrdId;
    }

    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");
        FStTime = sStTime + "0000";
        FEdTime = sEdTime + "5959";
        break;
    case 2: //일 통계(1일)
        sStTime = DtStDate->Date.FormatString("yyyyMMdd");
        sEdTime = DtEdDate->Date.FormatString("yyyyMMdd");
        FStTime = sStTime + "000000";
        FEdTime = sEdTime + "235959";
        break;
    case 3: //월 통계(1개월)
        sStTime = DtStDate->Date.FormatString("yyyyMM");
        sEdTime = DtEdDate->Date.FormatString("yyyyMM");
        sTemp.printf(L"%02d", ITSUtil_GetMonthLastDay(AnsiString(sEdTime).c_str()));
        FStTime = sStTime + "01000000";
        FEdTime = sEdTime + sTemp + "235959";
        break;
    case 4: //년 통계(1년)
        sStTime = DtStDate->Date.FormatString("yyyy");
        sEdTime = DtEdDate->Date.FormatString("yyyy");
        FStTime = sStTime + "0101000000";
        FEdTime = sEdTime + "1231235959";
        break;
    default:
        return;
    }

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

    FStatType = CbStatType->ItemIndex;

    String sTitle;
#if 0
	sTitle.printf(L"%s - %s(%s~%s) %s ", PgLink->Properties->ActivePage->Caption.c_str(),
                                         CbStatType->Text.c_str(),
                                         sStTime.c_str(),
                                         sEdTime.c_str(),
                                         FLinkId.c_str());
#endif
    if (ALink)
    {
        sTitle.printf(L"%s-(%s) %s ", PgLink->Properties->ActivePage->Caption.c_str(),
                                             CbStatType->Text.c_str(),
                                             FLinkId.c_str());
    }
    else
    {
        sTitle.printf(L"%s-(%s) %s ", PgLink->Properties->ActivePage->Caption.c_str(),
                                             CbStatType->Text.c_str(),
                                             FAtrdNm.c_str());
    }

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

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



        IST00701 = new TIST00701(this, Handle, FLinkId, sStTime, sEdTime, FLinkLevel, FStatType, ALink);
        IST00701->Parent = pSheet;
        IST00701->Show();
        PgTab->ActivePage = pSheet;
        pSheet = PgTab->ActivePage;
        m_pFormList->Add(IST00701);
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TIST0070M::SearchTimeLink(bool ALink/*=false*/)
{
    TcxGrid          *pCxList = NULL;
    TcxGridTableView *pTvList = NULL;
    int               nIdCol  = -1;

    if (PgLink->Properties->ActivePage->PageIndex == 0)
    {
        pCxList = FRAMELinkList1->CxList;
        pTvList = FRAMELinkList1->TvList;
        nIdCol  = FRAMELinkList1->Column01->Index;
        FLinkLevel = 0;
    }
    else
    if (PgLink->Properties->ActivePage->PageIndex == 1)
    {
        pCxList = FRAMEIfscList1->CxList;
        pTvList = FRAMEIfscList1->TvList;
        nIdCol  = FRAMEIfscList1->Column01->Index;
        FLinkLevel = 1;
    }
    else
    if (PgLink->Properties->ActivePage->PageIndex == 2)
    {
        pCxList = FRAMERoadList1->CxList;
        pTvList = FRAMERoadList1->TvList;
        nIdCol  = FRAMERoadList1->Column01->Index;
        FLinkLevel = 2;
    }
    if (!pTvList) return;

    if (ALink)
    {
        TcxDataController *pGDC = pTvList->DataController;
        int nIndex = pGDC->FocusedRecordIndex;
        if( nIndex < 0 )
        {
            Application->MessageBox(FrmLang->lblSelErr->Caption.c_str(),//L"링크를 먼저 선택 하세요.",
                                    FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
            ActiveControl = pCxList;
            return;
        }

        FLinkId = VarToStr(pGDC->Values[nIndex][nIdCol]);
    }
    else
    {
        if (FAtrdId == "")
        {
            Application->MessageBox(FrmLang->lblSelErr->Caption.c_str(),//L"간선도로를 먼저 선택 하세요.",
                                    FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
            ActiveControl = CbAtrd;
            return;
        }
        FLinkId = FAtrdId;
    }

    String sStTime, sEdTime, sTemp;
    switch(CbStatType->ItemIndex)
    {
    case 0: //15분 통계
    case 1: //시간 통계(1시간)
        sStTime = dtStrDate1->DateTime.FormatString("yyyyMMdd");
        sEdTime = dtEndDate1->DateTime.FormatString("yyyyMMdd");
        FStTime = sStTime + "000000";
        FEdTime = sEdTime + "235959";
        break;
    default:
        return;
    }

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

    if (CbStrTm1->ItemIndex > CbEndTm1->ItemIndex)
    {
        Application->MessageBox(FrmLang->lblQryHourErr->Caption.c_str(),//L"시간대 시작시각이 시간대 종료시각 보다 큽니다.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = CbStrTm1;
        return;
    }

    FStTerm.printf(L"%02d0000", CbStrTm1->ItemIndex);
    FEdTerm.printf(L"%02d5959", CbEndTm1->ItemIndex);

    FStatType = CbStatType1->ItemIndex;

    String sTitle;
#if 0
	sTitle.printf(L"%s - %s(%s~%s) %s ", PgLink->Properties->ActivePage->Caption.c_str(),
                                         CbStatType1->Text.c_str(),
                                         sStTime.c_str(),
                                         sEdTime.c_str(),
                                         FLinkId.c_str());
#endif
    if (ALink)
    {
        sTitle.printf(L"%s-(%s) %s ", PgLink->Properties->ActivePage->Caption.c_str(),
                                             CbStatType->Text.c_str(),
                                             FLinkId.c_str());
    }
    else
    {
        sTitle.printf(L"%s-(%s) %s ", PgLink->Properties->ActivePage->Caption.c_str(),
                                             CbStatType->Text.c_str(),
                                             FAtrdNm.c_str());
    }

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

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



        IST00702 = new TIST00702(this, Handle, FLinkId, FStTime, FEdTime, FLinkLevel, FStatType, FStTerm, FEdTerm, ALink);
        IST00702->Parent = pSheet;
        IST00702->Show();
        PgTab->ActivePage = pSheet;
        pSheet = PgTab->ActivePage;
        m_pFormList->Add(IST00702);
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TIST0070M::SearchSpotLink(bool ALink/*=false*/)
{
    TcxGrid          *pCxList = NULL;
    TcxGridTableView *pTvList = NULL;
    int               nIdCol  = -1;

    if (PgLink->Properties->ActivePage->PageIndex == 0)
    {
        pCxList = FRAMELinkList1->CxList;
        pTvList = FRAMELinkList1->TvList;
        nIdCol  = FRAMELinkList1->Column01->Index;
        FLinkLevel = 0;
    }
    else
    if (PgLink->Properties->ActivePage->PageIndex == 1)
    {
        pCxList = FRAMEIfscList1->CxList;
        pTvList = FRAMEIfscList1->TvList;
        nIdCol  = FRAMEIfscList1->Column01->Index;
        FLinkLevel = 1;
    }
    else
    if (PgLink->Properties->ActivePage->PageIndex == 2)
    {
        pCxList = FRAMERoadList1->CxList;
        pTvList = FRAMERoadList1->TvList;
        nIdCol  = FRAMERoadList1->Column01->Index;
        FLinkLevel = 2;
    }
    if (!pTvList) return;

    if (ALink)
    {
        TcxDataController *pGDC = pTvList->DataController;
        int nIndex = pGDC->FocusedRecordIndex;
        if( nIndex < 0 )
        {
            Application->MessageBox(FrmLang->lblSelErr->Caption.c_str(),//L"링크를 먼저 선택 하세요.",
                                    FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
            ActiveControl = pCxList;
            return;
        }

        FLinkId = VarToStr(pGDC->Values[nIndex][nIdCol]);
    }
    else
    {
        if (FAtrdId == "")
        {
            Application->MessageBox(FrmLang->lblSelErr->Caption.c_str(),//L"간선도로를 먼저 선택 하세요.",
                                    FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
            ActiveControl = CbAtrd;
            return;
        }
        FLinkId = FAtrdId;
    }

    int nSelects = CMM_GetCheckCount(TvList, ColumnSel->Index);
    if (0 == nSelects)
    {
        Application->MessageBox(FrmLang->lblSelNoDate->Caption.c_str(),//L"검색 지정일을 선택하지 않았습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = CxList;
        return;
    }

    bool bDateCheck = true;
    int nRowCnt = TvList->ViewData->RecordCount;
    for (int ii = 0; ii < nRowCnt; ii++)
    {
        TcxCustomGridRow* ARow = TvList->ViewData->Rows[ii];
        if (((bool)ARow->Values[ColumnSel->Index]))
        {
            try
            {
                if (VarIsNull(ARow->Values[Column01->Index]))
                {
                    bDateCheck = false;
                    break;
                }
                String sSelDay = ARow->Values[Column01->Index];
                sSelDay = StringReplace(sSelDay, DateSeparator, "", TReplaceFlags() << rfReplaceAll);
                if (sSelDay.Length() != 8)
                {
                    bDateCheck = false;
                    break;
                }
            }
            catch(Exception &e)
            {
            }
        }
    }
    if (!bDateCheck)
    {
        Application->MessageBox(FrmLang->lblSelDtErr->Caption.c_str(),//L"검색 지정일의 일자가 올바르지 않았습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = CxList;
        return;
    }

    if (CbStrTm2->ItemIndex > CbEndTm2->ItemIndex)
    {
        Application->MessageBox(FrmLang->lblSelHourOver->Caption.c_str(),//L"시간대 시작시각이 시간대 종료시각 보다 큽니다.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = CbStrTm1;
        return;
    }

    FStTerm.printf(L"%02d0000", CbStrTm2->ItemIndex);
    FEdTerm.printf(L"%02d5959", CbEndTm2->ItemIndex);

    FStatType = CbStatType2->ItemIndex;

    String sTitle;
#if 0
	sTitle.printf(L"%s - %s %s ", PgLink->Properties->ActivePage->Caption.c_str(),
                                  CbStatType2->Text.c_str(),
                                  FLinkId.c_str());

#endif

    if (ALink)
    {
        sTitle.printf(L"%s-(%s) %s ", PgLink->Properties->ActivePage->Caption.c_str(),
                                             CbStatType->Text.c_str(),
                                             FLinkId.c_str());
    }
    else
    {
        sTitle.printf(L"%s-(%s) %s ", PgLink->Properties->ActivePage->Caption.c_str(),
                                             CbStatType->Text.c_str(),
                                             FAtrdNm.c_str());
    }
    try
    {
        Application->ProcessMessages();
        LockWindowUpdate(Handle);

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



        IST00703 = new TIST00703(this, Handle, FLinkId, FStTime, FEdTime, FLinkLevel, FStatType, FStTerm, FEdTerm, ALink);

        IST00703->FSelDayCnt = 0;
        int nRowCnt = TvList->ViewData->RecordCount;
        for (int ii = 0; ii < nRowCnt; ii++)
        {
            TcxCustomGridRow* ARow = TvList->ViewData->Rows[ii];
            if (((bool)ARow->Values[ColumnSel->Index]))
            {
                try
                {
                    TDateTime dt = VarToDateTime(ARow->Values[Column01->Index]);
                    String sSelDay = dt.FormatString("yyyyMMdd");
                    IST00703->FSelDay[IST00703->FSelDayCnt++] = sSelDay;
                }
                catch(Exception &e)
                {
                }
            }
        }

        IST00703->Parent = pSheet;
        IST00703->Show();
        PgTab->ActivePage = pSheet;
        pSheet = PgTab->ActivePage;
        m_pFormList->Add(IST00702);
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TIST0070M::ClearAtrdList()
{
    CMM_ClearGridTableView(FRAMELinkList1->TvList);
    CMM_ClearGridTableView(FRAMEIfscList1->TvList);
    CMM_ClearGridTableView(FRAMERoadList1->TvList);
}
//---------------------------------------------------------------------------

void __fastcall TIST0070M::FillAtrdList(String AAtrdId)
{
    TItsAtrd *pAtrd = MyItsAtrdManager->FLists.Find(AAtrdId);
    if (!pAtrd) return;

    FOR_STL(TItsAtrdRoad*, pAtrdRoad, pAtrd->FRoads)
    {
        TItsRoad *pRoad = ItsRoadManager->FLists.Find(pAtrdRoad->ROAD_ID);
        if (!pRoad) continue;
        FRAMERoadList1->AddRoad(pRoad->ROAD_ID);

        std::map<int, String>::iterator itPos;
        for(itPos = pRoad->FIfscs.begin(); itPos != pRoad->FIfscs.end(); ++itPos)
        {
            String sIfscId = itPos->second;
            TItsIfsc *pIfsc = ItsIfscManager->FLists.Find(sIfscId);
            if (!pIfsc) continue;
            FRAMEIfscList1->AddIfsc(pIfsc->IFSC_ID);

            std::map<int, String>::iterator itPos2;
            for(itPos2 = pIfsc->FLinks.begin(); itPos2 != pIfsc->FLinks.end(); ++itPos2)
            {
                String sLinkId = itPos2->second;
                TItsLink *pLink = ItsLinkManager->FLists.Find(sLinkId);
                if (!pLink) continue;
                FRAMELinkList1->AddLink(pLink->LINK_ID);
            }
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TIST0070M::CbAtrdPropertiesChange(TObject *Sender)
{
    ClearAtrdList();
    int nIdx = CbAtrd->ItemIndex;
    if (nIdx < 0 || nIdx >= CbAtrd->Properties->Items->Count)
    {
        return;
    }

    TItsAtrd *pObj = (TItsAtrd*)CbAtrd->Properties->Items->Objects[CbAtrd->ItemIndex];
    if (!pObj) return;

    FAtrdId = pObj->ATRD_ID;
    FAtrdNm = pObj->ATRD_NM;

    FillAtrdList(FAtrdId);
}
//---------------------------------------------------------------------------

void __fastcall TIST0070M::BtnSearch5Click(TObject *Sender)
{
    if (PgQry->Properties->ActivePage->PageIndex == 0)
    {
        SearchTermLink(true);
    }
    else
    if (PgQry->Properties->ActivePage->PageIndex == 1)
    {
        SearchTimeLink(true);
    }
    else
    if (PgQry->Properties->ActivePage->PageIndex == 2)
    {
        SearchSpotLink(true);
    }
}
//---------------------------------------------------------------------------

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

    SearchTermLink(false);
}
//---------------------------------------------------------------------------

void __fastcall TIST0070M::BtnSearch1Click(TObject *Sender)
{
    Application->ProcessMessages();
    TSqlCursor sqlCrs((TControl*)BtnSearch1);

    SearchTimeLink(false);
}
//---------------------------------------------------------------------------

void __fastcall TIST0070M::BtnSearch2Click(TObject *Sender)
{
    Application->ProcessMessages();
    TSqlCursor sqlCrs((TControl*)BtnSearch2);

    SearchSpotLink(false);
}
//---------------------------------------------------------------------------

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

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

