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

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

#include "BDS0020MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#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 "cxGroupBox"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxSpinEdit"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma resource "*.dfm"

TBDS0020M *BDS0020M = NULL;
//---------------------------------------------------------------------------
__fastcall TBDS0020M::TBDS0020M(TComponent* Owner)
    : TForm(Owner)
{
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);
    Col16->Visible = false;
    Col16->VisibleForCustomization = false;
}
//---------------------------------------------------------------------------

/*
 * 닫기버튼 이나 x버튼 클릭시 공통으로 처리하는 로직이들어간다..
 * Form과 DataModule class를 delete시킨다.
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TBDS0020M::CommClose()
{
    try
    {
        TmrQry->Enabled = false;
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

/*
 * Form을 보여줄때 호출되는 event 메서드이다.
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TBDS0020M::FormShow(TObject *Sender)
{
    Refresh();
    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

/*
 * form 초기화
 *
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TBDS0020M::FormInit()
{
}
//---------------------------------------------------------------------------

/*
 * Form이 Show되고 난 후 최초 1회 수행되는 타이머 이벤트
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TBDS0020M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;

    TmrQry->Interval = 100;
    TmrQry->Enabled = true;
}
//---------------------------------------------------------------------------

/*
 * Refresh Data Event Function
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TBDS0020M::RefreshData()
{
    // 검색조건에 의한 링크 데이터 조회
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.*,                                     \r\n"
           "       B.UPDT_DT,                               \r\n"
           "       B.TRMN_ID,                               \r\n"
           "       B.EVT_TYPE,                              \r\n"
           "       DECODE(B.EVT_TYPE, '1', '1.주기보고',    \r\n"
           "                          '2', '2.사용자전송',  \r\n"
           "                          '3', '3.긴급전송',    \r\n"
           "                          '---') AS EVT_DESC,   \r\n"
           "       B.X_CRDN,                                \r\n"
           "       B.Y_CRDN,                                \r\n"
           "       B.GPS_DRCT,                              \r\n"
           "       B.SPED,                                  \r\n"
           "       B.VHRN_MODE,                             \r\n"
           "       DECODE(B.VHRN_MODE, '1', '1.주행모드',   \r\n"
           "                           '2', '2.주차모드',   \r\n"
           "                           '---') AS VHRN_DESC, \r\n"
           "       B.TRMN_VER,                              \r\n"
           "       B.LINK_ID, C.ROAD_NAME                   \r\n"
           "  FROM TB_BDS_TRMN     A,                       \r\n"
           "       TB_BDS_RAW_PNST B,                       \r\n"
           "       TB_LINK         C                        \r\n"
           " WHERE A.BDS_MNGM_NMBR = B.BDS_MNGM_NMBR(+)     \r\n"
           "   AND B.LINK_ID = C.LINK_ID(+)                 \r\n"
           " ORDER BY B.UPDT_DT, A.BDS_MNGM_NMBR            \r\n";

    CMM_ClearGridTableView(TvList);

    try
    {
        int nRow;
        TvList->BeginUpdate();
        try
        {
            int nORD;

            pADO = new TADOQuery(NULL);
            pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
            pADO->SQL->Clear();
            pADO->SQL->Text = sQry;

            pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                nRow = TvList->DataController->AppendRecord();

                TvList->DataController->Values[nRow][Col00->Index] = pADO->FieldByName("BDS_MNGM_NMBR")->AsInteger;
                TvList->DataController->Values[nRow][Col01->Index] = pADO->FieldByName("OPNC_NMBR")->AsString;
                TvList->DataController->Values[nRow][Col02->Index] = pADO->FieldByName("MDM_NMBR")->AsString;
                TvList->DataController->Values[nRow][Col03->Index] = pADO->FieldByName("USIM_NMBR")->AsString;
                TvList->DataController->Values[nRow][Col04->Index] = pADO->FieldByName("VHCL_NMBR")->AsString;
                TvList->DataController->Values[nRow][Col05->Index] = pADO->FieldByName("OWNR_NM")->AsString;
                TvList->DataController->Values[nRow][Col06->Index] = pADO->FieldByName("PRCT_NM")->AsString;
                TvList->DataController->Values[nRow][Col07->Index] = ITSUtil_StrToDateTime(pADO->FieldByName("UPDT_DT")->AsString).FormatString(STR_DATETIME);
                TvList->DataController->Values[nRow][Col16->Index] = pADO->FieldByName("TRMN_ID")->AsString;
                TvList->DataController->Values[nRow][Col08->Index] = pADO->FieldByName("EVT_DESC")->AsString;
                TvList->DataController->Values[nRow][Col09->Index] = pADO->FieldByName("X_CRDN")->AsString;
                TvList->DataController->Values[nRow][Col10->Index] = pADO->FieldByName("Y_CRDN")->AsString;
                TvList->DataController->Values[nRow][Col11->Index] = pADO->FieldByName("GPS_DRCT")->AsString;
                TvList->DataController->Values[nRow][Col12->Index] = pADO->FieldByName("SPED")->AsString;
                TvList->DataController->Values[nRow][Col13->Index] = pADO->FieldByName("VHRN_DESC")->AsString;
                TvList->DataController->Values[nRow][Col14->Index] = pADO->FieldByName("TRMN_VER")->AsString;
                String LINK_ID = pADO->FieldByName("LINK_ID")->AsString;
                if (LINK_ID.Trim() == "0") LINK_ID = "";
                TvList->DataController->Values[nRow][Col20->Index] = LINK_ID;
                TvList->DataController->Values[nRow][Col21->Index] = pADO->FieldByName("ROAD_NAME")->AsString;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("블랙박스 원시현황 조회", 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("블랙박스 원시현황 조회", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

/*
 * Close 버튼 클릭 이벤트 핸들러
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TBDS0020M::BtnCloseClick(TObject *Sender)
{
    Close();    
}
//---------------------------------------------------------------------------

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

void __fastcall TBDS0020M::TmrQryTimer(TObject *Sender)
{
    TmrQry->Enabled = false;

    RefreshData();

    TmrQry->Interval = 60 * 1000;
    TmrQry->Enabled = true;
    BtnSearch->Enabled = true;
}
//---------------------------------------------------------------------------

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

    BtnSearch->Enabled = false;
    TmrQry->Interval = 100;
    TmrQry->Enabled = true;
}
//---------------------------------------------------------------------------

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

