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

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

#include "IST00601F.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_ChartF"
#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 "cxProgressBar"
#pragma link "cxSplitter"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "cxLabel"
#pragma resource "*.dfm"

TIST00601 *IST00601 = NULL;

//---------------------------------------------------------------------------
__fastcall TIST00601::TIST00601(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;
}
//---------------------------------------------------------------------------
__fastcall TIST00601::~TIST00601(void)
{
}
//--------------------------------------------------------------------------

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

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<프로세스 상태통계 정보>";

    FRAMEChart1->FTitle = Caption;//"프로세스 상태통계";
    FRAMEChart1->SetChartLegend(lblText1->Caption, FrmLang->lblCnt->Caption);//"프로세스", "개수");
    FRAMEChart1->ChartValueDisplay(FRAMEChart1->ChkDisplayVal->Checked);
    FRAMEChart1->TvChart->ToolBox->CustomizeButton = true;
    FRAMEChart1->TvChart->Legend->Position = cppDefault;
    FRAMEChart1->TvChart->DiagramStackedBar->Enabled = true;
    FRAMEChart1->TvChart->DiagramStackedColumn->Enabled = true;
    //FRAMEChart1->TvChart->DiagramStackedColumn->Active = true;
    FRAMEChart1->TvChart->DiagramColumn->Active = true;
    LblSearch->Caption = FrmLang->lblQryCond->Caption + FStDateTime.SubString(1, 12) + " ~ " + FEdDateTime.SubString(1, 12);
}
//---------------------------------------------------------------------------

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

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

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

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

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

    String sQry;
    TADOQuery *pADO = ADOQry;

  sQry = "SELECT A.SYST_ID FCLT_ID, A.SYST_NM FCLT_NM,                                                                                                        \r\n"
         "       NVL(B.CNT, 0) CNT_SPS0, NVL(C.CNT, 0) CNT_SPS1, NVL(D.CNT, 0) CNT_SPS2, NVL(E.CNT, 0) CNT_SPS3, NVL(F.CNT, 0) CNT_SPS4                       \r\n"
         "  FROM TB_UNIT_SYST A,                                                                                                                              \r\n"
         "       (SELECT SYST_ID, COUNT(SYST_ID) CNT FROM TB_UNIT_SYST_STTS_HS WHERE SYST_STTS_CD = 'SPS0' AND CRTN_DT BETWEEN :p01 AND :p02 GROUP BY SYST_ID \r\n"
         "       ) B,                                                                                                                                         \r\n"
         "       (SELECT SYST_ID, COUNT(SYST_ID) CNT FROM TB_UNIT_SYST_STTS_HS WHERE SYST_STTS_CD = 'SPS1' AND CRTN_DT BETWEEN :p03 AND :p04 GROUP BY SYST_ID \r\n"
         "       ) C,                                                                                                                                         \r\n"
         "       (SELECT SYST_ID, COUNT(SYST_ID) CNT FROM TB_UNIT_SYST_STTS_HS WHERE SYST_STTS_CD = 'SPS2' AND CRTN_DT BETWEEN :p05 AND :p06 GROUP BY SYST_ID \r\n"
         "       ) D,                                                                                                                                         \r\n"
         "       (SELECT SYST_ID, COUNT(SYST_ID) CNT FROM TB_UNIT_SYST_STTS_HS WHERE SYST_STTS_CD = 'SPS3' AND CRTN_DT BETWEEN :p07 AND :p08 GROUP BY SYST_ID \r\n"
         "       ) E,                                                                                                                                         \r\n"
         "       (SELECT SYST_ID, COUNT(SYST_ID) CNT FROM TB_UNIT_SYST_STTS_HS WHERE SYST_STTS_CD = 'SPS4' AND CRTN_DT BETWEEN :p09 AND :p10 GROUP BY SYST_ID \r\n"
         "       ) F                                                                                                                                          \r\n"
         " WHERE A.SYST_ID = B.SYST_ID(+)                                                                                                                     \r\n"
         "   AND A.SYST_ID = C.SYST_ID(+)                                                                                                                     \r\n"
         "   AND A.SYST_ID = D.SYST_ID(+)                                                                                                                     \r\n"
         "   AND A.SYST_ID = E.SYST_ID(+)                                                                                                                     \r\n"
         "   AND A.SYST_ID = F.SYST_ID(+)                                                                                                                     \r\n"
         "   AND A.STTS_DSPL_YN = 'Y'                                                                                                                         \r\n"
