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

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

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

TWBH00101 *WBH00101 = NULL;

//---------------------------------------------------------------------------
__fastcall TWBH00101::TWBH00101(TComponent* Owner, HWND hHandle, String sFrom, String sTo, String sIdList)
    : TForm(Owner)
{           
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FParent     = hHandle;
    FStDateTime = sFrom;
    FEdDateTime = sTo;
    FIdList     = sIdList;

    Caption = "홈페이지 관리자 이력조회";
}
//---------------------------------------------------------------------------
__fastcall TWBH00101::~TWBH00101(void)
{
}
//--------------------------------------------------------------------------

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

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<홈페이지 관리자 이력 정보>";
	//TvList->ViewInfo->GroupByBoxViewInfo->Text = "그룹핑할 필드의 헤더를 이곳으로 드래그 하세요";

    LblSearch->Caption = "검색조건: [" + FIdList + "] " + FStDateTime.SubString(1, 12) + " ~ " + FEdDateTime.SubString(1, 12);
}
//---------------------------------------------------------------------------

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

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

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

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

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

    String sQry;
    TADOQuery *pADO = ADOQry;

    sQry = "SELECT B.EMAIL, B.NAME AS USERNAME,                             \r\n"
           "       NVL(C.PAGE_NAME, A.PAGE_ID) AS PAGENAME,                 \r\n"
           "       TO_CHAR(A.RGST_DT, 'YYYYMMDDHH24MISS') AS RGST_DT,       \r\n"
           "       A.IP_ADDR AS IP_ADDRESS, A.EXECUTE_ID, A.EXECUTE_RES,    \r\n"
           "       A.REMARKS                                                \r\n"
           "  FROM TB_WEB_USER_HS   A,                                      \r\n"
           "       TB_WEB_USER_INFR B,                                      \r\n"
           "       TB_WEB_PAGE      C                                       \r\n"
           " WHERE A.RGST_DT BETWEEN TO_DATE(:p01, 'YYYYMMDDHH24MISS')      \r\n"
           "                     AND TO_DATE(:p02, 'YYYYMMDDHH24MISS')      \r\n"
           "   AND A.USER_ID IN (" + FIdList + ")                           \r\n"
           "   AND A.USER_ID = B.USER_ID                                    \r\n"
           "   AND A.PAGE_ID = C.PAGE_ID(+)                                 \r\n"
           " ORDER BY B.EMAIL, PAGENAME                                     \r\n";

    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 TWBH00101::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][Column01->Index] = pADO->FieldByName("EMAIL")->AsString;
                m_pGDC->Values[nRow][Column02->Index] = pADO->FieldByName("USERNAME")->AsString;
                String sDbDate = pADO->FieldByName("RGST_DT")->AsString;
                m_pGDC->Values[nRow][Column03->Index] = ITSUtil_FormatStr(sDbDate, STR_DATETIME);
                m_pGDC->Values[nRow][Column04->Index] = pADO->FieldByName("IP_ADDRESS")->AsString;
                m_pGDC->Values[nRow][Column05->Index] = pADO->FieldByName("PAGENAME")->AsString;

                String sJobType = pADO->FieldByName("EXECUTE_ID")->AsString;
                if      (sJobType == "I") sJobType = "로그인";
                else if (sJobType == "O") sJobType = "로그아웃";
                else if (sJobType == "C") sJobType = "등록";
                else if (sJobType == "R") sJobType = "조회";
                else if (sJobType == "U") sJobType = FrmLang->lblModify->Caption;//"수정";
                else if (sJobType == "D") sJobType = FrmLang->lblDelete->Caption;//"삭제";

                m_pGDC->Values[nRow][Column06->Index] = sJobType;

                String sJobRes = pADO->FieldByName("EXECUTE_RES")->AsString;
                if      (sJobRes == "0") sJobRes = "성공";
                else if (sJobRes == "1") sJobRes = "실패";

                m_pGDC->Values[nRow][Column07->Index] = sJobRes;

                m_pGDC->Values[nRow][Column08->Index] = pADO->FieldByName("REMARKS")->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)
    {
        ::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 TWBH00101::OnMessage(TMessage &Msg)
{
    switch (Msg.Msg)
    {
    case WM_PARAM_DATABASE:
        if (WP_DB_SELECT_OK == Msg.WParam)
        {
            //ShowMessage("Select Ok");
        }
        break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TWBH00101::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle = "홈페이지관리자이력조회";

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

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

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

void __fastcall TWBH00101::Column07CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
	if (AViewInfo->Value == "실패")
    {
	    ACanvas->Canvas->Brush->Color = clRed;
	    ACanvas->Canvas->Font->Color  = clWhite;
	}
}
//---------------------------------------------------------------------------

