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

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

#include "FRAME_CctvStateListF.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 link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma resource "*.dfm"
//TFRAMEWCamStateList *FRAMEWCamStateList;
//---------------------------------------------------------------------------
__fastcall TFRAMECctvStateList::TFRAMECctvStateList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    m_pGDC = TvList->DataController;
    m_pGDC2 = TvList2->DataController;

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

void __fastcall TFRAMECctvStateList::UpdateList()
{
    CMM_ClearGridTableView(TvList);
    CMM_ClearGridTableView(TvList2);

    int nRow = 0;

    TvList->BeginUpdate();
    TvList2->BeginUpdate();

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

                if (pObj->CCTV_AREA_DVSN == 1)
                {
                    // 북구
                    nRow = m_pGDC->AppendRecord();
                    m_pGDC->Values[nRow][ColumnSel->Index] = false;
                    m_pGDC->Values[nRow][Col01->Index]  = pObj->CCTV_CTLR_NMBR.ToIntDef(0);
                    m_pGDC->Values[nRow][Col02->Index]  = pObj->CCTV_CTLR_ID;
                    m_pGDC->Values[nRow][Col03->Index]  = pObj->CCTV_NM;
                    m_pGDC->Values[nRow][Col11->Index]  = pObj->CCTV_CTLR_IP;
                    m_pGDC->Values[nRow][Col04->Index]  = pObj->CCTV_TYPE_NM;
                    m_pGDC->Values[nRow][Col99->Index]  = (int)pObj;
                }
                else
                if (pObj->CCTV_AREA_DVSN == 2)
                {
                    // 남구
                    nRow = m_pGDC2->AppendRecord();
                    m_pGDC2->Values[nRow][ColumnSel2->Index] = false;
                    m_pGDC2->Values[nRow][ColNmbr->Index]  = pObj->CCTV_CTLR_NMBR.ToIntDef(0);
                    m_pGDC2->Values[nRow][ColId->Index]  = pObj->CCTV_CTLR_ID;
                    m_pGDC2->Values[nRow][ColNm->Index]  = pObj->CCTV_NM;
                    m_pGDC2->Values[nRow][ColIp->Index]  = pObj->CCTV_CTLR_IP;
                    m_pGDC2->Values[nRow][ColType->Index]  = pObj->CCTV_TYPE_NM;
                    m_pGDC2->Values[nRow][Column99->Index]  = (int)pObj;
                }
            }
        }
        __finally
        {
            CctvCtlrManager->FLists.UnLock();
        }
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        Col01->SortIndex = Col01->Index;
        Col01->SortOrder = soAscending;
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;

        TvList2->EndUpdate();
        ColNmbr->SortIndex = ColNmbr->Index;
        ColNmbr->SortOrder = soAscending;
        TvList2->DataController->GotoFirst();
        TvList2->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMECctvStateList::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];
            TCctvCtlr *pObj = (TCctvCtlr*)nMemPtr;
            if (!pObj) continue;

            if      (pObj->STATE.Comm == cctv_normal) pGDC->Values[nRow][Col10->Index] = FrmLang->lblComm0->Caption;//"정상";
            else if (pObj->STATE.Comm == cctv_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();
    }

    pGDC = TvList2->DataController;
    try
    {
        TvList2->BeginUpdate();
        pGDC->BeginUpdate();

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

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

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

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

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

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

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

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

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

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

