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

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

#include "IHS00501F.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_ChartF"
#pragma link "cxButtons"
#pragma link "cxCheckBox"
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxEdit"
#pragma link "cxFilter"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

TIHS00501 *IHS00501 = NULL;

//---------------------------------------------------------------------------
__fastcall TIHS00501::TIHS00501(TComponent* Owner, HWND hHandle, String sFacType, String sFrom, String sTo, String sIdList, String sTypeNm)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FParent     = hHandle;
    FFacType    = sFacType;
    FStDateTime = sFrom;
    FEdDateTime = sTo;
    FIdList     = sIdList;
    FTypeNm     = sTypeNm;
}
//---------------------------------------------------------------------------
__fastcall TIHS00501::~TIHS00501(void)
{
}
//--------------------------------------------------------------------------

void __fastcall TIHS00501::FormInit()
{
    ADOQry->Connection = ITSDb_GetConnection();
    m_pGDC = TvList->DataController;

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<시설물 장애내역 정보>";

    LblSearch->Caption = FrmLang->lblQryCond->Caption + FFacType + " (" + FStDateTime.SubString(1, 12) + " ~ " + FEdDateTime.SubString(1, 12) + ")";
}
//---------------------------------------------------------------------------

void __fastcall TIHS00501::FormShow(TObject *Sender)
{
    FormInit();
    Refresh();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TIHS00501::CommClose()
{
    CMM_SaveForm(g_sFormsDir, this);
    //IHS00501 = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TIHS00501::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    Application->ProcessMessages();
    SelHistory();
}
//---------------------------------------------------------------------------

void __fastcall TIHS00501::ChkExpandClick(TObject *Sender)
{
    CMM_ExpandCollapseChk(TvList, ChkExpand->Checked);
}
//---------------------------------------------------------------------------

void __fastcall TIHS00501::SelHistory()
{
    TSqlCursor sqlCrs;

    String sQry;
    TADOQuery *pADO = ADOQry;

//'20140713000000' AND '20140713235959'

//    int nLevel = StrToInt(FIdList);
    int nLevel = 0;
    if      (FTypeNm == "CCTV")     nLevel = 1;
    else if (FTypeNm == "VMS")      nLevel = 2;
    else if (FTypeNm == "VDS")      nLevel = 3;
    else if (FTypeNm == "AVI")      nLevel = 4;
    else if (FTypeNm == "DSRC")     nLevel = 5;
    else if (FTypeNm == FrmLang->lblWebCam->Caption) nLevel = 6;
    else if (FTypeNm == FrmLang->lblCrsCam->Caption) nLevel = 7;
    else if (FTypeNm == "주차장")   nLevel = 8;
    else                            nLevel = 0; //전 체

#if 0
String cctv  = "SELECT 'CCTV' AS FTYPE, A.CCTV_CTLR_NMBR AS MNGR_ID, A.CCTV_CTLR_ID AS FCLT_ID,     \r\n"
               "       A.ISTL_LCTN_NM AS FCLT_NM,                                                   \r\n"
               "       B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT            \r\n"
               "  FROM TB_CCTV_CTLR A,                                                              \r\n"
               "       (SELECT CCTV_CTLR_NMBR, CONN_STTS_CD AS CONN_STTS_CD,                        \r\n"
               "               'CDS0' AS DOOR_STTS_CD, 0 AS CBOX_TMPR, CRTN_DT                      \r\n"
               "          FROM TB_CCTV_CTLR_STTS_HS                                                 \r\n"
               "         WHERE CRTN_DT BETWEEN :ctv1 AND :ctv2                                      \r\n"
               "           AND (CONN_STTS_CD      <> 'CMS0')                                        \r\n"
               "       ) B                                                                          \r\n"
               " WHERE A.CCTV_CTLR_NMBR = B.CCTV_CTLR_NMBR                                          \r\n";

String vms   = "SELECT 'VMS' AS FTYPE, A.VMS_CTLR_NMBR AS MNGR_ID, A.VMS_CTLR_ID AS FCLT_ID,        \r\n"
               "        A.VMS_NM AS FCLT_NM,                                                        \r\n"
               "        B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT           \r\n"
               "  FROM TB_VMS_CTLR A,                                                               \r\n"
               "       (SELECT VMS_CTLR_NMBR, CMNC_STTS_CD AS CONN_STTS_CD,                         \r\n"
               "               CBOX_DOOR_STTS_CD AS DOOR_STTS_CD, CBOX_TMPR, CRTN_DT                \r\n"
               "          FROM TB_VMS_CTLR_STTS_HS                                                  \r\n"
               "         WHERE CRTN_DT BETWEEN :vms1 AND :vms2                                      \r\n"
               "           AND (CMNC_STTS_CD <> 'CMS0' OR                                           \r\n"
               "                CBOX_DOOR_STTS_CD <> 'CDS0' OR                                      \r\n"
               "                CBOX_TMPR > :vms3)                                                  \r\n"
               "       ) B                                                                          \r\n"
               " WHERE A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR                                            \r\n";

String vds   = "SELECT 'VDS' AS FTYPE, A.VDS_CTLR_NMBR AS MNGR_ID, A.VDS_CTLR_ID AS FCLT_ID,                                \r\n"
               "        A.VDS_NM AS FCLT_NM,                                                                                \r\n"
               "        B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT                                   \r\n"
               "   FROM TB_VDS_CTLR A,                                                                                      \r\n"
               "        (SELECT VDS_CTLR_NMBR, CMNC_STTS_CD AS CONN_STTS_CD,                                                \r\n"
               "                DECODE(FRONT_DOOR_STTS_CD, 'CDS0', BACK_DOOR_STTS_CD, FRONT_DOOR_STTS_CD) AS DOOR_STTS_CD,  \r\n"
               "                CBOX_TMPR AS CBOX_TMPR, CRTN_DT                                                             \r\n"
               "           FROM TB_VDS_CTLR_STTS_HS                                                                         \r\n"
               "          WHERE CRTN_DT BETWEEN :vds1 AND :vds2                                                             \r\n"
               "            AND (CMNC_STTS_CD       <> 'CMS0' OR                                                            \r\n"
               "                 CBOX_DOOR_STTS_CD  <> 'CDS0' OR                                                            \r\n"
               "                 CBOX_TMPR > :vds3)                                                                         \r\n"
               "        ) B                                                                                                 \r\n"
               "  WHERE A.VDS_CTLR_NMBR = B.VDS_CTLR_NMBR                                                                   \r\n";

String avi   = "SELECT 'AVI' AS FTYPE, A.AVI_CTLR_MNGM_NMBR AS MNGR_ID, A.AVI_ID AS FCLT_ID, A.AVI_LCTN_NM AS FCLT_NM, \r\n"
               "       B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT                               \r\n"
               "  FROM TB_AVI_CTLR A,                                                                                  \r\n"
               "       (SELECT AVI_CTLR_MNGM_NMBR, CONN_STTS_CD AS CONN_STTS_CD,                                       \r\n"
               "               CBOX_DOOR_STTS_CD AS DOOR_STTS_CD, CBOX_TMPR AS CBOX_TMPR, CRTN_DT                      \r\n"
               "          FROM TB_AVI_CTLR_STTS_HS                                                                     \r\n"
               "         WHERE CRTN_DT BETWEEN :avi1 AND :avi2                                                         \r\n"
               "           AND (CONN_STTS_CD      <> 'CMS0' OR                                                         \r\n"
               "                CBOX_DOOR_STTS_CD <> 'CDS0' OR                                                         \r\n"
               "                CBOX_TMPR > :avi3)                                                                     \r\n"
               "       ) B                                                                                             \r\n"
               " WHERE A.AVI_CTLR_MNGM_NMBR = B.AVI_CTLR_MNGM_NMBR                                                     \r\n";

String dsrc  = "SELECT 'DSRC' AS FTYPE, A.ID AS MNGR_ID, A.RSE_ID AS FCLT_ID, A.ISTL_LCTN_NM AS FCLT_NM, \r\n"
               "       B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CLCT_DT AS COLL_DT                 \r\n"
               "  FROM TB_RSE_MSTR A,                                                                    \r\n"
               "       (SELECT ID, 'CMS'||CMNC_STTS AS CONN_STTS_CD,                                     \r\n"
               "               '-' AS DOOR_STTS_CD, 0 AS CBOX_TMPR, CLCT_DT                              \r\n"
               "          FROM TB_RSE_STTS_HS                                                            \r\n"
               "         WHERE CLCT_DT BETWEEN :dsrc1 AND :dsrc2                                         \r\n"
               "           AND (CMNC_STTS <> '0')                                                        \r\n"
               "       ) B                                                                               \r\n"
               " WHERE A.ID = B.ID                                                                       \r\n";

#ifdef USE_WEB_CMRA
String wcam  = "SELECT '웹카메라' AS FTYPE, A.WEB_CMRA_NMBR AS MNGR_ID, A.WEB_CMRA_ID AS FCLT_ID, A.ISTL_LCTN_NM AS FCLT_NM,    \r\n"
               "       B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT                                        \r\n"
               "  FROM TB_WEB_CMRA A,                                                                                           \r\n"
               "       (SELECT WEB_CMRA_NMBR, CMNC_STTS_CD AS CONN_STTS_CD,                                                     \r\n"
               "               '-' AS DOOR_STTS_CD, 0 AS CBOX_TMPR, CRTN_DT                                                     \r\n"
               "          FROM TB_WEB_CMRA_STTS_HS                                                                              \r\n"
               "         WHERE CRTN_DT BETWEEN :wcam1 AND :wcam2                                                                \r\n"
               "           AND (CMNC_STTS_CD <> 'CMS0')                                                                         \r\n"
               "       ) B                                                                                                      \r\n"
               " WHERE A.WEB_CMRA_NMBR = B.WEB_CMRA_NMBR                                                                        \r\n";
#else
String wcam  = "SELECT '웹카메라' AS FTYPE, A.WCAM_CTLR_NMBR AS MNGR_ID, A.WCAM_CTLR_ID AS FCLT_ID, A.ISTL_LCTN_NM AS FCLT_NM,  \r\n"
               "       B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT                                        \r\n"
               "  FROM TB_WCAM_CTLR A,                                                                                          \r\n"
               "       (SELECT WCAM_CTLR_NMBR, CMNC_STTS_CD AS CONN_STTS_CD,                                                    \r\n"
               "               '-' AS DOOR_STTS_CD, 0 AS CBOX_TMPR, CRTN_DT                                                     \r\n"
               "          FROM TB_WCAM_CTLR_STTS_HS                                                                             \r\n"
               "         WHERE CRTN_DT BETWEEN :wcam1 AND :wcam2                                                                \r\n"
               "           AND (CMNC_STTS_CD <> 'CMS0')                                                                         \r\n"
               "       ) B                                                                                                      \r\n"
               " WHERE A.WCAM_CTLR_NMBR = B.WCAM_CTLR_NMBR                                                                      \r\n";
#endif
String ccam  = "SELECT '교차로감시' AS FTYPE, A.CCAM_CTLR_NMBR AS MNGR_ID, A.CCAM_CTLR_ID AS FCLT_ID,                       \r\n"
               "        A.CCAM_NM AS FCLT_NM,                                                                               \r\n"
               "        B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT                                   \r\n"
               "   FROM TB_CCAM_CTLR A,                                                                                     \r\n"
               "        (SELECT CCAM_CTLR_NMBR, CMNC_STTS_CD AS CONN_STTS_CD,                                               \r\n"
               "                DECODE(FRONT_DOOR_STTS_CD, 'CDS0', BACK_DOOR_STTS_CD, FRONT_DOOR_STTS_CD) AS DOOR_STTS_CD,  \r\n"
               "                CBOX_TMPR AS CBOX_TMPR, CRTN_DT                                                             \r\n"
               "           FROM TB_CCAM_CTLR_STTS_HS                                                                        \r\n"
               "          WHERE CRTN_DT BETWEEN :ccam1 AND :ccam2                                                           \r\n"
               "            AND (CMNC_STTS_CD       <> 'CMS0' OR                                                            \r\n"
               "                 CBOX_DOOR_STTS_CD  <> 'CDS0' OR                                                            \r\n"
               "                 CBOX_TMPR > :ccam3)                                                                        \r\n"
               "        ) B                                                                                                 \r\n"
               "  WHERE A.CCAM_CTLR_NMBR = B.CCAM_CTLR_NMBR                                                                 \r\n";
String park  = "SELECT '주차장' AS FTYPE, A.PRLT_CTLR_NMBR AS MNGR_ID, A.PRLT_CTLR_ID AS FCLT_ID, A.PRLT_NM AS FCLT_NM,     \r\n"
               "       B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT                                    \r\n"
               "  FROM TB_PRLT_CTLR A,                                                                                      \r\n"
               "       (SELECT PRLT_CTLR_NMBR, CMNC_STTS_CD AS CONN_STTS_CD,                                                \r\n"
               "               '-' AS DOOR_STTS_CD, 0 AS CBOX_TMPR, CRTN_DT                                                 \r\n"
               "          FROM TB_PRLT_CTLR_STTS_HS                                                                         \r\n"
               "         WHERE CRTN_DT BETWEEN :park1 AND :park2                                                            \r\n"
               "           AND (CMNC_STTS_CD <> 'CMS0')                                                                     \r\n"
               "       ) B                                                                                                  \r\n"
               " WHERE A.REAL_DATA_YN  = 'Y'                                                                                \r\n"
               "   AND A.PRLT_CTLR_NMBR = B.PRLT_CTLR_NMBR                                                                  \r\n";
#else
//TODO: GUMI
String cctv  = "SELECT 'CCTV' AS FTYPE, A.CCTV_MNGM_NMBR||CHR(0) AS MNGR_ID, A.CCTV_CTLR_ID AS FCLT_ID, \r\n"
               "       A.ISTL_LCTN_NM AS FCLT_NM,                                                       \r\n"
               "       B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT                \r\n"
               "  FROM TB_CCTV_CTLR A,                                                                  \r\n"
               "       (SELECT CCTV_MNGM_NMBR, CMNC_STTS_CD AS CONN_STTS_CD,                            \r\n"
               "               CBOX_DOOR_STTS_CD AS DOOR_STTS_CD, CBOX_TMPR, CRTN_DT                    \r\n"
               "          FROM TB_CCTV_STTS_HS                                                          \r\n"
               "         WHERE CRTN_DT BETWEEN :ctv1 AND :ctv2                                          \r\n"
               "           AND (CMNC_STTS_CD      <> 'CMS0' OR                                          \r\n"
               "                CBOX_DOOR_STTS_CD <> 'CDS0' OR                                          \r\n"
               "                CBOX_TMPR > :ctv3)                                                      \r\n"
               "       ) B                                                                              \r\n"
               " WHERE A.CCTV_MNGM_NMBR = B.CCTV_MNGM_NMBR                                              \r\n";

String vms   = "SELECT 'VMS' AS FTYPE, A.VMS_CTLR_NMBR||CHR(0) AS MNGR_ID, A.VMS_CTLR_ID AS FCLT_ID,    \r\n"
               "        A.VMS_NM AS FCLT_NM,                                                            \r\n"
               "       B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.RGST_DT AS COLL_DT                \r\n"
               "  FROM TB_VMS_CTLR A,                                                                   \r\n"
               "       (SELECT VMS_CTLR_NMBR, CONN_STTS_CD AS CONN_STTS_CD,                             \r\n"
               "               CBOXDOOR_OPEN_STTS_CD AS DOOR_STTS_CD, CBOX_TMPR, RGST_DT                \r\n"
               "          FROM TB_VMS_STTS_HS                                                           \r\n"
               "         WHERE RGST_DT BETWEEN :vms1 AND :vms2                                          \r\n"
               "           AND (CONN_STTS_CD          <> 'CMS0' OR                                      \r\n"
               "                CBOXDOOR_OPEN_STTS_CD <> 'CDS0' OR                                      \r\n"
               "                CBOX_TMPR > :vms3)                                                      \r\n"
               "       ) B                                                                              \r\n"
               " WHERE A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR                                                \r\n";

String vds   = "SELECT 'VDS' AS FTYPE, A.VDS_CTLR_NMBR||CHR(0) AS MNGR_ID, A.VDS_CTLR_ID AS FCLT_ID,                        \r\n"
               "        A.VDS_NM AS FCLT_NM,                                                                                \r\n"
               "        B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT                                   \r\n"
               "   FROM TB_VDS_CTLR A,                                                                                      \r\n"
               "        (SELECT VDS_CTLR_NMBR, CMNC_STTS_CD AS CONN_STTS_CD,                                                \r\n"
               "                DECODE(FRONT_DOOR_STTS_CD, 'CDS0', BACK_DOOR_STTS_CD, FRONT_DOOR_STTS_CD) AS DOOR_STTS_CD,  \r\n"
               "                CBOX_TMPR AS CBOX_TMPR, CRTN_DT                                                             \r\n"
               "           FROM TB_VDS_CTLR_STTS_HS                                                                         \r\n"
               "          WHERE CRTN_DT BETWEEN :vds1 AND :vds2                                                             \r\n"
               "            AND (CMNC_STTS_CD       <> 'CMS0' OR                                                            \r\n"
               "                 CBOX_DOOR_STTS_CD  <> 'CDS0' OR                                                            \r\n"
               "                 CBOX_TMPR > :vds3)                                                                         \r\n"
               "        ) B                                                                                                 \r\n"
               "  WHERE A.VDS_CTLR_NMBR = B.VDS_CTLR_NMBR                                                                   \r\n";

String avi   = "SELECT 'AVI' AS FTYPE, A.AVI_CTLR_MNGM_NMBR AS MNGR_ID, A.AVI_ID AS FCLT_ID, A.AVI_LCTN_NM AS FCLT_NM, \r\n"
               "       B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT                               \r\n"
               "  FROM TB_AVI_CTLR A,                                                                                  \r\n"
               "       (SELECT AVI_CTLR_MNGM_NMBR, CONN_STTS_CD AS CONN_STTS_CD,                                       \r\n"
               "               CBOX_DOOR_STTS_CD AS DOOR_STTS_CD, CBOX_TMPR AS CBOX_TMPR, CRTN_DT                      \r\n"
               "          FROM TB_AVI_CTLR_STTS_HS                                                                     \r\n"
               "         WHERE CRTN_DT BETWEEN :avi1 AND :avi2                                                         \r\n"
               "           AND (CONN_STTS_CD      <> 'CMS0' OR                                                         \r\n"
               "                CBOX_DOOR_STTS_CD <> 'CDS0' OR                                                         \r\n"
               "                CBOX_TMPR > :avi3)                                                                     \r\n"
               "       ) B                                                                                             \r\n"
               " WHERE A.AVI_CTLR_MNGM_NMBR = B.AVI_CTLR_MNGM_NMBR                                                     \r\n";

String dsrc  = "SELECT 'DSRC' AS FTYPE, A.ID AS MNGR_ID, A.RSE_ID AS FCLT_ID, A.ISTL_LCTN_NM AS FCLT_NM, \r\n"
               "       B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CLCT_DT AS COLL_DT                 \r\n"
               "  FROM TB_RSE_MSTR A,                                                                    \r\n"
               "       (SELECT ID, 'CMS'||CMNC_STTS AS CONN_STTS_CD,                                     \r\n"
               "               '-' AS DOOR_STTS_CD, 0 AS CBOX_TMPR, CLCT_DT                              \r\n"
               "          FROM TB_RSE_STTS_HS                                                            \r\n"
               "         WHERE CLCT_DT BETWEEN :dsrc1 AND :dsrc2                                         \r\n"
               "           AND (CMNC_STTS <> '0')                                                        \r\n"
               "       ) B                                                                               \r\n"
               " WHERE A.ID = B.ID                                                                       \r\n";

#ifdef USE_WEB_CMRA
String wcam  = "SELECT '웹카메라' AS FTYPE, A.WEB_CMRA_NMBR||CHR(0) AS MNGR_ID, A.WEB_CMRA_ID AS FCLT_ID, A.ISTL_LCTN_NM AS FCLT_NM,    \r\n"
               "       B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT                                                \r\n"
               "  FROM TB_WEB_CMRA A,                                                                                                   \r\n"
               "       (SELECT WEB_CMRA_NMBR, CMNC_STTS_CD AS CONN_STTS_CD,                                                             \r\n"
               "               '-' AS DOOR_STTS_CD, 0 AS CBOX_TMPR, CRTN_DT                                                             \r\n"
               "          FROM TB_WEB_CMRA_STTS_HS                                                                                      \r\n"
               "         WHERE CRTN_DT BETWEEN :wcam1 AND :wcam2                                                                        \r\n"
               "           AND (CMNC_STTS_CD <> 'CMS0')                                                                                 \r\n"
               "       ) B                                                                                                              \r\n"
               " WHERE A.WEB_CMRA_NMBR = B.WEB_CMRA_NMBR                                                                                \r\n";
#else
String wcam  = "SELECT '웹카메라' AS FTYPE, A.WCAM_CTLR_NMBR||CHR(0) AS MNGR_ID, A.WCAM_CTLR_ID AS FCLT_ID, A.ISTL_LCTN_NM AS FCLT_NM,  \r\n"
               "       B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT                                                \r\n"
               "  FROM TB_WCAM_CTLR A,                                                                                                  \r\n"
               "       (SELECT WCAM_CTLR_NMBR, CMNC_STTS_CD AS CONN_STTS_CD,                                                            \r\n"
               "               '-' AS DOOR_STTS_CD, 0 AS CBOX_TMPR, CRTN_DT                                                             \r\n"
               "          FROM TB_WCAM_CTLR_STTS_HS                                                                                     \r\n"
               "         WHERE CRTN_DT BETWEEN :wcam1 AND :wcam2                                                                        \r\n"
               "           AND (CMNC_STTS_CD <> 'CMS0')                                                                                 \r\n"
               "       ) B                                                                                                              \r\n"
               " WHERE A.WCAM_CTLR_NMBR = B.WCAM_CTLR_NMBR                                                                              \r\n";
#endif

String ccam  = "SELECT '교차로감시' AS FTYPE, A.CCAM_CTLR_NMBR AS MNGR_ID, A.CCAM_CTLR_ID AS FCLT_ID,                       \r\n"
               "        A.CCAM_NM AS FCLT_NM,                                                                               \r\n"
               "        B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT                                   \r\n"
               "   FROM TB_CCAM_CTLR A,                                                                                     \r\n"
               "        (SELECT CCAM_CTLR_NMBR, CMNC_STTS_CD AS CONN_STTS_CD,                                               \r\n"
               "                DECODE(FRONT_DOOR_STTS_CD, 'CDS0', BACK_DOOR_STTS_CD, FRONT_DOOR_STTS_CD) AS DOOR_STTS_CD,  \r\n"
               "                CBOX_TMPR AS CBOX_TMPR, CRTN_DT                                                             \r\n"
               "           FROM TB_CCAM_CTLR_STTS_HS                                                                        \r\n"
               "          WHERE CRTN_DT BETWEEN :ccam1 AND :ccam2                                                           \r\n"
               "            AND (CMNC_STTS_CD       <> 'CMS0' OR                                                            \r\n"
               "                 CBOX_DOOR_STTS_CD  <> 'CDS0' OR                                                            \r\n"
               "                 CBOX_TMPR > :ccam3)                                                                        \r\n"
               "        ) B                                                                                                 \r\n"
               "  WHERE A.CCAM_CTLR_NMBR = B.CCAM_CTLR_NMBR                                                                 \r\n";
String park  = "SELECT '주차장' AS FTYPE, A.PRLT_CTLR_NMBR AS MNGR_ID, A.PRLT_CTLR_ID AS FCLT_ID, A.PRLT_NM AS FCLT_NM,     \r\n"
               "       B.CONN_STTS_CD, B.DOOR_STTS_CD, B.CBOX_TMPR, B.CRTN_DT AS COLL_DT                                    \r\n"
               "  FROM TB_PRLT_CTLR A,                                                                                      \r\n"
               "       (SELECT PRLT_CTLR_NMBR, CMNC_STTS_CD AS CONN_STTS_CD,                                                \r\n"
               "               '-' AS DOOR_STTS_CD, 0 AS CBOX_TMPR, CRTN_DT                                                 \r\n"
               "          FROM TB_PRLT_CTLR_STTS_HS                                                                         \r\n"
               "         WHERE CRTN_DT BETWEEN :park1 AND :park2                                                            \r\n"
               "           AND (CMNC_STTS_CD <> 'CMS0')                                                                     \r\n"
               "       ) B                                                                                                  \r\n"
               " WHERE A.REAL_DATA_YN  = 'Y'                                                                                \r\n"
               "   AND A.PRLT_CTLR_NMBR = B.PRLT_CTLR_NMBR                                                                  \r\n";
#endif

    switch(nLevel)
    {
    case 0:
    Column03->Visible = true;
    Column04->Visible = true;
    Column05->Visible = true;
    sQry = "SELECT '' AS FTYPE, '' AS MNGR_ID, '' AS FCLT_ID, '' AS FCLT_NM,                 \r\n"
           "       '' AS CONN_STTS_CD, '' AS DOOR_STTS_CD,  0 AS CBOX_TMPR, '' AS COLL_DT    \r\n"
           "  FROM DUAL WHERE 1 = 0                                                          \r\n";
#ifdef USE_CCTV
    sQry += "UNION ALL \r\n";
    sQry += cctv;
#endif
#ifdef USE_VMS
    sQry += "UNION ALL \r\n";
    sQry += vms;
#endif
#ifdef USE_VDS
    sQry += "UNION ALL \r\n";
    sQry += vds;
#endif
#ifdef USE_AVI
    sQry += "UNION ALL \r\n";
    sQry += avi;
#endif
#ifdef USE_DSRC
    sQry += "UNION ALL \r\n";
    sQry += dsrc;
#endif
#ifdef USE_WCAM
    sQry += "UNION ALL \r\n";
    sQry += wcam;
#endif
#ifdef USE_CCAM
    sQry += "UNION ALL \r\n";
    sQry += ccam;
#endif
#ifdef USE_PARK
    sQry += "UNION ALL \r\n";
    sQry += park;
#endif
        break;
    case 1: //CCTV
        Column03->Visible = true;
        Column04->Visible = true;
        Column05->Visible = true;
        sQry = cctv;
        break;
    case 2: //VMS
        Column03->Visible = true;
        Column04->Visible = true;
        Column05->Visible = true;
        sQry = vms;
        break;
    case 3: //VDS
        Column03->Visible = true;
        Column04->Visible = true;
        Column05->Visible = true;
        sQry = vds;
        break;
    case 4: //AVI
        Column03->Visible = true;
        //Column04->Visible = true;
        //Column05->Visible = true;
        sQry = avi;
        break;
    case 5: //DSRC
        Column03->Visible = true;
        Column04->Visible = false;
        Column05->Visible = false;
        sQry = dsrc;
        break;
    case 6: //웹카메라
        Column03->Visible = true;
        Column04->Visible = false;
        Column05->Visible = false;
        sQry = wcam;
        break;
    case 7: //교차로감시카메라
        Column03->Visible = true;
        //Column04->Visible = true;
        //Column05->Visible = true;
        sQry = ccam;
        break;
    case 8: //주차장
        Column03->Visible = true;
        Column04->Visible = false;
        Column05->Visible = false;
        sQry = wcam;
        break;
    default: return;
    }

    try
    {
        ITSDb_SQLText(pADO, sQry);

        switch(nLevel)
        {
        case 0:
#ifdef USE_CCTV
            ITSDb_SQLBind(pADO, "ctv1", FStDateTime);
            ITSDb_SQLBind(pADO, "ctv2", FEdDateTime);
            ITSDb_SQLBind(pADO, "ctv3", g_AppCfg.Temp.AlarmValue);
#endif
#ifdef USE_VMS
            ITSDb_SQLBind(pADO, "vms1", FStDateTime);
            ITSDb_SQLBind(pADO, "vms2", FEdDateTime);
            ITSDb_SQLBind(pADO, "vms3", g_AppCfg.Temp.AlarmValue);
#endif
#ifdef USE_VDS
            ITSDb_SQLBind(pADO, "vds1", FStDateTime);
            ITSDb_SQLBind(pADO, "vds2", FEdDateTime);
            ITSDb_SQLBind(pADO, "vds3", g_AppCfg.Temp.AlarmValue);
#endif
#ifdef USE_AVI
            ITSDb_SQLBind(pADO, "avi1", FStDateTime);
            ITSDb_SQLBind(pADO, "avi2", FEdDateTime);
            ITSDb_SQLBind(pADO, "avi3", g_AppCfg.Temp.AlarmValue);
#endif
#ifdef USE_DSRC
            ITSDb_SQLBind(pADO, "dsrc1", FStDateTime);
            ITSDb_SQLBind(pADO, "dsrc2", FEdDateTime);
#endif
#ifdef USE_WCAM
            ITSDb_SQLBind(pADO, "wcam1", FStDateTime);
            ITSDb_SQLBind(pADO, "wcam2", FEdDateTime);
#endif
#ifdef USE_CCAM
            ITSDb_SQLBind(pADO, "ccam1", FStDateTime);
            ITSDb_SQLBind(pADO, "ccam2", FEdDateTime);
            ITSDb_SQLBind(pADO, "ccam3", g_AppCfg.Temp.AlarmValue);
#endif
#ifdef USE_PARK
            ITSDb_SQLBind(pADO, "park1", FStDateTime);
            ITSDb_SQLBind(pADO, "park2", FEdDateTime);
#endif
            break;
        case 1:
            ITSDb_SQLBind(pADO, "ctv1", FStDateTime);
            ITSDb_SQLBind(pADO, "ctv2", FEdDateTime);
            ITSDb_SQLBind(pADO, "ctv3", g_AppCfg.Temp.AlarmValue);
            break;
        case 2:
            ITSDb_SQLBind(pADO, "vms1", FStDateTime);
            ITSDb_SQLBind(pADO, "vms2", FEdDateTime);
            ITSDb_SQLBind(pADO, "vms3", g_AppCfg.Temp.AlarmValue);
            break;
        case 3:
            ITSDb_SQLBind(pADO, "vds1", FStDateTime);
            ITSDb_SQLBind(pADO, "vds2", FEdDateTime);
            ITSDb_SQLBind(pADO, "vds3", g_AppCfg.Temp.AlarmValue);
            break;
        case 4:
            ITSDb_SQLBind(pADO, "avi1", FStDateTime);
            ITSDb_SQLBind(pADO, "avi2", FEdDateTime);
            ITSDb_SQLBind(pADO, "avi3", g_AppCfg.Temp.AlarmValue);
            break;
        case 5:
            ITSDb_SQLBind(pADO, "dsrc1", FStDateTime);
            ITSDb_SQLBind(pADO, "dsrc2", FEdDateTime);
            break;
        case 6:
            ITSDb_SQLBind(pADO, "wcam1", FStDateTime);
            ITSDb_SQLBind(pADO, "wcam2", FEdDateTime);
            break;
        case 7:
            ITSDb_SQLBind(pADO, "ccam1", FStDateTime);
            ITSDb_SQLBind(pADO, "ccam2", FEdDateTime);
            ITSDb_SQLBind(pADO, "ccam3", g_AppCfg.Temp.AlarmValue);
            break;
        case 8:
            ITSDb_SQLBind(pADO, "park1", FStDateTime);
            ITSDb_SQLBind(pADO, "park2", FEdDateTime);
            break;
        }
        ITSDb_SQLOpen(pADO);

        DspHistory();
    }
    catch(EDatabaseError &E)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQry);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &exception)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(exception.ClassName()), exception.Message, sQry);
        throw Exception(String(exception.ClassName()) + exception.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TIHS00501::DspHistory()
{
    TADOQuery *pADO = ADOQry;

    try
    {
        CMM_ClearGridTableView(TvList);

        m_pGDC = TvList->DataController;

        int nRow = 0;
        int nDataCnt = 0;
        try {
            TvList->BeginUpdate();

            nDataCnt = pADO->RecordCount;
            m_pGDC->RecordCount = nDataCnt;

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                m_pGDC->Values[nRow][Column07->Index] = pADO->FieldByName("FTYPE")->AsString;
                m_pGDC->Values[nRow][Column01->Index] = pADO->FieldByName("FCLT_ID")->AsString;
                m_pGDC->Values[nRow][Column02->Index] = pADO->FieldByName("FCLT_NM")->AsString;
                m_pGDC->Values[nRow][Column06->Index] = ITSUtil_StrToTime(pADO->FieldByName("COLL_DT")->AsString);
                
			    String sConn = pADO->FieldByName("CONN_STTS_CD")->AsString;
			    String sDoor = pADO->FieldByName("DOOR_STTS_CD")->AsString;

                if      (sConn == "CMS0") sConn = FrmLang->lblCMS0->Caption;//"정상";
                else if (sConn == "CMS1") sConn = FrmLang->lblCMS1->Caption;//"통신두절";
                else if (sConn == "-")    sConn = "-";
                else                      sConn = FrmLang->lblNull->Caption;//"정보없음";

                if      (sDoor == "CDS0") sDoor = FrmLang->lblCDS0->Caption;//"정상";
                else if (sDoor == "CDS1") sDoor = FrmLang->lblCDS1->Caption;//"열림";
                else if (sDoor == "-")    sDoor = "-";
                else                      sDoor = FrmLang->lblNull->Caption;//"정보없음";

                m_pGDC->Values[nRow][Column03->Index] = sConn;
                m_pGDC->Values[nRow][Column04->Index] = sDoor;
                m_pGDC->Values[nRow][Column05->Index] = pADO->FieldByName("CBOX_TMPR")->AsString;
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
            }

            TvList->EndUpdate();
            //CxList->SetFocus();
            LblRecords->Caption = FormatFloat("##,##0", m_pGDC->RecordCount) + FrmLang->lblEA->Caption;//" 건";

            String sEnd = FrmLang->lblQrySel->Caption + " [" + LblRecords->Caption + "]";//"데이터 " + LblRecords->Caption + " 이 조회 되었습니다.";
            Application->MessageBox(sEnd.c_str(),
                                    FrmLang->lblQryEnd->Caption.c_str(),//L"데이터 조회 완료",
                                    MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        }
    }
    catch(EDatabaseError &E)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(E.ClassName()), E.Message, pADO->SQL->Text);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(e.ClassName()), e.Message, pADO->SQL->Text);
        throw Exception(String(e.ClassName()) + e.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TIHS00501::OnMessage(TMessage &Msg)
{
    switch (Msg.Msg)
    {
    case WM_PARAM_DATABASE:
        if (WP_DB_SELECT_OK == Msg.WParam)
        {
            //ShowMessage("Select Ok");
        }
        break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIHS00501::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"시설물 장애내역";

    CMM_ExportToExcelFile(sTitle, pGrid, pView, this);
}
//---------------------------------------------------------------------------

void __fastcall TIHS00501::FormClose(TObject *Sender, TCloseAction &Action)
{
    POST_MSG(FParent, WM_SUBFORM_CLOSE, 0, 0);
    CommClose();
    //IHS00501 = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TIHS00501::FormDestroy(TObject *Sender)
{
    CommClose();
}
//---------------------------------------------------------------------------

void __fastcall TIHS00501::Column03CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
	if (AViewInfo)
	{
        if (AViewInfo->GridRecord->RecordIndex < 0) return;
        if (VarIsNull(AViewInfo->GridRecord->DisplayTexts[Column03->Index])) return;

        String sConn = AViewInfo->GridRecord->DisplayTexts[Column03->Index];

        if      (sConn == FrmLang->lblCMS0->Caption) ACanvas->Canvas->Brush->Color = clLime;
        else if (sConn == FrmLang->lblCMS1->Caption) ACanvas->Canvas->Brush->Color = clRed;
        else if (sConn == "-")                       ACanvas->Canvas->Brush->Color = clSilver;
        else                                         ACanvas->Canvas->Brush->Color = clSilver;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIHS00501::Column04CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
	if (AViewInfo)
	{
        if (AViewInfo->GridRecord->RecordIndex < 0) return;
        if (VarIsNull(AViewInfo->GridRecord->DisplayTexts[Column04->Index])) return;

        String sDoor = AViewInfo->GridRecord->DisplayTexts[Column04->Index];

        if      (sDoor == FrmLang->lblCDS0->Caption) ACanvas->Canvas->Brush->Color = clLime;
        else if (sDoor == FrmLang->lblCDS1->Caption) ACanvas->Canvas->Brush->Color = clRed;
        else if (sDoor == "-")                       ACanvas->Canvas->Brush->Color = clSilver;
        else                                         ACanvas->Canvas->Brush->Color = clSilver;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIHS00501::Column05CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    //if (Column05->Visible == false) return;

	if (AViewInfo)
	{
        if (AViewInfo->GridRecord->RecordIndex < 0) return;
        if (VarIsNull(AViewInfo->GridRecord->DisplayTexts[Column05->Index])) return;

        String sValue = AViewInfo->GridRecord->DisplayTexts[Column05->Index];
        int nValue = 0;
        if (TryStrToInt(sValue, nValue))
        {
            if (nValue > g_AppCfg.Temp.AlarmValue)
            {
                ACanvas->Canvas->Brush->Color = clRed;
                //ACanvas->Canvas->Font->Color = clBlack;
            }
            else
            {
                ACanvas->Canvas->Brush->Color = clLime;
            }
        }
    }
}
//---------------------------------------------------------------------------

