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

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

#include "FrmCctvLogSttsF.h"

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

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxHint"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSpinEdit"
#pragma link "cxTextEdit"
#pragma link "dxBar"
#pragma link "dxCustomHint"
#pragma link "dxRibbon"
#pragma link "dxRibbonGallery"
#pragma link "dxRibbonSkins"
#pragma link "dxRibbonStatusBar"
#pragma link "dxScreenTip"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinChooserGallery"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinsdxBarPainter"
#pragma link "dxSkinsdxRibbonPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinsForm"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "dxStatusBar"
#pragma link "dxTabbedMDI"
#pragma link "cxButtons"
#pragma link "cxCalendar"
#pragma link "cxDropDownEdit"
#pragma link "cxGroupBox"
#pragma link "cxSplitter"
#pragma link "cxTimeEdit"
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxFilter"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxStyles"
#pragma resource "*.dfm"

TFrmCctvLogStts *FrmCctvLogStts = NULL;

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

__fastcall TFrmCctvLogStts::TFrmCctvLogStts(TComponent* Owner)
    : TForm(Owner)//TdxCustomRibbonForm(Owner)//TForm(Owner)
{
    DoubleBuffered = true;

    g_AppCfg.lMainWinHandle = (long)Handle;

    LangTrans->Translate(this, ITSDb_GetConnection());

    CMM_SetFormPosition(g_AppCfg.bSaveForm);

    ITSSkin_Initialize(Application);
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

#if 0
    String sCaption = "";
    ReadConfigInfo(g_AppCfg.sConfigFile, "APPLICATION", "CAPTION", sCaption);
    ITSSkin_Caption(sCaption == "1" ? true : false);
#endif

    SetColorScheme(g_AppCfg.sSkinName);

    FFormList = new TList();
    TsList01->TabVisible = false;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogStts::FormCreate(TObject *Sender)
{
    Application->ShowMainForm = false;

    try {
        Application->Icon->LoadFromResourceName(((unsigned int)HInstance), "MAINICON");
    } catch(...) { ShowMessage("LoadFromResourceName failed"); }

    Application->ShowMainForm = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogStts::FormShow(TObject *Sender)
{
    Application->ProcessMessages();
    FormInit();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogStts::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    Action = caFree;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogStts::CommClose()
{
    try
    {
        for (int idx = FFormList->Count-1; idx >= 0; idx--)
        {
            TFrmCctvLogSttsSub *pLogForm = (TFrmCctvLogSttsSub*)FFormList->Items[idx];
            delete pLogForm;
            FFormList->Delete(idx);
        }
        delete FFormList;

        ITSSkin_Term();
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogStts::SetColorScheme(String ASkinName)
{
    dxSkinController1->SkinName = ASkinName;
    dxSkinController1->Refresh();
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmCctvLogStts::FormInit()
{
    DtStrDate->Date = Now();
    DtStrTime->DateTime = StrToDateTime("00:00");//Now();   //오늘 00시00분부터
    //DtStrTime->DateTime = Now();
    DtEndDate->Date = Now();
    DtEndTime->DateTime = Now();

    FillCctvList();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogStts::FillCctvList()
{
    String     sQry;
    TADOQuery *pADO = NULL;

    CMM_ClearGridTableView(TvList);

#if 0
    sQry = "SELECT A.CCTV_CTLR_NMBR,       \r\n"
           "       A.CCTV_CTLR_ID,         \r\n"
           "       A.CCTV_NM,              \r\n"
           "       A.DEL_YN                \r\n"
           "  FROM TB_CCTV_CTLR A          \r\n"
           " ORDER BY A.CCTV_CTLR_NMBR     \r\n";
#else
    sQry = "SELECT A.CCTV_MNGM_NMBR AS CCTV_CTLR_NMBR,  \r\n"
           "       A.CCTV_CTLR_ID,                      \r\n"
           "       A.ISTL_LCTN_NM AS CCTV_NM,           \r\n"
           "       A.DEL_YN                             \r\n"
           "  FROM TB_CCTV_CTLR A                       \r\n"
           " WHERE A.CCTV_TYPE = 1                      \r\n"
           " ORDER BY A.CCTV_MNGM_NMBR                  \r\n";
#endif

    int nRow = 0;
    TcxDataController *pGDC = TvList->DataController;
    try
    {
        TvList->BeginUpdate(lsimImmediate);
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                String DEL_YN = pADO->FieldByName("DEL_YN")->AsString;
                if (DEL_YN == "Y") continue;

                String CCTV_CTLR_NMBR = pADO->FieldByName("CCTV_CTLR_NMBR")->AsString;
                String CCTV_CTLR_ID   = pADO->FieldByName("CCTV_CTLR_ID")->AsString;
                String ISTL_LCTN_NM   = pADO->FieldByName("CCTV_NM")->AsString;

                nRow = pGDC->AppendRecord();

                pGDC->Values[nRow][ColumnSel->Index] = false;
                pGDC->Values[nRow][Column00->Index]  = "-";
                pGDC->Values[nRow][Column01->Index]  = CCTV_CTLR_NMBR;
                pGDC->Values[nRow][Column02->Index]  = CCTV_CTLR_ID;
                pGDC->Values[nRow][Column03->Index]  = ISTL_LCTN_NM;

                pGDC->Values[nRow][Column11->Index]  = CCTV_CTLR_NMBR;
                pGDC->Values[nRow][Column12->Index]  = CCTV_CTLR_ID;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("CCTV Information Query", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("CCTV Information Query", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogStts::OnSubFormClose(TMessage Msg)
{
    int nActiveIdx = PgTab->ActivePageIndex;
    if (nActiveIdx <= 0) return;

    LockWindowUpdate(Handle);

    TcxTabSheet *pSheet = PgTab->ActivePage;
    if (pSheet)
    {
        delete pSheet;
    }
    nActiveIdx--;   // 첫번째 탭은 화면에 숨겨져 있다.
    FFormList->Delete(nActiveIdx);

    LockWindowUpdate(0);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogStts::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 TFrmCctvLogStts::BtnAllSelectClick(TObject *Sender)
{
    TcxButton *pBtn = (TcxButton*)Sender;
    CMM_CheckAllListItem(TvList, ColumnSel->Index, pBtn->Tag);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogStts::BtnSearchClick(TObject *Sender)
{
    String sStrDate = DtStrDate->Date.FormatString("yyyymmdd");
    String sStrTime = DtStrTime->DateTime.FormatString("HHnn");
    String sEndDate = DtEndDate->Date.FormatString("yyyymmdd");
    String sEndTime = DtEndTime->DateTime.FormatString("HHnn");
    FStrTime = sStrDate + sStrTime;
    FEndTime = sEndDate + sEndTime;

    if (FStrTime > FEndTime)
    {
        Application->MessageBox(lblText3->Caption.c_str(),//L"검색 시작시각이 검색 종료시각 보다 큽니다.",
                                Caption.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = DtStrDate;
        return;
    }
    FStrTime = FStrTime + "00";
    FEndTime = FEndTime + "59";

    FIdList = "";
    int nSelects = CMM_GetSelectCount(TvList, ColumnSel->Index, Column01->Index, FIdList);
    if (0 == nSelects)
    {
        Application->MessageBox(lblText4->Caption.c_str(),//L"CCTV가 선택되지 않았습니다.\r\nCCTV 목록에서 CCTV를 선택하십시요.",
                                Caption.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        CxList->SetFocus();
        return;
    }

    String sTitle = "[" + FStrTime.SubString(1,10) + "-" + FEndTime.SubString(1,10) + "]";

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

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

        TFrmCctvLogSttsSub *pLogForm = new TFrmCctvLogSttsSub(this, Handle, FStrTime, FEndTime, FIdList);
        pLogForm->Caption= Caption;
        pLogForm->Parent = pSheet;
        pLogForm->Show();
        PgTab->ActivePage = pSheet;
        pSheet = PgTab->ActivePage;
        FFormList->Add(pLogForm);
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

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

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

