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

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

#include "DSRH0601F.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"

TDSRH0601 *DSRH0601 = NULL;

//---------------------------------------------------------------------------
__fastcall TDSRH0601::TDSRH0601(TComponent* Owner, HWND hHandle, String sFrom, String sTo, String sIdList, String sCTYP, String sOBU_KIND)
    : TForm(Owner)
{           
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FParent     = hHandle;
    FStDateTime = sFrom;
    FEdDateTime = sTo;
    FIdList     = sIdList;
    FCTYP       = sCTYP;
    FOBU_KIND   = sOBU_KIND;

    //Caption = "DSRC OBU 수집 이력";
}
//---------------------------------------------------------------------------

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

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

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<DSRC OBU 수집 이력 정보>";
	//TvList->ViewInfo->GroupByBoxViewInfo->Text = "그룹핑할 필드의 헤더를 이곳으로 드래그 하세요";
    LblSearch->Caption = "검색조건: " + FStDateTime.SubString(1, 12) + " ~ " + FEdDateTime.SubString(1, 12);
}
//---------------------------------------------------------------------------

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

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

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

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

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

    String     sQry;
    TADOQuery *pADO = ADOQry;
    String     sCTyp = "";
    String     sObuK = "";

    if (FCTYP != "ALL")
    {
        sCTyp = "   AND H.CTYP = :p03      \r\n";
    }
    if (FOBU_KIND != "ALL")
    {
        sObuK = "   AND H.OBU_KIND = :p04  \r\n";
    }


    sQry = "SELECT M.ID, M.RSE_ID, M.ISTL_LCTN_NM, H.CLCT_DT,                \r\n"
           "       H.OBU_IDNT_NMBR, H.CTYP, H.OBU_KIND,                      \r\n"
           "       NVL(C.CTYPNM,     H.CTYP)     AS CTYPNM,                  \r\n"
           "       NVL(D.OBU_KINDNM, H.OBU_KIND) AS OBU_KINDNM               \r\n"
           "  FROM TB_RSE_MSTR M,                                            \r\n"
           "       TB_RSE_OBU_CLCT_HS H,                                     \r\n"
           "       (SELECT CMMN_CD AS CTYP, CMMN_CD_KOR_NM AS CTYPNM         \r\n"
           "          FROM TB_CMMN_CD                                        \r\n"
           "         WHERE CMMN_CLSF_CD = 'RCT') C,                          \r\n"
           "       (SELECT CMMN_CD AS OBU_KIND, CMMN_CD_KOR_NM AS OBU_KINDNM \r\n"
           "          FROM TB_CMMN_CD                                        \r\n"
           "         WHERE CMMN_CLSF_CD = 'OBU') D                           \r\n"
           " WHERE M.ID IN (" + FIdList + ")                                 \r\n"
           "   AND M.ID = H.ID                                               \r\n"
           "   AND H.CLCT_DT BETWEEN :p01 AND :p02                           \r\n"
           "   AND H.CTYP     = C.CTYP(+)                                    \r\n"
           "   AND H.OBU_KIND = D.OBU_KIND(+)                                \r\n"
           " ORDER BY M.ID, H.CLCT_DT                                        \r\n";

    sQry = sQry + sCTyp + sObuK;

    try
    {
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", FStDateTime);
        ITSDb_SQLBind(pADO, "p02", FEdDateTime);
        ITSDb_SQLOpen(pADO);
        if (FCTYP != "ALL")
        {
            ITSDb_SQLBind(pADO, "p03", FCTYP);
        }
        if (FOBU_KIND != "ALL")
        {
            ITSDb_SQLBind(pADO, "p04", FOBU_KIND);
        }

        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 TDSRH0601::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;

            String sTimeFormat = STR_DATETIME;
            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                m_pGDC->Values[nRow][Column00->Index] = pADO->FieldByName("ID")->AsString;
                m_pGDC->Values[nRow][Column01->Index] = pADO->FieldByName("RSE_ID")->AsString;
                m_pGDC->Values[nRow][Column02->Index] = pADO->FieldByName("ISTL_LCTN_NM")->AsString;
                String sDbDate = pADO->FieldByName("CLCT_DT")->AsString;
                //m_pGDC->Values[nRow][Column03->Index] = ITSUtil_FormatStr(sDbDate, sTimeFormat);
                m_pGDC->Values[nRow][Column03->Index] = ITSUtil_StrToDateTime(sDbDate).FormatString(sTimeFormat);
                m_pGDC->Values[nRow][Column04->Index] = pADO->FieldByName("OBU_IDNT_NMBR")->AsString;
                m_pGDC->Values[nRow][Column05->Index] = pADO->FieldByName("CTYPNM")->AsString;
                m_pGDC->Values[nRow][Column06->Index] = pADO->FieldByName("OBU_KINDNM")->AsString;
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
            }

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

            String sEnd = "데이터 " + LblRecords->Caption + " 이 조회 되었습니다.";
            Application->MessageBox(sEnd.c_str(), L"데이터 조회 완료", MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        }
    }
    catch(EDatabaseError &E)
    {
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(...)
    {
        throw Exception(FrmLang->lblDbErr->Caption);//"알수없는 오류가 발생하였습니다.");
    }
}
//---------------------------------------------------------------------------

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

void __fastcall TDSRH0601::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle = "DSRC OBU 수집이력";

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

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

