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

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

#include "IAN00301F.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 "cxSplitter"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma resource "*.dfm"

TIAN00301 *IAN00301 = NULL;

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

    //Caption = "교통정보 제공결과 분석";
}
//---------------------------------------------------------------------------
__fastcall TIAN00301::~TIAN00301(void)
{
}
//--------------------------------------------------------------------------

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

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<수집데이터 이력 정보>";

    FRAMEChart1->FTitle = Caption;//"교통정보제공결과분석";
    FRAMEChart1->SetChartLegend(FrmLang->lblColTm->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;
    LblSearch->Caption = FrmLang->lblQryCond->Caption + FStDateTime.SubString(1, 12) + " ~ " + FEdDateTime.SubString(1, 12);
}
//---------------------------------------------------------------------------

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

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

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

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

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

    String sQry;
    TADOQuery *pADO = ADOQry;

    int nLevel = StrToInt(FIdList);
    switch(nLevel)
    {
      case 1:
      sQry = "SELECT PRCN_DT, TO_DATE(PRCN_DT, 'YYYYMMDDHH24MISS') AS PRCN_DT2,            \r\n"
             "       (SELECT COUNT(1) CNT FROM TB_LINK WHERE DEL_YN = 'N') Total,          \r\n"
             "       (LINK_LIGT_NUM + LINK_DELY_NUM + LINK_CNGS_NUM) GenerateCount,        \r\n"
             "       LINK_LIGT_NUM G1,                                                     \r\n"
             "       LINK_DELY_NUM G2,                                                     \r\n"
             "       LINK_CNGS_NUM G3,                                                     \r\n"
             "       (SELECT COUNT(1) CNT FROM TB_LINK  WHERE DEL_YN = 'N')                \r\n"
             "       - LINK_LIGT_NUM - LINK_DELY_NUM - LINK_CNGS_NUM NoneDataCount         \r\n"
             "  FROM TB_TRFC_PRCN_STAT                                                     \r\n"
             " WHERE PRCN_DT BETWEEN :p01 AND :p02                                         \r\n"
             " ORDER BY PRCN_DT                                                            \r\n";
        break;
      case 2:
      sQry = "SELECT PRCN_DT PRCN_DT, TO_DATE(PRCN_DT, 'YYYYMMDDHH24MISS') AS PRCN_DT2,             \r\n"
             "       (SELECT COUNT(1) CNT FROM TB_IFSC  WHERE DEL_YN = 'N') Total,                  \r\n"
             "       (IFSC_LIGT_NUM + IFSC_DELY_NUM + IFSC_CNGS_NUM) GenerateCount,                 \r\n"
             "       IFSC_LIGT_NUM G1,                                                              \r\n"
             "       IFSC_DELY_NUM G2,                                                              \r\n"
             "       IFSC_CNGS_NUM G3,                                                              \r\n"
             "       (SELECT COUNT(1) CNT FROM TB_IFSC WHERE DEL_YN = 'N')                          \r\n"
             "       - IFSC_LIGT_NUM - IFSC_DELY_NUM - IFSC_CNGS_NUM NoneDataCount                  \r\n"
             "  FROM TB_TRFC_PRCN_STAT                                                              \r\n"
             " WHERE PRCN_DT BETWEEN :p01 AND :p02                                                  \r\n"
             " ORDER BY PRCN_DT                                                                     \r\n";
        break;
      case 3:
      sQry = "SELECT PRCN_DT, TO_DATE(PRCN_DT, 'YYYYMMDDHH24MISS') AS PRCN_DT2,            \r\n"
             "       (SELECT COUNT(1) CNT FROM TB_ROAD WHERE DEL_YN = 'N') Total,          \r\n"
             "       (ROAD_LIGT_NUM + ROAD_DELY_NUM + ROAD_CNGS_NUM) GenerateCount,        \r\n"
             "       ROAD_LIGT_NUM G1,                                                     \r\n"
             "       ROAD_DELY_NUM G2,                                                     \r\n"
             "       ROAD_CNGS_NUM G3,                                                     \r\n"
             "       (SELECT COUNT(1) CNT FROM TB_ROAD WHERE DEL_YN = 'N')                 \r\n"
             "       - ROAD_LIGT_NUM - ROAD_DELY_NUM - ROAD_CNGS_NUM NoneDataCount         \r\n"
             "  FROM TB_TRFC_PRCN_STAT                                                     \r\n"
             " WHERE PRCN_DT BETWEEN :p01 AND :p02                                         \r\n"
             " ORDER BY PRCN_DT                                                            \r\n";
        break;
      case 4:
      sQry = "SELECT PRCN_DT, TO_DATE(PRCN_DT, 'YYYYMMDDHH24MISS') AS PRCN_DT2,                 \r\n"
             "       (SELECT COUNT(1) CNT FROM TB_VMS_IFSC) Total,                              \r\n"
             "       (VMS_IFSC_LIGT_NUM + VMS_IFSC_DELY_NUM + VMS_IFSC_CNGS_NUM) GenerateCount, \r\n"
             "       VMS_IFSC_LIGT_NUM G1,                                                      \r\n"
             "       VMS_IFSC_DELY_NUM G2,                                                      \r\n"
             "       VMS_IFSC_CNGS_NUM G3,                                                      \r\n"
             "       (SELECT COUNT(1) CNT FROM TB_VMS_IFSC) - VMS_IFSC_LIGT_NUM                 \r\n"
             "       - VMS_IFSC_DELY_NUM - VMS_IFSC_CNGS_NUM NoneDataCount                      \r\n"
             "  FROM TB_TRFC_PRCN_STAT                                                          \r\n"
             " WHERE PRCN_DT BETWEEN :p01 AND :p02                                              \r\n"
             " ORDER BY PRCN_DT                                                                 \r\n";
        break;
    default: return;
    }

    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 &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 TIAN00301::DspHistory()
{
    TADOQuery *pADO = ADOQry;
    TcxGridChartView *TvChart = FRAMEChart1->TvChart;

    try
    {
    	TcxGridChartSeries *pChart1 = NULL;
    	TcxGridChartSeries *pChart2 = NULL;
    	TcxGridChartSeries *pChart3 = NULL;
    	TcxGridChartSeries *pChart4 = NULL;
    	TcxGridChartSeries *pChart5 = NULL;
    	TcxGridChartSeries *pChart6 = 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->lblLTC1->Caption;//"원활";

			pChart2 = TvChart->CreateSeries();
			pChart2->DataBinding->ValueType = "Integer";
			pChart2->DisplayText = FrmLang->lblLTC2->Caption;//"지체";

			pChart3 = TvChart->CreateSeries();
			pChart3->DataBinding->ValueType = "Integer";
			pChart3->DisplayText = FrmLang->lblLTC3->Caption;//"정체";

			pChart4 = TvChart->CreateSeries();
			pChart4->DataBinding->ValueType = "Integer";
			pChart4->DisplayText = FrmLang->lblLTC0->Caption;//"정보없음";

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                String sDbDate = pADO->FieldByName("PRCN_DT")->AsString;
                m_pGDC->Values[nRow][Column01->Index] = ITSUtil_FormatStr(sDbDate, STR_DATETIME);

                m_pGDC->Values[nRow][Column02->Index] = pADO->FieldByName("Total")->AsInteger;
                m_pGDC->Values[nRow][Column03->Index] = pADO->FieldByName("GenerateCount")->AsInteger;
                m_pGDC->Values[nRow][Column04->Index] = pADO->FieldByName("G1")->AsInteger;
                m_pGDC->Values[nRow][Column05->Index] = pADO->FieldByName("G2")->AsInteger;
                m_pGDC->Values[nRow][Column06->Index] = pADO->FieldByName("G3")->AsInteger;
                m_pGDC->Values[nRow][Column07->Index] = pADO->FieldByName("NoneDataCount")->AsInteger;

				pChart1->Values[nRow] = m_pGDC->Values[nRow][Column04->Index];
				pChart2->Values[nRow] = m_pGDC->Values[nRow][Column05->Index];
				pChart3->Values[nRow] = m_pGDC->Values[nRow][Column06->Index];
				pChart4->Values[nRow] = m_pGDC->Values[nRow][Column07->Index];

				TvChart->Categories->Values[nRow] = pADO->FieldByName("PRCN_DT")->AsString.SubString(1,12);
            }
        }
        __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 TIAN00301::OnMessage(TMessage &Msg)
{
    switch (Msg.Msg)
    {
    case WM_PARAM_DATABASE:
        if (WP_DB_SELECT_OK == Msg.WParam)
        {
            //ShowMessage("Select Ok");
        }
        break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIAN00301::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"교통정보제공결과분석";

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

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

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

