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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"
#include "CDSCodeF.h"
#include "CDSNodeF.h"
#include "CDSLinkF.h"
#include "CDSServiceLinkF.h"
#include "CDSRoadF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include "ITS0050MF.h"
#include "ITSSEL2MF.h"
#include "ITSSELAMF.h"

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

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

    FTitle = Caption;//"반복정체구간관리(기상)";
}
//---------------------------------------------------------------------------

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

/*
 * form 초기화
 *
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TITS0050M::FormInit()
{
    //SendMessage(DateTimePicker1->Handle, DTM_SETFORMAT, 0, Longint("HH:mm:ss"));
    //DateTimePicker1->Time = FormatDateTime("HH:mm:ss", Time());

    m_pGDC1 = TvList1->DataController;
    m_pGDC2 = TvList2->DataController;

    TvList1->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<예상 반복정체구간(기상) 정보가 없습니다>";
    TvList2->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<확정 반복정체구간(기상) 정보가 없습니다>";

    FCodeRCS = ItsCodeManager->FLists.Find("RCS");
#if 0
    FCodeDTW = ItsCodeManager->FLists.Find("DTW");
#else
    FCodeDTW = new TItsCode();

    FCodeDTW->CMMN_CLSF_CD      = "ITS0050M";        //'공통 분류 코드';
    FCodeDTW->MSG_TYPE_CD       = "";         //''메시지 유형 코드';
    FCodeDTW->CMMN_CLSF_KOR_NM  = "반복정체기상코드";    //''공통 분류 한글 명';
    FCodeDTW->CMMN_CLSF_ENGL_NM = "";   //''공통 분류 영문 명';
    FCodeDTW->USE_SYST_CD       = "공통";         //''사용 시스템 코드';
    FCodeDTW->RMRK              = "";                //''비고';
    FCodeDTW->USE_YN            = "Y";              //''사용 여부';
    FCodeDTW->FRST_REGR_NMBR    = "";      //''최초 등록자 번호';
    FCodeDTW->FRST_RGST_DT      = "";        //''최초 등록 일시';
    FCodeDTW->LAST_CRPR_NMBR    = "";      //''최종 수정자 번호';
    FCodeDTW->LAST_CRCT_DT      = "";        //''최종 수정 일시';
    FCodeDTW->Completed = true;

    for (int ii = 0; ii < 4; ii++)
    {
        TItsSubCode *pSubCode = new TItsSubCode();

        pSubCode->CMMN_CD      = String(ii);
        pSubCode->CMMN_CLSF_CD = "ITS0050M";
        switch(ii)
        {
        case 0: pSubCode->CMMN_CD_KOR_NM = "없음";  break;
        case 1: pSubCode->CMMN_CD_KOR_NM = "비";    break;
        case 2: pSubCode->CMMN_CD_KOR_NM = "비/눈"; break;
        case 3: pSubCode->CMMN_CD_KOR_NM = "눈";    break;
        }
        pSubCode->USE_YN           = "Y";
        FCodeDTW->FSubLists.Push(pSubCode->CMMN_CD, pSubCode);
        pSubCode->Completed = true;
    }
#endif
    FCodeLTC = ItsCodeManager->FLists.Find("LTC");
    FCodeIFD = ItsCodeManager->FLists.Find("IFD");

    if (FCodeDTW)
    {
        CbDayType->Properties->Items->Clear();
        FOR_STL(TItsSubCode *, pSubCode, FCodeDTW->FSubLists)
        {
            CbDayType->Properties->Items->Add("[" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
        }
        CbDayType->ItemIndex = 0;
    }
#if 0
    CbDayType->Properties->Items->Clear();
    CbDayType->Properties->Items->Add("[0] 없음");
    CbDayType->Properties->Items->Add("[1] 비");
    CbDayType->Properties->Items->Add("[2] 비/눈");
    CbDayType->Properties->Items->Add("[3] 눈");
    CbDayType->ItemIndex = 0;
#endif
}
//---------------------------------------------------------------------------

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

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

/*
 * Search 버튼 클릭 이벤트 핸들러
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TITS0050M::BtnSearchClick(TObject *Sender)
{
    Application->ProcessMessages();
    TSqlCursor sqlCrs((TControl*)BtnSearch);

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

/*
 * Refresh Data Event Function
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TITS0050M::RefreshData()
{
    ItsRepeatCongestManager->LoadFromDbWthr();
    SelConfirmList();
    SelUnConfirmList();
}
//---------------------------------------------------------------------------

void __fastcall TITS0050M::SelConfirmList()
{
    TItsServiceLink *pSvcLink;
    TItsSubCode     *pSubCode;

    CMM_ClearGridTableView(TvList2);

    int nRow2 = 0;
    try
    {
        TvList2->BeginUpdate();

        try
        {
            ItsRepeatCongestManager->FListsCnfmWthr.Lock();
            FOR_STL(TItsRepeatCongestWthr*, pRpLink, ItsRepeatCongestManager->FListsCnfmWthr)
            {
                //링크기본정보
                pSvcLink = ItsServiceLinkManager->FLists.Find(pRpLink->IFSC_ID);
                if (!pSvcLink) continue;    //기본정보가 없는 것은 보여줄 필요가 없다.

                //String sStatTm = pRpLink->STAT_YM;
                //if (sStatTm != "") sStatTm.Insert("-", 5);

                String sDTW = "[" + pRpLink->WTHR_TYPE_CD + "] "; //요일 유형 코드
                if (FCodeDTW)
                {
                    pSubCode = FCodeDTW->FSubLists.Find(pRpLink->WTHR_TYPE_CD);
                    if (pSubCode) sDTW = sDTW + pSubCode->CMMN_CD_KOR_NM;
                }

                String sStTm = pRpLink->CNFS_STRT_HM;
                sStTm.Insert(":", 3);

                String sEdTm = pRpLink->CNFS_END_HM;
                sEdTm.Insert(":", 3);

                String sRCS = "[" + pRpLink->REPT_CNGS_SECT_ORGN_CD + "] "; //반복 정체 구간 원본 코드
                if (FCodeRCS)
                {
                    pSubCode = FCodeRCS->FSubLists.Find(pRpLink->REPT_CNGS_SECT_ORGN_CD);
                    if (pSubCode) sRCS = sRCS + pSubCode->CMMN_CD_KOR_NM;
                }
                if (pRpLink->REPT_CNGS_SECT_ORGN_CD == "") sRCS = "";

                String sIFD = "[" + pSvcLink->DRCT_CD  + "] ";
                if (FCodeIFD)
                {
                    pSubCode = FCodeIFD->FSubLists.Find(pSvcLink->DRCT_CD);                 // 방향코드
                    if (pSubCode) sIFD = sIFD + pSubCode->CMMN_CD_KOR_NM;
                }

                nRow2 = m_pGDC2->AppendRecord();

                m_pGDC2->Values[nRow2][BColumn01->Index] = pSvcLink->IFSC_ID;    //ID
                m_pGDC2->Values[nRow2][BColumn03->Index] = sDTW;                 //요일
                m_pGDC2->Values[nRow2][BColumn04->Index] = sStTm;                //혼잡시작시분
                m_pGDC2->Values[nRow2][BColumn05->Index] = sEdTm;                //혼잡종료시분
                m_pGDC2->Values[nRow2][BColumn06->Index] = pRpLink->AVRG_SPED;   //평균속도
                m_pGDC2->Values[nRow2][BColumn07->Index] = pRpLink->AVRG_TRVL_HH;//평균통행시간
                m_pGDC2->Values[nRow2][BColumn08->Index] = pRpLink->DCSN_YN;     //확정여부
                m_pGDC2->Values[nRow2][BColumn09->Index] = ITSUtil_FormatStr(pRpLink->CRTN_YMD, STR_DATE);               //생성일자
                m_pGDC2->Values[nRow2][BColumn10->Index] = sRCS;                 //생성자

                m_pGDC2->Values[nRow2][BColumn11->Index] = pSvcLink->IFSC_NM;    //명칭
                m_pGDC2->Values[nRow2][BColumn12->Index] = sIFD;                 //방향
                m_pGDC2->Values[nRow2][BColumn13->Index] = pSvcLink->STRT_NM;    //시점명
                m_pGDC2->Values[nRow2][BColumn14->Index] = pSvcLink->END_NM;     //종점명

                m_pGDC2->Values[nRow2][BColumn99->Index] = (int)pRpLink;
            }
        }
        __finally
        {
            ItsRepeatCongestManager->FListsCnfmWthr.UnLock();
        }
    }
    __finally
    {
        TvList2->EndUpdate();
        TvList2->DataController->GotoFirst();
        TvList2->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

void __fastcall TITS0050M::SelUnConfirmList()
{
    TItsServiceLink *pSvcLink;
    TItsSubCode     *pSubCode;

    CMM_ClearGridTableView(TvList1);

    int nRow1 = 0;
    try
    {
        TvList1->BeginUpdate();

        try
        {
            ItsRepeatCongestManager->FListsWthr.Lock();
            FOR_STL(TItsRepeatCongestWthr*, pRpLink, ItsRepeatCongestManager->FListsWthr)
            {
                //링크기본정보
                pSvcLink = ItsServiceLinkManager->FLists.Find(pRpLink->IFSC_ID);
                if (!pSvcLink) continue;    //기본정보가 없는 것은 보여줄 필요가 없다.

                String sDTW = "[" + pRpLink->WTHR_TYPE_CD + "] "; //요일 유형 코드
                if (FCodeDTW)
                {
                    pSubCode = FCodeDTW->FSubLists.Find(pRpLink->WTHR_TYPE_CD);
                    if (pSubCode) sDTW = sDTW + pSubCode->CMMN_CD_KOR_NM;
                }

                String sStTm = pRpLink->CNFS_STRT_HM;
                sStTm.Insert(":", 3);

                String sEdTm = pRpLink->CNFS_END_HM;
                sEdTm.Insert(":", 3);

                String sRCS = "[" + pRpLink->REPT_CNGS_SECT_ORGN_CD + "] "; //반복 정체 구간 원본 코드
                if (FCodeRCS)
                {
                    pSubCode = FCodeRCS->FSubLists.Find(pRpLink->REPT_CNGS_SECT_ORGN_CD);
                    if (pSubCode) sRCS = sRCS + pSubCode->CMMN_CD_KOR_NM;
                }
                if (pRpLink->REPT_CNGS_SECT_ORGN_CD == "") sRCS = "";

                String sIFD = "[" + pSvcLink->DRCT_CD  + "] ";
                if (FCodeIFD)
                {
                    pSubCode = FCodeIFD->FSubLists.Find(pSvcLink->DRCT_CD);                 // 방향코드
                    if (pSubCode) sIFD = sIFD + pSubCode->CMMN_CD_KOR_NM;
                }

                nRow1 = m_pGDC1->AppendRecord();

                m_pGDC1->Values[nRow1][AColumn01->Index] = pSvcLink->IFSC_ID;    //ID
                m_pGDC1->Values[nRow1][AColumn03->Index] = sDTW;                 //요일
                m_pGDC1->Values[nRow1][AColumn04->Index] = sStTm;                //혼잡시작시분
                m_pGDC1->Values[nRow1][AColumn05->Index] = sEdTm;                //혼잡종료시분
                m_pGDC1->Values[nRow1][AColumn06->Index] = pRpLink->AVRG_SPED;   //평균속도
                m_pGDC1->Values[nRow1][AColumn07->Index] = pRpLink->AVRG_TRVL_HH;//평균통행시간
                m_pGDC1->Values[nRow1][AColumn08->Index] = pRpLink->DCSN_YN;     //확정여부
                m_pGDC1->Values[nRow1][AColumn09->Index] = ITSUtil_FormatStr(pRpLink->CRTN_YMD, STR_DATE);               //생성일자
                m_pGDC1->Values[nRow1][AColumn10->Index] = sRCS;                 //생성자

                m_pGDC1->Values[nRow1][AColumn11->Index] = pSvcLink->IFSC_NM;    //명칭
                m_pGDC1->Values[nRow1][AColumn12->Index] = sIFD;                 //방향
                m_pGDC1->Values[nRow1][AColumn13->Index] = pSvcLink->STRT_NM;    //시점명
                m_pGDC1->Values[nRow1][AColumn14->Index] = pSvcLink->END_NM;     //종점명

                m_pGDC1->Values[nRow1][AColumn99->Index] = (int)pRpLink;
            }
        }
        __finally
        {
            ItsRepeatCongestManager->FListsWthr.UnLock();
        }
    }
    __finally
    {
        TvList1->EndUpdate();
        TvList1->DataController->GotoFirst();
        TvList1->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

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

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

void __fastcall TITS0050M::BtnOperatorInputClick(TObject *Sender)
{
    GrpOperatroInput->Visible = !GrpOperatroInput->Visible;
}
//---------------------------------------------------------------------------
void __fastcall TITS0050M::EdLinkIdPropertiesButtonClick(TObject *Sender, int AButtonIndex)
{
    PopupAddLink->Popup(EdLinkId->ClientOrigin.x, EdLinkId->ClientOrigin.y + EdLinkId->Height);
}
//---------------------------------------------------------------------------

void __fastcall TITS0050M::MnuListListClick(TObject *Sender)
{
    try {
        TITSSEL2M *pSelForm = new TITSSEL2M(this);
        pSelForm->m_bSelected = false;
        pSelForm->ShowModal();
        if (pSelForm->m_bSelected)
        {
            EdLinkId->Text = pSelForm->m_sSelLinkId;
        }

        delete pSelForm;
    } catch(...) { }
}
//---------------------------------------------------------------------------

void __fastcall TITS0050M::MnuGisAddClick(TObject *Sender)
{
    try {
        TITSSELAM *pSelForm = new TITSSELAM(this);
        pSelForm->MultiSelect = false;
        pSelForm->LinkLevel = 2;
        pSelForm->ShowModal();
        Application->ProcessMessages();
        if (pSelForm->Selected)
        {
            String sLinkId = pSelForm->TvList->DataController->Values[0][pSelForm->Column01->Index];

            TItsServiceLink *pLink = ItsServiceLinkManager->FLists.Find(sLinkId);
            if (!pLink)
            {
                Application->MessageBox(L"정보가 없는 구간을 선택하였습니다. 다른 구간을 선택하세요.",
                                        FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
                return;
            }

            EdLinkId->Text = sLinkId;
        }

        delete pSelForm;
    } catch(...) { }
}
//---------------------------------------------------------------------------

void __fastcall TITS0050M::BtnCnfmCancelClick(TObject *Sender)
{
    UnconfirmData(true);
}
//---------------------------------------------------------------------------

void __fastcall TITS0050M::BtnAppCancelClick(TObject *Sender)
{
    UnconfirmData(false);
}
//---------------------------------------------------------------------------

void __fastcall TITS0050M::UnconfirmData(bool AConfirm)
{
    TcxGridTableView  *TvList = TvList1;
    TcxDataController *pGDC   = m_pGDC1;
    int                nIdx   = AColumn99->Index;
    String             sTitle = "예상 반복정체구간(기상) 취소";

    if (AConfirm)
    {
        TvList = TvList2;
        pGDC   = m_pGDC2;
        nIdx   = BColumn99->Index;
        sTitle = "확정 반복정체구간(기상) 취소";
    }

    int nSelRows = TvList->Controller->SelectedRowCount;
    if (nSelRows <= 0)
    {
        Application->MessageBox(L"작업 할 정체구간(기상)을 선택하세요.",
                                sTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        return;
    }

    if (Application->MessageBox(L"선택한 정체구간(기상)을 취소 하시겠습니까?",
                                sTitle.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

    TADOQuery *pADO = NULL;
	String sQry;
    try
    {
        TItsRepeatCongestWthr *pRpLink;
        int nSelIdx, nMemPtr;
        TvList->BeginUpdate();
//IFSC_ID, CRTN_YMD, WTHR_TYPE_CD, CNFS_STRT_HM
        sQry = "DELETE TB_REPT_CNGS_SECT_WTHR   \r\n"
               " WHERE IFSC_ID      = :p01 \r\n"
               "   AND CRTN_YMD     = :p02 \r\n"
               "   AND WTHR_TYPE_CD = :p03 \r\n"
               "   AND CNFS_STRT_HM = :p04 \r\n";
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);

            ITSDb_GetConnection()->BeginTrans();

            for (int ii = 0; ii < nSelRows; ii++)
            {
                nSelIdx = TvList->Controller->SelectedRows[ii]->RecordIndex;
                nMemPtr = pGDC->Values[nSelIdx][nIdx];
                pRpLink = (TItsRepeatCongestWthr *)nMemPtr;

                ITSDb_SQLBind(pADO, "p01", pRpLink->IFSC_ID);
                ITSDb_SQLBind(pADO, "p02", pRpLink->CRTN_YMD);
                ITSDb_SQLBind(pADO, "p03", pRpLink->WTHR_TYPE_CD);
                ITSDb_SQLBind(pADO, "p04", pRpLink->CNFS_STRT_HM);
                ITSDb_SQLExec(pADO);
            }
            ITSDb_GetConnection()->CommitTrans();
            Application->MessageBox(L"반복정체구간(기상) 취소 작업을 완료하였습니다.", sTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(FrmLang->lblDbDelErr->Caption);//"데이터 삭제 중에 알수없는 DB 오류가 발생하였습니다.");
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }

        TvList->EndUpdate();
        RefreshData();
    }
}
//---------------------------------------------------------------------------


void __fastcall TITS0050M::BtnApplyClick(TObject *Sender)
{
    int nSelRows = TvList1->Controller->SelectedRowCount;
    if (nSelRows <= 0)
    {
        Application->MessageBox(L"반복정체구간(기상)으로 확정할 구간을 선택하세요.",
                                L"반복정체구간(기상) 확정", MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        return;
    }

    if (Application->MessageBox(L"선택한 구간을 반복정체구간으로 확정 하시겠습니까?",
                                L"반복정체구간 확정", MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

    TADOQuery *pADO = NULL;
	String sQry;
    try
    {
        TItsRepeatCongestWthr *pRpLink;
        int nSelIdx, nMemPtr;
        TvList1->BeginUpdate();
//IFSC_ID, CRTN_YMD, WTHR_TYPE_CD, CNFS_STRT_HM
        sQry = "UPDATE TB_REPT_CNGS_SECT_WTHR   \r\n"
               "   SET DCSN_YN      = 'Y'  \r\n"
               " WHERE IFSC_ID      = :p01 \r\n"
               "   AND CRTN_YMD     = :p02 \r\n"
               "   AND WTHR_TYPE_CD = :p03 \r\n"
               "   AND CNFS_STRT_HM = :p04 \r\n";
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);

            ITSDb_GetConnection()->BeginTrans();

            for (int ii = 0; ii < nSelRows; ii++)
            {
                nSelIdx = TvList1->Controller->SelectedRows[ii]->RecordIndex;
                nMemPtr = m_pGDC1->Values[nSelIdx][AColumn99->Index];
                pRpLink = (TItsRepeatCongestWthr *)nMemPtr;

                ITSDb_SQLBind(pADO, "p01", pRpLink->IFSC_ID);
                ITSDb_SQLBind(pADO, "p02", pRpLink->CRTN_YMD);
                ITSDb_SQLBind(pADO, "p03", pRpLink->WTHR_TYPE_CD);
                ITSDb_SQLBind(pADO, "p04", pRpLink->CNFS_STRT_HM);
                ITSDb_SQLExec(pADO);
            }
            ITSDb_GetConnection()->CommitTrans();
            Application->MessageBox(FrmLang->lblDbSave->Caption.c_str(),//L"예상 반복정체구간(기상)을 확정 반복정체구간(기상)으로 저장 하였습니다.",
                                    L"반복정체구간(기상) 확정", MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(FrmLang->lblDbDelErr->Caption);//"데이터 삭제 중에 알수없는 DB 오류가 발생하였습니다.");
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }

        TvList1->EndUpdate();
        RefreshData();
    }
}
//---------------------------------------------------------------------------

void __fastcall TITS0050M::BtnSaveClick(TObject *Sender)
{
    String sSvcId = EdLinkId->Text.Trim();
    TItsServiceLink *pSvcLink;

    pSvcLink = ItsServiceLinkManager->FLists.Find(sSvcId);
    if (!pSvcLink)
    {
        Application->MessageBox(L"등록되어 있지 않은 서비스 구간이거나 서비스 구간을 선택하지 않으셨습니다.\r\n서비스 구간을 확인 후 작업하세요.",
                                L"반복정체구간(기상) 운영자등록", MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = EdLinkId;
        return;
    }

	String sStTime = DtStTime->Time.FormatString("hhnn");
	String sEdTime = DtEdTime->Time.FormatString("hhnn");
    if (sStTime > sEdTime)
    {
        Application->MessageBox(L"혼잡 시작시분 과 혼잡 종료시분이 정확히 입력되지 않았습니다.\r\n혼잡 시작/종료 시각을 확인 후 작업하세요.",
                                L"반복정체구간(기상) 운영자등록", MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = DtStTime;
        return;
    }

    String sDayType = ITSUtil_GetCode(CbDayType->Text);

    TADOQuery *pADO = NULL;
	String sQry;
    try
    {
        TItsRepeatCongestWthr *pRpLink;
        int nSelIdx;
        TvList1->BeginUpdate();

        sQry = "INSERT INTO TB_REPT_CNGS_SECT_WTHR (      \r\n"
               "            IFSC_ID                ,      \r\n"
               "            CRTN_YMD               ,      \r\n"
               "            WTHR_TYPE_CD           ,      \r\n"
               "            CNFS_STRT_HM           ,      \r\n"
               "            CNFS_END_HM            ,      \r\n"
               "            AVRG_SPED              ,      \r\n"
               "            AVRG_TRVL_HH           ,      \r\n"
               "            DCSN_YN                ,      \r\n"
               "            REPT_CNGS_SECT_ORGN_CD )      \r\n"
               "     VALUES (                             \r\n"
               "           :p01,                          \r\n"
               "            TO_CHAR(SYSDATE, 'YYYYMMDD'), \r\n"
               "           :p02,                          \r\n"
               "           :p03,                          \r\n"
               "           :p04,                          \r\n"
               "           :p05,                          \r\n"
               "           :p06,                          \r\n"
               "            'Y',                          \r\n"
               "            'RCS2' )                      \r\n";
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);

            ITSDb_GetConnection()->BeginTrans();

            ITSDb_SQLBind(pADO, "p01", sSvcId);
            ITSDb_SQLBind(pADO, "p02", sDayType);
            ITSDb_SQLBind(pADO, "p03", sStTime);
            ITSDb_SQLBind(pADO, "p04", sEdTime);
            ITSDb_SQLBind(pADO, "p05", SpSpeed->Value);
            ITSDb_SQLBind(pADO, "p06", SpAvgTm->Value);

            ITSDb_SQLExec(pADO);

            ITSDb_GetConnection()->CommitTrans();
            Application->MessageBox(FrmLang->lblDbSave->Caption.c_str(),//L"운영자 등록 반복정체구간(기상) 정보를 저장 하였습니다.",
                                    L"반복정체구간(기상) 운영자등록", MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
            EdLinkId->Text = "";
            BtnOperatorInputClick(NULL);
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(FrmLang->lblDbDelErr->Caption);//"데이터 삭제 중에 알수없는 DB 오류가 발생하였습니다.");
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }

        TvList1->EndUpdate();
        RefreshData();
    }
}
//---------------------------------------------------------------------------

void __fastcall TITS0050M::TvList1DataControllerFilterChanged(TObject *Sender)
{
    CMM_SetFilterLike(TvList1);
}
//---------------------------------------------------------------------------

