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

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

#include "FRAME_WCamStateListF.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 link "dxSkinBlack"
#pragma link "dxSkinMcSkin"
#pragma link "cxDropDownEdit"
#pragma link "cxMaskEdit"
#pragma resource "*.dfm"
//TFRAMEWCamStateList *FRAMEWCamStateList;
//---------------------------------------------------------------------------
__fastcall TFRAMEWCamStateList::TFRAMEWCamStateList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    m_pGDC = TvList->DataController;

    FTitle = lblTitle->Caption;
    APP_FillCode(CbType, "WCAM");
}
//---------------------------------------------------------------------------

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

    int ii, jj;
    int nCols = 0;

    TcxDataController *pGDC = TvList->DataController;

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

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

            nRow = pGDC->AppendRecord();

            pGDC->Values[nRow][ColumnSel->Index] = false;
            pGDC->Values[nRow][Col01->Index] = pObj->WCAM_CTLR_NMBR;
            pGDC->Values[nRow][Col02->Index] = pObj->WCAM_CTLR_ID;
            pGDC->Values[nRow][Col03->Index] = pObj->WCAM_NM;
            //pGDC->Values[nRow][Col04->Index] = pObj->WCAM_TYPE_CD;
            pGDC->Values[nRow][Col04->Index]  = APP_GetCodeName(CbType, pObj->WCAM_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->WCAM_CTLR_IP;

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

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

void __fastcall TFRAMEWCamStateList::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];
            TWCamCtlr *pObj = (TWCamCtlr*)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 TFRAMEWCamStateList::BtnAllSelectClick(TObject *Sender)
{
    TcxButton *pBtn = (TcxButton*)Sender;
    CMM_CheckAllListItem(TvList, ColumnSel->Index, pBtn->Tag);
}
//---------------------------------------------------------------------------

int  __fastcall TFRAMEWCamStateList::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 TFRAMEWCamStateList::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 TFRAMEWCamStateList::TvListDataControllerFilterChanged(TObject *Sender)
{
    LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
}
//---------------------------------------------------------------------------

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

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

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

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

