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

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

#include "CCAM010MF.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 resource "*.dfm"
TCCAM010M *CCAM010M = NULL;

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

    FTitle = Caption;

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

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

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

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

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

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

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

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

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

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

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

    sQry = "SELECT B.CCAM_CTLR_NMBR, B.CCAM_CTLR_ID, B.CCAM_NM, B.CCAM_CTLR_IP,     \r\n"
           "       A.UPDT_DT, A.CMNC_STTS_CD                                        \r\n"
           "  FROM (SELECT *                                                        \r\n"
           "          FROM TB_CCAM_CTLR_STTS                                        \r\n"
           "         WHERE UPDT_DT >= TO_CHAR(SYSDATE-5/1440, 'YYYYMMDDHH24MISS')   \r\n"
           "       ) A,                                                             \r\n"
           "       TB_CCAM_CTLR B                                                   \r\n"
           " WHERE A.CCAM_CTLR_NMBR(+) = B.CCAM_CTLR_NMBR                              \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("CCAM_CTLR_NMBR")->AsString;
                TvList->DataController->Values[nRow][ColId->Index]   = pADO->FieldByName("CCAM_CTLR_ID")->AsString;
                TvList->DataController->Values[nRow][ColName->Index] = pADO->FieldByName("CCAM_NM")->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 CMNC_STTS_CD = pADO->FieldByName("CMNC_STTS_CD")->AsString;
                    if (CMNC_STTS_CD == "CMS0") TvList->DataController->Values[nRow][ColCommS->Index] = FrmLang->lblComm0->Caption;
                    else
                    if (CMNC_STTS_CD == "CMS1") TvList->DataController->Values[nRow][ColCommS->Index] = FrmLang->lblComm1->Caption;
                    else                        TvList->DataController->Values[nRow][ColCommS->Index] = FrmLang->lblUnknown->Caption;
                }
                else
                {
                    TvList->DataController->Values[nRow][ColUpdtDt->Index] = "-";
                    TvList->DataController->Values[nRow][ColCommS->Index] = FrmLang->lblUnknown->Caption;
                }
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TCCAM010M::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("TCCAM010M::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 TCCAM010M::RefreshData()
{
    try
    {
        LockWindowUpdate(Handle);

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

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

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

    try
    {
        if (!VarIsNull(AViewInfo->Value))
        {
            String sTmp = AViewInfo->Value;
            if (sTmp == FrmLang->lblComm0->Caption)
            {
                tColor = clLime;
            }
            else
            if (sTmp == FrmLang->lblComm1->Caption)
            {
                tColor = clRed;
            }
            else
            {
                tColor = clSilver;
            }
        }

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

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

