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

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

#include "VDSH0101F.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#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 "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 "dxSkinMcSkin"
#pragma resource "*.dfm"

TVDSH0101 *VDSH0101 = NULL;

//---------------------------------------------------------------------------
__fastcall TVDSH0101::TVDSH0101(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;

    //Column10->Visible = true;

    //Caption = "VDS-제어기 상태 이력";
}
//---------------------------------------------------------------------------

__fastcall TVDSH0101::~TVDSH0101(void)
{
}
//--------------------------------------------------------------------------

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

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<VDS-제어기 상태 이력 정보>";
	//TvList->ViewInfo->GroupByBoxViewInfo->Text = "그룹핑할 필드의 헤더를 이곳으로 드래그 하세요";
    LblSearch->Caption = FrmLang->lblQryCond->Caption + FStDateTime.SubString(1, 12) + " ~ " + FEdDateTime.SubString(1, 12);
}
//---------------------------------------------------------------------------

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

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

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

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

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

    String sQry;
    TADOQuery *pADO = ADOQry;

#if 0
    sQry = "SELECT A.VDS_CTLR_NMBR, B.VDS_CTLR_ID, B.VDS_NM, A.VIDEO_INPUT, A.CBOX_TMPR,                                                       \r\n"
           "       NVL(TO_CHAR(TO_DATE(A.CRTN_DT, 'YYYYMMDDHH24MISS'), 'YYYY-MM-DD HH24:MI:SS'), '정보없음')  AS CRTN_DT,                      \r\n"
		   "       (SELECT CMMN_CD_KOR_NM FROM TB_CMMN_CD WHERE CMMN_CD = A.CMNC_STTS_CD       AND CMMN_CLSF_CD = 'CMS') AS CONN_STTS_CD,      \r\n"
		   "       (SELECT CMMN_CD_KOR_NM FROM TB_CMMN_CD WHERE CMMN_CD = A.CBOX_DOOR_STTS_CD  AND CMMN_CLSF_CD = 'CDS') AS CBOX_DOOR_STTS_CD, \r\n"
		   "       (SELECT CMMN_CD_KOR_NM FROM TB_CMMN_CD WHERE CMMN_CD = A.FRONT_DOOR_STTS_CD AND CMMN_CLSF_CD = 'CDS') AS FRONT_DOOR_STTS_CD, \r\n"
		   "       (SELECT CMMN_CD_KOR_NM FROM TB_CMMN_CD WHERE CMMN_CD = A.BACK_DOOR_STTS_CD  AND CMMN_CLSF_CD = 'CDS') AS BACK_DOOR_STTS_CD, \r\n"
		   "       (SELECT CMMN_CD_KOR_NM FROM TB_CMMN_CD WHERE CMMN_CD = A.FAN_STTS_CD        AND CMMN_CLSF_CD = 'PAS') AS FAN_STTS_CD,       \r\n"
		   "       (SELECT CMMN_CD_KOR_NM FROM TB_CMMN_CD WHERE CMMN_CD = A.HETR_STTS_CD       AND CMMN_CLSF_CD = 'HTS') AS HETR_STTS_CD,      \r\n"
		   "       (SELECT CMMN_CD_KOR_NM FROM TB_CMMN_CD WHERE CMMN_CD = A.VIDEO_INPUT        AND CMMN_CLSF_CD = 'VDI') AS VIDEO_INPUT_CD     \r\n"
           "  FROM TB_VDS_CTLR_STTS_HS A, TB_VDS_CTLR B                                                                                        \r\n"
           " WHERE A.CRTN_DT BETWEEN :p01 AND :p02                                                                                             \r\n"
           "   AND B.VDS_CTLR_NMBR IN(" + FIdList + ")                                                                                         \r\n"
           "   AND A.VDS_CTLR_NMBR = B.VDS_CTLR_NMBR                                                                                           \r\n"
           " ORDER BY A.VDS_CTLR_NMBR, A.CRTN_DT                                                                                               \r\n";
