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

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

#include "VMSM010MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#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 link "cxSpinEdit"
#pragma resource "*.dfm"
TVMSM010M *VMSM010M = NULL;

//---------------------------------------------------------------------------
__fastcall TVMSM010M::TVMSM010M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    LoadLocalSkin();

    FTitle = Caption;

    CMM_LoadForm(g_sFormsDir, this);
}
//---------------------------------------------------------------------------

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

void __fastcall TVMSM010M::FormCreate(TObject *Sender)
{
    //
}
//---------------------------------------------------------------------------

void __fastcall TVMSM010M::FormShow(TObject *Sender)
{
    Refresh();
    Application->ProcessMessages();

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

void __fastcall TVMSM010M::LoadLocalSkin()
{
}
//---------------------------------------------------------------------------

void __fastcall TVMSM010M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    BtnSearchClick(NULL);
}
//---------------------------------------------------------------------------

void __fastcall TVMSM010M::FormClose(TObject *Sender, TCloseAction &Action)
{
    try {
        CommClose();

        VMSM010M = NULL;
        Action = caFree;
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TVMSM010M::OnMainFormMessage(TMessage &Msg)
{
}
//---------------------------------------------------------------------------

void __fastcall TVMSM010M::Refreshtatus(bool ARealtime/*=false*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT X.VMS_CTLR_NMBR, X.VMS_CTLR_ID, X.VMS_NM, T.VMS_TYPE_NM, X.OPER_MODE,    \r\n"
           "       Y.UPDT_DT, Y.CMNC_STTS_CD, Y.PWER_STTS_CD, Y.MODL_STTS_CD,               \r\n"
           "       Y.CBOX_DOOR_STTS_CD, Y.FAN_STTS_CD, Y.HETR_STTS_CD, Y.CBOX_TMPR,         \r\n"
           "       Z.SVC_DT, Z.MSG_CNT,                                                     \r\n"
           "       (CASE WHEN Y.UPDT_DT <= TO_CHAR(SYSDATE - 5/1440, 'YYYYMMDDHH24MISS')    \r\n"
           "             THEN 'Y' ELSE 'N' END) AS COMMERR                                  \r\n"
           "  FROM TB_VMS_TYPE T,                                                           \r\n"
           "       TB_VMS_CTLR X,                                                           \r\n"
           "       TB_VMS_CTLR_STTS Y,                                                      \r\n"
           "       (SELECT A.VMS_CTLR_NMBR,                                                 \r\n"
           "               TO_CHAR(SYSDATE - 8/1440, 'YYYYMMDDHH24MISS') AS SVC_DT,         \r\n"
           "               COUNT(1) AS MSG_CNT                                              \r\n"
           "          FROM TB_VMS_DSPL_PRST A,                                              \r\n"
           "               (SELECT VMS_CTLR_NMBR, MAX(DSPL_DT) AS DSPL_DT                   \r\n"
           "                  FROM TB_VMS_DSPL_PRST                                         \r\n"
           "                 GROUP BY VMS_CTLR_NMBR) B                                      \r\n"
           "         WHERE A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR                                \r\n"
           "           AND A.DSPL_DT       = B.DSPL_DT                                      \r\n"
           "         GROUP BY A.VMS_CTLR_NMBR) Z                                            \r\n"
           " WHERE T.VMS_TYPE_CD   = X.VMS_TYPE_CD                                          \r\n"
           "   AND X.VMS_CTLR_NMBR = Y.VMS_CTLR_NMBR                                        \r\n"
           "   AND Y.VMS_CTLR_NMBR = Z.VMS_CTLR_NMBR                                        \r\n"
           " ORDER BY X.VMS_CTLR_NMBR                                                       \r\n";
//           "   AND Y.UPDT_DT >= TO_CHAR(SYSDATE-2/1440, 'YYYYMMDDHH24MISS')                 \r\n"

    CMM_ClearGridTableView(TvList);

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

            pADO->Prepared = true;
            pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                nRow = TvList->DataController->AppendRecord();
                TvList->DataController->Values[nRow][ColNmbr->Index]  = pADO->FieldByName("VMS_CTLR_NMBR")->AsInteger;
                TvList->DataController->Values[nRow][ColId->Index]    = pADO->FieldByName("VMS_CTLR_ID")->AsString;
                TvList->DataController->Values[nRow][ColName->Index]  = pADO->FieldByName("VMS_NM")->AsString;
                TvList->DataController->Values[nRow][ColCtrlS->Index] = pADO->FieldByName("COMMERR")->AsString;
                TvList->DataController->Values[nRow][ColTypeNm->Index] = pADO->FieldByName("VMS_TYPE_NM")->AsString;
                String OperMode = pADO->FieldByName("OPER_MODE")->AsString;
                TvList->DataController->Values[nRow][ColOperMode->Index] = OperMode == "A" ? "자동" : "수동";
                TvList->DataController->Values[nRow][ColMsgDt->Index] = pADO->FieldByName("SVC_DT")->AsString;
                TvList->DataController->Values[nRow][ColMsgCnt->Index] = pADO->FieldByName("MSG_CNT")->AsString;

                String UPDT_DT = pADO->FieldByName("UPDT_DT")->AsString;
                if (UPDT_DT != "")
                {
                    TvList->DataController->Values[nRow][ColUpdtDt->Index] = ITSUtil_StrToDateTime(UPDT_DT).FormatString(STR_DATETIME);

                    //모듈전원상태
                    String sModl = pADO->FieldByName("MODL_STTS_CD")->AsString;
                    if      (sModl == "MOS0") TvList->DataController->Values[nRow][ColPower->Index] = lblPWS0->Caption;//"켜짐";
                    else if (sModl == "MOS1") TvList->DataController->Values[nRow][ColPower->Index] = lblPWS1->Caption;//"꺼짐";
                    else                      TvList->DataController->Values[nRow][ColPower->Index] = "?";

                    //전광판
                    String sModlOnOff = pADO->FieldByName("PWER_STTS_CD")->AsString;
                    if      (sModlOnOff == "MOS0") TvList->DataController->Values[nRow][ColModule->Index] = lblPWS0->Caption;//"켜짐";
                    else if (sModlOnOff == "MOS1") TvList->DataController->Values[nRow][ColModule->Index] = lblPWS1->Caption;//"꺼짐";
                    else                           TvList->DataController->Values[nRow][ColModule->Index] = "?";

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

                    //도어
                    String sDoor = pADO->FieldByName("CBOX_DOOR_STTS_CD")->AsString;
                    if      (sDoor == "CDS0") TvList->DataController->Values[nRow][ColDoorS->Index] = lblCDS0->Caption;//"닫힘";
                    else if (sDoor == "CDS1") TvList->DataController->Values[nRow][ColDoorS->Index] = lblCDS1->Caption;//"열림";
                    else                      TvList->DataController->Values[nRow][ColDoorS->Index] = lblUnknown->Caption;

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

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

                    //온도
                    TvList->DataController->Values[nRow][ColTemp->Index] = pADO->FieldByName("CBOX_TMPR")->AsString;
                }
                else
                {
                    TvList->DataController->Values[nRow][ColUpdtDt->Index]  = lblUnknown->Caption;
                    TvList->DataController->Values[nRow][ColCommS->Index]   = lblUnknown->Caption;
                    TvList->DataController->Values[nRow][ColDoorS->Index]   = lblUnknown->Caption;
                    TvList->DataController->Values[nRow][ColFanS->Index]    = lblUnknown->Caption;
                    TvList->DataController->Values[nRow][ColHeaterS->Index] = lblUnknown->Caption;
                    TvList->DataController->Values[nRow][ColTemp->Index]    = lblUnknown->Caption;
                    TvList->DataController->Values[nRow][ColCtrlS->Index]   = "Y";
                }
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVMSM010M::Refreshtatus", 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("TVMSM010M::Refreshtatus", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSM010M::RefreshData()
{
    try
    {
        LockWindowUpdate(Handle);

        Refreshtatus();
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSM010M::BtnSearchClick(TObject *Sender)
{
    RefreshData();
}
//---------------------------------------------------------------------------

void __fastcall TVMSM010M::ColCommSCustomDrawCell(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(lblCDS1->Caption))     tColor = clYellow;
        else if (sStatus.Pos(lblPAS0->Caption))     tColor = clYellow;
        else if (sStatus.Pos(lblHTS0->Caption))     tColor = clYellow;
        else if (sStatus.Pos(lblPWS0->Caption))     tColor = clLime;
        else if (sStatus.Pos(lblPWS1->Caption))     tColor = clRed;
        //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 TVMSM010M::BtnCloseClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------

void __fastcall TVMSM010M::ColOperModeCustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    try
    {
        TColor tColor;
        String sVmsMode = AViewInfo->GridRecord->DisplayTexts[AViewInfo->Item->Index];
        tColor = ACanvas->Brush->Color;
#if 1
        if      (sVmsMode == "자동") tColor = clLime;
        else if (sVmsMode == "고정") tColor = clFuchsia;
        else                         tColor = clYellow;
#else
        if      (sVmsMode == FrmLang->lblOperA->Caption.Trim()) tColor = clLime;
        else if (sVmsMode == FrmLang->lblOperF->Caption.Trim()) tColor = clFuchsia;
        else                                                    tColor = clYellow;
#endif

        ACanvas->SetBrushColor(tColor);
        ACanvas->Font->Color  = clBlack;
    }
    catch(Exception &e)
    {
    }
#if 0
    int nRecordIdx = AViewInfo->GridRecord->RecordIndex;
    if (nRecordIdx >= 0)
    {
        int nIdx = AViewInfo->Item->Index;
        String sVmsMode = AViewInfo->GridRecord->DisplayTexts[nIdx];
        if (sVmsMode == "고정") ACanvas->Canvas->Brush->Color = clLime;
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TVMSM010M::ColTempCustomDrawCell(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)
    {
    }
}
//---------------------------------------------------------------------------

