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

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

#include "DSRM050MF.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"
TDSRM050M *DSRM050M = NULL;

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

    FTitle = Caption;

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

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

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

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

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

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

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

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

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

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

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

    sQry = "SELECT B.ID AS CTLR_NMBR, B.RSE_ID AS CTLR_ID, B.ISTL_LCTN_NM,                   \r\n"
           "       (CASE WHEN CLCT_DT <= TO_CHAR(SYSDATE - 5/1440, 'YYYYMMDDHH24MISS')       \r\n"
           "              THEN 'Y' ELSE 'N' END) AS COMMERR,                                 \r\n"
           "       A.*                                                                       \r\n"
           "  FROM TB_RSE_STTS_PNST A, TB_RSE_MSTR B                                         \r\n"
           " WHERE A.ID(+) = B.ID                                                            \r\n"
           "   AND B.DEL_YN = 'N'                                                            \r\n"
           " ORDER BY B.ID                                                                   \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("CTLR_NMBR")->AsInteger;
                TvList->DataController->Values[nRow][ColId->Index]    = pADO->FieldByName("CTLR_ID")->AsString;
                TvList->DataController->Values[nRow][ColName->Index]  = pADO->FieldByName("ISTL_LCTN_NM")->AsString;
                TvList->DataController->Values[nRow][ColCtrlS->Index] = pADO->FieldByName("COMMERR")->AsString;

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

                if (CLCT_DT != "")
                {
                    TvList->DataController->Values[nRow][ColUpdtDt->Index] = ITSUtil_StrToDateTime(CLCT_DT).FormatString(STR_DATETIME);

                    String sDevcStts = pADO->FieldByName("CNTL_DEVC_STTS")->AsString;
                    String sCmncStts = pADO->FieldByName("CMNC_STTS")->AsString;
                    String sAtn1Stts = pADO->FieldByName("ATN_1_STTS")->AsString;
                    String sAtn2Stts = pADO->FieldByName("ATN_2_STTS")->AsString;
                    String sAtn3Stts = pADO->FieldByName("ATN_3_STTS")->AsString;
                    String sAtn4Stts = pADO->FieldByName("ATN_4_STTS")->AsString;
                    String sAtn1Mtns = pADO->FieldByName("ATN_1_MTNS")->AsString;

                    if      (sCmncStts == "0") sCmncStts = "정상";
                    else if (sCmncStts == "1") sCmncStts = "장애";
                    else if (sCmncStts == "2") sCmncStts = "정보 없음";

                    if      (sDevcStts == "0") sDevcStts = "정상";
                    else if (sDevcStts == "1") sDevcStts = "문열림";
                    else if (sDevcStts == "2") sDevcStts = "팬동작";
                    else if (sDevcStts == "3") sDevcStts = "내부온도 이상";
                    else if (sDevcStts == "4") sDevcStts = "정보 없음";


                    if      (sAtn1Stts == "0") sAtn1Stts = "정상";
                    else if (sAtn1Stts == "1") sAtn1Stts = "장애";
                    else if (sAtn1Stts == "2") sAtn1Stts = "정보 없음";


                    if      (sAtn1Mtns == "0") sAtn1Mtns = "송출종료";
                    else if (sAtn1Mtns == "1") sAtn1Mtns = "송출개시";
                    else if (sAtn1Mtns == "2") sAtn1Mtns = "유지보수 근무개시";
                    else if (sAtn1Mtns == "3") sAtn1Mtns = "정보 없음";

                    if (sCmncStts != "정상")
                    {
                        sDevcStts = "?";
                        sAtn1Stts = "?";
                        sAtn1Mtns = "?";
                    }

                    TvList->DataController->Values[nRow][ColCommS->Index] = sCmncStts;
                    TvList->DataController->Values[nRow][ColDoorS->Index] = sDevcStts;
                    TvList->DataController->Values[nRow][ColFanS->Index] = sAtn1Stts;
                    TvList->DataController->Values[nRow][ColHeaterS->Index] = sAtn1Mtns;
                }
                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("TDSRM050M::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("TDSRM050M::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 TDSRM050M::RefreshData()
{
    try
    {
        LockWindowUpdate(Handle);

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

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

void __fastcall TDSRM050M::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(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 TDSRM050M::BtnCloseClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------

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

