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

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

#include "FRAME_DsrcListF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#pragma link "cxClasses"
#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 "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
__fastcall TFRAMEDsrcList::TFRAMEDsrcList(TComponent* Owner)
    : TFrame(Owner)
{
    ColumnSel->Options->Filtering = false;
    m_pGDC = TvList->DataController;

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<DSRC 정보가 없습니다>";

    MyItsDsrcManager = new TItsDsrcManager();
    MyItsDsrcManager->LoadFromDb();
	//FCodeIFD = ItsCodeManager->FLists.Find("IFD"); // 방향코드
}
//---------------------------------------------------------------------------

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

    int nRow = 0;
    try
    {
        TvList->BeginUpdate();

        try
        {
            MyItsDsrcManager->FLists.Lock();
            FOR_STL(TItsDsrc*, pObj, MyItsDsrcManager->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->DSRC_CTLR_NMBR;              //ID
                m_pGDC->Values[nRow][Column02->Index]  = pObj->DSRC_CTLR_ID;    //RSE ID
                m_pGDC->Values[nRow][Column03->Index]  = pObj->ISTL_LCTN_NM;    //명칭
                m_pGDC->Values[nRow][Column04->Index]  = pObj->CTLR_IP;         //IP Address
                m_pGDC->Values[nRow][Column99->Index]  = (int)pObj;

                m_pGDC->Values[nRow][Column11->Index]  = pObj->DSRC_CTLR_NMBR.ToIntDef(0);              //ID
                m_pGDC->Values[nRow][Column12->Index]  = pObj->DSRC_CTLR_ID;          //RSE ID
            }
        }
        __finally
        {
            MyItsDsrcManager->FLists.UnLock();
        }
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        Column11->SortIndex = Column11->Index;
        Column11->SortOrder = soAscending;
        //CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEDsrcList::UpdateListStatus()
{
    CMM_ClearGridTableView(TvList);

    int nRow = 0;
    try
    {
        TItsFacility *pFacility = NULL;
        TvList->BeginUpdate();

        try
        {
            MyItsDsrcManager->FLists.Lock();
            FOR_STL(TItsDsrc*, pObj, MyItsDsrcManager->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->DSRC_CTLR_NMBR;  //ID
                m_pGDC->Values[nRow][Column02->Index]  = pObj->DSRC_CTLR_ID;    //RSE ID
                m_pGDC->Values[nRow][Column03->Index]  = pObj->ISTL_LCTN_NM;    //명칭
                m_pGDC->Values[nRow][Column04->Index]  = pObj->CTLR_IP;         //IP Address
                m_pGDC->Values[nRow][Column99->Index]  = (int)pObj;

                m_pGDC->Values[nRow][Column11->Index]  = pObj->DSRC_CTLR_NMBR.ToIntDef(0);              //ID
                m_pGDC->Values[nRow][Column12->Index]  = pObj->DSRC_CTLR_ID;          //RSE ID

                m_pGDC->Values[nRow][Column06->Index]  = str_state_error;
                pFacility = ItsFacilityManager->FLists.Find(pObj->DSRC_CTLR_ID);
                if (!pFacility)
                {
                    m_pGDC->Values[nRow][Column05->Index] = FrmLang->lblUnknown->Caption;
                }
                else
                {
                    String sComm = FrmLang->lblComm0->Caption;//"정상";
                    if (pFacility->FComm == str_state_normal)  sComm = FrmLang->lblComm0->Caption;//"정상";
                    else
                    if (pFacility->FComm == str_state_error )  sComm = FrmLang->lblComm1->Caption;//"통신이상";
                    else
                    if (pFacility->FComm == str_state_module)  sComm = FrmLang->lblComm2->Caption;//"함체문열림";
                    else
                    if (pFacility->FComm == str_state_collect) sComm = FrmLang->lblClctErr->Caption;//"수집이상";
                    else
                    if (pFacility->FComm == str_state_collect) sComm = "-";
                    else                                       sComm = FrmLang->lblUnknown->Caption;//"알수없음";

                    m_pGDC->Values[nRow][Column05->Index] = sComm;
                    m_pGDC->Values[nRow][Column06->Index] = pFacility->FComm;
#if 0
                    if (pFacility->FComm == str_state_normal)
                    {
                        m_pGDC->Values[nRow][Column05->Index] = FrmLang->lblComm0->Caption;
                    }
                    else
                    {
                        m_pGDC->Values[nRow][Column05->Index] = FrmLang->lblComm1->Caption;
                    }
#endif
                }
            }
        }
        __finally
        {
            MyItsDsrcManager->FLists.UnLock();
        }
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        Column11->SortIndex = Column11->Index;
        Column11->SortOrder = soAscending;
        //CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------

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

int  __fastcall TFRAMEDsrcList::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 TFRAMEDsrcList::GetSelIds(TStringList *AStringList)
{
    int nSelCnt = 0;

    try
    {
        TvList->BeginUpdate();

#if 0
        int nRowCnt = m_pGDC->RecordCount;
        for (int ii = 0; ii < nRowCnt; ii++)
        {
            if (((bool)m_pGDC->Values[ii][ColumnSel->Index]))
            {
                AStringList->Add(m_pGDC->Values[ii][Column00->Index]);
                nSelIdx++;
            }
        }
#else
        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++;
            }
        }
#endif
    }
    __finally
    {
        TvList->EndUpdate();
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------

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

void __fastcall TFRAMEDsrcList::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= "DSRC정보";

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

void __fastcall TFRAMEDsrcList::OnCloseQuery(bool &CanClose)
{
    try {
        if (MyItsDsrcManager)
        {
            delete MyItsDsrcManager;
            MyItsDsrcManager = NULL;
        }
    } catch(...) {}
}
//---------------------------------------------------------------------------

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

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

