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

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

#include "VDSM000MF.h"
#include "FrmMainF.h"
#include "ITS_OPLibF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#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 "cxGridBandedTableView"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinBlack"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

TVDSM000M *VDSM000M = NULL;

//---------------------------------------------------------------------------
__fastcall TVDSM000M::TVDSM000M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FTitle = Caption;//"VDS 기본정보 관리";

    FFacilityType = "";
}
//---------------------------------------------------------------------------

void __fastcall TVDSM000M::CommClose()
{
    try
    {
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

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

void __fastcall TVDSM000M::FormInit()
{
    //TvList->OptionsView->NoDataToDisplayInfoText = "<VDS-검지기 상태현황>";
}
//---------------------------------------------------------------------------

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

void __fastcall TVDSM000M::BtnSearchClick(TObject *Sender)
{
    BtnSearch->Enabled = false;
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TVDSM000M::RefreshData()
{
    BtnSearch->Enabled = false;

    UpdateFacility();

    BtnSearch->Enabled = true;
}
//---------------------------------------------------------------------------

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

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

void __fastcall TVDSM000M::UpdateFacility()
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry =  "SELECT A.*,                                                                            \r\n"
            "       B.VDS_DTCT_NMBR, B.VDS_DTCT_ID, B.VDS_CMRA_NO, B.VDS_DTCT_NM,                   \r\n"
            "       B.ISTL_LANE, B.DETECT_LANE,                                                     \r\n"
            "       C.UPDT_DT,                                                                      \r\n"
            "       (SELECT CMMN_CD_KOR_NM                                                          \r\n"
            "          FROM TB_CMMN_CD                                                              \r\n"
            "         WHERE CMMN_CLSF_CD = 'VDST'                                                   \r\n"
            "           AND CMMN_CD      = A.VDS_TYPE_CD) AS VDS_TYPE_NM,                           \r\n"
            "       (SELECT CMMN_CD_KOR_NM                                                          \r\n"
            "          FROM TB_CMMN_CD                                                              \r\n"
            "         WHERE CMMN_CLSF_CD = 'VCD'                                                    \r\n"
            "           AND CMMN_CD      = B.DTCT_DIR) AS DTCT_DIR_NM,                              \r\n"
            "       (SELECT CMMN_CD_KOR_NM                                                          \r\n"
            "          FROM TB_CMMN_CD                                                              \r\n"
            "         WHERE CMMN_CLSF_CD = 'DTT'                                                    \r\n"
            "           AND CMMN_CD      = B.DTCT_TYPE) AS DTCT_TYPE_NM,                            \r\n"
            "       DECODE(NVL(DTCT_STTS, 2), 0, '정상', 3, 'Oscillation', '알수없음') AS STTS_NM,  \r\n"
            "       D.CLCT_DT, D.SPED, D.TFVL, NVL(D.CLCT_ERR, 'Y') AS CLCT_ERR                     \r\n"
            "  FROM (SELECT B.VDS_CTLR_NMBR, B.VDS_CTLR_ID, B.VDS_NM, B.VDS_TYPE_CD,                \r\n"
            "               CMNC_STTS_CD AS STTS_CD, CBOX_DOOR_STTS_CD AS DOOR_STTS_CD,             \r\n"
            "               DECODE(CMNC_STTS_CD, 'CMS0', '0', 'CMS1', '1', '2') AS COMM,            \r\n"
            "               DECODE(CBOX_DOOR_STTS_CD, 'CDS0', '0', 'CDS1', '1', '0') AS DOOR,       \r\n"
            "               NVL(A.CBOX_TMPR, 0) AS CBOX_TMPR,                                       \r\n"
            "               A.UPDT_DT AS STS00,                                                     \r\n"
            "               (SELECT CMMN_CD_KOR_NM                                                  \r\n"
            "                  FROM TB_CMMN_CD                                                      \r\n"
            "                 WHERE CMMN_CLSF_CD = 'CDS'                                            \r\n"
            "                   AND CMMN_CD      = A.CBOX_DOOR_STTS_CD) AS STS01,                   \r\n"
            "               (SELECT CMMN_CD_KOR_NM                                                  \r\n"
            "                  FROM TB_CMMN_CD                                                      \r\n"
            "                 WHERE CMMN_CLSF_CD = 'PAS'                                            \r\n"
            "                   AND CMMN_CD      = A.FAN_STTS_CD) AS STS02,                         \r\n"
            "               (SELECT CMMN_CD_KOR_NM                                                  \r\n"
            "                  FROM TB_CMMN_CD                                                      \r\n"
            "                 WHERE CMMN_CLSF_CD = 'HTS'                                            \r\n"
            "                   AND CMMN_CD      = A.HETR_STTS_CD) AS STS03,                        \r\n"
            "               (SELECT CMMN_CD_KOR_NM                                                  \r\n"
            "                  FROM TB_CMMN_CD                                                      \r\n"
            "                 WHERE CMMN_CLSF_CD = 'CDS'                                            \r\n"
            "                   AND CMMN_CD      = A.FRONT_DOOR_STTS_CD) AS STS04,                  \r\n"
            "               (SELECT CMMN_CD_KOR_NM                                                  \r\n"
            "                  FROM TB_CMMN_CD                                                      \r\n"
            "                 WHERE CMMN_CLSF_CD = 'CDS'                                            \r\n"
            "                   AND CMMN_CD      = A.BACK_DOOR_STTS_CD) AS STS05,                   \r\n"
            "               (SELECT CMMN_CD_KOR_NM                                                  \r\n"
            "                  FROM TB_CMMN_CD                                                      \r\n"
            "                 WHERE CMMN_CLSF_CD = 'VDI'                                            \r\n"
            "                   AND CMMN_CD      = A.VIDEO_INPUT) AS STS06,                         \r\n"
            "               CBOX_TMPR AS STS07, '0' AS STS08, '0' AS STS09, '0' AS STS10            \r\n"
            "          FROM (SELECT *                                                               \r\n"
            "                  FROM TB_VDS_CTLR_STTS                                                \r\n"
            "                 WHERE UPDT_DT >= TO_CHAR(SYSDATE-5/1440, 'YYYYMMDDHH24MISS')          \r\n"
            "               ) A,                                                                    \r\n"
            "               TB_VDS_CTLR B                                                           \r\n"
            "         WHERE B.DEL_YN = 'N'                                                          \r\n"
            "           AND A.VDS_CTLR_NMBR(+) = B.VDS_CTLR_NMBR                                    \r\n"
            "       ) A,                                                                            \r\n"
            "       TB_VDS_DTCT B,                                                                  \r\n"
            "       (SELECT *                                                                       \r\n"
            "          FROM TB_VDS_DTCT_STTS                                                        \r\n"
            "         WHERE UPDT_DT >= TO_CHAR(SYSDATE-5/1440, 'YYYYMMDDHH24MISS')                  \r\n"
            "       ) C,                                                                            \r\n"
            "       (SELECT VDS_DTCT_NMBR, CLCT_DT, SPED, TFVL,                                     \r\n"
            "               (CASE WHEN CLCT_DT <= TO_CHAR(SYSDATE - 5/1440, 'YYYYMMDDHH24MISS')     \r\n"
            "                     THEN 'Y' ELSE 'N' END) AS CLCT_ERR                                \r\n"
            "          FROM TB_VDS_DTCT_CLCT_PNST                                                   \r\n"
            "       ) D                                                                             \r\n"
            " WHERE B.DEL_YN = 'N'                                                                  \r\n"
            "   AND A.VDS_CTLR_NMBR = B.VDS_CTLR_NMBR                                               \r\n"
            "   AND B.VDS_DTCT_NMBR = C.VDS_DTCT_NMBR(+)                                            \r\n"
            "   AND B.VDS_DTCT_NMBR = D.VDS_DTCT_NMBR(+)                                            \r\n"
            " ORDER BY A.VDS_CTLR_NMBR, B.VDS_DTCT_NMBR, B.DETECT_LANE                              \r\n";

#if 0
SELECT C.VDS_CTLR_ID, COUNT(1) AS COL_CNT
  FROM TB_VDS_DTCT_CLCT_PNST A,
       TB_VDS_DTCT           B,
       TB_VDS_CTLR           C
 WHERE A.CLCT_DT >= TO_CHAR(SYSDATE-5/1440, 'YYYYMMDDHH24MISS')
   AND A.VDS_DTCT_NMBR = B.VDS_DTCT_NMBR
   AND B.VDS_CTLR_NMBR = C.VDS_CTLR_NMBR
 GROUP BY C.VDS_CTLR_ID
#endif
    try
    {
        int nRow = 0;
        int nDataCnt = 0;
        TcxDataController *pGDC = TvList->DataController;

        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();

            CMM_ClearGridTableView(TvList);
            TvList->BeginUpdate();

            nDataCnt = pADO->RecordCount;
            pGDC->RecordCount = nDataCnt;

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                String VDS_TYPE_CD = pADO->FieldByName("VDS_TYPE_CD")->AsString;

                pGDC->Values[nRow][Col00->Index] = pADO->FieldByName("VDS_CTLR_NMBR")->AsString;
                pGDC->Values[nRow][Col01->Index] = pADO->FieldByName("VDS_NM")->AsString;
                pGDC->Values[nRow][Col02->Index] = pADO->FieldByName("ISTL_LANE")->AsString;
                pGDC->Values[nRow][Col03->Index] = pADO->FieldByName("DTCT_DIR_NM")->AsString;
                pGDC->Values[nRow][Col04->Index] = pADO->FieldByName("VDS_DTCT_NMBR")->AsString;
                pGDC->Values[nRow][Col05->Index] = pADO->FieldByName("VDS_DTCT_NM")->AsString;

                String dtctTime = pADO->FieldByName("UPDT_DT")->AsString;
                pGDC->Values[nRow][Col06->Index] = APP_FormatStr(dtctTime, STR_DATETIME);
                String ctlrTime = pADO->FieldByName("STS00")->AsString;
                pGDC->Values[nRow][Col08->Index] = APP_FormatStr(ctlrTime, STR_DATETIME);

                String sComm = pADO->FieldByName("COMM")->AsString;
                if (sComm == "0")
                {
                    pGDC->Values[nRow][Col07->Index] = pADO->FieldByName("STTS_NM")->AsString;
                    pGDC->Values[nRow][Col09->Index] = "정상";

                    if (VDS_TYPE_CD == "I")
                    {
                        pGDC->Values[nRow][Col16->Index] = pADO->FieldByName("STS04")->AsString; //앞문
                        pGDC->Values[nRow][Col17->Index] = pADO->FieldByName("STS05")->AsString; //뒷문
                        pGDC->Values[nRow][Col18->Index] = pADO->FieldByName("STS02")->AsString; //팬
                        pGDC->Values[nRow][Col19->Index] = pADO->FieldByName("STS03")->AsString; //히터
                        pGDC->Values[nRow][Col20->Index] = pADO->FieldByName("STS06")->AsString; //비디오입력
                        pGDC->Values[nRow][Col21->Index] = pADO->FieldByName("STS07")->AsString; //함체온도
                    }
                    else
                    {
                        pGDC->Values[nRow][Col16->Index] = "-";
                        pGDC->Values[nRow][Col17->Index] = "-";
                        pGDC->Values[nRow][Col18->Index] = "-";
                        pGDC->Values[nRow][Col19->Index] = "-";
                        pGDC->Values[nRow][Col20->Index] = "-";
                        pGDC->Values[nRow][Col21->Index] = "-";
                    }
                }
                else
                {
                    pGDC->Values[nRow][Col07->Index] = "알수없음";
                    pGDC->Values[nRow][Col09->Index] = "이상";

                    pGDC->Values[nRow][Col16->Index] = "-";
                    pGDC->Values[nRow][Col17->Index] = "-";
                    pGDC->Values[nRow][Col18->Index] = "-";
                    pGDC->Values[nRow][Col19->Index] = "-";
                    pGDC->Values[nRow][Col20->Index] = "-";
                    pGDC->Values[nRow][Col21->Index] = "-";
                }
                pGDC->Values[nRow][Col10->Index] = pADO->FieldByName("VDS_CTLR_ID")->AsString;
                pGDC->Values[nRow][Col11->Index] = pADO->FieldByName("VDS_DTCT_ID")->AsString;
                pGDC->Values[nRow][Col12->Index] = pADO->FieldByName("DETECT_LANE")->AsString;
                pGDC->Values[nRow][Col13->Index] = pADO->FieldByName("VDS_CMRA_NO")->AsString;
                pGDC->Values[nRow][Col14->Index] = pADO->FieldByName("DTCT_TYPE_NM")->AsString;
                pGDC->Values[nRow][Col15->Index] = pADO->FieldByName("VDS_TYPE_NM")->AsString;

                String CLCT_DT = pADO->FieldByName("CLCT_DT")->AsString;
                pGDC->Values[nRow][Col23->Index] = APP_FormatStr(CLCT_DT, STR_DATETIME);
                pGDC->Values[nRow][Col24->Index] = pADO->FieldByName("SPED")->AsString;
                pGDC->Values[nRow][Col25->Index] = pADO->FieldByName("TFVL")->AsString;
                pGDC->Values[nRow][Col26->Index] = pADO->FieldByName("CLCT_ERR")->AsString == "N" ? "정상" : "수집이상";
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVDSM000M::UpdateFacility", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVDSM000M::UpdateFacility", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
    }
    CMM_ExpandCollapseChk(TvList, ChkExpand->Checked);
}
//---------------------------------------------------------------------------

void __fastcall TVDSM000M::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= FTitle;

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

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

void __fastcall TVDSM000M::TvListDataControllerFilterChanged(TObject *Sender)
{
    CMM_SetFilterLike(TvList);
}
//---------------------------------------------------------------------------

void __fastcall TVDSM000M::Col07CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas,
          TcxGridTableDataCellViewInfo *AViewInfo, bool &ADone)
{
	if( AViewInfo )
	{
		if(AViewInfo->Text == "정상")
		{
			ACanvas->Canvas->Font->Color = clTeal;
		}
		//else if(AViewInfo->Text == "통신장애")
		//{
		//	ACanvas->Canvas->Font->Color = clRed;
		//}
		else if(AViewInfo->Text == "알수없음")
		{
			ACanvas->Canvas->Font->Color = clGray;
		}
		else
		{
			ACanvas->Canvas->Font->Color = clRed;
			//ACanvas->Canvas->Font->Color = clFuchsia;
		}
	}
}
//---------------------------------------------------------------------------

void __fastcall TVDSM000M::Col09CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas,
          TcxGridTableDataCellViewInfo *AViewInfo, bool &ADone)
{
	if( AViewInfo )
	{
		if(AViewInfo->Text == "정상")
		{
			ACanvas->Canvas->Font->Color = clTeal;
		}
		else
		{
			ACanvas->Canvas->Font->Color = clRed;
		}
	}
}
//---------------------------------------------------------------------------

