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

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

#include "CTVM010MF.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"
TCTVM010M *CTVM010M = NULL;

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

    FTitle = Caption;

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

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

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

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

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

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

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

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

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

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

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

    sQry = "SELECT B.CCTV_MNGM_NMBR AS CCTV_CTLR_NMBR, B.CCTV_CTLR_ID, B.ISTL_LCTN_NM,       \r\n"
           "       (CASE WHEN UPDT_DT <= TO_CHAR(SYSDATE - 5/1440, 'YYYYMMDDHH24MISS')       \r\n"
           "              THEN 'Y' ELSE 'N' END) AS COMMERR,                                 \r\n"
           "       A.*, B.CCTV_TYPE                                                          \r\n"
           "  FROM TB_CCTV_STTS A, TB_CCTV_CTLR B                                            \r\n"
           " WHERE A.CCTV_MNGM_NMBR(+) = B.CCTV_MNGM_NMBR                                    \r\n"
           //"   AND B.CCTV_TYPE = '1'                                                         \r\n"
           " ORDER BY B.CCTV_MNGM_NMBR                                                       \r\n";
//           "   AND 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("CCTV_CTLR_NMBR")->AsInteger;
                TvList->DataController->Values[nRow][ColId->Index]    = pADO->FieldByName("CCTV_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 UPDT_DT   = pADO->FieldByName("UPDT_DT")->AsString;
                String CCTV_TYPE = pADO->FieldByName("CCTV_TYPE")->AsString;

                TvList->DataController->Values[nRow][ColTypeNm->Index] = CCTV_TYPE;

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

                    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";
                }
                if (CCTV_TYPE != "1")
                {
                    TvList->DataController->Values[nRow][ColDoorS->Index]   = "-";
                    TvList->DataController->Values[nRow][ColFanS->Index]    = "-";
                    TvList->DataController->Values[nRow][ColHeaterS->Index] = "-";
                    TvList->DataController->Values[nRow][ColTemp->Index]    = "-";
                }
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TCTVM010M::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("TCTVM010M::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 TCTVM010M::RefreshData()
{
    try
    {
        LockWindowUpdate(Handle);

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

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

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

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