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

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

#include "IST0050MF.h"
#include "IST00501F.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalendar"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxDropDownEdit"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSpinEdit"
#pragma link "cxSplitter"
#pragma link "cxTextEdit"
#pragma link "cxTimeEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

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

    CbType->Properties->Items->Clear();
#ifdef USE_CCTV
    CbType->Properties->Items->Add("CCTV");
#endif
#ifdef USE_VMS
    CbType->Properties->Items->Add("VMS");
#endif
#ifdef USE_VDS
    CbType->Properties->Items->Add("VDS");
#endif
#ifdef USE_AVI
    CbType->Properties->Items->Add("AVI");
#endif
#ifdef USE_DSRC
    CbType->Properties->Items->Add("DSRC");
#endif
#ifdef USE_WCAM
    CbType->Properties->Items->Add(FrmLang->lblWebCam->Caption);//"웹카메라");
#endif
#ifdef USE_CCAM
    CbType->Properties->Items->Add(FrmLang->lblCrsCam->Caption);//"웹카메라");
#endif
#ifdef USE_PARK
    CbType->Properties->Items->Add("주차장");
#endif
    //CbType->Properties->Items->Add("기상연계");

    CbType->ItemIndex = 0;
}
//---------------------------------------------------------------------------

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

void __fastcall TIST0050M::FormInit()
{
    DtStDate->Date = Now() - 1;
    DtEdDate->Date = Now() - 1;
    //DtStTime->EditValue = "00";//Now().FormatString("hh");
    //DtEdTime->EditValue = "23";//Now().FormatString("hh");
    DtStTime->Time = StrToDateTime("00:00");
    DtEdTime->Time = StrToDateTime("23:59");
}
//---------------------------------------------------------------------------

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

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

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

#if 0
	FStDateTime = DtStDate->Date.FormatString("yyyymmdd") + DtStTime->Time.FormatString("hh");
	FEdDateTime = DtEdDate->Date.FormatString("yyyymmdd") + DtEdTime->Time.FormatString("hh");
#else
	FStDateTime = DtStDate->Date.FormatString("yyyymmdd") + DtStTime->Time.FormatString("hhnn");
	FEdDateTime = DtEdDate->Date.FormatString("yyyymmdd") + DtEdTime->Time.FormatString("hhnn");
#endif
    if (FStDateTime > FEdDateTime)
    {
        Application->MessageBox(FrmLang->lblQryDtErr->Caption.c_str(),//L"검색 시작시각이 검색 종료시각 보다 큽니다.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = DtStDate;
        return;
    }
#if 0
    FStDateTime = FStDateTime + "0000";
    FEdDateTime = FEdDateTime + "5959";
#else
    FStDateTime = FStDateTime + "00";
    FEdDateTime = FEdDateTime + "59";
#endif

    String sTypeNm = CbType->Text.Trim();

    if (sTypeNm == "" || CbType->ItemIndex < 0)
    {
        Application->MessageBox(lblSelErr->Caption.c_str(),//L"시설물을 선택하세요.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = CbType;
        return;
    }

    FIdList = String(CbType->ItemIndex);

#if 0
    String sTitle = "[" + FStDateTime + "-" + FEdDateTime + "]";
#else
    String sTitle = "[" + sTypeNm + "]";
#endif

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

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

        IST00501 = new TIST00501(this, Handle, CbType->Text, FStDateTime, FEdDateTime, FIdList, sTypeNm);
        IST00501->Parent = pSheet;
        IST00501->Show();
        PgTab->ActivePage = pSheet;
        pSheet = PgTab->ActivePage;
        m_pFormList->Add(IST00501);
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

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

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

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

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

