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

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

#include "FRAME_VmsStateListF.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 TFRAMEVmsStateList::TFRAMEVmsStateList(TComponent* Owner)
    : TFrame(Owner)
{
    m_pGDC = TvList->DataController;

    //TvList->OptionsView->NoDataToDisplayInfoText = "<VMS 정보가 없습니다>";
    FTitle = lblTitle->Caption;
}
//---------------------------------------------------------------------------

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

    int ii, jj;
    int nCols = 0;

    TcxDataController *pGDC = TvList->DataController;

    TvList->BeginUpdate();
    pGDC->BeginUpdate();

    VmsCtlrManager->FLists.Lock();
    try
    {
        int nRow;
        FOR_STL(TVmsCtlr*, pObj, VmsCtlrManager->FLists)
        {
            if (pObj->DEL_YN == "Y") continue;

            nRow = pGDC->AppendRecord();

            pGDC->Values[nRow][ColumnSel->Index] = false;
            pGDC->Values[nRow][Col01->Index] = (pObj->VMS_CTLR_NMBR);
            pGDC->Values[nRow][Col02->Index] = pObj->VMS_CTLR_ID;
            pGDC->Values[nRow][Col03->Index] = pObj->VMS_NM;
            pGDC->Values[nRow][Col04->Index] = pObj->VMS_TYPE_DESC;

            TVmsSize *pSize = VmsCtlrManager->FVmsSize.Find(pObj->VMS_TYPE_CD);
            if (pSize) pGDC->Values[nRow][Col05->Index] = pSize->TypeNm+" ";
            else       pGDC->Values[nRow][Col05->Index] = "--?-- ";

            pGDC->Values[nRow][Col06->Index] = VmsOperMode(pObj);
            //pGDC->Values[nRow][Col33->Index] = pObj->VMS_PHSE_CHNG_CYCL;

            pGDC->Values[nRow][Col07->Index] = ITSUtil_FormatStr(pObj->VmsMsg->OFFER_DT, STR_DATETIME);
            pGDC->Values[nRow][Col08->Index] = String(pObj->VmsMsg->Total);
            pGDC->Values[nRow][Col09->Index] = pObj->VmsMsg->OFFER_YN ? "Y" : "N";

            pGDC->Values[nRow][Col10->Index] = VmsComm(pObj);               //통신상태

            pGDC->Values[nRow][Col11->Index] = VmsModuleState(pObj);        //모듈상태
            pGDC->Values[nRow][Col12->Index] = VmsModulePower(pObj);        //전원상태
            pGDC->Values[nRow][Col13->Index] = VmsModuleLuminance(pObj);    //휘도

            pGDC->Values[nRow][Col14->Index] = VmsModuleDoor(pObj);     //도어
            pGDC->Values[nRow][Col15->Index] = VmsModuleFan(pObj);      //팬
            pGDC->Values[nRow][Col16->Index] = VmsModuleHeater(pObj);   //히터
            pGDC->Values[nRow][Col17->Index] = VmsModuleTemp(pObj);     //온도

            pGDC->Values[nRow][Col18->Index] = String(pObj->STATE.ModuleOperatingTemperature);      //모듈정지온도
            pGDC->Values[nRow][Col19->Index] = String(pObj->STATE.FanOperatingTemperature);         //팬동작온도
            pGDC->Values[nRow][Col20->Index] = String(pObj->STATE.HeaterOperatingTemperature);      //히터동작온도
            pGDC->Values[nRow][Col21->Index] = String(pObj->STATE.ModuleBasicFailureRate);          //모듈장애율
            pGDC->Values[nRow][Col28->Index] = String(pObj->STATE.ScheduledMessageOperatingTime);   //계획된 메시지의 동작시간(초)

            pGDC->Values[nRow][Col29->Index] = pObj->PANL_ON_TIME;   //전광판 ON
            pGDC->Values[nRow][Col30->Index] = pObj->PANL_OFF_TIME;  //전광판 OFF

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

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

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

            pGDC->Values[nRow][Col06->Index] = VmsOperMode(pObj);
            //pGDC->Values[nRow][Col33->Index] = pObj->VMS_PHSE_CHNG_CYCL;
            pGDC->Values[nRow][Col07->Index] = ITSUtil_FormatStr(pObj->VmsMsg->OFFER_DT, STR_DATETIME);
            pGDC->Values[nRow][Col08->Index] = String(pObj->VmsMsg->Total);
            pGDC->Values[nRow][Col09->Index] = pObj->VmsMsg->OFFER_YN ? "Y" : "N";

            pGDC->Values[nRow][Col10->Index] = VmsComm(pObj);               //통신상태

            pGDC->Values[nRow][Col11->Index] = VmsModuleState(pObj);        //모듈상태
            pGDC->Values[nRow][Col12->Index] = VmsModulePower(pObj);        //전원상태
            pGDC->Values[nRow][Col13->Index] = VmsModuleLuminance(pObj);    //휘도

            pGDC->Values[nRow][Col14->Index] = VmsModuleDoor(pObj);     //도어
            pGDC->Values[nRow][Col15->Index] = VmsModuleFan(pObj);      //팬
            pGDC->Values[nRow][Col16->Index] = VmsModuleHeater(pObj);   //히터
            pGDC->Values[nRow][Col17->Index] = VmsModuleTemp(pObj);     //온도

            pGDC->Values[nRow][Col18->Index] = String(pObj->STATE.ModuleOperatingTemperature);      //모듈정지온도
            pGDC->Values[nRow][Col19->Index] = String(pObj->STATE.FanOperatingTemperature);         //팬동작온도
            pGDC->Values[nRow][Col20->Index] = String(pObj->STATE.HeaterOperatingTemperature);      //히터동작온도
            pGDC->Values[nRow][Col21->Index] = String(pObj->STATE.ModuleBasicFailureRate);          //모듈장애율
            pGDC->Values[nRow][Col28->Index] = String(pObj->STATE.ScheduledMessageOperatingTime);   //계획된 메시지의 동작시간(초)

            pGDC->Values[nRow][Col29->Index] = pObj->PANL_ON_TIME;   //전광판 ON
            pGDC->Values[nRow][Col30->Index] = pObj->PANL_OFF_TIME;  //전광판 OFF
        }
    }
    __finally
    {
        pGDC->EndUpdate();
        TvList->EndUpdate();
        //CxList->LayoutChanged();
    }
