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

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

#include "VDSH0701F.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 "cxGridBandedTableView"
#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 link "dxSkinMcSkin"
#pragma resource "*.dfm"

TVDSH0701 *VDSH0701 = NULL;

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

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

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

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

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

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

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

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

    String sQry;
    TADOQuery *pADO = ADOQry;

    sQry = "SELECT A.VDS_DTCT_NMBR, A.CRTN_DT AS OCRR_DT, A.DTCT_STTS,                              \r\n"
           "       DECODE(A.DTCT_STTS, 0, '정상', 3, 'Oscillation', '알수없음') AS DTCT_STTS_NM,    \r\n"
           "       B.VDS_DTCT_ID, B.VDS_DTCT_NM, B.ISTL_LANE,                                       \r\n"
           "       B.DTCT_TYPE,                                                                     \r\n"
           "       (SELECT CMMN_CD_KOR_NM                                                           \r\n"
           "          FROM TB_CMMN_CD                                                               \r\n"
           "         WHERE CMMN_CLSF_CD = 'DTT'                                                     \r\n"
           "           AND CMMN_CD      = B.DTCT_TYPE) AS DTCT_TYPE_NM,                             \r\n"
           "       B.DTCT_DIR,                                                                      \r\n"
           "       (SELECT CMMN_CD_KOR_NM                                                           \r\n"
           "          FROM TB_CMMN_CD                                                               \r\n"
           "         WHERE CMMN_CLSF_CD = 'VCD'                                                     \r\n"
           "           AND CMMN_CD      = B.DTCT_DIR) AS DTCT_DIR_NM                                \r\n"
           "  FROM TB_VDS_DTCT_STTS_HS A,                                                           \r\n"
           "       TB_VDS_DTCT         B                                                            \r\n"
           " WHERE A.CRTN_DT BETWEEN :p01 AND :p02                                                  \r\n"
           "   AND B.VDS_DTCT_NMBR IN(" + FIdList + ")                                              \r\n"
           "   AND A.VDS_DTCT_NMBR = B.VDS_DTCT_NMBR                                                \r\n"
           " ORDER BY A.VDS_DTCT_NMBR, A.CRTN_DT                                                    \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 &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 TVDSH0701::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;

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                m_pGDC->Values[nRow][Column00->Index] = pADO->FieldByName("VDS_DTCT_NMBR")->AsString;
                m_pGDC->Values[nRow][Column01->Index] = pADO->FieldByName("VDS_DTCT_ID")->AsString;
                m_pGDC->Values[nRow][Column02->Index] = pADO->FieldByName("ISTL_LANE")->AsString;
                m_pGDC->Values[nRow][Column03->Index] = pADO->FieldByName("DTCT_TYPE")->AsString;
                m_pGDC->Values[nRow][Column04->Index] = pADO->FieldByName("VDS_DTCT_NM")->AsString;

                String sDbDate = pADO->FieldByName("OCRR_DT")->AsString;
                m_pGDC->Values[nRow][Column05->Index] = APP_FormatStr(sDbDate, STR_DATETIME);
                m_pGDC->Values[nRow][Column06->Index] = pADO->FieldByName("DTCT_STTS_NM")->AsString;
                m_pGDC->Values[nRow][Column07->Index] = pADO->FieldByName("DTCT_TYPE_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 TVDSH0701::OnMessage(TMessage &Msg)
{
    switch (Msg.Msg)
    {
    case WM_PARAM_DATABASE:
        if (WP_DB_SELECT_OK == Msg.WParam)
        {
            //ShowMessage("Select Ok");
        }
        break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TVDSH0701::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"VDS-검지기 보정이력";

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

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

void __fastcall TVDSH0701::Column06CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
	if( AViewInfo )
	{
		if(AViewInfo->Text == "Oscillation")
		{
			ACanvas->Canvas->Font->Color = clTeal;
		}
		else if(AViewInfo->Text == "알수없음")
		{
			ACanvas->Canvas->Font->Color = clGray;
		}
		else
		{
			ACanvas->Canvas->Font->Color = clLime;
		}
	}
}
//---------------------------------------------------------------------------

