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

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

TVDSM400M *VDSM400M = NULL;

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

    FTitle = Caption;

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

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

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

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

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

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

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

    UpdateFacility();

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

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

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

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

#if 0
    sQry =  "SELECT B.VDS_CTLR_NMBR AS VDS_CTLR_NMBR, B.VDS_CTLR_ID AS VDS_CTLR_ID, B.ISTL_LCTN_NM,  \r\n"
            "       B.VDS_NM, B.VDS_TYPE_CD, A.UPDT_DT AS 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      = B.VDS_TYPE_CD) AS VDS_TYPE_NM,                            \r\n"
            "       A.CMNC_STTS_CD AS CMNC_STTS_CD,                                                  \r\n"
            "       (CASE WHEN A.UPDT_DT <= TO_CHAR(SYSDATE - 5/1440, 'YYYYMMDDHH24MISS')            \r\n"
            "             THEN 'Y' ELSE 'N' END) AS COMMERR,                                         \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 CBOX_DOOR_STTS_CD,                \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 FAN_STTS_CD,                            \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 HETR_STTS_CD,                          \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 FRONT_DOOR_STTS_CD,              \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 BACK_DOOR_STTS_CD,                \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 VIDEO_INPUT,                            \r\n"
            "       NVL(A.CBOX_TMPR, 0) AS CBOX_TMPR                                                 \r\n"
            "  FROM TB_VDS_CTLR_STTS A, TB_VDS_CTLR B                                                \r\n"
            " WHERE B.DEL_YN = 'N'                                                                   \r\n"
            "   AND A.VDS_CTLR_NMBR(+) = B.VDS_CTLR_NMBR                                             \r\n"
            " ORDER BY B.VDS_CTLR_NMBR                                                               \r\n";
#else
    sQry =  "SELECT B.VDS_CTLR_NMBR AS VDS_CTLR_NMBR, B.VDS_CTLR_ID AS VDS_CTLR_ID, B.ISTL_LCTN_NM,  \r\n"
            "       B.VDS_NM, B.VDS_TYPE_CD, A.UPDT_DT AS 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      = B.VDS_TYPE_CD) AS VDS_TYPE_NM,                            \r\n"
            "       A.CMNC_STTS_CD AS CMNC_STTS_CD,                                                  \r\n"
            "       (CASE WHEN A.UPDT_DT <= TO_CHAR(SYSDATE - 5/1440, 'YYYYMMDDHH24MISS')            \r\n"
            "             THEN 'Y' ELSE 'N' END) AS COMMERR,                                         \r\n"
            "       A.CBOX_DOOR_STTS_CD   AS CBOX_DOOR_STTS_CD,                                      \r\n"
            "       A.FAN_STTS_CD         AS FAN_STTS_CD,                                            \r\n"
            "       A.HETR_STTS_CD        AS HETR_STTS_CD,                                           \r\n"
            "       A.FRONT_DOOR_STTS_CD  AS FRONT_DOOR_STTS_CD,                                     \r\n"
            "       A.BACK_DOOR_STTS_CD   AS BACK_DOOR_STTS_CD,                                      \r\n"
            "       A.VIDEO_INPUT         AS VIDEO_INPUT,                                            \r\n"
            "       NVL(A.CBOX_TMPR, 0)   AS CBOX_TMPR                                               \r\n"
            "  FROM TB_VDS_CTLR_STTS A, TB_VDS_CTLR B                                                \r\n"
            " WHERE B.DEL_YN = 'N'                                                                   \r\n"
            "   AND A.VDS_CTLR_NMBR(+) = B.VDS_CTLR_NMBR                                             \r\n"
            " ORDER BY B.VDS_CTLR_NMBR                                                               \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 VDS_TYPE_CD = pADO->FieldByName("VDS_TYPE_CD")->AsString;

                pGDC->Values[nRow][Col00->Index] = pADO->FieldByName("VDS_CTLR_NMBR")->AsString;
                pGDC->Values[nRow][Col10->Index] = pADO->FieldByName("VDS_CTLR_ID")->AsString;
                pGDC->Values[nRow][Col01->Index] = pADO->FieldByName("VDS_NM")->AsString;
                pGDC->Values[nRow][Col15->Index] = pADO->FieldByName("VDS_TYPE_NM")->AsString;

                String UPDT_DT = pADO->FieldByName("UPDT_DT")->AsString;

                pGDC->Values[nRow][ColCtrlS->Index] = pADO->FieldByName("COMMERR")->AsString;

                if (UPDT_DT != "")
                {
                    pGDC->Values[nRow][Col08->Index] = APP_FormatStr(UPDT_DT, STR_DATETIME);

                    String CMNC_STTS_CD = pADO->FieldByName("CMNC_STTS_CD")->AsString;
                    if (CMNC_STTS_CD == "CMS0") pGDC->Values[nRow][Col09->Index] = lblMOS0->Caption;
                    else
                    if (CMNC_STTS_CD == "CMS1") pGDC->Values[nRow][Col09->Index] = lblMOS1->Caption;
                    else                        pGDC->Values[nRow][Col09->Index] = lblUnknown->Caption;

#if 0
                    //도어  ==> 여기서는 사용안함, 전체 도어 상태 볼때 사용
                    String sDoor = pADO->FieldByName("CBOX_DOOR_STTS_CD")->AsString;
                    if      (sDoor == "CDS0") pGDC->Values[nRow][ColDoorS->Index] = lblCDS0->Caption;//"닫힘";
                    else if (sDoor == "CDS1") pGDC->Values[nRow][ColDoorS->Index] = lblCDS1->Caption;//"열림";
                    else                      pGDC->Values[nRow][ColDoorS->Index] = lblUnknown->Caption;
#endif

                    //앞문
                    String sFDoor = pADO->FieldByName("FRONT_DOOR_STTS_CD")->AsString;
                    if      (sFDoor == "CDS0") pGDC->Values[nRow][Col16->Index] = lblCDS0->Caption;//"닫힘";
                    else if (sFDoor == "CDS1") pGDC->Values[nRow][Col16->Index] = lblCDS1->Caption;//"열림";
                    else                       pGDC->Values[nRow][Col16->Index] = lblUnknown->Caption;

                    //뒷문
                    String sBDoor = pADO->FieldByName("BACK_DOOR_STTS_CD")->AsString;
                    if      (sBDoor == "CDS0") pGDC->Values[nRow][Col17->Index] = lblCDS0->Caption;//"닫힘";
                    else if (sBDoor == "CDS1") pGDC->Values[nRow][Col17->Index] = lblCDS1->Caption;//"열림";
                    else                       pGDC->Values[nRow][Col17->Index] = lblUnknown->Caption;

                    //팬상태
                    String sFans = pADO->FieldByName("FAN_STTS_CD")->AsString;
                    if      (sFans == "PAS0") pGDC->Values[nRow][Col18->Index] = lblPAS0->Caption;//"동작";
                    else if (sFans == "PAS1") pGDC->Values[nRow][Col18->Index] = lblPAS1->Caption;//"정지";
                    else                      pGDC->Values[nRow][Col18->Index] = lblUnknown->Caption;

                    //히터상태
                    String sHets = pADO->FieldByName("HETR_STTS_CD")->AsString;
                    if      (sHets == "HTS0") pGDC->Values[nRow][Col19->Index] = lblHTS0->Caption;//"가동";
                    else if (sHets == "HTS1") pGDC->Values[nRow][Col19->Index] = lblHTS1->Caption;//"중지";
                    else                      pGDC->Values[nRow][Col19->Index] = lblUnknown->Caption;

#if 0
                    //비디오입력 ==> 제어기 상태정보 없음
                    String sVdi = pADO->FieldByName("HETR_STTS_CD")->AsString;
                    if      (sVdi == "VDI0") pGDC->Values[nRow][Col20->Index] = lblHTS0->Caption;//"가동";
                    else if (sVdi == "VDI1") pGDC->Values[nRow][Col20->Index] = lblHTS1->Caption;//"중지";
                    else                     pGDC->Values[nRow][Col20->Index] = lblUnknown->Caption;
#endif

                    //함체온도
                    pGDC->Values[nRow][Col21->Index] = pADO->FieldByName("CBOX_TMPR")->AsString;
                }
                else
                {
                    pGDC->Values[nRow][Col08->Index] = lblUnknown->Caption;
                    pGDC->Values[nRow][Col09->Index] = lblUnknown->Caption;
                    pGDC->Values[nRow][Col16->Index] = lblUnknown->Caption;
                    pGDC->Values[nRow][Col17->Index] = lblUnknown->Caption;
                    pGDC->Values[nRow][Col18->Index] = lblUnknown->Caption;
                    pGDC->Values[nRow][Col19->Index] = lblUnknown->Caption;
                    pGDC->Values[nRow][Col20->Index] = lblUnknown->Caption;
                    pGDC->Values[nRow][Col21->Index] = lblUnknown->Caption;;
                    pGDC->Values[nRow][ColCtrlS->Index]   = "Y";
                }

                if (VDS_TYPE_CD != "I")
                {
                    // 영상검지기가 아닌경우 잘 몰라....
                    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] = "-";
                }