//         "   AND A.SYST_ID IN ('CTV01', 'DSRC01', 'EXT03', 'UOT01', 'UTP01', 'VMS01', 'WALL01', 'WEB01', 'WEB02')                                             \r\n"
         " ORDER BY A.SYST_ID                                                                                                                                 \r\n";


    try
    {
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", FStDateTime);
        ITSDb_SQLBind(pADO, "p02", FEdDateTime);
        ITSDb_SQLBind(pADO, "p03", FStDateTime);
        ITSDb_SQLBind(pADO, "p04", FEdDateTime);
        ITSDb_SQLBind(pADO, "p05", FStDateTime);
        ITSDb_SQLBind(pADO, "p06", FEdDateTime);
        ITSDb_SQLBind(pADO, "p07", FStDateTime);
        ITSDb_SQLBind(pADO, "p08", FEdDateTime);
        ITSDb_SQLBind(pADO, "p09", FStDateTime);
        ITSDb_SQLBind(pADO, "p10", 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 TIST00601::DspHistory()
{
    TADOQuery *pADO = ADOQry;
    TcxGridChartView *TvChart = FRAMEChart1->TvChart;

    try
    {
    	TcxGridChartSeries *pChart1 = NULL;
    	TcxGridChartSeries *pChart2 = NULL;

        TvChart->ClearSeries();
        FRAMEChart1->ChartValueDisplay(FRAMEChart1->ChkDisplayVal->Checked);

        CMM_ClearGridTableView(TvList);

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

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

			pChart1 = TvChart->CreateSeries();
			pChart1->DataBinding->ValueType = "Integer";
			pChart1->DisplayText = FrmLang->lblChartTotal->Caption;//"전체";

			pChart2 = TvChart->CreateSeries();
			pChart2->DataBinding->ValueType = "Integer";
			pChart2->DisplayText = lblText2->Caption;//"실행중";

            int nSPS0;
            int nSPS1;
            int nSPS2;
            int nSPS3;
            int nSPS4;
            //int nSPS_Total = nSPS0 + nSPS1 + nSPS2 + nSPS3 + nSPS4;
            int nSPS_Total = 288;
            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                m_pGDC->Values[nRow][Column01->Index] = pADO->FieldByName("FCLT_ID")->AsString;

                m_pGDC->Values[nRow][Column02->Index] = pADO->FieldByName("FCLT_NM")->AsString;
                nSPS0                                 = pADO->FieldByName("CNT_SPS0")->AsInteger;
                nSPS1                                 = pADO->FieldByName("CNT_SPS1")->AsInteger;
                nSPS2                                 = pADO->FieldByName("CNT_SPS2")->AsInteger;
                nSPS3                                 = pADO->FieldByName("CNT_SPS3")->AsInteger;
                nSPS4                                 = pADO->FieldByName("CNT_SPS4")->AsInteger;
                m_pGDC->Values[nRow][Column03->Index] = nSPS_Total;
                m_pGDC->Values[nRow][Column04->Index] = nSPS1;
                m_pGDC->Values[nRow][Column05->Index] = nSPS_Total ? (double)nSPS1 / (double)nSPS_Total * (double)100 : 0;

				pChart1->Values[nRow] = nSPS_Total;
				pChart2->Values[nRow] = nSPS1;

				TvChart->Categories->Values[nRow] = m_pGDC->Values[nRow][Column02->Index];
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
            }

    		TvChart->EndUpdate();
            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 TIST00601::OnMessage(TMessage &Msg)
{
    switch (Msg.Msg)
    {
    case WM_PARAM_DATABASE:
        if (WP_DB_SELECT_OK == Msg.WParam)
        {
            //ShowMessage("Select Ok");
        }
        break;
    }
}
//---------------------------------------------------------------------------

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

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

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

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

