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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "AppGlobalF.h"
#include "CDSCodeF.h"
#include "CDSFacilityF.h"
#include "CDSNodeF.h"
#include "CDSLinkF.h"
#pragma hdrstop

#include "AVIM020MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_AviListF"
#pragma link "FRAME_AviSectListF"
#pragma link "FRAME_FlashMapF"
#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 "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"

TAVIM020M *AVIM020M = NULL;
//---------------------------------------------------------------------------
void TAVIM020M_FlashCall(TObject *ASender, const WideString request)
{
    try
    {
        if (!AVIM020M) return;
        AVIM020M->ShockwaveFlash1FlashCall1(ASender, request);
    }
    catch(Exception &exception)
    {
    }
}

//---------------------------------------------------------------------------
__fastcall TAVIM020M::TAVIM020M(TComponent* Owner)
    : TForm(Owner)
{
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FUpdate= false;
    FTitle = "AVI-구간 관리";
    MapStart = false;

    m_pGDC = FRAMEAviSectList1->TvList->DataController;

    FRAMEGisMap1->FlashCallbackFunc = TAVIM020M_FlashCall;
    FRAMEGisMap1->DrawMode = 1; //구간정보표출

    FRAMEGisMap1->ImgPan->Visible = true;
    FRAMEGisMap1->ImgZoomIn->Visible = true;
    FRAMEGisMap1->ImgZoomOut->Visible = true;
    FRAMEGisMap1->ImgSel->Visible = true;
    FRAMEGisMap1->ImgStnd->Visible = true;
    FRAMEGisMap1->ImgRefresh->Visible = true;

    FRAMEGisMap1->LbDef->Caption = "(선택버튼을 클릭하여 선택합니다.)";
    FRAMEGisMap1->LbDef->Hint = FRAMEGisMap1->LbDef->Caption;
    FRAMEGisMap1->LbDef->Visible = true;
    FRAMEGisMap1->ImgSel->Visible = false;

    FRAMEAviList1->PnlTop->Visible = false;
    FRAMEAviList1->PnlBottom->Visible = false;
    FRAMEAviList1->ColumnSel->Visible = false;
    FRAMEAviList1->Column02->Visible = false;

    FRAMEAviSectList1->PnlTop->Visible = false;
    FRAMEAviSectList1->PnlBottom->Visible = false;
    FRAMEAviSectList1->ColumnSel->Visible = false;

    TvLink->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<AVI 구간 링크구성정보>";
}
//---------------------------------------------------------------------------

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

