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

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

#include "FRAME_CctvListF.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 link "cxSpinEdit"
#pragma resource "*.dfm"
//TFRAMECctvList *FRAMECctvList;
//---------------------------------------------------------------------------
__fastcall TFRAMECctvList::TFRAMECctvList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    m_pGDC = TvList->DataController;

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

#ifdef D_CCTV_HANTE
    APP_FillCode(CbType, "CTVT");
    Column07->Visible = true;
    Column09->Visible = true;
    Column06->Visible = false;
    Column13->Visible = false;
#else
    APP_FillCode(CbType, "CTVT");
    APP_FillCode(CbLc, "ISTLT");
    Column07->Visible = false;
    Column09->Visible = false;
    Column06->Visible = true;
    Column13->Visible = true;
#endif

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

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

    int nRow = 0;

    TvList->BeginUpdate();

    try
    {
        CctvCtlrManager->FLists.Lock();
        try
        {
            FOR_STL(TCctvCtlr*, pObj, CctvCtlrManager->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]  = pObj->CCTV_CTLR_NMBR.ToIntDef(0);
                m_pGDC->Values[nRow][Column02->Index]  = pObj->CCTV_CTLR_ID;
                m_pGDC->Values[nRow][Column03->Index]  = pObj->CCTV_NM;

                m_pGDC->Values[nRow][Column05->Index]  = pObj->CCTV_CTLR_IP;
                m_pGDC->Values[nRow][Column08->Index]  = pObj->STRM_SESN_NM;

#ifdef D_CCTV_HANTE
                m_pGDC->Values[nRow][Column04->Index]  = APP_GetCodeName(CbType, pObj->CCTV_TYPE_CD);
                m_pGDC->Values[nRow][Column07->Index]  = pObj->CCTV_IP;
                m_pGDC->Values[nRow][Column09->Index]  = pObj->STRM_SVR_IP;
#else
                m_pGDC->Values[nRow][Column04->Index]  = APP_GetCodeName(CbType, pObj->CCTV_TYPE);
                m_pGDC->Values[nRow][Column06->Index]  = pObj->CCTV_CTLR_PORT;
                m_pGDC->Values[nRow][Column13->Index]  = pObj->INTR_RTMP_ADDR;
#endif
                m_pGDC->Values[nRow][Column10->Index]  = pObj->STRM_RTSP_ADDR;
                m_pGDC->Values[nRow][Column11->Index]  = pObj->STRM_RTMP_ADDR;
                m_pGDC->Values[nRow][Column12->Index]  = pObj->STRM_HTTP_ADDR;
                m_pGDC->Values[nRow][Column14->Index]  = pObj->CCTV_SBST_DSPL_YN;
                m_pGDC->Values[nRow][Column15->Index]  = APP_GetCodeName(CbLc, pObj->CCTV_AREA_DVSN);

                m_pGDC->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();
        Column01->SortIndex = Column01->Index;
        Column01->SortOrder = soAscending;
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

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

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

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

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

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

void __fastcall TFRAMECctvList::TvListCustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo && Column14->Visible)
    {
        String sDispImg = AViewInfo->GridRecord->DisplayTexts[Column14->Index];
        if (sDispImg == "Y")
        {
            ACanvas->Brush->Color = clYellow;
        }
    }
}
//---------------------------------------------------------------------------

