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

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

#include "FRAME_FacilityStatusListF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxClasses"
#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 "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "cxTextEdit"
#pragma link "cxContainer"
#pragma resource "*.dfm"
//TFRAMEFacilityStatusList *FRAMEFacilityStatusList;
//---------------------------------------------------------------------------
__fastcall TFRAMEFacilityStatusList::TFRAMEFacilityStatusList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    m_pGDC = TvList->DataController;

    //TvList->OptionsView->NoDataToDisplayInfoText = "<VMS 정보가 없습니다>";

    //Column08->Visible = true;
    FTitle = lblTitle->Caption;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityStatusList::UpdateList()
{
    CMM_ClearGridTableView(TvList);

    int ii, jj;
    int nCols = 0;

    TcxDataController *pGDC = TvList->DataController;

    TvList->BeginUpdate();
    pGDC->BeginUpdate();

    VmsCtlrManager->FLists.Lock();
    try
    {
        int nRow;
        FOR_STL(TVmsCtlr*, pObj, VmsCtlrManager->FLists)
        {
            if (pObj->DEL_YN == "Y") continue;

            nRow = pGDC->AppendRecord();

            pGDC->Values[nRow][Column01->Index] = (pObj->VMS_CTLR_NMBR);
            pGDC->Values[nRow][Column02->Index] = pObj->VMS_CTLR_ID;
            pGDC->Values[nRow][Column03->Index] = pObj->VMS_NM;

            pGDC->Values[nRow][Column04->Index] = VmsComm(pObj);               //통신상태
            pGDC->Values[nRow][Column06->Index] = pObj->RunState;


            pGDC->Values[nRow][Column99->Index] = (int)pObj;
        }
    }
    __finally
    {
        VmsCtlrManager->FLists.UnLock();

        pGDC->EndUpdate();
        TvList->EndUpdate();
        //RefreshStatus();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityStatusList::RefreshList()
{
    TcxDataController *pGDC = TvList->DataController;
    try
    {
        TvList->BeginUpdate();
        pGDC->BeginUpdate();

        int nRows = pGDC->RecordCount;
        for(int nRow = 0; nRow < nRows; nRow++)
        {
            int nMemPtr = TvList->DataController->Values[nRow][Column99->Index];
            TVmsCtlr *pObj = (TVmsCtlr*)nMemPtr;
            if (!pObj) continue;

            pGDC->Values[nRow][Column04->Index] = VmsComm(pObj);               //통신상태
            pGDC->Values[nRow][Column06->Index] = pObj->RunState;
        }
    }
    __finally
    {
        pGDC->EndUpdate();
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityStatusList::TvListDataControllerFilterChanged(TObject *Sender)
{
    CMM_SetFilterLike(TvList);
    LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityStatusList::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= FTitle;//"VMS통신상태";

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

void __fastcall TFRAMEFacilityStatusList::Column04CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo->GridRecord->RecordIndex < 0) return;

    Graphics::TBitmap *pBmp = NULL;
    pBmp = ImgStop->Picture->Bitmap;
	if (!VarIsNull(AViewInfo->GridRecord->DisplayTexts[Column06->Index]))
    {
        String sCode = AViewInfo->GridRecord->DisplayTexts[Column06->Index];

        if      (sCode == state_normal) pBmp = ImgState0->Picture->Bitmap;
        else if (sCode == state_error ) pBmp = ImgState1->Picture->Bitmap;
        else if (sCode == state_module) pBmp = ImgState2->Picture->Bitmap;
        else                            pBmp = ImgState3->Picture->Bitmap;

        if (!VarIsNull(AViewInfo->GridRecord->DisplayTexts[Column08->Index]))
        {
            String sValue = AViewInfo->GridRecord->DisplayTexts[Column08->Index];
            int nValue = 0;
            if (TryStrToInt(sValue, nValue))
            {
                if (nValue > g_AppCfg.Temp.AlarmValue)
                {
                    pBmp = ImgModule->Picture->Bitmap;
                }
            }
        }
    }

    if (!pBmp) return;
    pBmp->Transparent = true;

    int nW, nH;
    nW = nH = 21;
    try
    {
        TColor bBrush = ACanvas->Brush->Color;
        if (AViewInfo->GridRecord->Selected)
        {
            if (AViewInfo->GridRecord->Index % 2 == 0)
            {
                bBrush = Sender->LookAndFeelPainter->DefaultContentEvenColor();
            }
            else
            {
                bBrush = Sender->LookAndFeelPainter->DefaultContentOddColor();
            }
        }
        ACanvas->Brush->Color = bBrush;

        TRect rc;
        TRect rcOrg = AViewInfo->Bounds;
        ACanvas->FillRect(rcOrg);

        nW = rcOrg.Bottom - rcOrg.Top - 4;
        rc.Top    = rcOrg.Top+2;
        rc.Bottom = rcOrg.Bottom - 2;
        rc.Left   = rcOrg.Left + (rcOrg.Width() - nW) / 2;
        rc.Right  = rc.Left + nW;

        ACanvas->Canvas->StretchDraw(rc, pBmp);
        //ACanvas->Draw(AViewInfo->ContentBounds.Left, AViewInfo->ContentBounds.Top, pBmp);
        ADone = true;
    }
    catch(...) {}
}
//---------------------------------------------------------------------------


void __fastcall TFRAMEFacilityStatusList::Column08CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
#if 0
	if (AViewInfo)
	{
        if (AViewInfo->GridRecord->RecordIndex < 0) return;

        if (VarIsNull(AViewInfo->GridRecord->DisplayTexts[Column08->Index])) return;

        String sValue = AViewInfo->GridRecord->DisplayTexts[Column08->Index];
        int nValue = 0;
        if (TryStrToInt(sValue, nValue))
        {
            if (nValue > g_AppCfg.Temp.AlarmValue)
            {
                ACanvas->Canvas->Brush->Color = clFuchsia;
                ACanvas->Canvas->Font->Color = clBlack;
            }
        }
    }
#endif
}
//---------------------------------------------------------------------------

