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

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

#include "IAN0050MF.h"
#include "IAN00501F.h"
#include "CDSCodeF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_LinkListF"
#pragma link "FRAME_RoadListF"
#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 "cxSplitter"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "cxLabel"
#pragma link "FRAME_IfscListF"
#pragma resource "*.dfm"

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

    FRAMELinkList1->PnlTop->Visible = false;
    FRAMELinkList1->PnlBottom->Visible = false;

    FRAMEIfscList1->PnlTop->Visible = false;
    FRAMEIfscList1->PnlBottom->Visible = false;

    FRAMERoadList1->PnlTop->Visible = false;
    FRAMERoadList1->PnlBottom->Visible = false;

    cxPageControl1->ActivePageIndex = 0;
}
//---------------------------------------------------------------------------

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

void __fastcall TIAN0050M::FormInit()
{
	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() - 60;
    DtDiffMon->Date = Now() - 30;
    DtStTime->Time = StrToDateTime("00:00");
    DtEdTime->Time = StrToDateTime("23:59");
}
//---------------------------------------------------------------------------

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

    FRAMELinkList1->UpdateList();
    FRAMEIfscList1->UpdateList();
    FRAMERoadList1->UpdateList();
}
//---------------------------------------------------------------------------

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

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

    TcxGrid          *pCxList;
    TcxGridTableView *pTvList = NULL;
    int               nIdCol  = -1;

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

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

	FBaseMon = DtBaseMon->Date.FormatString("yyyymm");
	FDiffMon = DtDiffMon->Date.FormatString("yyyymm");
    if (FBaseMon == FDiffMon)
    {
        Application->MessageBox(lblText1->Caption.c_str(),//L"기준월과 비교월이 같습니다. 다르게 선택 하세요.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = DtBaseMon;
        return;
    }

	FStTime = DtStTime->Time.FormatString("hhnn");
	FEdTime = DtEdTime->Time.FormatString("hhnn");
    if (FStTime > FEdTime)
    {
        Application->MessageBox(FrmLang->lblQryDtErr->Caption.c_str(),//L"검색 시작시각이 검색 종료시각 보다 큽니다.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = DtStTime;
        return;
    }

    FPatternType = CbPatternType->ItemIndex;
    FPatternWeek = ITSUtil_GetCode(CbPatternWeek->Text);

    String sTitle;
	sTitle.printf(L"%s - %s(%s)[%s vs %s]%s ", cxPageControl1->Properties->ActivePage->Caption.c_str(),
                                          CbPatternType->Text.t_str(),
                                          CbPatternWeek->Text.t_str(),
                                          FBaseMon.c_str(),
                                          FDiffMon.c_str(),
                                          FLinkId.c_str());

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

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



        IAN00501 = new TIAN00501(this, Handle, FLinkId, FBaseMon, FDiffMon, FStTime, FEdTime, FLinkLevel, FPatternType, FPatternWeek, CbPatternWeek->Text);
        IAN00501->Parent = pSheet;
        IAN00501->Show();
        PgTab->ActivePage = pSheet;
        pSheet = PgTab->ActivePage;
        m_pFormList->Add(IAN00501);
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

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

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

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

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