#else
    sQry = "SELECT A.*, B.VDS_CTLR_ID, B.VDS_NM             \r\n"
           "  FROM TB_VDS_CTLR_STTS_HS A, TB_VDS_CTLR B     \r\n"
           " WHERE A.CRTN_DT BETWEEN :p01 AND :p02          \r\n"
           "   AND B.VDS_CTLR_NMBR IN(" + FIdList + ")      \r\n"
           "   AND A.VDS_CTLR_NMBR = B.VDS_CTLR_NMBR        \r\n"
           " ORDER BY A.VDS_CTLR_NMBR, A.CRTN_DT            \r\n";
#endif

    try
    {
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", FStDateTime);
        ITSDb_SQLBind(pADO, "p02", FEdDateTime);
        ITSDb_SQLOpen(pADO);

        DspHistory();
    }
    catch(EDatabaseError &E)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQry);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQry);
        throw Exception(String(e.ClassName()) + e.Message);
    }
}
//---------------------------------------------------------------------------

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

    try
    {
    	TcxGridChartSeries *pChart = NULL;

        CMM_ClearGridTableView(TvList);

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

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

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                String CRTN_DT      = pADO->FieldByName("CRTN_DT")->AsString;
                String CMNC_STTS_CD = pADO->FieldByName("CMNC_STTS_CD")->AsString;

                m_pGDC->Values[nRow][Column00->Index] = pADO->FieldByName("VDS_CTLR_NMBR")->AsString;
                m_pGDC->Values[nRow][Column11->Index] = pADO->FieldByName("VDS_CTLR_ID")->AsString;
                m_pGDC->Values[nRow][Column09->Index] = pADO->FieldByName("VDS_NM")->AsString;
                m_pGDC->Values[nRow][Column01->Index] = ITSUtil_StrToDateTime(CRTN_DT).FormatString(STR_DATETIME);

                if (CMNC_STTS_CD == "CMS0") TvList->DataController->Values[nRow][Column02->Index] = lblMOS0->Caption;
                else
                if (CMNC_STTS_CD == "CMS1") TvList->DataController->Values[nRow][Column02->Index] = lblMOS1->Caption;
                else                        TvList->DataController->Values[nRow][Column02->Index] = lblUnknown->Caption;

                if (CMNC_STTS_CD != "CMS0")
                {
                    m_pGDC->Values[nRow][Column04->Index] = "-";
                    m_pGDC->Values[nRow][Column12->Index] = "-";
                    m_pGDC->Values[nRow][Column13->Index] = "-";
                    m_pGDC->Values[nRow][Column05->Index] = "-";
                    m_pGDC->Values[nRow][Column06->Index] = "-";
                    m_pGDC->Values[nRow][Column10->Index] = "-";
                    m_pGDC->Values[nRow][Column07->Index] = "-";
                }
                else
                {
                    //도어
                    String sDoor = pADO->FieldByName("CBOX_DOOR_STTS_CD")->AsString;
                    if      (sDoor == "CDS0") TvList->DataController->Values[nRow][Column04->Index] = lblCDS0->Caption;//"닫힘";
                    else if (sDoor == "CDS1") TvList->DataController->Values[nRow][Column04->Index] = lblCDS1->Caption;//"열림";
                    else                      TvList->DataController->Values[nRow][Column04->Index] = lblUnknown->Caption;

                    String sFDoor = pADO->FieldByName("CBOX_DOOR_STTS_CD")->AsString;
                    if      (sDoor == "CDS0") TvList->DataController->Values[nRow][Column12->Index] = lblCDS0->Caption;//"닫힘";
                    else if (sDoor == "CDS1") TvList->DataController->Values[nRow][Column12->Index] = lblCDS1->Caption;//"열림";
                    else                      TvList->DataController->Values[nRow][Column12->Index] = lblUnknown->Caption;

                    String sBDoor = pADO->FieldByName("CBOX_DOOR_STTS_CD")->AsString;
                    if      (sDoor == "CDS0") TvList->DataController->Values[nRow][Column13->Index] = lblCDS0->Caption;//"닫힘";
                    else if (sDoor == "CDS1") TvList->DataController->Values[nRow][Column13->Index] = lblCDS1->Caption;//"열림";
                    else                      TvList->DataController->Values[nRow][Column13->Index] = lblUnknown->Caption;

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

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

                    //온도
                    TvList->DataController->Values[nRow][Column10->Index] = pADO->FieldByName("CBOX_TMPR")->AsString;
                }

#if 0
                String sCmnc  = pADO->FieldByName("CONN_STTS_CD")->AsString;
                //String sPwer  = pADO->FieldByName("PRNT_PWER_STTS_CD")->AsString;
                String sDoor  = pADO->FieldByName("CBOX_DOOR_STTS_CD")->AsString;
                String sFDoor = pADO->FieldByName("FRONT_DOOR_STTS_CD")->AsString;
                String sBDoor = pADO->FieldByName("BACK_DOOR_STTS_CD")->AsString;
                String sFan   = pADO->FieldByName("FAN_STTS_CD")->AsString;
                String sHetr  = pADO->FieldByName("HETR_STTS_CD")->AsString;
                String sTmpr  = pADO->FieldByName("CBOX_TMPR")->AsString;


                m_pGDC->Values[nRow][Column02->Index] = sCmnc;
                //m_pGDC->Values[nRow][Column03->Index] = sPwer;
                m_pGDC->Values[nRow][Column04->Index] = sDoor;
                m_pGDC->Values[nRow][Column12->Index] = sFDoor;
                m_pGDC->Values[nRow][Column13->Index] = sBDoor;
                m_pGDC->Values[nRow][Column05->Index] = sFan;
                m_pGDC->Values[nRow][Column06->Index] = sHetr;
                m_pGDC->Values[nRow][Column10->Index] = sTmpr;

                m_pGDC->Values[nRow][Column07->Index] = pADO->FieldByName("VIDEO_INPUT_CD")->AsString;
                //m_pGDC->Values[nRow][Column08->Index] = pADO->FieldByName("VIDEO_INPUT")->AsString;
#endif
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
            }

            TvList->EndUpdate();
            //CxList->SetFocus();
            LblRecords->Caption = FormatFloat("##,##0", m_pGDC->RecordCount) + FrmLang->lblEA->Caption;//" 건";

            String sEnd = FrmLang->lblQrySel->Caption + " [" + LblRecords->Caption + "]";//"데이터 " + LblRecords->Caption + " 이 조회 되었습니다.";
            Application->MessageBox(sEnd.c_str(),
                                    FrmLang->lblQryEnd->Caption.c_str(),//L"데이터 조회 완료",
                                    MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        }
    }
    catch(EDatabaseError &E)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(E.ClassName()), E.Message, pADO->SQL->Text);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(e.ClassName()), e.Message, pADO->SQL->Text);
        throw Exception(String(e.ClassName()) + e.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVDSH0101::OnMessage(TMessage &Msg)
{
    switch (Msg.Msg)
    {
    case WM_PARAM_DATABASE:
        if (WP_DB_SELECT_OK == Msg.WParam)
        {
            //ShowMessage("Select Ok");
        }
        break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TVDSH0101::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"VDS-제어기 상태이력";

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

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

void __fastcall TVDSH0101::Column02CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
#if 0
	if( AViewInfo )
	{
		if(AViewInfo->Text == "정상")
		{
			ACanvas->Canvas->Font->Color = clTeal;
		}
		else if(AViewInfo->Text == "장애")
		{
			ACanvas->Canvas->Font->Color = clFuchsia;
		}
		else
		{
			ACanvas->Canvas->Font->Color = clGray;
		}
	}
#else
    TColor tColor = ACanvas->Brush->Color;
    TColor tFont  = clBlack;
    try
    {
        TColor tColor;
        String sStatus = AViewInfo->GridRecord->DisplayTexts[AViewInfo->Item->Index];
        tColor = ACanvas->Brush->Color;

        if      (sStatus.Pos(lblUnknown->Caption))  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("-"))                  tColor = tColor;
        //else if (sStatus.Pos("0"))                  tColor = clRed;
        else                                        tColor = clLime;

        ACanvas->SetBrushColor(tColor);
        if (tColor == clRed) tFont = clWhite;
        ACanvas->Font->Color = tFont;
    }
    catch(Exception &e)
    {
    }
#endif
}
//---------------------------------------------------------------------------

