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

#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 "CRSM000MF.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"

TCRSM000M *CRSM000M = NULL;

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

    FTitle = Caption;

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

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

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

void __fastcall TCRSM000M::FormInit()
{
    //TvList->OptionsView->NoDataToDisplayInfoText = "<교차로교통량 CCTV 상태현황>";
}
//---------------------------------------------------------------------------

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

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

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

    UpdateFacility();

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

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

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

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

#ifdef USE_SC_TBL
#if 0
    sQry = "SELECT A.IXR_ID AS CROSS_ID, A.IXR_NM AS CROSS_NAME, A.IXR_TYPE AS CROSS_TYPE, \r\n"
           "       B.CMRA_ID AS CAM_ID, B.ISTL_LCTN AS CAM_NAME,                           \r\n"
           "       TO_CHAR(C.CLCT_DT,'YYYYMMDDHH24MISS') AS CLCT_DATE,                     \r\n"
           "       (CASE WHEN NVL(C.CLCT_DT, SYSDATE-60/140) < SYSDATE - 10/1440           \r\n"
           "             THEN 'N' ELSE 'N' END) AS MISSING,                                \r\n"
           "       DECODE(C.CMRA_STTS, 1, '1', '0') AS STATUS                              \r\n"
           "  FROM TB_SC_IXR_MNGM A, TB_SC_CMRA_MNGM B, TB_SC_CMRA_STTS C                  \r\n"
           " WHERE A.IXR_ID = B.IXR_ID                                                     \r\n"
           "   AND B.CMRA_ID = C.CMRA_ID(+)                                                \r\n"
           "   AND A.USE_EN = 1                                                            \r\n"
           "   AND B.USE_EN = 1                                                            \r\n"
           " ORDER BY A.IXR_ID, B.CMRA_ID                                                  \r\n";
#else
#if 0
    sQry = "SELECT A.IXR_ID AS CROSS_ID, A.IXR_NM AS CROSS_NAME, A.IXR_TYPE AS CROSS_TYPE,          \r\n"
           "       B.CMRA_ID AS CAM_ID, D.DRCT_LCTN AS CAM_NAME,                                    \r\n"
           "       TO_CHAR(C.CLCT_DT,'YYYYMMDDHH24MISS') AS CLCT_DATE,                              \r\n"
           "       (CASE WHEN NVL(C.CLCT_DT, SYSDATE-60/140) < SYSDATE - 10/1440                    \r\n"
           "             THEN 'N' ELSE 'N' END) AS MISSING,                                         \r\n"
           "       DECODE(C.CMRA_STTS, 1, '1', '0') AS STATUS                                       \r\n"
           "  FROM TB_SC_IXR_MNGM A, TB_SC_CMRA_MNGM B, TB_SC_CMRA_STTS C, TB_SC_CMRA_DRCT_MNGM D   \r\n"
           " WHERE A.IXR_ID = B.IXR_ID                                                              \r\n"
           "   AND B.CMRA_ID = C.CMRA_ID(+)                                                         \r\n"
           "   AND A.USE_EN = 1                                                                     \r\n"
           "   AND B.USE_EN = 1                                                                     \r\n"
           "   AND A.IXR_ID = D.IXR_ID                                                              \r\n"
           "   AND B.CMRA_ID = D.CMRA_ID                                                            \r\n"
           " ORDER BY A.IXR_ID, B.CMRA_ID                                                           \r\n";
