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

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

#include "FrmCctvLogSttsSubF.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 "cxSplitter"
#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 "cxLabel"
#pragma link "cxGridBandedTableView"
#pragma resource "*.dfm"

//TFrmCctvLogSttsSub *FrmCctvLogSttsSub = NULL;

//---------------------------------------------------------------------------
__fastcall TFrmCctvLogSttsSub::TFrmCctvLogSttsSub(TComponent* Owner, HWND hHandle, String sFrom, String sTo, String sIdList)
    : TForm(Owner)
{           
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    //CMM_LoadForm(g_sFormsDir, this);

    FParent     = hHandle;
    FStDateTime = sFrom;
    FEdDateTime = sTo;
    FIdList     = sIdList;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogSttsSub::FormInit()
{
    ADOQry->Connection = ITSDb_GetConnection();
    m_pGDC = TvList->DataController;

    TvList->OptionsView->NoDataToDisplayInfoText = Caption + ":  " + lblText1->Caption;//조회결과가 없습니다.";
    LblSearch->Caption = lblText2->Caption + FStDateTime.SubString(1, 10) + " ~ " + FEdDateTime.SubString(1, 10);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogSttsSub::FormShow(TObject *Sender)
{
    FormInit();
    Refresh();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogSttsSub::CommClose()
{
    //CMM_SaveForm(g_sFormsDir, this);
    //FrmVmsLogSub = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogSttsSub::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false; 
    SelHistory();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogSttsSub::ChkExpandClick(TObject *Sender)
{
    CMM_ExpandCollapseChk(TvList, ChkExpand->Checked);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogSttsSub::SelHistory()
{
    TSqlCursor sqlCrs;

    String sQry;
    TADOQuery *pADO = ADOQry;

#if 0
    sQry = "SELECT A.*, B.CCTV_CTLR_ID, B.CCTV_NM, B.CCTV_TYPE      \r\n"
           "  FROM TB_CCTV_CTLR_STTS_HS  A,                         \r\n"
           "       TB_CCTV_CTLR          B                          \r\n"
           " WHERE B.CCTV_CTLR_NMBR IN(" + FIdList + ")             \r\n"
           "   AND A.CRTN_DT BETWEEN :p01 AND :p02                  \r\n"
           "   AND A.CCTV_CTLR_NMBR = B.CCTV_CTLR_NMBR              \r\n";
#else
    sQry = "SELECT A.*, B.CCTV_CTLR_ID, B.ISTL_LCTN_NM AS CCTV_NM, B.CCTV_TYPE  \r\n"
           "  FROM TB_CCTV_STTS_HS       A,                                     \r\n"
           "       TB_CCTV_CTLR          B                                      \r\n"
           " WHERE B.CCTV_MNGM_NMBR IN(" + FIdList + ")                         \r\n"
           "   AND A.CRTN_DT BETWEEN :p01 AND :p02                              \r\n"
           "   AND A.CCTV_MNGM_NMBR = B.CCTV_MNGM_NMBR                          \r\n";
#endif

    try
    {
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", FStDateTime);
        ITSDb_SQLBind(pADO, "p02", FEdDateTime);
        ITSDb_SQLOpen(pADO);
    }
    catch(EDatabaseError &E)
    {
        DBERRORMSG("History Qeury", String(E.ClassName()), E.Message, sQry);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        DBERRORMSG("History Qeury", String(e.ClassName()), e.Message, sQry);
        throw Exception(String(e.ClassName()) + e.Message);
    }
    DspHistory();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogSttsSub::DspHistory()
{
    TADOQuery *pADO = ADOQry;

    try
    {
    	TcxGridChartSeries *pChart = NULL;

        CMM_ClearGridTableView(TvList);

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

            nDataCnt = pADO->RecordCount;
            m_pGDC->RecordCount = nDataCnt;

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                m_pGDC->Values[nRow][Col00->Index] = pADO->FieldByName("CCTV_MNGM_NMBR")->AsString;
                m_pGDC->Values[nRow][Col01->Index] = pADO->FieldByName("CCTV_CTLR_ID")->AsString;
                m_pGDC->Values[nRow][Col02->Index] = pADO->FieldByName("CCTV_NM")->AsString;
                m_pGDC->Values[nRow][Col03->Index] = ITSUtil_StrToDateTime(pADO->FieldByName("CRTN_DT")->AsString).FormatString(STR_DATETIME);

                String sComm = pADO->FieldByName("CMNC_STTS_CD")->AsString;
                if (sComm != "CMS0")
                {
                    m_pGDC->Values[nRow][Col04->Index] = lblMOS1->Caption;//"장애";
                    m_pGDC->Values[nRow][Col13->Index] = "-";   //문열림
                    m_pGDC->Values[nRow][Col11->Index] = "-";   //팬
                    m_pGDC->Values[nRow][Col09->Index] = "-";   //히터
                    m_pGDC->Values[nRow][Col08->Index] = "-";   //함체온도
                    continue;
                }

#if 1
                String sType = pADO->FieldByName("CCTV_TYPE")->AsString;
                m_pGDC->Values[nRow][Col04->Index] = lblMOS0->Caption;//"장애";
                if (sType != "1")
                {
                    m_pGDC->Values[nRow][Col13->Index] = "-";   //문열림
                    m_pGDC->Values[nRow][Col11->Index] = "-";   //팬
                    m_pGDC->Values[nRow][Col09->Index] = "-";   //히터
                    m_pGDC->Values[nRow][Col08->Index] = "-";   //함체온도
                    continue;
                }
#endif


                //도어
                String sDoor = pADO->FieldByName("CBOX_DOOR_STTS_CD")->AsString;
                if      (sDoor == "CDS0") m_pGDC->Values[nRow][Col13->Index] = lblCDS0->Caption;//"닫힘";
                else if (sDoor == "CDS1") m_pGDC->Values[nRow][Col13->Index] = lblCDS1->Caption;//"열림";
                else                      m_pGDC->Values[nRow][Col13->Index] = "?";

                //팬상태
                String sFans = pADO->FieldByName("FAN_STTS_CD")->AsString;
                if      (sFans == "PAS0") m_pGDC->Values[nRow][Col11->Index] = lblPAS0->Caption;//"동작";
                else if (sFans == "PAS1") m_pGDC->Values[nRow][Col11->Index] = lblPAS1->Caption;//"정지";
                else                      m_pGDC->Values[nRow][Col11->Index] = "?";

                //히터상태
                String sHets = pADO->FieldByName("HETR_STTS_CD")->AsString;
                if      (sHets == "HTS0") m_pGDC->Values[nRow][Col09->Index] = lblHTS0->Caption;//"가동";
                else if (sHets == "HTS1") m_pGDC->Values[nRow][Col09->Index] = lblHTS1->Caption;//"중지";
                else                      m_pGDC->Values[nRow][Col09->Index] = "?";

                //온도
                m_pGDC->Values[nRow][Col08->Index] = pADO->FieldByName("CBOX_TMPR")->AsString;
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
            }

            TvList->EndUpdate();
            //CxList->SetFocus();
            LblRecords->Caption = FormatFloat("##,##0", m_pGDC->RecordCount) + lblCnt->Caption;//" 건";
        }
    }
    catch(EDatabaseError &E)
    {
        DBERRORMSG("History Results", String(E.ClassName()), E.Message, pADO->SQL->Text);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        DBERRORMSG("History Results", String(e.ClassName()), e.Message, pADO->SQL->Text);
        throw Exception(String(e.ClassName()) + e.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogSttsSub::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;

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

void __fastcall TFrmCctvLogSttsSub::FormClose(TObject *Sender, TCloseAction &Action)
{
    POST_MSG(FParent, WM_SUBFORM_CLOSE, 0, 0);
    CommClose();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogSttsSub::FormDestroy(TObject *Sender)
{
    //
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogSttsSub::Col05CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    try
    {
        TColor tColor;
        String sStatus = AViewInfo->GridRecord->DisplayTexts[AViewInfo->Item->Index];
        tColor = ACanvas->Brush->Color;
#if 1
        if      (sStatus.Pos("-"))              tColor = clGray;
        else if (sStatus.Pos(lblMOS1->Caption)) tColor = clRed;
        else if (sStatus.Pos(lblPWS0->Caption)) tColor = clYellow;
        else if (sStatus.Pos(lblCDS1->Caption)) tColor = clYellow;
        else if (sStatus.Pos(lblPAS0->Caption)) tColor = clYellow;
        else if (sStatus.Pos(lblHTS0->Caption)) tColor = clYellow;
        else if (sStatus.Pos("0"))              tColor = clRed;
        else                                    tColor = clLime;
#else
        if      (sStatus.Pos("-"))    tColor = clGray;
        else if (sStatus.Pos("장애")) tColor = clRed;
        else if (sStatus.Pos("켜짐")) tColor = clYellow;
        else if (sStatus.Pos("열림")) tColor = clYellow;
        else if (sStatus.Pos("동작")) tColor = clYellow;
        else if (sStatus.Pos("가동")) tColor = clYellow;
        else if (sStatus.Pos("0"))    tColor = clRed;
        else                          tColor = clLime;
#endif

        ACanvas->SetBrushColor(tColor);
        ACanvas->Font->Color = clBlack;
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogSttsSub::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 1
        if      (sStatus.Pos("-"))              tColor = clGray;
        else if (sStatus.Pos(lblMOS1->Caption)) tColor = clRed;
        else if (sStatus.Pos(lblPWS1->Caption)) tColor = clRed;
        else                                    tColor = clLime;
#else
        if      (sStatus.Pos("-"))     tColor = clGray;
        else if (sStatus.Pos("Error")) tColor = clRed;
        else if (sStatus.Pos("Off"))   tColor = clRed;
        else                           tColor = clLime;
#endif

        ACanvas->SetBrushColor(tColor);
        ACanvas->Font->Color = clBlack;
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvLogSttsSub::Col08CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    try
    {
        TColor tBrush = ACanvas->Brush->Color;
        TColor tFont  = ACanvas->Font->Color;
        String sValue = AViewInfo->GridRecord->DisplayTexts[AViewInfo->Item->Index];
        if (sValue != "-" && sValue.ToIntDef(0) > g_AppCfg.Temp.AlarmValue)
        {
            tBrush = clFuchsia;
            tFont  = clWhite;
        }
        ACanvas->SetBrushColor(tBrush);
        ACanvas->Font->Color = tFont;
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

