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

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

#include "VDSS010MF.h"
#include "VDSS0101F.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_VdsDtctListF"
#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 resource "*.dfm"

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

    FTitle = Caption;//"VDS-검지기 소통정보 통계";
    m_pFormList = new TList();
    TsList01->TabVisible = false;

    FRAMEVdsDtctList1->PnlBottom->Visible = false;
    //FRAMEVdsDtctList1->ColumnSel->Visible = false;
}
//---------------------------------------------------------------------------

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

void __fastcall TVDSS010M::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");
}
//---------------------------------------------------------------------------

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

    FRAMEVdsDtctList1->UpdateList();
}
//---------------------------------------------------------------------------

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

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

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

    pCxList = FRAMEVdsDtctList1->CxList;
    pTvList = FRAMEVdsDtctList1->TvList;
    nIdCol  = FRAMEVdsDtctList1->Column02->Index;

    if (!pTvList) return;

    TcxDataController *pGDC = pTvList->DataController;
#if 0
	int nIndex = pGDC->FocusedRecordIndex;
	if( nIndex < 0 )
    {
        Application->MessageBox(FrmLang->lblSelErr->Caption.c_str(),//L"VDS-검지기를 먼저 선택 하세요.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = pCxList;
		return;
    }
    FSelId = VarToStr(pGDC->Values[nIndex][nIdCol]);
#else
    int nSelects = CMM_GetSelectCount(FRAMEVdsDtctList1->TvList, FRAMEVdsDtctList1->ColumnSel->Index, FRAMEVdsDtctList1->Column02->Index, FSelId);
    if (0 == nSelects)
    {
        Application->MessageBox(FrmLang->lblSelLstErr->Caption.c_str(),//L"검색 목록을 선택하지 않았습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        FRAMEVdsDtctList1->CxList->SetFocus();
        return;
    }
#endif

    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";
        FStTime = sStTime + "00000000";
        FEdTime = sEdTime + sTemp + "235959";
        break;
    }

    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;
	sTitle.printf(L"%s(%s~%s) %s ", CbStatType->Text.c_str(),
                                    sStTime.c_str(),
                                    sEdTime.c_str(),
                                    FSelId.c_str());

    String sCaption;
#if 0
	sCaption.printf(L"%s [%s] ", CbStatType->Text.c_str(), FSelId.c_str());
#else
	//sCaption.printf(L"%s [%s] ", CbStatType->Text.c_str(), FSelId.c_str());
    //sCaption = CMM_GetSelectTitle(FRAMEVdsDtctList1->TvList, FRAMEVdsDtctList1->ColumnSel->Index, FRAMEVdsDtctList1->Column02->Index);

    sCaption.printf(L" %s ", CbStatType->Text.c_str());
#endif

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

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

        VDSS0101 = new TVDSS0101(this, Handle, FSelId, sStTime, sEdTime, FStatType);
        VDSS0101->Parent = pSheet;
        VDSS0101->Show();
        PgTab->ActivePage = pSheet;
        pSheet = PgTab->ActivePage;
        m_pFormList->Add(VDSS0101);
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

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

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

void __fastcall TVDSS010M::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;
    }
}
//---------------------------------------------------------------------------

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

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

