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

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

#include "FRAME_VdsStateListF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxDropDownEdit"
#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 "cxMaskEdit"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinBlack"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"
//TFRAMEVdsStateList *FRAMEVdsStateList;
//---------------------------------------------------------------------------
__fastcall TFRAMEVdsStateList::TFRAMEVdsStateList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    m_pGDC = TvList->DataController;

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

    APP_FillCode(CbType, "VDST");

    FTitle = lblTitle->Caption;
}
//---------------------------------------------------------------------------

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

    int nRow = 0;

    TvList->BeginUpdate();

    try
    {
        VdsCtlrManager->FLists.Lock();
        try
        {
            FOR_STL(TVdsCtlr*, pObj, VdsCtlrManager->FLists)
            {
                if (pObj->DEL_YN == "Y") continue;

                nRow = m_pGDC->AppendRecord();

                m_pGDC->Values[nRow][ColumnSel->Index] = false;
                m_pGDC->Values[nRow][Column00->Index]  = "-";
                m_pGDC->Values[nRow][Column01->Index]  = String(pObj->VDS_CTLR_NMBR);
                m_pGDC->Values[nRow][Column02->Index]  = pObj->VDS_CTLR_ID;
                m_pGDC->Values[nRow][Column03->Index]  = pObj->VDS_NM;
                m_pGDC->Values[nRow][Column04->Index]  = APP_GetCodeName(CbType, pObj->VDS_TYPE_CD);
                m_pGDC->Values[nRow][Column05->Index]  = pObj->VDS_CTLR_IP;
                m_pGDC->Values[nRow][Column06->Index]  = pObj->VDS_CTLR_PORT;
                m_pGDC->Values[nRow][Column07->Index]  = String(pObj->GROUP_NO);
                m_pGDC->Values[nRow][Column08->Index]  = String(pObj->VDS_CTLR_LOCAL_NO);
                m_pGDC->Values[nRow][Column09->Index]  = String(pObj->TRAF_CLCT_CYCL);
                m_pGDC->Values[nRow][Column10->Index]  = String(pObj->STTS_CLCT_CYCL);

                if      (pObj->STATE.Comm == vds_normal) m_pGDC->Values[nRow][Column11->Index] = FrmLang->lblComm0->Caption;//"정상";
                else if (pObj->STATE.Comm == vds_error ) m_pGDC->Values[nRow][Column11->Index] = FrmLang->lblComm1->Caption;//"통신이상";
                else                                     m_pGDC->Values[nRow][Column11->Index] = FrmLang->lblUnknown->Caption;

                m_pGDC->Values[nRow][Column99->Index]  = (int)pObj;
            }
        }
        __finally
        {
            VdsCtlrManager->FLists.UnLock();
        }
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        Column01->SortIndex = Column01->Index;
        Column01->SortOrder = soAscending;
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEVdsStateList::RefreshStatus()
{
    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];
            TVdsCtlr *pObj = (TVdsCtlr*)nMemPtr;
            if (!pObj) continue;

            if      (pObj->STATE.Comm == vds_normal) m_pGDC->Values[nRow][Column11->Index] = FrmLang->lblComm0->Caption;//"정상";
            else if (pObj->STATE.Comm == vds_error ) m_pGDC->Values[nRow][Column11->Index] = FrmLang->lblComm1->Caption;//"통신이상";
            else                                     m_pGDC->Values[nRow][Column11->Index] = FrmLang->lblUnknown->Caption;
        }
    }
    __finally
    {
        pGDC->EndUpdate();
        TvList->EndUpdate();
        //CxList->LayoutChanged();
    }
}
//---------------------------------------------------------------------------


void __fastcall TFRAMEVdsStateList::BtnAllSelectClick(TObject *Sender)
{
    TcxButton *pBtn = (TcxButton*)Sender;
    CMM_CheckAllListItem(TvList, ColumnSel->Index, pBtn->Tag);
}
//---------------------------------------------------------------------------

int  __fastcall TFRAMEVdsStateList::GetSelIds(String &ALinkIds)
{
    ALinkIds = "";

    if (TvList->ViewData->RecordCount <= 0) return 0;
	int nIndex = m_pGDC->FocusedRecordIndex;
	if( nIndex < 0 )
		return 0;

    String sLinkId = VarToStr(m_pGDC->Values[nIndex][Column01->Index]);
    ALinkIds = sLinkId;
    return 1;
}
//---------------------------------------------------------------------------

int  __fastcall TFRAMEVdsStateList::GetSelIds(TStringList *AStringList)
{
    int nSelCnt = 0;

    try
    {
        TvList->BeginUpdate();

        int nRowCnt = TvList->ViewData->RecordCount;

        for (int ii = 0; ii < nRowCnt; ii++)
        {
            TcxCustomGridRow* ARow = TvList->ViewData->Rows[ii];
            if (((bool)ARow->Values[ColumnSel->Index]))
            {
                AStringList->Add(ARow->Values[Column01->Index]);
                nSelCnt++;
            }
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------

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

void __fastcall TFRAMEVdsStateList::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= FTitle;//"Vds정보";

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

void __fastcall TFRAMEVdsStateList::OnCloseQuery(bool &CanClose)
{
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEVdsStateList::Column11CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    try
    {
        TColor tColor = ACanvas->Brush->Color;

        if (!VarIsNull(AViewInfo->GridRecord->DisplayTexts[Column11->Index]))
        {
            String sCode = AViewInfo->GridRecord->DisplayTexts[Column11->Index];

            if      (sCode == FrmLang->lblComm0->Caption) tColor = clLime;
            else if (sCode == FrmLang->lblComm1->Caption) tColor = clRed;
            else                                          tColor = clGray;

        }
        ACanvas->SetBrushColor(tColor);
        ACanvas->Font->Color  = clBlack;
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