#if 0
    if (ARecordIndex < 0) return;

    int nMemPtr = TvList->DataController->Values[ARecordIndex][Col99->Index];
    TVmsCtlr *pObj = (TVmsCtlr*)nMemPtr;
    if (!pObj) return;

    switch(Sender->Tag)
    {
    case 0: AText = VmsOperMode(pObj);               break;
    case 1: AText = ITSUtil_FormatStr(pObj->VmsMsg->OFFER_DT, STR_DATETIME); break;
    case 2: AText = String(pObj->VmsMsg->Total);                             break;
    case 3: AText = pObj->VmsMsg->OFFER_YN ? "Y" : "N";                      break;
    }
#endif
}
//---------------------------------------------------------------------------

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

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

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

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

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

void __fastcall TFRAMEVmsStateList::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      (sVmsMode == "자동") tColor = clLime;
        else if (sVmsMode == "고정") tColor = clFuchsia;
        else                         tColor = clYellow;

        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 TFRAMEVmsStateList::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 TFRAMEVmsStateList::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      (sStatus.Pos("-"))    tColor = clGray;
        else 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("가동")) Color = clYellow;
        else                          tColor = clLime;

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

void __fastcall TFRAMEVmsStateList::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("장애")) tColor = clRed;
        else if (sStatus.Pos("꺼짐")) tColor = clRed;
        else                          tColor = clLime;
#endif

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

