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

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

#include "IST0040MF.h"
#include "IST00401F.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 "cxCalendar"
#pragma resource "*.dfm"

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

#if 0
    FCodeIDTU = ItsCodeManager->FLists.Find("IDTU");
    CbInciType->Properties->Items->Clear();
	if (FCodeIDTU)
	{
		FOR_STL(TItsSubCode *, pSubCode, FCodeIDTU->FSubLists)
		{
			CbInciType->Properties->Items->Add("[" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
	CbInciType->ItemIndex = 0;
#endif
}
//---------------------------------------------------------------------------

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

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

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

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

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

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

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

    TcxGrid          *pCxList;
    TcxGridTableView *pTvList = NULL;

    String sStTime, sEdTime, sTemp;
    switch(CbStatTerm->ItemIndex)
    {
    case 0: //일간통계
        sStTime = DtStDate->Date.FormatString("yyyyMMdd");
        sEdTime = DtEdDate->Date.FormatString("yyyyMMdd");
        FStTime = sStTime + "000000";
        FEdTime = sEdTime + "235959";
        break;
    case 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 2: //년통계
        sStTime = DtStDate->Date.FormatString("yyyy");
        sEdTime = DtEdDate->Date.FormatString("yyyy");
        FStTime = sStTime + "0101000000";
        FEdTime = sEdTime + "1231235959";
        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;
    FTermType     = CbStatTerm->ItemIndex;
    FIncidentType = "";//ITSUtil_GetCode(CbInciType->Text);

    String sTitle;
    sTitle.printf(L"%s - %s(%s~%s) ", CbStatType->Text.c_str(),
                                      CbStatTerm->Text.c_str(),
                                      sStTime.c_str(),
                                      sEdTime.c_str());

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

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

        IST00401 = new TIST00401(this, Handle, FStatType, FTermType, FStTime, FEdTime, true, FIncidentType, sTitle);
        IST00401->Parent = pSheet;
        IST00401->Show();
        PgTab->ActivePage = pSheet;
        pSheet = PgTab->ActivePage;
        m_pFormList->Add(IST00401);
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

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

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

void __fastcall TIST0040M::CbStatTermPropertiesChange(TObject *Sender)
{
    switch(CbStatTerm->ItemIndex)
    {
    case 0: //일간통계
        DtStDate->Format = "  yyyy년 MM월 dd일";
        DtEdDate->Format = "  yyyy년 MM월 dd일";
        break;
    case 1: //월통계
        DtStDate->Format = "     yyyy년 MM월";
        DtEdDate->Format = "     yyyy년 MM월";
        break;
    case 2: //년통계
        DtStDate->Format = "         yyyy년";
        DtEdDate->Format = "         yyyy년";
        break;
    }
}
//---------------------------------------------------------------------------

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

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

