/****************************************************************************
 * @source      : 
 * @description : 
 ****************************************************************************
 * DATE         AUTHOR    DESCRIPTION
 * --------------------------------------------------------------------------
 * 2011/12/24   정승호    최초작성
 *
 ****************************************************************************/
//---------------------------------------------------------------------------

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

#include "FrmVmsLogCtrlSubF.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 resource "*.dfm"

//TFrmVmsLogCtlSub *FrmVmsLogCtlSub = NULL;

//---------------------------------------------------------------------------
__fastcall TFrmVmsLogCtrlSub::TFrmVmsLogCtrlSub(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 TFrmVmsLogCtrlSub::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 TFrmVmsLogCtrlSub::FormShow(TObject *Sender)
{
    FormInit();
    Refresh();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

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

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

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

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

    String sQry;
    TADOQuery *pADO = ADOQry;

    sQry = "SELECT A.*, B.VMS_CTLR_ID, B.VMS_NM         \r\n"
           "  FROM TB_VMS_CTRL_HS A,                    \r\n"
           "       TB_VMS_CTLR    B                     \r\n"
           " WHERE B.VMS_CTLR_NMBR IN(" + FIdList + ")  \r\n"
           "   AND A.CTRL_DT BETWEEN :p01 AND :p02      \r\n"
           "   AND A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR    \r\n";

    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 TFrmVmsLogCtrlSub::DspHistory()
{
    TADOQuery *pADO = ADOQry;

    try
    {
        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("VMS_CTLR_NMBR")->AsString;
                m_pGDC->Values[nRow][Col01->Index] = pADO->FieldByName("VMS_CTLR_ID")->AsString;
                m_pGDC->Values[nRow][Col02->Index] = pADO->FieldByName("VMS_NM")->AsString;

                String sCtlType = pADO->FieldByName("CTRL_TYPE")->AsString;
                //제어유형(V01:전광판ON,V02:전광판OFF,V03:제어기리셋,V04:함체환경설정,V05:전광판ON/OFF시각설정)
                if      (sCtlType == "V01") sCtlType = lblV01->Caption;//"전광판ON";
                else if (sCtlType == "V02") sCtlType = lblV02->Caption;//"전광판OFF";
                else if (sCtlType == "V03") sCtlType = lblV03->Caption;//"제어기리셋";
                else if (sCtlType == "V04") sCtlType = lblV04->Caption;//"함체환경설정";
                else if (sCtlType == "V05") sCtlType = lblV05->Caption;//"전광판ON/OFF시각설정";
                else if (sCtlType == "V06") sCtlType = lblV06->Caption;//"운영모드변경";
                else if (sCtlType == "V07") sCtlType = lblV07->Caption;//"휘도제어";
                else if (sCtlType == "V08") sCtlType = lblV08->Caption;//"기본메시지다운로드";
                else if (sCtlType == "V09") sCtlType = lblV09->Caption;//"폼 표출주기";
                else if (sCtlType == "V10") sCtlType = lblV10->Caption;//"폼 즉시 다운로드";

#if 0
                if      (sCtlType == "V01") sCtlType = "Board On";
                else if (sCtlType == "V02") sCtlType = "Board Off";
                else if (sCtlType == "V03") sCtlType = "Reset";
                else if (sCtlType == "V04") sCtlType = "Parameter Set";
                else if (sCtlType == "V05") sCtlType = "Board ON/OFF Time Set";
                else if (sCtlType == "V06") sCtlType = "Operating Mode Change";
                else if (sCtlType == "V07") sCtlType = "Luminance contorol";
                else if (sCtlType == "V08") sCtlType = "Default Message Download";
                else if (sCtlType == "V09") sCtlType = "Form Display Cycle Time";
                else if (sCtlType == "V10") sCtlType = "Form Immediately Download";
#endif

                m_pGDC->Values[nRow][Col03->Index] = sCtlType;

                m_pGDC->Values[nRow][Col04->Index] = ITSUtil_StrToDateTime(pADO->FieldByName("CTRL_DT")->AsString).FormatString(STR_DATETIME);
                m_pGDC->Values[nRow][Col05->Index] = pADO->FieldByName("CTRL_VAL")->AsString;
                m_pGDC->Values[nRow][Col06->Index] = pADO->FieldByName("CTRL_RESULT")->AsInteger == 1 ? lblSucc->Caption : lblFai->Caption;//"성공" : "실패";
                m_pGDC->Values[nRow][Col07->Index] = pADO->FieldByName("USER_ID")->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 TFrmVmsLogCtrlSub::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;

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

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

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

void __fastcall TFrmVmsLogCtrlSub::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 == lblFai->Caption)//"실패")
            tColor = clRed;
        //else                    tColor = clLime;

        ACanvas->SetBrushColor(tColor);
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