/*
 * Form을 보여줄때 호출되는 event 메서드이다.
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TAVIM020M::FormShow(TObject *Sender)
{
    Refresh();

    CbAVI_CTLR_MNGM_NMBR->Properties->Items->Clear();
    try
    {
        FRAMEAviList1->MyItsAviManager->FLists.Lock();
        FOR_STL(TItsAvi*, pObj, FRAMEAviList1->MyItsAviManager->FLists)
        {
            if (pObj->DEL_YN == "Y") continue;

            String sId = pObj->AVI_CTLR_MNGM_NMBR;      //관리번호
            String sNm = pObj->AVI_LCTN_NM;             //위치
            CbAVI_CTLR_MNGM_NMBR->Properties->Items->Add("[" + sId + "] " + sNm);
        }
    }
    __finally
    {
        FRAMEAviList1->MyItsAviManager->FLists.UnLock();
    }

    CbAVI_CTLR_MNGM_NMBR->ItemIndex = -1;
    CbAVI_CTLR_MNGM_NMBR->Properties->ReadOnly = true;
    EdId->Properties->ReadOnly            = true;
    EdAVI_SECT_NMBR->Properties->ReadOnly = true;
    EdSTRT_SPOT_NM->Properties->ReadOnly  = true;
    EdEND_SPOT_NM->Properties->ReadOnly   = true;
    EdAVI_SECT_NM->Properties->ReadOnly   = true;
    SeAVI_SECT_DSTC->Properties->ReadOnly = true;
    SeSHRT_PASS_HH->Properties->ReadOnly  = true;
    SeLNST_PASS_HH->Properties->ReadOnly  = true;
    SeSPOT_NUM->Properties->ReadOnly      = true;
    SeSTND_DRFT_VAL->Properties->ReadOnly = true;
    SeMIN_SPED->Properties->ReadOnly      = true;
    SeMAX_SPED->Properties->ReadOnly      = true;
    SeSMTH_FCTR->Properties->ReadOnly     = true;

    FormInit();
    ChangeEditMode(1);
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

/*
 * form 초기화
 *
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TAVIM020M::FormInit()
{
    CbAVI_CTLR_MNGM_NMBR->ItemIndex = -1;
    EdId->Text             = "";
    EdAVI_SECT_NMBR->Text  = "";
    EdSTRT_SPOT_NM->Text   = "";
    EdEND_SPOT_NM->Text    = "";
    EdAVI_SECT_NM->Text    = "";
    SeAVI_SECT_DSTC->Value = 0;
    SeSHRT_PASS_HH->Value  = 0;
    SeLNST_PASS_HH->Value  = 0;
    SeSPOT_NUM->Value      = 0;
    SeSTND_DRFT_VAL->Value = 0;
    SeMIN_SPED->Value      = 0;
    SeMAX_SPED->Value      = 0;
    SeSMTH_FCTR->Value     = 0;

    CMM_ClearGridTableView(TvLink);

    FNewDb = false;
}
//---------------------------------------------------------------------------

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

    FRAMEGisMap1->LinkLevel = 1;
    FRAMEGisMap1->LoadGisMap();

    FRAMEAviList1->UpdateList();
    //FRAMEAviSectList1->UpdateList();

    FRAMEAviSectList1->MyItsAviManager->LoadFromSectLinkDb();

    UpdateList();
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::UpdateList()
{
    FRAMEAviSectList1->UpdateList();
}
//---------------------------------------------------------------------------

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

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

void __fastcall TAVIM020M::FormCloseQuery(TObject *Sender, bool &CanClose)
{
#if 0
//xxxxxxxx
    if (FUpdate)
    {
        POST_MSG(Application->MainForm->Handle, WM_DATABASE_REFRESH, WP_MSG_14, 0);
    }
#endif
    FRAMEAviList1->OnCloseQuery(CanClose);
    FRAMEAviSectList1->OnCloseQuery(CanClose);
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::DisplayInfo(TItsAviSect* AObj)
{
    FormInit();

    if (!AObj) return;

    CMM_SetComboBoxItem(CbAVI_CTLR_MNGM_NMBR, AObj->AVI_CTLR_MNGM_NMBR);
    EdId->Text             = AObj->AVI_CTLR_MNGM_NMBR;
    EdAVI_SECT_NMBR->Text  = AObj->AVI_SECT_NMBR;
    EdSTRT_SPOT_NM->Text   = AObj->STRT_SPOT_NM;
    EdEND_SPOT_NM->Text    = AObj->END_SPOT_NM;
    EdAVI_SECT_NM->Text    = AObj->AVI_SECT_NM;
    SeAVI_SECT_DSTC->Value = AObj->AVI_SECT_DSTC.ToIntDef(0);
    SeSHRT_PASS_HH->Value  = AObj->SHRT_PASS_HH.ToIntDef(0);
    SeLNST_PASS_HH->Value  = AObj->LNST_PASS_HH.ToIntDef(0);
    SeSPOT_NUM->Value      = AObj->SPOT_NUM.ToIntDef(0);
    SeSTND_DRFT_VAL->Value = AObj->STND_DRFT_VAL.ToIntDef(0);
    SeMIN_SPED->Value      = AObj->MIN_SPED.ToIntDef(0);
    SeMAX_SPED->Value      = AObj->MAX_SPED.ToIntDef(0);
    SeSMTH_FCTR->Value     = StrToFloatDef(AObj->SMTH_FCTR, 0);

    ChangeEditMode(3);

    DisplayLink(AObj);
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::DisplayLink(TItsAviSect* AObj)
{
    if (!AObj) return;

    CMM_ClearGridTableView(TvLink);

    try
    {
        int nRow;
        TcxDataController *pGDC = TvLink->DataController;
        TvLink->BeginUpdate();

        FOR_STL(TItsAviLink *, pObj, AObj->FLinkLists)
        {
            String sLinkId = pObj->LINK_ID;
            nRow = pGDC->AppendRecord();
            pGDC->Values[nRow][ColLink2->Index] = sLinkId;

            TItsLink *pLink = ItsLinkManager->FLists.Find(sLinkId);
            if (pLink)
            {
                TItsNode *pNode;
                pGDC->Values[nRow][ColLink3->Index] = pLink->F_NODEID;
                pGDC->Values[nRow][ColLink5->Index] = pLink->T_NODEID;
                pNode = ItsNodeManager->FLists.Find(pLink->F_NODEID);
                if (pNode)
                {
                    pGDC->Values[nRow][ColLink4->Index] = pNode->NODE_NAME;
                }
                pNode = ItsNodeManager->FLists.Find(pLink->T_NODEID);
                if (pNode)
                {
                    pGDC->Values[nRow][ColLink6->Index] = pNode->NODE_NAME;
                }
            }
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }

    RefreshLink();
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::UpdateAviIcon()
{
    if (!MapStart) return;

    TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "RemoveFacility", FAC_AVI);

    TItsFacility *pFacility;
    try
    {
        //if (ChkViewIcon->Checked)
        {
            String sObjList = "";

            try
            {
                FRAMEAviList1->MyItsAviManager->FLists.Lock();
                FOR_STL(TItsAvi*, pObj, FRAMEAviList1->MyItsAviManager->FLists)
                {
                    if (pObj->DEL_YN == "Y") continue;

                    //pFacility = FRAMEAviList1->MyItsAviManager->FLists.Find(pObj->AVI_ID);
                    //if (!pFacility) continue;

                    String sLoc = TCommFlash::ReplaceName(pObj->AVI_LCTN_NM);
                    sObjList += (pObj->AVI_ID + "," + "0," + sLoc + "," + pObj->X_CRDN + "," + pObj->Y_CRDN + "," + pObj->AVI_ID + "|");
                }
            }
            __finally
            {
                FRAMEAviList1->MyItsAviManager->FLists.UnLock();

                if (sObjList != "")
                {
                    sObjList = sObjList.SubString(1, sObjList.Length()-1);
                    TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "AddFacility", FAC_AVI + "|" + sObjList);
                }
            }
        }
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::BtnEditClick(TObject *Sender)
{
    if (BtnEdit->Caption == "편집")
    {
        ChangeEditMode(0);
        FNewDb = false;
    }
    else
    {
        String sId = EdId->Text;
        String sSectId = EdAVI_SECT_NMBR->Text;

        ChangeEditMode(1);

        TItsAvi *pAvi = FRAMEAviSectList1->MyItsAviManager->FLists.Find(sId);
        if (pAvi)
        {
            TItsAviSect *pObj = pAvi->FSubLists.Find(sSectId);
            if (pObj)
            {
                CMM_SetGridRow(FRAMEAviSectList1->CxList, FRAMEAviSectList1->TvList, sSectId, FRAMEAviSectList1->Column02->Index);
                DisplayInfo(pObj);
            }
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::ChangeEditMode(int AEdit)
{
    bool bGisEdit = true;
    bool bEnabled = false;

    switch(AEdit)
    {
    case 0: //편집버튼클릭
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = false;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = true;

            BtnEdit->Caption = FrmLang->lblCancel->Caption;//"취소";
            BtnEdit->Hint    = FrmLang->lblCancel->Hint;//데이터 편집 취소";
        }
        break;
    case 1: //편집[취소]버튼클릭
        {
            FormInit(); //화면을 모두 지운다.
            BtnEdit->Enabled = false;
            BtnInsert->Enabled = true;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = false;

            BtnEdit->Caption = FrmLang->lblEdit->Caption;//"편집";
            BtnEdit->Hint    = FrmLang->lblEdit->Hint;//데이터 편집";
            bEnabled = true;
            bGisEdit = false;
        }
        break;
    case 2: //추가
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = false;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = true;

            BtnEdit->Caption = FrmLang->lblCancel->Caption;//"취소";
            BtnEdit->Hint    = FrmLang->lblCancel->Hint;//데이터 편집 취소";
        }
        break;
    case 3: //수정모드
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = true;
            BtnDelete->Enabled = true;
            BtnSave->Enabled = false;

            BtnEdit->Caption = FrmLang->lblEdit->Caption;//"편집";
            BtnEdit->Hint    = FrmLang->lblEdit->Hint;//데이터 편집";
            bEnabled = true;
        }
        break;
    }

    if (AEdit == 0 || AEdit == 2)
    {
        FRAMEAviSectList1->CxList->Enabled = false;
    }
    else
    {
        FRAMEAviSectList1->CxList->Enabled = true;
    }

    //CbAVI_CTLR_MNGM_NMBR->ItemIndex = -1;
    CbAVI_CTLR_MNGM_NMBR->Properties->ReadOnly = bEnabled;
    EdId->Properties->ReadOnly            = bEnabled;
    EdAVI_SECT_NMBR->Properties->ReadOnly = bEnabled;
    EdSTRT_SPOT_NM->Properties->ReadOnly  = bEnabled;
    EdEND_SPOT_NM->Properties->ReadOnly   = bEnabled;
    EdAVI_SECT_NM->Properties->ReadOnly   = bEnabled;
    SeAVI_SECT_DSTC->Properties->ReadOnly = bEnabled;
    SeSHRT_PASS_HH->Properties->ReadOnly  = bEnabled;
    SeLNST_PASS_HH->Properties->ReadOnly  = bEnabled;
    SeSPOT_NUM->Properties->ReadOnly      = bEnabled;
    SeSTND_DRFT_VAL->Properties->ReadOnly = bEnabled;
    SeMIN_SPED->Properties->ReadOnly      = bEnabled;
    SeMAX_SPED->Properties->ReadOnly      = bEnabled;
    SeSMTH_FCTR->Properties->ReadOnly     = bEnabled;

    BtnLinkAdd->Enabled = !bEnabled;
    BtnLinkDel->Enabled = !bEnabled;

    FRAMEGisMap1->ImgSel->Visible = false;
    if (FRAMEGisMap1->MapStart)
    {
        //TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "ClearSelLink", "0");
        RefreshLink();
        FRAMEGisMap1->ImgSel->Visible = bGisEdit;
        if (bGisEdit == false)
        {
            if (MapStart)
            {
                TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "SelectMode", "0");   //PAN
            }
        }
    }
}
//---------------------------------------------------------------------------


void __fastcall TAVIM020M::BtnInsertClick(TObject *Sender)
{
    FormInit();
    //신규아이디를 얻어와서 화면을 초기화한다.
    String sId;
#if 0
    if (!FRAMEAviSectList1->MyItsAviManager->GetNextAviSectId(sId))
    {
        Application->MessageBox(L"새로운 AVI-구간 번호를 생성하지 못하였습니다.\r\n프로그램 종료후에 다시 시도해 보세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }
#else
	sId = InputBox("AVI-구간 번호", "신규 AVI-구간 번호를 입력하세요(20Byte).", "AVI-구간 번호" );

    sId = sId.Trim();
	if (sId == "AVI-구간 번호" || sId == "")
	{
		return;
	}
	if (AnsiString(sId).Length() > 10)
	{
        Application->MessageBox(L"AVI-구간 번호는  10Byte를 초과할 수 없습니다.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
	}

    try
    {
        FRAMEAviSectList1->MyItsAviManager->FLists.Lock();
        FOR_STL(TItsAvi*, pObj, FRAMEAviSectList1->MyItsAviManager->FLists)
        {
            TItsAviSect *pDb = pObj->FSubLists.Find(sId);
            if (pDb)
            {
                String sErr = FrmLang->lbIdErr->Caption + "[" + sId + "]";
                Application->MessageBox(sErr.c_str(),//("[" + sId + "]는 사용할 수 없는 검지기 번호 입니다.").c_str(),
                                        FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
                return;
            }
        }
    }
    __finally
    {
        FRAMEAviSectList1->MyItsAviManager->FLists.UnLock();
    }
#endif

    EdId->Text   = "";
    EdAVI_SECT_NMBR->Text = sId;

    ChangeEditMode(2);
    FNewDb = true;
}
//---------------------------------------------------------------------------

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

    String sId = EdAVI_SECT_NMBR->Text;

    if (sId.IsEmpty())
    {
        Application->MessageBox(L"삭제할 AVI-구간 정보를 목록에서 선택하세요.\r\n목록에서 더블클릭하여 선택합니다.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }
    String sMsg;
    sMsg = "AVI-구간 번호: " + sId;
    sMsg+= "\r\n선택하신 AVI-구간 정보를 삭제하시겠습니까?\r\n관련정보도 함께 삭제 됩니다.";

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

    TItsAviSect *pObj = NULL;
    try
    {
        FRAMEAviSectList1->MyItsAviManager->FLists.Lock();
        FOR_STL(TItsAvi*, pAvi, FRAMEAviSectList1->MyItsAviManager->FLists)
        {
            pObj = pAvi->FSubLists.Find(sId);
            if (pObj)
            {
                break;
            }
        }
    }
    __finally
    {
        FRAMEAviSectList1->MyItsAviManager->FLists.UnLock();
    }

	if (pObj)
	{
		if (FRAMEAviSectList1->MyItsAviManager->DeleteAviSect(pObj))
		{
            pObj->DEL_YN = "Y";
            UpdateList();
		}

        Application->MessageBox(L"AVI-구간 정보 삭제\r\nAVI-구간 정보를 삭제 하였습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

        FUpdate = true;

        FormInit();
        ChangeEditMode(1);
	}
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::BtnSaveClick(TObject *Sender)
{
    TItsAvi     *pAvi;
    TItsAviSect *pObj;

    String sId     = EdAVI_SECT_NMBR->Text;
    String sCtrlId = ITSUtil_GetCode(CbAVI_CTLR_MNGM_NMBR->Text);   // 제어기 관리 번호

    pAvi = FRAMEAviSectList1->MyItsAviManager->FLists.Find(sCtrlId);

    if (!pAvi                                ||
        EdAVI_SECT_NMBR->Text.IsEmpty()      ||
        EdAVI_SECT_NM->Text.Trim().IsEmpty() )
	{
        Application->MessageBox(L"★항목은 필수 입력 항목입니다.\r\n모두 입력후 진행해 주세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
	}

    if (!FNewDb)
    {
        //수정모드일때 컨트롤러가 다르다면 신규로 처리한다.
        if (EdId->Text != sCtrlId)
        {
            TItsAvi *pTmpAvi = FRAMEAviSectList1->MyItsAviManager->FLists.Find(EdId->Text);
            if (pTmpAvi)
            {
                pTmpAvi->FSubLists.Remove(sId);
            }
            FNewDb = true;
        }
    }

    if (FNewDb)
    {
        pObj = new TItsAviSect();
        pObj->ORD = pAvi->FSubLists.Size() + 1;
    }
    else
        pObj = pAvi->FSubLists.Find(sId);

    if (pObj)
    {
        pObj->AVI_CTLR_MNGM_NMBR = sCtrlId;
        pObj->AVI_SECT_NMBR  = sId;
        pObj->STRT_SPOT_NM   = EdSTRT_SPOT_NM->Text.Trim();
        pObj->END_SPOT_NM    = EdEND_SPOT_NM->Text.Trim();
        pObj->AVI_SECT_NM    = EdAVI_SECT_NM->Text.Trim();
        pObj->AVI_SECT_DSTC  = VarToStr(SeAVI_SECT_DSTC->Value);
        pObj->SHRT_PASS_HH   = VarToStr(SeSHRT_PASS_HH->Value);
        pObj->LNST_PASS_HH   = VarToStr(SeLNST_PASS_HH->Value);
        pObj->SPOT_NUM       = VarToStr(SeSPOT_NUM->Value);
        pObj->STND_DRFT_VAL  = VarToStr(SeSTND_DRFT_VAL->Value);
        pObj->MIN_SPED       = VarToStr(SeMIN_SPED->Value);
        pObj->MAX_SPED       = VarToStr(SeMAX_SPED->Value);
        pObj->SMTH_FCTR      = VarToStr(SeSMTH_FCTR->Value);
        pObj->DEL_YN         = "N";
        pObj->FRST_REGR_NMBR = "";
        pObj->FRST_RGST_DT   = "";
        pObj->LAST_CRPR_NMBR = "";
        pObj->LAST_CRCT_DT   = "";

        pObj->Completed      = true;
        pObj->MEM_DEL        = "N";

        pObj->FLinkLists.RemoveAll();

        try {
            TvLink->BeginUpdate();
            for (int ii = 0; ii < TvLink->DataController->RecordCount; ii++)
            {
                String sLinkId = TvLink->DataController->Values[ii][ColLink2->Index];

				TItsAviLink *pObjLnk = new TItsAviLink();

                pObjLnk->AVI_CTLR_MNGM_NMBR  = pObj->AVI_CTLR_MNGM_NMBR;
                pObjLnk->AVI_SECT_NMBR       = pObj->AVI_SECT_NMBR;
                pObjLnk->LINK_ID             = sLinkId;
                pObjLnk->ORD                 = ii+1;

				pObj->FLinkLists.Push(pObjLnk->ORD, pObjLnk);

                pObjLnk->Completed = true;
                pObjLnk->MEM_DEL   = "N";
            }
        }
        __finally
        {
            TvLink->EndUpdate();
        }

        bool bResult;
        bResult = FRAMEAviSectList1->MyItsAviManager->SaveAviSect(pObj);

        if (bResult)
        {
            Application->MessageBox(FrmLang->lblDbSave->Caption.c_str(),//L"AVI-구간 정보저장\r\nAVI-구간 정보를 저장 하였습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

            if (FNewDb)
            {
                pAvi->FSubLists.Push(pObj->AVI_SECT_NMBR, pObj);
            }

            FUpdate = true;

            UpdateList();
            CMM_SetGridRow(FRAMEAviSectList1->CxList, FRAMEAviSectList1->TvList, sId, FRAMEAviSectList1->Column02->Index);
#if 0
            DisplayInfo(pObj);
#else
            int nRow = FRAMEAviSectList1->TvList->DataController->FocusedRecordIndex;
            if( nRow <= -1 )
                return;

            int nMemPtr = FRAMEAviSectList1->TvList->DataController->Values[nRow][FRAMEAviSectList1->Column99->Index];
            TItsAviSect* pObj = (TItsAviSect*)nMemPtr;
            DisplayInfo(pObj);
#endif
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::RefreshLink()
{
    if (!MapStart) return;

    TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "ClearSelLink", "0");

    try
    {
        int nRow;
        TcxDataController *pGDC = TvLink->DataController;
        TvLink->BeginUpdate();
        try
        {
            for (int ii = 0; ii < pGDC->RecordCount; ii++)
            {
                String sLinkId = pGDC->Values[ii][ColLink2->Index];
                TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "AddSelLink", sLinkId);
            }
        }
        catch(...)
        {
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::BtnLinkDelClick(TObject *Sender)
{
    int nSelRows = TvLink->Controller->SelectedRowCount;
    if (nSelRows <= 0)
    {
        Application->MessageBox(L"삭제를 원하는 링크구간을 목록에서 선택하세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }
    TvLink->Controller->DeleteSelection();
    RefreshLink();
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::BtnLinkAddClick(TObject *Sender)
{
    if (EdLinkId->Text.IsEmpty()) return;

    String sNewId = EdLinkId->Text;
    bool bFound = false;
    try
    {
        int nRow;
        TcxDataController *pGDC = TvLink->DataController;
        TvLink->BeginUpdate();

        for (int ii = 0; ii < pGDC->RecordCount; ii++)
        {
            String sLinkId = pGDC->Values[ii][ColLink2->Index];
            if (sNewId == sLinkId)
            {
                bFound = true;
                break;
            }
        }
        if (bFound)
        {
            Application->MessageBox(L"이미 등록되어 있는 링크구간 입니다.\r\n링크 구간은 중복해서 등록 할 수 없습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }

        TItsLink *pLink = ItsLinkManager->FLists.Find(sNewId);
        if (pLink)
        {
            nRow = pGDC->AppendRecord();
            pGDC->Values[nRow][ColLink2->Index] = sNewId;

            TItsNode *pNode;
            pGDC->Values[nRow][ColLink3->Index] = pLink->F_NODEID;
            pGDC->Values[nRow][ColLink5->Index] = pLink->T_NODEID;
            pNode = ItsNodeManager->FLists.Find(pLink->F_NODEID);
            if (pNode)
            {
                pGDC->Values[nRow][ColLink4->Index] = pNode->NODE_NAME;
            }
            pNode = ItsNodeManager->FLists.Find(pLink->T_NODEID);
            if (pNode)
            {
                pGDC->Values[nRow][ColLink6->Index] = pNode->NODE_NAME;
            }
        }
        else
        {
            Application->MessageBox(L"데이터베이스에 등록되어 있지 않는 링크구간 입니다.\r\n데이터베이스의 정보를 확인하세요.",
                                    FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }

    RefreshLink();
}
//---------------------------------------------------------------------------
void __fastcall TAVIM020M::TvLinkCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
    if (!MapStart) return;

	if (!ACellViewInfo) return;
	int nRow = TvLink->DataController->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

    String sLinkId = TvLink->DataController->Values[nRow][ColLink2->Index];
    TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "FindLink", sLinkId);
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::FRAMEAviSectList1TvListCellDblClickSect(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
	if (!ACellViewInfo) return;
	int nRow = FRAMEAviSectList1->TvList->DataController->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

    int nMemPtr = FRAMEAviSectList1->TvList->DataController->Values[nRow][FRAMEAviSectList1->Column99->Index];
    TItsAviSect* pObj = (TItsAviSect*)nMemPtr;
    DisplayInfo(pObj);
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::FRAMEAviList1TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
	if (!ACellViewInfo) return;
	int nRow = FRAMEAviList1->TvList->DataController->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

    int nMemPtr = FRAMEAviList1->TvList->DataController->Values[nRow][FRAMEAviList1->Column99->Index];
    TItsAvi* pObj = (TItsAvi*)nMemPtr;
    try {
    FRAMEGisMap1->SelectObjectByPos(StrToFloatDef(pObj->X_CRDN, 0), StrToFloatDef(pObj->Y_CRDN, 0), true);
    } catch(...) { }
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::ChkGisZoomClick(TObject *Sender)
{
    PnlInfo->Visible = !ChkGisZoom->Checked;
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::ShockwaveFlash1FlashCall1(TObject *ASender, const WideString request)
{
    try
    {
        String sTemp;
        String sFunc;
        String sArgs;

        sTemp = request;

        int nFuncStrIdx = sTemp.Pos("\"");
        int nFuncEndIdx = sTemp.Pos("\" returntype");
        int nArgsStrIdx = sTemp.Pos("string>");
        int nArgsEndIdx = sTemp.Pos("</string");

        sFunc = sTemp.SubString(nFuncStrIdx + 1, nFuncEndIdx - (nFuncStrIdx + 1));
        sArgs = sTemp.SubString(nArgsStrIdx + 7, nArgsEndIdx - (nArgsStrIdx + 7));

        if (sFunc == "OnMapStart")
        {
            MapStart = true;

            String sShowIcon = ChkViewIcon->Checked ? "1" : "0";
            String sShowName = ChkViewName->Checked ? "1" : "0";
            TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "ShowFacility", "RSE|" + sShowIcon + "|" + sShowName);
            TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "MultiSelect",  "1");
        }
        else
        if (sFunc == "OnDrawTrafLink")
        {
            static bool bFirst = true;
            if (bFirst)
            {
                UpdateAviIcon();
                bFirst = true;
            }
        }
        else
        if (sFunc == "OnMousePos")
        {
            if (FRAMEGisMap1->ModeSelect)
            {
                TStringList *pStringList = new TStringList;
                pStringList->CommaText = sArgs;
                String sMapX = pStringList->Strings[0]; //lat
                String sMapY = pStringList->Strings[1]; //lng
                delete pStringList;
                try {
                    //SePosX->Value = sMapX.ToDouble();
                    //SePosY->Value = sMapY.ToDouble();
                } catch(...) {}
            }
        }
        else
        if (sFunc == "OnSelLink")
        {
            if (FRAMEGisMap1->ModeSelect)// && EdRseSectName->Properties->ReadOnly == false)
            {
                TStringList *pStringList = new TStringList;
                pStringList->CommaText = sArgs;

                String sLevl = pStringList->Strings[0]; //링크레벨
                String sId   = pStringList->Strings[1]; //링크아이디
                String sZoom = pStringList->Strings[2]; //줌레벨
                String sPosX = pStringList->Strings[3]; //x
                String sPosY = pStringList->Strings[4]; //y
                String sMapX = pStringList->Strings[5]; //lat
                String sMapY = pStringList->Strings[6]; //lng

                delete pStringList;

                EdLinkId->Text = sId;
#if 0
                if (BtnLinkAdd->Enabled)
                {
                    if (Shift.Contains(ssCtrl))
                    {
                        BtnLinkAddClick((TObject*)BtnLinkAdd);
                    }
                }
#endif
            }
        }
        else
        if (sFunc == "OnClickFacility")
        {
            if (FRAMEGisMap1->ModeSelect)
            {
                TStringList *pStringList = new TStringList;
                pStringList->CommaText = sArgs;

                String sType = pStringList->Strings[0]; //유형
                String sId   = pStringList->Strings[1]; //아이디
                String sName = pStringList->Strings[2]; //아이디

                delete pStringList;

#if 0
                if (cxCheckBox1->Checked)
                {
                    EdStartSpotId->Text = sId;
                    EdStartSpotNm->Text = sName;
                }
                else
                if (cxCheckBox2->Checked)
                {
                    EdEndSpotId->Text = sId;
                    EdEndSpotNm->Text = sName;
                }
#endif
            }
        }
    }
    catch(Exception &exception)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TAVIM020M::ChkViewIconClick(TObject *Sender)
{
    if (!MapStart) return;

    String sShowIcon = ChkViewIcon->Checked ? "1" : "0";
    String sShowName = ChkViewName->Checked ? "1" : "0";
    TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "ShowFacility", "AVI|" + sShowIcon + "|" + sShowName);
}
//---------------------------------------------------------------------------

