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

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

#include "FRAME_CCamStateListF.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 "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 "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxSpinEdit"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma resource "*.dfm"
//TFRAMECCamStateList *FRAMECCamStateList;
//---------------------------------------------------------------------------
__fastcall TFRAMECCamStateList::TFRAMECCamStateList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    m_pGDC = TvList->DataController;

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

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

    int ii, jj;
    int nCols = 0;

    TcxDataController *pGDC = TvList->DataController;

    CCamCtlrManager->FLists.Lock();
    try
    {
        int nRow;
        TvList->BeginUpdate();
        pGDC->BeginUpdate();

        FOR_STL(TCCamCtlr*, pObj, CCamCtlrManager->FLists)
        {
            if (pObj->DEL_YN == "Y") continue;

            nRow = pGDC->AppendRecord();

            pGDC->Values[nRow][ColumnSel->Index] = false;
            pGDC->Values[nRow][Col01->Index] = pObj->CCAM_CTLR_NMBR;
            pGDC->Values[nRow][Col02->Index] = pObj->CCAM_CTLR_ID;
            pGDC->Values[nRow][Col03->Index] = pObj->CCAM_NM;
            pGDC->Values[nRow][Col04->Index] = pObj->CCAM_TYPE_CD;

            if      (pObj->STATE.Comm == wcam_normal) pGDC->Values[nRow][Col10->Index] = FrmLang->lblComm0->Caption;//"정상";
            else if (pObj->STATE.Comm == wcam_error ) pGDC->Values[nRow][Col10->Index] = FrmLang->lblComm1->Caption;//"통신이상";
            else                                      pGDC->Values[nRow][Col10->Index] = FrmLang->lblUnknown->Caption;

            pGDC->Values[nRow][Col11->Index] = pObj->CCAM_CTLR_IP;

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

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

void __fastcall TFRAMECCamStateList::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][Col99->Index];
            TCCamCtlr *pObj = (TCCamCtlr*)nMemPtr;
            if (!pObj) continue;

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

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

int  __fastcall TFRAMECCamStateList::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][Col01->Index]);
    ALinkIds = sLinkId;
    return 1;
}
//---------------------------------------------------------------------------

int  __fastcall TFRAMECCamStateList::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[Col01->Index]);
                nSelCnt++;
            }
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------

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

void __fastcall TFRAMECCamStateList::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= FTitle;

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

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

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

        if (!VarIsNull(AViewInfo->GridRecord->DisplayTexts[Col10->Index]))
        {
            String sCode = AViewInfo->GridRecord->DisplayTexts[Col10->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)
    {
    }
}
//---------------------------------------------------------------------------

