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

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

#include "IHS0010MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalendar"
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxDropDownEdit"
#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 "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSpinEdit"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "cxTimeEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma resource "*.dfm"

TIHS0010M *IHS0010M = NULL;
//---------------------------------------------------------------------------
__fastcall TIHS0010M::TIHS0010M(TComponent* Owner)
	: TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FTitle = Caption;//"운영자 접속 이력조회";
}
//---------------------------------------------------------------------------

void __fastcall TIHS0010M::FormShow(TObject *Sender)
{
    PgList->ActivePageIndex = 0;

    // form 초기화
    FormInit();
}
//---------------------------------------------------------------------------

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

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<운영자 접속 이력 정보>";

    DtStDate->Date = Now() - 30;
    DtEdDate->Date = Now() - 1;

    //DtEdTime->EditValue = Now().FormatString("hh");
    DtStTime->Time = StrToDateTime("00:00");
    DtEdTime->Time = StrToDateTime("23:59");

    if (BtnSearch->Enabled)
        BtnSearch->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TIHS0010M::CommClose()
{
    try
    {
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TIHS0010M::TmrRefreshTimer(TObject *Sender)
{
	TmrRefresh->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TIHS0010M::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"운영자 접속이력";

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

void __fastcall TIHS0010M::BtnSearchClick(TObject *Sender)
{
    Application->ProcessMessages();
    TSqlCursor sqlCrs((TControl*)BtnSearch);

	RefreshData();
}
//---------------------------------------------------------------------------

void __fastcall TIHS0010M::RefreshData()
{
#if 0
	FStDateTime = DtStDate->Date.FormatString("yyyymmdd") + DtStTime->Time.FormatString("hh");
	FEdDateTime = DtEdDate->Date.FormatString("yyyymmdd") + DtEdTime->Time.FormatString("hh");
#else
	FStDateTime = DtStDate->Date.FormatString("yyyymmdd") + DtStTime->Time.FormatString("hhnn");
	FEdDateTime = DtEdDate->Date.FormatString("yyyymmdd") + DtEdTime->Time.FormatString("hhnn");
#endif
	if (wcscmp(FStDateTime.c_str(), FEdDateTime.c_str()) >= 0)
	{
        Application->MessageBox(FrmLang->lblQryDtErr->Caption.c_str(),//L"시작 시각이 종료 시각 보다 작거나 같습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = DtStDate;
        return;
    }
#if 0
    FStDateTime = FStDateTime + "0000";
    FEdDateTime = FEdDateTime + "5959";
#else
    FStDateTime = FStDateTime + "00";
    FEdDateTime = FEdDateTime + "59";
#endif

    SelHistory();
}
//---------------------------------------------------------------------------

void __fastcall TIHS0010M::SelHistory()
{
    String sQry;
    TADOQuery *pADO = ADOQry;

    sQry = "SELECT USER_ID, LOGIN_HMS, LOGOUT_HMS  \r\n"
           "  FROM TB_USER_CNNC_HS                 \r\n"
           " WHERE LOGIN_HMS BETWEEN :p01          \r\n"
           "                     AND :p02          \r\n"
           " ORDER BY USER_ID, LOGIN_HMS           \r\n";

    try
    {
        //pADO->Connection = ITSDb_GetConnection();

        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 &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);
    }
}
//---------------------------------------------------------------------------

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

    try
    {
        CMM_ClearGridTableView(TvList);

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

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                String sUsId = pADO->FieldByName("USER_ID")->AsString;
                String sInDt = pADO->FieldByName("LOGIN_HMS")->AsString;
                String sOuDt = pADO->FieldByName("LOGOUT_HMS")->AsString;

                m_pGDC->Values[nRow][Column01->Index] = sUsId;
                if (sInDt.IsEmpty())
                    m_pGDC->Values[nRow][Column02->Index] = "-";
                else
                    m_pGDC->Values[nRow][Column02->Index] = sInDt.SubString( 1, 4) + "-" +
                                                            sInDt.SubString( 5, 2) + "-" +
                                                            sInDt.SubString( 7, 2) + " " +
                                                            sInDt.SubString( 9, 2) + ":" +
                                                            sInDt.SubString(11, 2) + ":" +
                                                            sInDt.SubString(13, 2);

                if (sOuDt.IsEmpty())
                    m_pGDC->Values[nRow][Column03->Index] = "-";
                else
                    m_pGDC->Values[nRow][Column03->Index] = sOuDt.SubString( 1, 4) + "-" +
                                                            sOuDt.SubString( 5, 2) + "-" +
                                                            sOuDt.SubString( 7, 2) + " " +
                                                            sOuDt.SubString( 9, 2) + ":" +
                                                            sOuDt.SubString(11, 2) + ":" +
                                                            sOuDt.SubString(13, 2);
            }
        }
        __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 TIHS0010M::BtnCloseClick(TObject *Sender)
{
    Close();    
}
//---------------------------------------------------------------------------

void __fastcall TIHS0010M::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    IHS0010M = NULL;
    Action = caFree;
}
//---------------------------------------------------------------------------

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

void __fastcall TIHS0010M::TvListDataControllerFilterChanged(TObject *Sender)
{
    CMM_SetFilterLike(TvList);
}
//---------------------------------------------------------------------------

