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

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

#include "FRAME_FacilityStatusListF.h"
#include "CDSFacilityF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#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 "cxLabel"
#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 "cxContainer"
#pragma resource "*.dfm"
//TFRAMEFacilityStatusList *FRAMEFacilityStatusList;

#define FAC_STATUS      0
#define FAC_TEMPALARM   0

//---------------------------------------------------------------------------
__fastcall TFRAMEFacilityStatusList::TFRAMEFacilityStatusList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());

    m_pGDC = TvList->DataController;

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

	FCodeCMS = ItsCodeManager->FLists.Find("CMS");
	FCodeCDS = ItsCodeManager->FLists.Find("CDS");

    //Column08->Visible = true;
}
//---------------------------------------------------------------------------

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

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

        TItsSubCode *pSubCode;
        try
        {
            ItsFacilityManager->FLists.Lock();

#if FAC_STATUS
            TFacilityStatus *pStatus;
            ItsFacilityManager->FListStatus.RemoveAll();
#endif

            FOR_STL(TItsFacility*, pFacility, ItsFacilityManager->FLists)
            {
                pFacility->StatusGridIndex = -1;
                if (pFacility->DEL_YN == "Y") continue;

                nRow = m_pGDC->AppendRecord();
                pFacility->StatusGridIndex = nRow;

                m_pGDC->Values[nRow][Column01->Index] = pFacility->FCLT_TYPE;

                if (pFacility->FCLT_TYPE == "PARK")
                    m_pGDC->Values[nRow][Column09->Index] = "주차장";
                else
                if (pFacility->FCLT_TYPE == "CCAM")
                    m_pGDC->Values[nRow][Column09->Index] = "교차로감시";
                else
                if (pFacility->FCLT_TYPE == "WCAM")
                    m_pGDC->Values[nRow][Column09->Index] = "CCTV(웹카메라)";
                else
                if (pFacility->FCLT_TYPE == "VDS")
                {
                    if (pFacility->FCLT_KIND == "C")
                    {
                        m_pGDC->Values[nRow][Column01->Index] = "CRSSVR";
                        m_pGDC->Values[nRow][Column09->Index] = "교차로분석서버";
                    }
                    else
                        m_pGDC->Values[nRow][Column09->Index] = pFacility->FCLT_TYPE;
                }
                else
                    m_pGDC->Values[nRow][Column09->Index] = pFacility->FCLT_TYPE;

                m_pGDC->Values[nRow][Column02->Index] = pFacility->FCLT_ID;
                m_pGDC->Values[nRow][Column03->Index] = pFacility->FCLT_LCTN;

                String sComm = FrmLang->lblComm0->Caption;//"정상";
                if (pFacility->FComm == state_normal)  sComm = FrmLang->lblComm0->Caption;//"정상";
                else
                if (pFacility->FComm == state_error )  sComm = FrmLang->lblComm1->Caption;//"통신이상";
                else
                if (pFacility->FComm == state_module)  sComm = FrmLang->lblComm2->Caption;//"함체문열림";
                else
                if (pFacility->FComm == state_collect) sComm = FrmLang->lblClctErr->Caption;//"수집이상";
                else
                if (pFacility->FComm == state_default) sComm = "-";
                else                                   sComm = FrmLang->lblUnknown->Caption;//"알수없음";

#if FAC_STATUS
                pStatus = ItsFacilityManager->FListStatus.Find(pFacility->FCLT_TYPE);
                if (!pStatus)
                {
                    pStatus = new TFacilityStatus();
                    pStatus->Type   = pFacility->FCLT_TYPE;
                    pStatus->Total  = 0;
                    pStatus->Normal = 0;
                    pStatus->Error  = 0;
                    pStatus->Collect= 0;

                    ItsFacilityManager->FListStatus.Push(pFacility->FCLT_TYPE, pStatus);
                }
                pStatus->Total++;   /* 전체 갯수를 증가 시킴 */
                if (pFacility->FComm == state_error)
                {
                    pStatus->Error++;
                }
                else
                if (pFacility->FComm == state_module)
                {
                    pStatus->Module++;
                }
                if (pFacility->FCollError)
                {
                    pStatus->Collect++;
                }
#endif

#if FAC_TEMPALARM
                try
                {
                    String sValue = pFacility->FCBoxTmpr;
                    int nValue = 0;
                    if (sValue != "-")
                    {
                        if (TryStrToInt(sValue, nValue))
                        {
                            if (nValue > g_AppCfg.Temp.AlarmValue)
                            {
                                sComm = FrmLang->lblHamTemp->Caption;//"함체온도";
                            }
                        }
                    }
                } catch(...) {}
#endif

                m_pGDC->Values[nRow][Column04->Index] = sComm;
                m_pGDC->Values[nRow][Column05->Index] = pFacility->FDoor;
                m_pGDC->Values[nRow][Column06->Index] = pFacility->FComm;
                m_pGDC->Values[nRow][Column07->Index] = pFacility->FDoorStatus;

                m_pGDC->Values[nRow][Column08->Index] = pFacility->FCBoxTmpr;

                //m_pGDC->Values[nRow][Column99->Index]  = 0;
            }
        }
        __finally
        {
            ItsFacilityManager->FLists.UnLock();
        }
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        CMM_ExpandCollapseChk(TvList, true);
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityStatusList::RefreshList()
{
    m_pGDC = TvList->DataController;

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

        TItsSubCode *pSubCode;
        try
        {
            ItsFacilityManager->FLists.Lock();

#if FAC_STATUS
            TFacilityStatus *pStatus;
            ItsFacilityManager->FListStatus.RemoveAll();
#endif

            FOR_STL(TItsFacility*, pFacility, ItsFacilityManager->FLists)
            {
                if (pFacility->DEL_YN == "Y") continue;
                if (pFacility->StatusGridIndex < 0) continue;

                nRow = pFacility->StatusGridIndex;

                m_pGDC->Values[nRow][Column01->Index] = pFacility->FCLT_TYPE;

                if (pFacility->FCLT_TYPE == "PARK")
                    m_pGDC->Values[nRow][Column09->Index] = "주차장";
                else
                if (pFacility->FCLT_TYPE == "CCAM")
                    m_pGDC->Values[nRow][Column09->Index] = "교차로감시";
                else
                if (pFacility->FCLT_TYPE == "WCAM")
                    m_pGDC->Values[nRow][Column09->Index] = "CCTV(웹카메라)";
                else
                if (pFacility->FCLT_TYPE == "VDS")
                {
                    if (pFacility->FCLT_KIND == "C")
                    {
                        m_pGDC->Values[nRow][Column01->Index] = "CRSSVR";
                        m_pGDC->Values[nRow][Column09->Index] = "교차로분석서버";
                    }
                    else
                        m_pGDC->Values[nRow][Column09->Index] = pFacility->FCLT_TYPE;
                }
                else
                    m_pGDC->Values[nRow][Column09->Index] = pFacility->FCLT_TYPE;

                m_pGDC->Values[nRow][Column02->Index] = pFacility->FCLT_ID;
                m_pGDC->Values[nRow][Column03->Index] = pFacility->FCLT_LCTN;

                String sComm = FrmLang->lblComm0->Caption;//"정상";
                if (pFacility->FComm == state_normal)  sComm = FrmLang->lblComm0->Caption;//"정상";
                else
                if (pFacility->FComm == state_error )  sComm = FrmLang->lblComm1->Caption;//"통신이상";
                else
                if (pFacility->FComm == state_module)  sComm = FrmLang->lblComm2->Caption;//"함체문열림";
                else
                if (pFacility->FComm == state_collect) sComm = FrmLang->lblClctErr->Caption;//"수집이상";
                else
                if (pFacility->FComm == state_collect) sComm = "-";
                else                                   sComm = FrmLang->lblUnknown->Caption;//"알수없음";

#if FAC_STATUS
                pStatus = ItsFacilityManager->FListStatus.Find(pFacility->FCLT_TYPE);
                if (!pStatus)
                {
                    pStatus = new TFacilityStatus();
                    pStatus->Type   = pFacility->FCLT_TYPE;
                    pStatus->Total  = 0;
                    pStatus->Normal = 0;
                    pStatus->Error  = 0;
                    pStatus->Collect= 0;

                    ItsFacilityManager->FListStatus.Push(pFacility->FCLT_TYPE, pStatus);
                }
                pStatus->Total++;   /* 전체 갯수를 증가 시킴 */
                if (pFacility->FComm == state_error)
                {
                    pStatus->Error++;
                }
                else
                if (pFacility->FComm == state_module)
                {
                    pStatus->Module++;
                }
                if (pFacility->FCollError)
                {
                    pStatus->Collect++;
                }
#endif

#if FAC_TEMPALARM
                try
                {
                    String sValue = pFacility->FCBoxTmpr;
                    int nValue = 0;
                    if (sValue != "-")
                    {
                        if (TryStrToInt(sValue, nValue))
                        {
                            if (nValue > g_AppCfg.Temp.AlarmValue)
                            {
                                sComm = FrmLang->lblHamTemp->Caption;//"함체온도";
                            }
                        }
                    }
                } catch(...) {}
#endif

                m_pGDC->Values[nRow][Column04->Index] = sComm;
                m_pGDC->Values[nRow][Column05->Index] = pFacility->FDoor;
                m_pGDC->Values[nRow][Column06->Index] = pFacility->FComm;
                m_pGDC->Values[nRow][Column07->Index] = pFacility->FDoorStatus;

                m_pGDC->Values[nRow][Column08->Index] = pFacility->FCBoxTmpr;
            }
        }
        __finally
        {
            ItsFacilityManager->FLists.UnLock();
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

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

void __fastcall TFRAMEFacilityStatusList::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= lblFileName->Caption;//"시설물통신상태";

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

void __fastcall TFRAMEFacilityStatusList::Column04CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo->GridRecord->RecordIndex < 0) return;

    Graphics::TBitmap *pBmp = NULL;
    pBmp = ImgState3->Picture->Bitmap;
	if (!VarIsNull(AViewInfo->GridRecord->DisplayTexts[Column06->Index]))
    {
        String sCode = AViewInfo->GridRecord->DisplayTexts[Column06->Index];

        if      (sCode == state_normal)  pBmp = ImgState0->Picture->Bitmap;
        else if (sCode == state_error )  pBmp = ImgState1->Picture->Bitmap;
        else if (sCode == state_module)  pBmp = ImgState2->Picture->Bitmap;
        else if (sCode == state_collect) pBmp = ImgState3->Picture->Bitmap;
        else if (sCode == state_default) pBmp = ImgState5->Picture->Bitmap;
        else                             pBmp = ImgState3->Picture->Bitmap;

#if FAC_TEMPALARM
        if (!VarIsNull(AViewInfo->GridRecord->DisplayTexts[Column08->Index]))
        {
            String sValue = AViewInfo->GridRecord->DisplayTexts[Column08->Index];
            int nValue = 0;
            if (TryStrToInt(sValue, nValue))
            {
                if (nValue > g_AppCfg.Temp.AlarmValue)
                {
                    pBmp = ImgState3->Picture->Bitmap;
                }
            }
        }
#endif
    }

    if (!pBmp) return;
    pBmp->Transparent = true;

    int nW, nH;
    nW = nH = 21;
    try
    {
        TColor bBrush = ACanvas->Brush->Color;
        if (AViewInfo->GridRecord->Selected)
        {
            if (AViewInfo->GridRecord->Index % 2 == 0)
            {
                bBrush = Sender->LookAndFeelPainter->DefaultContentEvenColor();
            }
            else
            {
                bBrush = Sender->LookAndFeelPainter->DefaultContentOddColor();
            }
        }
        ACanvas->Brush->Color = bBrush;

        TRect rc;
        TRect rcOrg = AViewInfo->Bounds;
        ACanvas->FillRect(rcOrg);

        nW = rcOrg.Bottom - rcOrg.Top - 4;
        rc.Top    = rcOrg.Top+2;
        rc.Bottom = rcOrg.Bottom - 2;
        rc.Left   = rcOrg.Left + (rcOrg.Width() - nW) / 2;
        rc.Right  = rc.Left + nW;

        ACanvas->Canvas->StretchDraw(rc, pBmp);
        //ACanvas->Draw(AViewInfo->ContentBounds.Left, AViewInfo->ContentBounds.Top, pBmp);
        ADone = true;
    }
    catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityStatusList::Column05CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
#if 0
	if (AViewInfo)
	{
        String sSatus = AViewInfo->GridRecord->DisplayTexts[Column06->Index];
        if (sSatus == "CDS0" )
        {
            ACanvas->Canvas->Font->Color = clBlue;//clTeal;
        }
        else if(sSatus == "CDS1" )
        {
            ACanvas->Canvas->Font->Color = clRed;//clFuchsia;
        }
        else
        {
            ACanvas->Canvas->Font->Color = clGray;
        }
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityStatusList::Column08CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
#if FAC_TEMPALARM
	if (AViewInfo)
	{
        if (AViewInfo->GridRecord->RecordIndex < 0) return;

        if (VarIsNull(AViewInfo->GridRecord->DisplayTexts[Column08->Index])) return;

        String sValue = AViewInfo->GridRecord->DisplayTexts[Column08->Index];
        int nValue = 0;
        if (TryStrToInt(sValue, nValue))
        {
            if (nValue > g_AppCfg.Temp.AlarmValue)
            {
                ACanvas->Canvas->Brush->Color = clFuchsia;
                ACanvas->Canvas->Font->Color = clBlack;
            }
        }
    }
#endif
}
//---------------------------------------------------------------------------