#else
    sQry = "SELECT A.IXR_ID AS CROSS_ID, A.IXR_NM AS CROSS_NAME, A.IXR_TYPE AS CROSS_TYPE,                     \r\n"
           "       B.CMRA_ID AS CAM_ID, D.DRCT_LCTN AS CAM_NAME,                                               \r\n"
           "       C.CLCT_DT AS CLCT_DATE,                                                                     \r\n"
           "       (CASE WHEN NVL(TO_DATE(C.CLCT_DT,'YYYYMMDDHH24MISS'), SYSDATE-60/140) < SYSDATE - 10/1440   \r\n"
           "             THEN 'N' ELSE 'N' END) AS MISSING,                                                    \r\n"
           "       DECODE(C.CMRA_STTS, 1, '1', '0') AS STATUS                                                  \r\n"
           "  FROM TB_SC_IXR_MNGM A, TB_SC_CMRA_MNGM B, TB_SC_CMRA_STTS C, TB_SC_CMRA_DRCT_MNGM D              \r\n"
           " WHERE A.IXR_ID = B.IXR_ID                                                                         \r\n"
           "   AND B.CMRA_ID = C.CMRA_ID(+)                                                                    \r\n"
           "   AND A.USE_EN = 1                                                                                \r\n"
           "   AND B.USE_EN = 1                                                                                \r\n"
           "   AND A.IXR_ID = D.IXR_ID                                                                         \r\n"
           "   AND B.CMRA_ID = D.CMRA_ID                                                                       \r\n"
           " ORDER BY A.IXR_ID, B.CMRA_ID                                                                      \r\n";
#endif

#endif
#else
    sQry =  "SELECT A.CROSS_ID, A.NAME AS CROSS_NAME, A.CROSS_TYPE,             \r\n"
            "       B.CAM_ID, B.NAME AS CAM_NAME,                               \r\n"
            "       C.UPDT_DT AS CLCT_DATE,                                     \r\n"
            "       (CASE WHEN NVL(TO_DATE(C.UPDT_DT, 'YYYYMMDDHH24MISS'),      \r\n"
            "                  SYSDATE-60/140) < SYSDATE - 10/1440              \r\n"
            "             THEN 'Y' ELSE 'N' END) AS MISSING,                    \r\n"
            "       DECODE(C.CMNC_STTS_CD, 'CMS0', '1', '0') AS STATUS          \r\n"
            "  FROM TB_CRS A, TB_CRS_CMRA B, TB_CRS_CMRA_STTS C                 \r\n"
            " WHERE A.CROSS_ID = B.CROSS_ID                                     \r\n"
            "   AND B.CAM_ID = C.CAM_ID(+)                                      \r\n"
            "   AND A.DEL_YN = 'N'                                              \r\n"
            "   AND B.DEL_YN = 'N'                                              \r\n"
            " ORDER BY A.CROSS_ID, B.CAM_ID                                     \r\n";
#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 CROSS_ID = pADO->FieldByName("CROSS_ID")->AsString;
                String CROSS_NAME = pADO->FieldByName("CROSS_NAME")->AsString;

                pGDC->Values[nRow][Col00->Index] = CROSS_ID + ": " + CROSS_NAME;

                pGDC->Values[nRow][Col10->Index] = CROSS_ID;
                pGDC->Values[nRow][Col01->Index] = CROSS_NAME;
                pGDC->Values[nRow][Col15->Index] = pADO->FieldByName("CROSS_TYPE")->AsString;

                pGDC->Values[nRow][Col11->Index] = pADO->FieldByName("CAM_ID")->AsString;
                pGDC->Values[nRow][Col05->Index] = pADO->FieldByName("CAM_NAME")->AsString;

                String CLCT_DATE = pADO->FieldByName("CLCT_DATE")->AsString;
                pGDC->Values[nRow][Col06->Index] = APP_FormatStr(CLCT_DATE, STR_DATETIME);
                //pGDC->Values[nRow][Col07->Index] = pADO->FieldByName("STATUS")->AsString;


                String MISSING = pADO->FieldByName("MISSING")->AsString;
                String STATUS = pADO->FieldByName("STATUS")->AsString;
                if (MISSING == "Y" || STATUS != "1")
                {
                    pGDC->Values[nRow][Col07->Index] = "이상";
                }
                else
                {
                    pGDC->Values[nRow][Col07->Index] = "정상";
                }
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TCRSM000M::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("TCRSM000M::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 TCRSM000M::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= FTitle;

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

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

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

void __fastcall TCRSM000M::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 TCRSM000M::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 TCRSM000M::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 TCRSM000M::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 TCRSM000M::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 TCRSM000M::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 TCRSM000M::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)
    {
    }
}
//---------------------------------------------------------------------------