#if 0
                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;

                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" ? "정상" : "수집이상";
#endif
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVDSM400M::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("TVDSM400M::UpdateFacility", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
    }

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

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

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

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

void __fastcall TVDSM400M::Col08CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    TColor tColor = ACanvas->Brush->Color;
    TColor tFont  = clBlack;

    try
    {
        String sStts = AViewInfo->GridRecord->DisplayTexts[ColCtrlS->Index];
        if (sStts == "Y")
        {
            tColor = clYellow;
        }

        ACanvas->Font->Color = tFont;
        ACanvas->SetBrushColor(tColor);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TVDSM400M::Col09CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    TColor tColor = ACanvas->Brush->Color;
    TColor tFont  = clBlack;
    try
    {
        TColor tColor;
        String sStatus = AViewInfo->GridRecord->DisplayTexts[AViewInfo->Item->Index];
        tColor = ACanvas->Brush->Color;

        if      (sStatus.Pos(lblUnknown->Caption))  tColor = clGray;
        else if (sStatus.Pos(lblMOS1->Caption))     tColor = clRed;
        else if (sStatus.Pos(lblPWS0->Caption))     tColor = clYellow;
        else if (sStatus.Pos(lblCDS1->Caption))     tColor = clYellow;
        else if (sStatus.Pos(lblPAS0->Caption))     tColor = clYellow;
        else if (sStatus.Pos(lblHTS0->Caption))     tColor = clYellow;
        else if (sStatus.Pos("-"))                  tColor = tColor;
        //else if (sStatus.Pos("0"))                  tColor = clRed;
        else                                        tColor = clLime;

        ACanvas->SetBrushColor(tColor);
        if (tColor == clRed) tFont = clWhite;
        ACanvas->Font->Color = tFont;
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

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

