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

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

#include "IAN00601F.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 link "cxLabel"
#pragma resource "*.dfm"

TIAN00601 *IAN00601 = NULL;

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

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

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<반복정체구간 분석 정보>";
    LblSearch->Caption = FrmLang->lblQryCond->Caption + FStDateTime + ", [" + FIdList + "]";
}
//---------------------------------------------------------------------------

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

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

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

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

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

    String sQry;
    String sWhere;
    TADOQuery *pADO = ADOQry;

    sWhere = "";
    if (FIdList != "ALL")
    {
        sWhere = "    AND A.DAY_TYPE_CD = '" + FIdList + "'  \r\n";
    }

    sQry = "SELECT (SELECT '['||CMMN_CD||'] '||CMMN_CD_KOR_NM                   \r\n"
           "          FROM TB_CMMN_CD                                           \r\n"
           "         WHERE CMMN_CLSF_CD = 'DTW'                                 \r\n"
           "           AND CMMN_CD      = A.DAY_TYPE_CD                         \r\n"
           "       ) AS DAY_TYPE_NM,                                            \r\n"
           "       A.STAT_YM, A.IFSC_ID,                                        \r\n"
           "       B.IFSC_NM, B.STRT_NM, B.END_NM, B.SECT_LNGT,                 \r\n"
           "       A.CNFS_STRT_HM, A.CNFS_END_HM, A.AVRG_SPED, A.AVRG_TRVL_HH,  \r\n"
           "       A.DCSN_YN, A.CRTN_YMD,                                       \r\n"
           "       (SELECT CMMN_CD_KOR_NM                                       \r\n"
           "          FROM TB_CMMN_CD                                           \r\n"
           "         WHERE CMMN_CLSF_CD = 'RCS'                                 \r\n"
           "           AND CMMN_CD      = A.REPT_CNGS_SECT_ORGN_CD              \r\n"
           "        ) AS REPT_CNGS_SECT_ORGN_NM                                 \r\n"
           "   FROM TB_REPT_CNGS_SECT A,                                        \r\n"
           "        TB_IFSC           B                                         \r\n"
           "  WHERE B.DEL_YN      = 'N'                                         \r\n"
           "    AND A.IFSC_ID     = B.IFSC_ID                                   \r\n"
           "    AND A.STAT_YM     = :p01                                        \r\n"
           + sWhere +
           "  ORDER BY A.CNFS_STRT_HM                                           \r\n";

    try
    {
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", FStDateTime);
        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 TIAN00601::DspHistory()
{
    TADOQuery *pADO = ADOQry;

    try
    {
        CMM_ClearGridTableView(TvList);

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

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

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                m_pGDC->Values[nRow][TvListColumn1->Index] = pADO->FieldByName("DAY_TYPE_NM")->AsString;

                String STAT_YM = pADO->FieldByName("STAT_YM")->AsString;
                m_pGDC->Values[nRow][TvListColumn2->Index] = ITSUtil_FormatStr(STAT_YM, STR_MM);

                m_pGDC->Values[nRow][TvListColumn3->Index] = pADO->FieldByName("IFSC_ID")->AsString;
                m_pGDC->Values[nRow][TvListColumn4->Index] = pADO->FieldByName("IFSC_NM")->AsString;
                m_pGDC->Values[nRow][TvListColumn5->Index] = pADO->FieldByName("STRT_NM")->AsString;
                m_pGDC->Values[nRow][TvListColumn6->Index] = pADO->FieldByName("END_NM")->AsString;
                m_pGDC->Values[nRow][TvListColumn7->Index] = pADO->FieldByName("SECT_LNGT")->AsString;

                String CNFS_STRT_HM = pADO->FieldByName("CNFS_STRT_HM")->AsString;
                String CNFS_END_HM  = pADO->FieldByName("CNFS_END_HM")->AsString;

                m_pGDC->Values[nRow][TvListColumn8->Index] = CNFS_STRT_HM.SubString(1,2) + ":" + CNFS_STRT_HM.SubString(3,2);
                m_pGDC->Values[nRow][TvListColumn9->Index] = CNFS_END_HM.SubString(1,2) + ":" + CNFS_END_HM.SubString(3,2);

                m_pGDC->Values[nRow][TvListColumn10->Index] = pADO->FieldByName("AVRG_SPED")->AsString;
                m_pGDC->Values[nRow][TvListColumn11->Index] = pADO->FieldByName("AVRG_TRVL_HH")->AsString;
                m_pGDC->Values[nRow][TvListColumn12->Index] = pADO->FieldByName("DCSN_YN")->AsString;

                String CRTN_YMD = pADO->FieldByName("CRTN_YMD")->AsString;
                m_pGDC->Values[nRow][TvListColumn13->Index] = ITSUtil_FormatStr(CRTN_YMD, STR_DATE);

                m_pGDC->Values[nRow][TvListColumn14->Index] = pADO->FieldByName("REPT_CNGS_SECT_ORGN_NM")->AsString;
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
            }

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

void __fastcall TIAN00601::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"반복정체구간분석";

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

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

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

