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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "WindowMsgF.h"
#include "AppGlobalF.h"
#include "CDSCodeF.h"
#include "CDSNodeF.h"
#include "CDSLinkF.h"
#include "CDSIfscF.h"
#include "ITSSEL0MF.h"
#include "ITSSEL1MF.h"
#include "ITSSEL2MF.h"
#include "ITSSEL3MF.h"
#include "ITSSELAMF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include "ITS0020MF.h"

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

TITS0020M *ITS0020M = NULL;

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

    FTitle = Caption;//"소통정보제보 관리";

    FLinkLevel = 2;     //서비스링크레벨로 제보소통정보를 관리

    if (FLinkLevel == 1)
        Column01->Caption = lblLvl1->Caption;//"링크 ID";
    else if (FLinkLevel == 2)
        Column01->Caption = lblLvl2->Caption;//"서비스링크 ID";
}
//---------------------------------------------------------------------------

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

void __fastcall TITS0020M::CommClose()
{
    try
    {
        if (ItsTrafficOprLinkManager)
        {
            delete ItsTrafficOprLinkManager;
            ItsTrafficOprLinkManager = NULL;
        }
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

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

void __fastcall TITS0020M::FormInit()
{
    m_pGDC = TvList->DataController;

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<현재 제보 소통 정보가 없습니다>";
    if (!ItsTrafficOprLinkManager)
    {
        ItsTrafficOprLinkManager = new TItsTrafficOprLinkManager();
    }
    TcxDateEditProperties *pcxColumn02 = (TcxDateEditProperties*)Column02->Properties;
    //pcxColumn02->Kind = ;
    pcxColumn02->DisplayFormat = STR_DATETIME;
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    BtnSearchClick((TObject*)BtnSearch);
}
//---------------------------------------------------------------------------

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

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

void __fastcall TITS0020M::RefreshData()
{
    ItsTrafficOprLinkManager->LoadFromDb();
    SelListData();

    if (TvList->DataController->RecordCount > 0)
        BtnAllDelete->Enabled = true;
    else
        BtnAllDelete->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::SelListData()
{
    FUpdate = false;
    CMM_ClearGridTableView(TvList);

    try
    {
        TvList->BeginUpdate();

        try
        {
            ItsTrafficOprLinkManager->FLists.Lock();
            FOR_STL(TItsTrafficOprLink*, pObj, ItsTrafficOprLinkManager->FLists)
            {
                pObj->EditMode = DB_INIT;   // 미리초기화해준다.(삭제된것)
                pObj->Completed = true;

                AddObjectItem(pObj, "-");
            }
        }
        __finally
        {
            ItsTrafficOprLinkManager->FLists.UnLock();
        }
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        //CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::BtnCloseClick(TObject *Sender)
{
    Close();    
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"소통정보제보정보";

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

void __fastcall TITS0020M::BtnInsertClick(TObject *Sender)
{
    PopupAddLink->Popup(BtnInsert->ClientOrigin.x, BtnInsert->ClientOrigin.y + BtnInsert->Height);
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::BtnDeleteClick(TObject *Sender)
{
    int nIdx = m_pGDC->FocusedRecordIndex;
    if (nIdx < 0) return;

    int nMemPtr = m_pGDC->Values[nIdx][Column99->Index];
    TItsTrafficOprLink *pObj = (TItsTrafficOprLink*)nMemPtr;
    pObj->EditMode = DB_DELETE;
    m_pGDC->Values[nIdx][Column00->Index] = FrmLang->lblDelete->Caption;//"삭제";
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::BtnEditClick(TObject *Sender)
{
    TListFocus tvFocus(TvList);

    if (BtnEdit->Caption == FrmLang->lblCancel->Caption)//"취소")
    {
        CxList->SetFocus();
        Application->ProcessMessages();
        SaveData();
    }

    FrmLang->ITSSkin_ButtonClick(BtnSearch, BtnEdit, BtnInsert, BtnDelete, BtnApply);
    ChangeEditMode(BtnEdit->Down);

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

void __fastcall TITS0020M::ChangeEditMode(bool AEdit)
{
    bool bEditing = AEdit;

    try 
    {
        TvList->BeginUpdate();
        
        Column00->Visible = bEditing;
    
        Column03->Options->Editing = bEditing;
        Column04->Options->Editing = bEditing;
        Column06->Options->Editing = bEditing;
        Column08->Options->Editing = bEditing;
    }
    __finally 
    {
        TvList->EndUpdate();
        if (bEditing)
        {
            BtnEdit->Caption = FrmLang->lblCancel->Caption;//"취소";
            BtnEdit->Hint    = FrmLang->lblCancel->Hint;//데이터 편집 취소";
        }
        else
        {
            BtnEdit->Caption = FrmLang->lblEdit->Caption;//"편집";
            BtnEdit->Hint    = FrmLang->lblEdit->Hint;//데이터 편집";
        }
    }

    if (TvList->DataController->RecordCount > 0)
        BtnAllDelete->Enabled = true;
    else
        BtnAllDelete->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::BtnBestFitClick(TObject *Sender)
{
    try
    {
        TvList->BeginUpdate();
		TvList->ApplyBestFit(NULL, false, false);
	}
	__finally
	{
        TvList->EndUpdate();
	}
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::ChkAutoWidthPropertiesChange(TObject *Sender)
{
    try
    {
        TvList->BeginUpdate();
		TvList->OptionsView->ColumnAutoWidth = ChkAutoWidth->Checked;
        BtnBestFit->Visible = !ChkAutoWidth->Checked;
	}
	__finally
	{
        TvList->EndUpdate();
	}
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::MnuDeleteCancelClick(TObject *Sender)
{
    //    
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::BtnApplyClick(TObject *Sender)
{
    FUpdate = true;
    BtnEdit->Down = false;
    BtnEdit->Click();
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::MnuListAddClick(TObject *Sender)
{
    TListBox *SelList = NULL;
    TITSSEL1M *pSelForm1 = NULL;
    TITSSEL2M *pSelForm2 = NULL;

    try {
        if (FLinkLevel == 1)
        {
            pSelForm1 = new TITSSEL1M(this);
            pSelForm1->m_bSelected = false;
            pSelForm1->MultiSelect = true;
            pSelForm1->ShowModal();
            if (pSelForm1->m_bSelected)
            {
                SelList = pSelForm1->SelList;
            }
        }
        else if (FLinkLevel == 2)
        {
            pSelForm2 = new TITSSEL2M(this);
            pSelForm2->m_bSelected = false;
            pSelForm2->MultiSelect = true;
            pSelForm2->ShowModal();
            if (pSelForm2->m_bSelected)
            {
                SelList = pSelForm2->SelList;
            }
        }
        else return;

        try
        {
            TItsLink *pLink;
            TvList->BeginUpdate();
            for (int ii = 0; ii < SelList->Items->Count; ii++)
            {
                String sLinkId = SelList->Items->Strings[ii];

                if (FLinkLevel == 1)
                {
                    TItsLink *pLink = ItsLinkManager->FLists.Find(sLinkId);
                    if (!pLink) continue;
                }
                else if (FLinkLevel == 2)
                {
                    TItsIfsc *pIfsc = ItsIfscManager->FLists.Find(sLinkId);
                    if (!pIfsc) continue;
                }

                TItsTrafficOprLink *pOprLink = new TItsTrafficOprLink();

                pOprLink->LINK_ID      = sLinkId;
                pOprLink->CRTN_DT      = Now().FormatString("yyyymmddhhnnss");
                pOprLink->APLY_STRT_DT = Now().FormatString("yyyymmddhhnnss");
                pOprLink->APLY_END_DT  = IncHour(Now(), 1).FormatString("yyyymmddhhnnss");
                pOprLink->TFVL         = "0";
                pOprLink->SPED         = "20";
                pOprLink->OCPY_RATE    = "0";
                pOprLink->TRVL_HH      = "0";

                ItsTrafficOprLinkManager->FLists.Push(String(ItsTrafficOprLinkManager->FDataSeq++), pOprLink);
                pOprLink->Completed = false;

                AddObjectItem(pOprLink, FrmLang->lblNew->Caption);//"신규");

                pOprLink->EditMode = DB_NEW;
            }
        }
        __finally
        {
            LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
            TvList->EndUpdate();
            TvList->DataController->GotoFirst();
            TvList->DataController->FocusedRecordIndex = 0;
        }
        if (FLinkLevel == 1)
        {
            delete pSelForm1;
        }
        else if (FLinkLevel == 2)
        {
            delete pSelForm2;
        }

    } catch(...) { }
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::MnuGisAddClick(TObject *Sender)
{
    try {
        TITSSELAM *pSelForm = new TITSSELAM(this);
        pSelForm->MultiSelect = true;
        pSelForm->LinkLevel = FLinkLevel;
        pSelForm->ShowModal();
        Application->ProcessMessages();
        if (pSelForm->Selected)
        {
            try
            {
                TvList->BeginUpdate();
                for (int ii = 0; ii < pSelForm->TvList->DataController->RecordCount; ii++)
                {
                    String sLinkId = pSelForm->TvList->DataController->Values[ii][pSelForm->Column01->Index];
                    if (FLinkLevel == 1)
                    {
                        TItsLink *pLink = ItsLinkManager->FLists.Find(sLinkId);
                        if (!pLink) continue;
                    }
                    else if (FLinkLevel == 2)
                    {
                        TItsIfsc *pIfsc = ItsIfscManager->FLists.Find(sLinkId);
                        if (!pIfsc) continue;
                    }

                    TItsTrafficOprLink *pOprLink = new TItsTrafficOprLink();

                    pOprLink->LINK_ID      = sLinkId;
                    pOprLink->CRTN_DT      = Now().FormatString("yyyymmddhhnnss");
                    pOprLink->APLY_STRT_DT = Now().FormatString("yyyymmddhhnnss");
                    pOprLink->APLY_END_DT  = IncHour(Now(), 1).FormatString("yyyymmddhhnnss");
                    pOprLink->TFVL         = "0";
                    pOprLink->SPED         = "20";
                    pOprLink->OCPY_RATE    = "0";
                    pOprLink->TRVL_HH      = "0";

                    ItsTrafficOprLinkManager->FLists.Push(String(ItsTrafficOprLinkManager->FDataSeq++), pOprLink);
                    pOprLink->Completed = false;

                    AddObjectItem(pOprLink, FrmLang->lblNew->Caption);//"신규");

                    pOprLink->EditMode = DB_NEW;
                }
            }
            __finally
            {
                LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
                TvList->EndUpdate();
                TvList->DataController->GotoFirst();
                TvList->DataController->FocusedRecordIndex = 0;
            }
        }

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

void __fastcall TITS0020M::TvListCustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo)
    {
        String sType = AViewInfo->GridRecord->DisplayTexts[Column00->Index];
        if (sType == FrmLang->lblModify->Caption)//"수정")
        {
            ACanvas->Brush->Color = clOlive;
        }
        else if (sType == FrmLang->lblDelete->Caption)//"삭제")
        {
            ACanvas->Brush->Color = clRed;
        }
        else if (sType == FrmLang->lblNew->Caption)//"신규")
        {
            ACanvas->Brush->Color = clLime;
            if (AViewInfo->Focused) ACanvas->Font->Color = clBlack;
        }
    }
}
//---------------------------------------------------------------------------

bool __fastcall TITS0020M::AddObjectItem(TItsTrafficOprLink *pObj, String AMode)
{
    TItsNode *pNode;
    TItsLink *pLink;

    TItsIfsc *pIfsc;
    int       nRow;

    String sFNode;
    String sTNode;

    try
    {
        if (FLinkLevel == 1)
        {
            pLink = ItsLinkManager->FLists.Find(pObj->LINK_ID);
            if (!pLink) return false;

            sFNode = "[" + pLink->F_NODE_ID + "] ";
            sTNode = "[" + pLink->T_NODE_ID + "] ";
            pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
            if (pNode) sFNode = sFNode + pNode->NODE_NAME;

            pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
            if (pNode) sTNode = sTNode + pNode->NODE_NAME;
        }
        else if (FLinkLevel == 2)
        {
            pIfsc = ItsIfscManager->FLists.Find(pObj->LINK_ID);
            if (!pIfsc) return false;

            sFNode = pIfsc->STRT_NM;
            sTNode = pIfsc->END_NM;
        }
        else return false;

        nRow = m_pGDC->AppendRecord();
        pObj->FGridIndex = nRow;

        m_pGDC->Values[nRow][Column00->Index] = AMode;
        m_pGDC->Values[nRow][Column01->Index] = pObj->LINK_ID;
        m_pGDC->Values[nRow][Column10->Index] = "";
        m_pGDC->Values[nRow][Column11->Index] = "";

        m_pGDC->Values[nRow][Column10->Index] = sFNode;
        m_pGDC->Values[nRow][Column11->Index] = sTNode;

        m_pGDC->Values[nRow][Column02->Index] = ITSUtil_StrToDateTime(pObj->CRTN_DT);//.FormatString(STR_DATETIME);   //TODO
        m_pGDC->Values[nRow][Column03->Index] = "";
        m_pGDC->Values[nRow][Column04->Index] = "";

        if (pObj->APLY_STRT_DT.Length() == 14)
        m_pGDC->Values[nRow][Column03->Index] = ITSUtil_StrToDateTime(pObj->APLY_STRT_DT);//.FormatString(STR_DATETIME);   //TODO
        if (pObj->APLY_END_DT.Length() == 14)
        m_pGDC->Values[nRow][Column04->Index] = ITSUtil_StrToDateTime(pObj->APLY_END_DT);//.FormatString(STR_DATETIME);   //TODO


        //m_pGDC->Values[nRow][Column05->Index] = pObj->TFVL;       //교통량
        m_pGDC->Values[nRow][Column06->Index] = pObj->SPED;  //속도
        //m_pGDC->Values[nRow][Column07->Index] = pObj->OCPY_RATE;       //점유율
        m_pGDC->Values[nRow][Column08->Index] = pObj->TRVL_HH;    //통행시간

        m_pGDC->Values[nRow][Column99->Index] = (int)pObj;

        return true;
    }
    catch(...)
    {
    }
    return false;
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::ColumnPropertiesValidate(TObject *Sender, Variant &DisplayValue, TCaption &ErrorText, bool &Error)
{
    if (TvList->Controller->FocusedRow->IsFilterRow) return;

    int nIndex = m_pGDC->FocusedRecordIndex;
    if (nIndex < 0) return;

    int nMemPtr = TvList->DataController->Values[nIndex][Column99->Index];
    TItsTrafficOprLink *pObj = (TItsTrafficOprLink*)nMemPtr;
    if (pObj->Completed == false)
    {
        //신규이거나 삭제인 경우 편집 내용에 따른 변화가 필요없다.
        return;
    }

    String sStTime = "";
    String sEdTime = "";
    if (!VarIsNull(m_pGDC->Values[nIndex][Column03->Index]))
    {
        TDateTime dtTmp = m_pGDC->Values[nIndex][Column03->Index];
        sStTime = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    if (!VarIsNull(m_pGDC->Values[nIndex][Column04->Index]))
    {
        TDateTime dtTmp = m_pGDC->Values[nIndex][Column04->Index];
        sEdTime = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    String    sSped    = VarToStrDef(m_pGDC->Values[nIndex][Column06->Index], "0");
    String    sTrvTm   = VarToStrDef(m_pGDC->Values[nIndex][Column08->Index], "0");

    TcxGridColumn *pColumn = TvList->Controller->FocusedColumn;
    if (pColumn == Column03)
    {
        TDateTime dtTmp = DisplayValue;
        sStTime = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    else
    if (pColumn == Column04)
    {
        TDateTime dtTmp = DisplayValue;
        sEdTime = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    else
    if (pColumn == Column06)
        sSped = DisplayValue;
    else
    if (pColumn == Column08)
        sTrvTm = DisplayValue;

    if (sStTime != pObj->APLY_STRT_DT ||
        sEdTime != pObj->APLY_END_DT  ||
        sSped   != pObj->SPED         ||
        sTrvTm  != pObj->TRVL_HH )
    {
        m_pGDC->Values[nIndex][Column00->Index] = FrmLang->lblModify->Caption;//"수정";
        pObj->EditMode = DB_UPDATE;
    }
    else
    {
        m_pGDC->Values[nIndex][Column00->Index] = "-";
        pObj->EditMode = DB_INIT;
    }

#if 0
    String sDisplayValue = DisplayValue;
    if (sDisplayValue != pDb->FData[pColumn->Index])
    {
        //cxGrid_DataTV->DataController->Values[cxGrid_DataTV->DataController->FocusedRecordIndex][enGridPosition_EditMode] = FrmLang->lblModify->Caption;//"수정";
    }
#endif
}
//---------------------------------------------------------------------------

bool __fastcall TITS0020M::IsUpdate()
{
    try
    {
        ItsTrafficOprLinkManager->FLists.Lock();
        FOR_STL(TItsTrafficOprLink*, pObj, ItsTrafficOprLinkManager->FLists)
        {
            if (pObj->Completed && pObj->EditMode != DB_INIT)
                return true;

            if (!pObj->Completed && pObj->EditMode == DB_NEW)
                return true;
        }
    }
    __finally
    {
        ItsTrafficOprLinkManager->FLists.UnLock();
    }
    return false;
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::SaveData()
{
    if (!IsUpdate()) return;

    if (!FUpdate)
    {
        if (Application->MessageBox(FrmLang->lblEidtConfirm->Caption.c_str(),//L"변경된 정보가 있습니다.\r\n변경된 정보를 적용 하시겠습니까?",
                                    FTitle.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;
    }

    TADOQuery *pADOI = NULL;
    TADOQuery *pADOU = NULL;
    TADOQuery *pADOD = NULL;
	String sQryI;
	String sQryU;
	String sQryD;
    int    nEditMode;
    try
    {
        sQryI = "INSERT INTO TB_LINK_SYOP_TRAF ( \r\n"
                "            LINK_ID      ,      \r\n"
                "            CRTN_DT      ,      \r\n"
                "            APLY_STRT_DT ,      \r\n"
                "            APLY_END_DT  ,      \r\n"
                "            TFVL         ,      \r\n"
                "            SPED         ,      \r\n"
                "            OCPY_RATE    ,      \r\n"
                "            TRVL_HH       )     \r\n"
                "   VALUES (                     \r\n"
                "           :p01,                \r\n"
                "           :p02,                \r\n"
                "           :p03,                \r\n"
                "           :p04,                \r\n"
                "           :p05,                \r\n"
                "           :p06,                \r\n"
                "           :p07,                \r\n"
                "           :p08 )               \r\n";

        sQryU = "UPDATE TB_LINK_SYOP_TRAF    \r\n"
                "   SET APLY_STRT_DT = :p03, \r\n"
                "       APLY_END_DT  = :p04, \r\n"
                "       TFVL         = :p05, \r\n"
                "       SPED         = :p06, \r\n"
                "       OCPY_RATE    = :p07, \r\n"
                "       TRVL_HH      = :p08  \r\n"
                " WHERE LINK_ID      = :p01  \r\n"
                "   AND CRTN_DT      = :p02  \r\n";

        sQryD = "DELETE TB_LINK_SYOP_TRAF   \r\n"
                " WHERE LINK_ID      = :p01 \r\n"
                "   AND CRTN_DT      = :p02 \r\n";

        int nRow;
        TvList->BeginUpdate();
        try
        {
            pADOI = new TADOQuery(NULL);
			pADOI->Close();
            pADOI->Connection = ITSDb_GetConnection();

            pADOU = new TADOQuery(NULL);
			pADOU->Close();
            pADOU->Connection = ITSDb_GetConnection();

            pADOD = new TADOQuery(NULL);
			pADOD->Close();
            pADOD->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADOI, sQryI);
            ITSDb_SQLText(pADOU, sQryU);
            ITSDb_SQLText(pADOD, sQryD);

            ITSDb_GetConnection()->BeginTrans();

            try
            {
                ItsTrafficOprLinkManager->FLists.Lock();
                FOR_STL(TItsTrafficOprLink*, pObj, ItsTrafficOprLinkManager->FLists)
                {
                    if (pObj->FGridIndex < 0) continue;

                    if (pObj->Completed && pObj->EditMode == DB_INIT)
                        continue;

                    if (!pObj->Completed && pObj->EditMode != DB_NEW)
                        continue;

                    nRow = pObj->FGridIndex;

                    String    sLinkId  = VarToStr(m_pGDC->Values[nRow][Column01->Index]);
                    TDateTime dtCrTime = m_pGDC->Values[nRow][Column02->Index];
                    TDateTime dtStTime = m_pGDC->Values[nRow][Column03->Index];
                    TDateTime dtEdTime = m_pGDC->Values[nRow][Column04->Index];
                    String    sSped    = VarToStrDef(m_pGDC->Values[nRow][Column06->Index], "0");
                    String    sTrvTm   = VarToStrDef(m_pGDC->Values[nRow][Column08->Index], "0");

                    String sCrTime = dtCrTime.FormatString("YYYYMMDDHHNNSS");
                    String sStTime = dtStTime.FormatString("YYYYMMDDHHNNSS");
                    String sEdTime = dtEdTime.FormatString("YYYYMMDDHHNNSS");


                    pObj->LINK_ID      = sLinkId;
                    pObj->CRTN_DT      = sCrTime;
                    pObj->APLY_STRT_DT = sStTime;
                    pObj->APLY_END_DT  = sEdTime;
                    //pObj->TFVL         = "0";
                    pObj->SPED         = sSped;
                    //pObj->OCPY_RATE    = "0";
                    pObj->TRVL_HH      = sTrvTm;

                    nEditMode = pObj->EditMode;
                    if (pObj->Completed)
                    {
                        if (pObj->EditMode == DB_DELETE)
                        {
                            //delete
                            ITSDb_SQLBind(pADOD, "p01", pObj->LINK_ID);
                            ITSDb_SQLBind(pADOD, "p02", pObj->CRTN_DT);
                            ITSDb_SQLExec(pADOD);
                        }
                        else
                        if (pObj->EditMode == DB_UPDATE)
                        {
                            //update
                            ITSDb_SQLBind(pADOU, "p01", pObj->LINK_ID);
                            ITSDb_SQLBind(pADOU, "p02", pObj->CRTN_DT);
                            ITSDb_SQLBind(pADOU, "p03", pObj->APLY_STRT_DT);
                            ITSDb_SQLBind(pADOU, "p04", pObj->APLY_END_DT);
                            ITSDb_SQLBind(pADOU, "p05", pObj->TFVL);
                            ITSDb_SQLBind(pADOU, "p06", pObj->SPED);
                            ITSDb_SQLBind(pADOU, "p07", pObj->OCPY_RATE);
                            ITSDb_SQLBind(pADOU, "p08", pObj->TRVL_HH);
                            ITSDb_SQLExec(pADOU);
                        }
                    }
                    else
                    {
                        if (pObj->EditMode == DB_NEW)
                        {
                            //insert
                            ITSDb_SQLBind(pADOI, "p01", pObj->LINK_ID);
                            ITSDb_SQLBind(pADOI, "p02", pObj->CRTN_DT);
                            ITSDb_SQLBind(pADOI, "p03", pObj->APLY_STRT_DT);
                            ITSDb_SQLBind(pADOI, "p04", pObj->APLY_END_DT);
                            ITSDb_SQLBind(pADOI, "p05", pObj->TFVL);
                            ITSDb_SQLBind(pADOI, "p06", pObj->SPED);
                            ITSDb_SQLBind(pADOI, "p07", pObj->OCPY_RATE);
                            ITSDb_SQLBind(pADOI, "p08", pObj->TRVL_HH);
                            ITSDb_SQLExec(pADOI);
                        }
                    }
                    pObj->Completed = true;
                }
            }
            __finally
            {
                ItsTrafficOprLinkManager->FLists.UnLock();
            }

            ITSDb_GetConnection()->CommitTrans();
            Application->MessageBox(lblSave->Caption.c_str(),//L"제보소통정보를 적용 하였습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            if (nEditMode == DB_DELETE)
                DBERRORMSG(Caption, String(E.ClassName()), E.Message, pADOD->SQL->Text);
            else
            if (nEditMode == DB_UPDATE)
                DBERRORMSG(Caption, String(E.ClassName()), E.Message, pADOU->SQL->Text);
            else
            if (nEditMode == DB_NEW)
                DBERRORMSG(Caption, String(E.ClassName()), E.Message, pADOI->SQL->Text);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            if (nEditMode == DB_DELETE)
                DBERRORMSG(Caption, String(e.ClassName()), e.Message, pADOD->SQL->Text);
            else
            if (nEditMode == DB_UPDATE)
                DBERRORMSG(Caption, String(e.ClassName()), e.Message, pADOU->SQL->Text);
            else
            if (nEditMode == DB_NEW)
                DBERRORMSG(Caption, String(e.ClassName()), e.Message, pADOI->SQL->Text);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADOI)
        {
            pADOI->Close();
            delete pADOI;
        }
        if (pADOU)
        {
            pADOU->Close();
            delete pADOU;
        }
        if (pADOD)
        {
            pADOD->Close();
            delete pADOD;
        }
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::FormCloseQuery(TObject *Sender, bool &CanClose)
{
    SaveData();
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::TvListKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if (Key == VK_DELETE)
	{
		if (BtnDelete->Enabled) BtnDeleteClick(Sender);
	}
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::TvListDataControllerFilterChanged(TObject *Sender)
{
    CMM_SetFilterLike(TvList);
    LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::BtnMoveGisClick(TObject *Sender)
{
    TcxDataController *pGDC = TvList->DataController;
	int nRow = pGDC->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

    String sLinkId = pGDC->Values[nRow][Column01->Index];

    if (sLinkId.IsEmpty()) return;
    g_SelLink.pForm = this;
    g_SelLink.Level = FLinkLevel;
    g_SelLink.Id    = sLinkId;
    POST_MSG(Application->MainForm->Handle, WM_PLUGINFORM, WP_MSG_15, LP_MSG_15);
}
//---------------------------------------------------------------------------

void __fastcall TITS0020M::BtnAllApplyClick(TObject *Sender)
{
    TcxButton *pBtn = (TcxButton*)Sender;

    String sMsg;
    if (pBtn->Tag == 0)
    {
        //All Apply
        sMsg = pBtn->Caption + "\r\n" + lblAllApply->Caption;//"\r\n모든 구간에 대하여 소통정보 제보를 적용 하시겠습니까?";
    }
    else
    {
        //All Delete
        sMsg = pBtn->Caption + "\r\n" + lblAllClear->Caption;//"\r\n모든 구간에 대하여 소통정보 제보를 해제 하시겠습니까?";
    }

    if (Application->MessageBox(sMsg.c_str(), FTitle.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

    TSqlCursor sqlCrs((TControl*)pBtn);
    //Application->ProcessMessages();

    bool bResult = false;
    bResult = ItsTrafficOprLinkManager->AllDelete();

    //Application->ProcessMessages();
    if (bResult)
    {
        if (pBtn->Tag == 0)
        {
    TSqlCursor sqlCrs((TControl*)pBtn);
            ItsTrafficOprLinkManager->AllApply(FLinkLevel);
            Application->ProcessMessages();
        }
    }

    if (bResult)
    {
        Application->MessageBox(lblAll->Caption.c_str(),//L"모든 구간에 대하여 소통정보 제보 적용/해제 하였습니다.",
                                FTitle.c_str(), MB_OK | MB_ICONINFORMATION | MB_APPLMODAL);
    }
    //Application->ProcessMessages();

    //ChangeEditMode(BtnEdit->Down);
    RefreshData();
}
//---------------------------------------------------------------------------

