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

#include <vcl.h>
#include "AppGlobalF.h"
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSLangTransF.h"
#include "FrmResourceF.h"
#include "FrmResourceF.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 "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#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 "cxGridBandedTableView"
#pragma link "cxSpinEdit"
#pragma link "cxLabel"
#pragma resource "*.dfm"
//TFRAMEVmsStateList *FRAMEVmsStateList;
//---------------------------------------------------------------------------
__fastcall TFRAMECctvStateList::TFRAMECctvStateList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    m_pGDC = TvList->DataController;

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

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

    int ii, jj;
    int nCols = 0;

    TcxDataController *pGDC = TvList->DataController;

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

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

            nRow = pGDC->AppendRecord();

            pGDC->Values[nRow][ColumnSel->Index] = false;
            pGDC->Values[nRow][Col01->Index] = String(pObj->CCTV_CTLR_NMBR);
            pGDC->Values[nRow][Col02->Index] = pObj->CCTV_CTLR_ID;
            pGDC->Values[nRow][Col03->Index] = pObj->CCTV_NM;
            pGDC->Values[nRow][Col04->Index] = pObj->CCTV_TYPE_CD;

            pGDC->Values[nRow][Col10->Index] = pObj->STATE.Comm == cctv_normal ? FrmLang->lblState0->Caption : FrmLang->lblState1->Caption;               //통신상태
            pGDC->Values[nRow][Col11->Index] = pObj->CCTV_CTLR_IP;

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

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

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;

            pGDC->Values[nRow][Col10->Index] = pObj->STATE.Comm == cctv_normal ? FrmLang->lblState0->Caption : FrmLang->lblState1->Caption;               //통신상태
        }
    }
    __finally
    {
        pGDC->EndUpdate();
        TvList->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;//"VMS상태정보";

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

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

void __fastcall TFRAMECctvStateList::Col06CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    try
    {
        TColor tColor;
        String sVmsMode = AViewInfo->GridRecord->DisplayTexts[AViewInfo->Item->Index];
        tColor = ACanvas->Brush->Color;
#if 0
        if      (sVmsMode == "자동") tColor = clLime;
        else if (sVmsMode == "고정") tColor = clFuchsia;
        else                         tColor = clYellow;
#else
        if      (sVmsMode == FrmLang->lblOperA->Caption.Trim()) tColor = clLime;
        else if (sVmsMode == FrmLang->lblOperF->Caption.Trim()) tColor = clFuchsia;
        else                                                    tColor = clYellow;
#endif

        ACanvas->SetBrushColor(tColor);
        ACanvas->Font->Color  = clBlack;
    }
    catch(Exception &e)
    {
    }
#if 0
    int nRecordIdx = AViewInfo->GridRecord->RecordIndex;
    if (nRecordIdx >= 0)
    {
        int nIdx = AViewInfo->Item->Index;
        String sVmsMode = AViewInfo->GridRecord->DisplayTexts[nIdx];
        if (sVmsMode == "고정") ACanvas->Canvas->Brush->Color = clLime;
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TFRAMECctvStateList::Col07CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    try
    {
        if (AViewInfo->GridRecord->RecordIndex < 0) return;
        String sResult = AViewInfo->GridRecord->DisplayTexts[Col09->Index];
        if (sResult == "N")
        {
            ACanvas->Canvas->Brush->Color = clRed;
        }
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMECctvStateList::Col10CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    try
    {
        TColor tColor;
        String sStatus = AViewInfo->GridRecord->DisplayTexts[AViewInfo->Item->Index];
        tColor = ACanvas->Brush->Color;

#if 0
        if      (sStatus.Pos("-"))    tColor = clGray;
        else if (sStatus.Pos("장애")) tColor = clRed;
        else if (sStatus.Pos("켜짐")) tColor = clYellow;
        else if (sStatus.Pos("열림")) tColor = clRed;
        else if (sStatus.Pos("동작")) tColor = clYellow;
        else if (sStatus.Pos("가동")) tColor = clYellow;
        else                          tColor = clLime;
#else
        if      (sStatus.Pos("-"))                                tColor = clGray;
        else if (sStatus.Pos(FrmLang->lblState1->Caption.Trim())) tColor = clRed;
        else if (sStatus.Pos(FrmLang->lblPOWER0->Caption.Trim())) tColor = clYellow;
        else if (sStatus.Pos(FrmLang->lblCDS1->Caption.Trim()))   tColor = clRed;
        else if (sStatus.Pos(FrmLang->lblFAN0->Caption.Trim()))   tColor = clYellow;
        //else if (sStatus.Pos(frmLang->lblHEAT0->Caption.Trim()))  tColor = clYellow;
        else                                                      tColor = clLime;
#endif

        ACanvas->SetBrushColor(tColor);
        ACanvas->Font->Color  = clBlack;
        /*if (AViewInfo->GridRecord->Selected)
        {
            ACanvas->Font->Color  = clBlack;
        }*/
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMECctvStateList::Col12CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    try
    {
        TColor tColor;
        String sStatus = AViewInfo->GridRecord->DisplayTexts[AViewInfo->Item->Index];
        tColor = ACanvas->Brush->Color;

#if 0
        if      (sStatus.Pos("-"))    tColor = clGray;
        else if (sStatus.Pos("장애")) tColor = clRed;
        else if (sStatus.Pos("켜짐")) tColor = clYellow;
        else if (sStatus.Pos("열림")) tColor = clRed;
        else if (sStatus.Pos("동작")) tColor = clYellow;
        else if (sStatus.Pos("가동")) tColor = clYellow;
        else                          tColor = clLime;
#else
        if      (sStatus.Pos("-"))                                tColor = clGray;
        else if (sStatus.Pos(FrmLang->lblState1->Caption.Trim())) tColor = clRed;
        else if (sStatus.Pos(FrmLang->lblPOWER1->Caption.Trim())) tColor = clRed;
        else                                                      tColor = clLime;

#if 0
        if      (sStatus.Pos("-"))     tColor = clGray;
        else if (sStatus.Pos("Error")) tColor = clRed;
        else if (sStatus.Pos("Off"))   tColor = clRed;
        else                           tColor = clLime;
#endif
#endif

        ACanvas->SetBrushColor(tColor);
        ACanvas->Font->Color  = clBlack;
        /*if (AViewInfo->GridRecord->Selected)
        {
            ACanvas->Font->Color  = clBlack;
        }*/
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

