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

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

#include "AVIS020MF.h"
#include "AVIS0201F.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 "cxSplitter"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "FRAME_AviDtctListF"
#pragma resource "*.dfm"

TAVIS020M *AVIS020M = NULL;
//---------------------------------------------------------------------------
__fastcall TAVIS020M::TAVIS020M(TComponent* Owner)
    : TForm(Owner)
{
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

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

    FRAMEAviDtctList1->PnlTop->Visible = false;
    FRAMEAviDtctList1->PnlBottom->Visible = false;
    FRAMEAviDtctList1->ColumnSel->Visible = false;
}
//---------------------------------------------------------------------------

/*
 * 닫기버튼 이나 x버튼 클릭시 공통으로 처리하는 로직이들어간다..
 * Form과 DataModule class를 delete시킨다.
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TAVIS020M::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(...)
    {
    }
}
//---------------------------------------------------------------------------

/*
 * Form을 보여줄때 호출되는 event 메서드이다.
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TAVIS020M::FormShow(TObject *Sender)
{
    Refresh();
    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

/*
 * form 초기화
 *
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TAVIS020M::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");
}
//---------------------------------------------------------------------------

/*
 * Form이 Show되고 난 후 최초 1회 수행되는 타이머 이벤트
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TAVIS020M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;

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

/*
 * Refresh Data Event Function
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TAVIS020M::RefreshData()
{
    // 검색조건에 의한 링크 데이터 조회
    try
    {
        //SelListData();
	}
	__finally
	{
	}
}
//---------------------------------------------------------------------------

/*
 * Search 버튼 클릭 이벤트 핸들러
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TAVIS020M::BtnSearchClick(TObject *Sender)
{
    Application->ProcessMessages();
    TSqlCursor sqlCrs((TControl*)BtnSearch);

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

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

    if (!pTvList) return;

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

    FSelId = VarToStr(pGDC->Values[nIndex][nIdCol]);

    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;
    }

    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
    sTitle = VarToStr(pGDC->Values[nIndex][FRAMEAviDtctList1->Column02->Index]);
	sCaption.printf(L"%s [%s] ", CbStatType->Text.c_str(), sTitle.c_str());
#endif

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

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

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

/*
 * Close 버튼 클릭 이벤트 핸들러
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TAVIS020M::BtnCloseClick(TObject *Sender)
{
    Close();    
}
//---------------------------------------------------------------------------

/*
 * 조회결과 폼이 닫힐때 메시지를 받아 처리한다.
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TAVIS020M::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);
}
//---------------------------------------------------------------------------

/*
 * Tab Control의 Tab을 더블클릭하여 닫는다.
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TAVIS020M::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 TAVIS020M::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    AVIS020M = NULL;
    Action = caFree;    
}
//---------------------------------------------------------------------------

void __fastcall TAVIS020M::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 TAVIS020M::PgTabCanCloseEx(TObject *Sender, int ATabIndex, bool &ACanClose)
{
    int nActiveIdx = ATabIndex;
    if (nActiveIdx <= 0) return;

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