void __fastcall TVDSM000M::Col26CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
	if( AViewInfo )
	{
		if(AViewInfo->Text == "정상")
		{
			ACanvas->Canvas->Font->Color = clTeal;
		}
		else
		{
			ACanvas->Canvas->Font->Color = clRed;
		}
	}
}
//---------------------------------------------------------------------------

void __fastcall TVDSM000M::Col16CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    //앞문/뒷문
	if( AViewInfo )
	{
		if(AViewInfo->Text == "닫힘")
		{
			ACanvas->Canvas->Font->Color = clTeal;
		}
		else
		{
			ACanvas->Canvas->Font->Color = clRed;
		}
	}
}
//---------------------------------------------------------------------------



void __fastcall TVDSM000M::Col18CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    //팬/히터
	if( AViewInfo )
	{
		if(AViewInfo->Text == "중지")
		{
			ACanvas->Canvas->Font->Color = clTeal;
		}
		else
		{
			ACanvas->Canvas->Font->Color = clRed;
		}
	}
}
//---------------------------------------------------------------------------

void __fastcall TVDSM000M::Col20CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    //비디오입력
	if( AViewInfo )
	{
		if(AViewInfo->Text.Pos("정상"))
		{
			ACanvas->Canvas->Font->Color = clTeal;
		}
		else
		{
			ACanvas->Canvas->Font->Color = clRed;
		}
	}

}
//---------------------------------------------------------------------------

void __fastcall TVDSM000M::Col21CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    try
    {
        TColor tBrush = ACanvas->Brush->Color;
        TColor tFont  = ACanvas->Font->Color;
        String sValue = AViewInfo->GridRecord->DisplayTexts[AViewInfo->Item->Index];
        if (sValue != "-" && sValue.ToIntDef(0) > g_AppCfg.Temp.AlarmValue)
        {
            tBrush = clFuchsia;
            tFont  = clWhite;
        }
        ACanvas->SetBrushColor(tBrush);
        ACanvas->Font->Color = tFont;
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

