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

#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 "IHS00201F.h"

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

TIHS00201 *IHS00201 = NULL;

//---------------------------------------------------------------------------
__fastcall TIHS00201::TIHS00201(TComponent* Owner, HWND hHandle, int ARptType, String ASystId, String sFrom, String sTo, String ATitle)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FParent     = hHandle;
    FStDateTime = sFrom;
    FEdDateTime = sTo;
    FRptType    = ARptType;
    FSystId     = ASystId;
    FTitle      = ATitle;

    Caption = Caption;//"프로세스 상태이력";
}
//---------------------------------------------------------------------------
__fastcall TIHS00201::~TIHS00201(void)
{
}
//--------------------------------------------------------------------------

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

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<프로세스 상태이력 정보>";
    LblSearch->Caption = FTitle;
}
//---------------------------------------------------------------------------

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

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

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

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

    String sQry;
    TADOQuery *pADO = ADOQry;

    sQry = "SELECT X.CRTN_IDX, X.HHMM, Y.SYST_ID, Y.SYST_NM, Y.CRTN_DT, Y.SYST_STTS_CD, Y.RMRK                                          \r\n"
           "  FROM (SELECT ROWNUM AS CRTN_IDX,                                                                                          \r\n"
           "               TO_CHAR(TRUNC(SYSDATE)+((LEVEL-1)*5)/1440, 'HH24:MI') AS HHMM                                                \r\n"
           "          FROM DUAL                                                                                                         \r\n"
           "          CONNECT BY LEVEL <= 288 )X,                                                                                       \r\n"
           "       (SELECT A.SYST_ID, A.SYST_NM, B.CRTN_DT, TRUNC((B.CHOUR * 60 + B.CMIN) / 5, 0)+1 AS CRTN_IDX, B.SYST_STTS_CD, B.RMRK \r\n"
           "          FROM TB_UNIT_SYST A,                                                                                              \r\n"
           "               (SELECT SYST_ID, CRTN_DT,                                                                                    \r\n"
           "                       TO_NUMBER(SUBSTR(CRTN_DT, 9, 2)) AS CHOUR, TO_NUMBER(SUBSTR(CRTN_DT, 11, 2)) AS CMIN,                \r\n"
           "                       SYST_STTS_CD, RMRK                                                                                   \r\n"
           "                   FROM TB_UNIT_SYST_STTS_HS                                                                                \r\n"
           "                   WHERE SYST_ID = :p02                                                                                     \r\n"
           "                     AND CRTN_DT BETWEEN :p03 AND :p04                                                                      \r\n"
           "               ) B                                                                                                          \r\n"
           "         WHERE A.SYST_ID = :p01                                                                                             \r\n"
           "           AND A.SYST_ID = B.SYST_ID) Y                                                                                     \r\n"
           " WHERE X.CRTN_IDX = Y.CRTN_IDX(+)                                                                                           \r\n"
           " ORDER BY X.CRTN_IDX                                                                                                        \r\n";


    try
    {
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", FSystId);
        ITSDb_SQLBind(pADO, "p02", FSystId);
        ITSDb_SQLBind(pADO, "p03", FStDateTime);
        ITSDb_SQLBind(pADO, "p04", 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 &exception)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(exception.ClassName()), exception.Message, sQry);
        throw Exception(String(exception.ClassName()) + exception.Message);
    }
}
//---------------------------------------------------------------------------

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

    try
    {
        CMM_ClearGridTableView(TvList);

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

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                nRow = m_pGDC->AppendRecord();

                m_pGDC->Values[nRow][Column01->Index] = pADO->FieldByName("CRTN_IDX")->AsString;
                m_pGDC->Values[nRow][Column02->Index] = pADO->FieldByName("HHMM")->AsString;
                String sCrtDt = VarToStr(pADO->FieldByName("CRTN_DT")->AsString);
                if (sCrtDt.IsEmpty())
                    m_pGDC->Values[nRow][Column03->Index] = "---";
                else
                    m_pGDC->Values[nRow][Column03->Index] = ITSUtil_StrToTime(sCrtDt);

                String sStatus = VarToStr(pADO->FieldByName("SYST_STTS_CD")->AsString);
                m_pGDC->Values[nRow][Column05->Index] = sStatus;

                if      (sStatus == "SPS0") sStatus = cxLabel1->Caption;//"알 수 없음";
                else if (sStatus == "SPS1") sStatus = cxLabel2->Caption;//"실행중";
                else if (sStatus == "SPS2") sStatus = cxLabel3->Caption;//"시작";
                else if (sStatus == "SPS3") sStatus = cxLabel4->Caption;//"종료";
                else if (sStatus == "SPS4") sStatus = cxLabel5->Caption;//"오류 발생";

                m_pGDC->Values[nRow][Column04->Index] = sStatus;
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
                delete pADO;
            }
            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 TIHS00201::OnMessage(TMessage &Msg)
{
    switch (Msg.Msg)
    {
    case WM_PARAM_DATABASE:
        if (WP_DB_SELECT_OK == Msg.WParam)
        {
            //ShowMessage("Select Ok");
        }
        break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIHS00201::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"프로세스상태이력";

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

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

void __fastcall TIHS00201::TvListCustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo)
    {
        String sStatus = AViewInfo->GridRecord->DisplayTexts[Column05->Index];

        if      (sStatus == "SPS0") ACanvas->Brush->Color = clOlive;
        else if (sStatus == "SPS1") ACanvas->Brush->Color = clLime;
        else if (sStatus == "SPS2") ACanvas->Brush->Color = clLime;
        else if (sStatus == "SPS3") ACanvas->Brush->Color = clRed;
        else if (sStatus == "SPS4") ACanvas->Brush->Color = clRed;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIHS00201::FormDestroy(TObject *Sender)
{
    CommClose();
}
//---------------------------------------------------------------------------

