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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "AppGlobalF.h"
#include "ITSLangTransF.h"
#include "ITSMapF.h"
#include "CDSCodeF.h"
#include "CDSFacilityF.h"
#include "CDSNodeF.h"
#include "CDSLinkF.h"
#include "CDSCctvCtlrF.h"
#include "WindowMsgF.h"
#include "ITS_OPLibF.h"
#pragma hdrstop

#include "CTVM100MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxDropDownEdit"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxSpinEdit"
#pragma link "cxSplitter"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinBlack"
#pragma link "dxSkinMcSkin"
#pragma link "FRAME_OpenMapF"
#pragma link "FRAME_CctvListF"
#pragma resource "*.dfm"

TCTVM100M *CTVM100M = NULL;
//---------------------------------------------------------------------------
void TCTVM100M_MapCallback(TObject *ASender, const String AFunc, const String AArgs, const bool AFirst)
{
    try
    {
        if (!CTVM100M) return;
        CTVM100M->OpenMapCallback(ASender, AFunc, AArgs, AFirst);
    }
    catch(Exception &exception)
    {
    }
}
//---------------------------------------------------------------------------

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

    FUpdate= false;
    FTitle = Caption;//"CCTV 기본정보 관리";

    FRAMECctvList1->PnlTop->Visible = false;
    FRAMECctvList1->PnlBottom->Visible = false;
    FRAMECctvList1->ColumnSel->Visible = false;
    FRAMECctvList1->Column13->Options->Editing = false;

    m_pGDC = FRAMECctvList1->TvList->DataController;

    FLayerIdx = LyrTp_Cctv;
    FRAMEOpenMap1->LayerIdx = FLayerIdx;
    FRAMEOpenMap1->MapCallbackFunc = TCTVM100M_MapCallback;

    EditMode = false;
}
//---------------------------------------------------------------------------

void __fastcall TCTVM100M::CommClose()
{
    try
    {
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TCTVM100M::FormShow(TObject *Sender)
{
    Refresh();

    FRAMEOpenMap1->OpenMap();

    APP_FillCode(CbType, "CTVT");
    APP_FillCode(CbLc, "ISTLT");

    CctvCtlrManager->FLists.Lock();
    try
    {
        FOR_STL(TCctvCtlr*, pObj, CctvCtlrManager->FLists)
        {
            if (pObj->DEL_YN == "Y") continue;
            pObj->FData3 = NULL;
        }
    }
    __finally
    {
        CctvCtlrManager->FLists.UnLock();
    }

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

void __fastcall TCTVM100M::FormInit()
{
    if (FRAMEOpenMap1->MapStart)
    {
        try
        {
            String sLinkId = Ed_LINK_ID->Text;

            if (sLinkId != "")
            {
                sJs.printf(L"updateEditLink('%s', 0)", sLinkId.c_str());
                FRAMEOpenMap1->ExecScript(sJs);
            }
        }
        catch(...)
        {
        }
    }
	EdNmbr->Text = "";
    EdCtlrId->Text = "";
	EdCtlrNm->Text = "";
    ISTL_LCTN_NM->Text = "";
    ISTL_LCTN_ADDR->Text = "";

    SePosX->Value = 0;
    SePosY->Value = 0;

    EdCtlrIp->Text = "";
    EdCmncPort->Text = "0";
    Se_CCTV_CTLR_PORT->Value = 0;

    Se_TYPE->Value = 1;

    Ed_CCTV_ENCD_IP->Text = ""; //인코드 IP
    Ed_CCTV_CAPT_IP->Text = ""; //자막기 IP

    Ed_STRM_SESN_NM->Text   = "";

    Ed_STRM_RTSP_ADDR->Text = "";
    Ed_STRM_RTMP_ADDR->Text = "";
	Ed_STRM_HTTP_ADDR->Text = "";
    Ed_INTR_RTMP_ADDR->Text = "";

    Ed_LINK_ID->Text        = "";
    EdTmpLinkId->Text       = "";

    EdCmraId->Text   = "";
    EdCmraPswd->Text = "";

    CbType->ItemIndex = 0;
    CbLc->ItemIndex = 0;

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

void __fastcall TCTVM100M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    FRAMECctvList1->UpdateList();
}
//---------------------------------------------------------------------------

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

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

void __fastcall TCTVM100M::FormCloseQuery(TObject *Sender, bool &CanClose)
{
    if (FUpdate)
    {
        POST_MSG(Application->MainForm->Handle, WM_DATABASE_REFRESH, WP_MSG_14, 0);
    }

    FRAMECctvList1->OnCloseQuery(CanClose);
    FRAMEOpenMap1->OnCloseQuery(CanClose);
}
//---------------------------------------------------------------------------

void __fastcall TCTVM100M::DisplayInfo(TCctvCtlr* AObj, bool AMoveObj/*=true*/)
{
    FormInit();

    if (!AObj) return;

    if (AMoveObj)
    {
        sJs.printf(L"selectLayerObject(%d, '%s', %d)", FLayerIdx, AObj->CCTV_CTLR_NMBR.c_str(), FRAMEOpenMap1->MapZoom);
        FRAMEOpenMap1->ExecScript(sJs);
    }

	EdNmbr->Text  = String(AObj->CCTV_CTLR_NMBR);
    EdCtlrId->Text = AObj->CCTV_CTLR_ID;
	EdCtlrNm->Text = AObj->CCTV_NM;
    ISTL_LCTN_NM->Text = AObj->ISTL_LCTN_NM;
    ISTL_LCTN_ADDR->Text = AObj->ISTL_LCTN_ADDR;

    SePosX->Value = AObj->X_CRDN;
    SePosY->Value = AObj->Y_CRDN;

    EdCtlrIp->Text = AObj->CCTV_CTLR_IP;
    //EdCmncPort->Text = AObj->CCTV_CTLR_PORT;
    Se_CCTV_CTLR_PORT->Value = AObj->CCTV_CTLR_PORT;

    //Se_TYPE->Value = AObj->CCTV_TYPE.ToIntDef(1);
    APP_SetCode(CbType, AObj->CCTV_TYPE);
    APP_SetCode(CbLc, AObj->CCTV_AREA_DVSN);

    Ed_CCTV_ENCD_IP->Text = AObj->CCTV_ENCD_IP; //인코드 IP
    Ed_CCTV_CAPT_IP->Text = AObj->CCTV_CAPT_IP; //자막기 IP

    Ed_STRM_SESN_NM->Text   = AObj->STRM_SESN_NM;

    Ed_STRM_RTSP_ADDR->Text = AObj->STRM_RTSP_ADDR;
    Ed_STRM_RTMP_ADDR->Text = AObj->STRM_RTMP_ADDR;
	Ed_STRM_HTTP_ADDR->Text = AObj->STRM_HTTP_ADDR;
    Ed_INTR_RTMP_ADDR->Text = AObj->STRM_STOR_ADDR; //제어기 URL

    Ed_LINK_ID->Text        = AObj->LINK_ID;
    EdCmraId->Text   = AObj->CMRA_ID;
    EdCmraPswd->Text = AObj->CMRA_PSWD;
    EdMacAddr->Text = AObj->MAC_ADDR;
    EdSerialNo->Text = AObj->SERIAL_NO;
    EdMacAddr->Text = AObj->MAC_ADDR;
    if (AObj->TTA_CNFN_YN == "Y") {
        CbTtaYn->ItemIndex = 0;
    }
    else {
        CbTtaYn->ItemIndex = 1;
    }


//    FRAMEGisMap1->SelectObjectByPos(SePosX->Value, SePosY->Value, true);

    ChangeEditMode(3);

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

void __fastcall TCTVM100M::DisplayLink(TCctvCtlr* AObj)
{
    if (!AObj) return;

    String sLinkId = AObj->LINK_ID;

    if (sLinkId != "")
    {
        sJs.printf(L"updateEditLink('%s', %d)", sLinkId.c_str(), 3);
        FRAMEOpenMap1->ExecScript(sJs);
    }
}
//---------------------------------------------------------------------------

void __fastcall TCTVM100M::UpdateIcon()
{
    if (!FRAMEOpenMap1->MapStart) return;

    sJs.printf(L"removeLayerObject(%d)", FLayerIdx);
    FRAMEOpenMap1->ExecScript(sJs);

    String arrObj = "";
    String sTemp;

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

            if (arrObj != "") arrObj += "|";

            sTemp.printf(L"%s,%s,%s,%.6f,%.6f,0,0,0,0", pObj->CCTV_CTLR_NMBR.c_str(), pObj->CCTV_CTLR_ID.c_str(), pObj->CCTV_NM.c_str(), pObj->X_CRDN, pObj->Y_CRDN);
            arrObj += sTemp;
        }
    }
    __finally
    {
        CctvCtlrManager->FLists.UnLock();
        if (arrObj != "")
        {
            sJs.printf(L"addFacility(%d, '%s')", FLayerIdx, arrObj.c_str());
            FRAMEOpenMap1->ExecScript(sJs);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TCTVM100M::BtnEditClick(TObject *Sender)
{
    String sId = EdNmbr->Text;
    TCctvCtlr *pObj = CctvCtlrManager->FLists.Find(sId);

    FNewDb = false;

    if (BtnEdit->Caption == FrmLang->lblEdit->Caption)//"편집")
    {
        //편집모드 시작
        ChangeEditMode(0);
        if (pObj)
        {
            //시설물아이콘을 편집아이콘으로 변경
            //function editFacilityObject(ALyrIdx, ANmbr, AComm(0:오류-편집,1:정상), AEdit(편집모드:1-편집,0-편집취소))
            sJs.printf(L"editFacilityObject(%d, %s, 1, 1)", FLayerIdx, pObj->CCTV_CTLR_NMBR.c_str());
            FRAMEOpenMap1->ExecScript(sJs);
        }
    }
    else
    {
        //편집모드 취소
        ChangeEditMode(1);
        if (pObj)
        {
            //원래 정보를 화면에 표출
            CMM_SetGridRow(FRAMECctvList1->CxList, FRAMECctvList1->TvList, sId, FRAMECctvList1->Column01->Index);
            DisplayInfo(pObj, false);

#if 0
            //지도시설물아이콘을 원래아이콘으로 변경
            sJs.printf(L"editFacilityObject(%d, %s, 0, 0)", FLayerIdx, pObj->CCTV_CTLR_NMBR.c_str());
            FRAMEOpenMap1->ExecScript(sJs);
#else
            //추가 모드에서 취소하는 경우 지도에 신규로 추가한 객체를 찾아서 삭제해 주는것 보다
            //새롭게 다시 그리자
            UpdateIcon();
#endif
        }
        else
        {
            //객체가 없는 경우는 추가 모드에서 취소한 경우임
            sJs.printf(L"removeFacilityObject(%d, %s)", FLayerIdx, sId.c_str());
            FRAMEOpenMap1->ExecScript(sJs);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TCTVM100M::ChangeEditMode(int AEdit)
{
    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;//데이터 편집 취소";
            EditMode = true;
        }
        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;
            EditMode = 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;//데이터 편집 취소";
            EditMode = true;
        }
        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;
            EditMode = false;
        }
        break;
    }

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

    PnlEdit->Enabled = EditMode;
}
//---------------------------------------------------------------------------


void __fastcall TCTVM100M::BtnInsertClick(TObject *Sender)
{
    FormInit();
    //신규아이디를 얻어와서 화면을 초기화한다.

    int nNmbr, nId;
    if (!CctvCtlrManager->GetNextCctvId(nNmbr, nId))
    {
        Application->MessageBox(lblText1->Caption.c_str(),//L"새로운 CCTV 정보를 생성하지 못하였습니다.\r\n프로그램 종료후에 다시 시도해 보세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }

    EdNmbr->Text = String(nNmbr);
    SePosX->Value = FRAMEOpenMap1->CenterX;
    SePosY->Value = FRAMEOpenMap1->CenterY;

    //지도에 새로운 시설물 아이콘 추가하면서 편집모드로 변경
    String arrObj = "";
    arrObj.printf(L"%d,New,New,%.6f,%.6f,1", nNmbr, FRAMEOpenMap1->CenterX, FRAMEOpenMap1->CenterY);
    sJs.printf(L"addFacilityObject(%d, '%s')", FLayerIdx, arrObj.c_str());
    FRAMEOpenMap1->ExecScript(sJs);

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

void __fastcall TCTVM100M::BtnDeleteClick(TObject *Sender)
{
    if (!FRAMEOpenMap1->MapStart) return;

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

    String sId = EdNmbr->Text;

    if (sId.IsEmpty())
    {
        Application->MessageBox(lblText2->Caption.c_str(),//L"삭제할 CCTV를 목록에서 선택하세요.\r\n목록에서 더블클릭하여 선택합니다.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }

    String sMsg;
    sMsg = lblText3->Caption + ": " + sId;//"CCTV번호: " + sId;
    sMsg+= "\r\n" + lblText4->Caption;//"선택하신 CCTV 정보를 삭제하시겠습니까?";

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

	TCctvCtlr *pObj = CctvCtlrManager->FLists.Find(sId);

	if (pObj)
	{
		if (CctvCtlrManager->DeleteCctv(pObj))
		{
            pObj->DEL_YN = "Y";
            FRAMECctvList1->UpdateList();
            UpdateIcon();
		}

        FUpdate = true;

        Application->MessageBox(lblText5->Caption.c_str(),//L"CCTV 정보삭제\r\nCCTV 정보를 삭제 하였습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

        FormInit();
        ChangeEditMode(1);

        UpdateIcon();
	}
}
//---------------------------------------------------------------------------

void __fastcall TCTVM100M::BtnSaveClick(TObject *Sender)
{
	if (EdNmbr->Text.IsEmpty()          ||
        EdCtlrId->Text.Trim().IsEmpty() ||
        EdCtlrNm->Text.Trim().IsEmpty() ||
        SePosX->Value == 0              ||
        SePosY->Value == 0              ||
        EdCtlrIp->Text.IsEmpty())
	{
        Application->MessageBox(FrmLang->lblNecessary->Caption.c_str(),//L"★항목은 필수 입력 항목입니다.\r\n모두 입력후 진행해 주세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
	}

    String sNmbr = EdNmbr->Text;
    String sCtlrId = EdCtlrId->Text.Trim();

    if (!ITSUtil_CheckIpAddress(EdCtlrIp->Text.Trim()))
    {
        Application->MessageBox(lblText6->Caption.c_str(),//L"제어기 IP가 유효하지 않습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
    }

    if (Ed_CCTV_ENCD_IP->Text.Trim() != "")
    {
        if (!ITSUtil_CheckIpAddress(Ed_CCTV_ENCD_IP->Text.Trim()))
        {
            Application->MessageBox(L"인코더 IP가 유효하지 않습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
            Ed_CCTV_ENCD_IP->SetFocus();
            return;
        }
    }
    if (Ed_CCTV_CAPT_IP->Text.Trim() != "")
    {
        if (!ITSUtil_CheckIpAddress(Ed_CCTV_CAPT_IP->Text.Trim()))
        {
            Application->MessageBox(L"자막기 IP가 유효하지 않습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
            Ed_CCTV_CAPT_IP->SetFocus();
            return;
        }
    }

    CctvCtlrManager->FLists.Lock();
    FOR_STL(TCctvCtlr*, pTmpObj, CctvCtlrManager->FLists)
    {
        if (pTmpObj->DEL_YN == "Y") continue;
        if (pTmpObj->CCTV_CTLR_NMBR == sNmbr) continue; //자기자신

        if (pTmpObj->CCTV_CTLR_ID == sCtlrId)
        {
            Application->MessageBox(lblText8->Caption.c_str(),//L"CCTV ID가 이미 사용 중입니다. CCTV ID를 확인하세요.",
                                    FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }
    }
    CctvCtlrManager->FLists.UnLock();

    TCctvCtlr *pObj;
    if (FNewDb)
        pObj = new TCctvCtlr();
    else
        pObj = CctvCtlrManager->FLists.Find(sNmbr);

    if (pObj)
    {
        pObj->CCTV_CTLR_NMBR = sNmbr;
        pObj->CCTV_CTLR_ID   = sCtlrId;
        pObj->CCTV_NM        = EdCtlrNm->Text.Trim();

        pObj->CCTV_CTLR_IP       = EdCtlrIp->Text;
        pObj->CCTV_CTLR_PORT     = Se_CCTV_CTLR_PORT->Value; //EdCmncPort->Text.ToIntDef(0);
        pObj->CCTV_CTLR_LOCAL_NO = 0;

        pObj->CCTV_TYPE = APP_GetCode(CbType);
        pObj->ISTL_LCTN_NM = ISTL_LCTN_NM->Text.Trim();
        pObj->ISTL_LCTN_ADDR = ISTL_LCTN_ADDR->Text.Trim();

        pObj->X_CRDN = SePosX->Value;
        pObj->Y_CRDN = SePosY->Value;

        pObj->CCTV_TYPE = APP_GetCode(CbType);
        pObj->CCTV_AREA_DVSN = APP_GetCode(CbLc);

        pObj->CCTV_ENCD_IP = Ed_CCTV_ENCD_IP->Text; //인코드 IP
        pObj->CCTV_CAPT_IP = Ed_CCTV_CAPT_IP->Text; //자막기 IP

        pObj->STRM_SESN_NM  = Ed_STRM_SESN_NM->Text;

        pObj->STRM_RTSP_ADDR = Ed_STRM_RTSP_ADDR->Text;
        pObj->STRM_RTMP_ADDR = Ed_STRM_RTMP_ADDR->Text;
        pObj->STRM_HTTP_ADDR = Ed_STRM_HTTP_ADDR->Text;
        pObj->STRM_STOR_ADDR = Ed_INTR_RTMP_ADDR->Text;
        pObj->LINK_ID        = Ed_LINK_ID->Text;

        pObj->CMRA_ID = EdCmraId->Text.Trim();
        pObj->CMRA_PSWD = EdCmraPswd->Text.Trim();

        pObj->FULL_STRM_ADDR = pObj->STRM_SESN_NM;

        pObj->MAC_ADDR = EdMacAddr->Text.Trim();
        pObj->SERIAL_NO = EdSerialNo->Text.Trim();
        pObj->TTA_CNFN_YN = (CbTtaYn->ItemIndex == 0) ? "Y" : "N";

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


        bool bResult;
        if (FNewDb)
        {
            pObj->DEL_YN = "N";
            bResult = CctvCtlrManager->InsertCctv(pObj);
            if (bResult)
            {
                CctvCtlrManager->FLists.Push(sNmbr, pObj);
            }
		}
        else
		{
            bResult = CctvCtlrManager->UpdateCctv(pObj);
		}

        if (bResult)
        {
            Application->MessageBox(lblText9->Caption.c_str(),//L"CCTV 정보저장\r\nCCTV 정보를 저장 하였습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

            FUpdate = true;

            FRAMECctvList1->UpdateList();
            UpdateIcon();
            CMM_SetGridRow(FRAMECctvList1->CxList, FRAMECctvList1->TvList, sNmbr, FRAMECctvList1->Column01->Index);
            DisplayInfo(pObj, true);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TCTVM100M::Ed_CMRA_PORTKeyPress(TObject *Sender, wchar_t &Key)
{
    if (Key > '9' || Key < '0')
    {
        if (Key != VK_BACK && Key != VK_TAB && Key != '-')
            Key = 0;
    }
}
//---------------------------------------------------------------------------

void __fastcall TCTVM100M::Se_CMRA_CHNLPropertiesChange(TObject *Sender)
{
    if (((TcxSpinEdit*)Sender)->Value < ((TcxSpinEdit*)Sender)->Properties->MinValue)
        ((TcxSpinEdit*)Sender)->Value = ((TcxSpinEdit*)Sender)->Properties->MinValue;
    else if(((TcxSpinEdit*)Sender)->Value > ((TcxSpinEdit*)Sender)->Properties->MaxValue &&
        ((TcxSpinEdit*)Sender)->Properties->MaxValue)
        ((TcxSpinEdit*)Sender)->Value = ((TcxSpinEdit*)Sender)->Properties->MaxValue;
}
//---------------------------------------------------------------------------
void __fastcall TCTVM100M::UpdateEditLink()
{
    if (!FRAMEOpenMap1->MapStart) return;

    TItsNode* pNode;
    String    STRT_NM;
    String    END_NM;

    try
    {
        ItsLinkManager->FLists.Lock();
        try
        {
            FOR_STL(TItsLink*, pLink, ItsLinkManager->FLists)
            {
                if (pLink->DEL_YN == "Y")   continue;

#if 0
                pVrtx = pLink->Vrtxs.Find(nZoom);
                if (!pVrtx) continue;

                pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
                if (pNode) STRT_NM = pNode->NODE_NAME; else STRT_NM = " ";
                pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
                if (pNode) END_NM = pNode->NODE_NAME; else END_NM = " ";

                sJs.printf(L"addEditLink('%s', '%s', '%s', '%s', '%s', '%s')",
                    pLink->LINK_ID.c_str(),
                    pLink->ROAD_NAME.c_str(),
                    STRT_NM.c_str(),
                    END_NM.c_str(),
                    pVrtx->X_CRDN_ARR.c_str(),
                    pVrtx->Y_CRDN_ARR.c_str());

                FRAMEOpenMap1->ExecScript(sJs);
#else
                if (pLink->IsVertex)
                {
                    pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
                    if (pNode) STRT_NM = pNode->NODE_NAME; else STRT_NM = " ";
                    pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
                    if (pNode) END_NM = pNode->NODE_NAME; else END_NM = " ";

                    sJs.printf(L"addEditLink('%s', '%s', '%s', '%s', '%s', '%s')",
                        pLink->LINK_ID.c_str(),
                        pLink->ROAD_NAME.c_str(),
                        STRT_NM.c_str(),
                        END_NM.c_str(),
                        pLink->X_CRDN_ARR.c_str(),
                        pLink->Y_CRDN_ARR.c_str());

                    FRAMEOpenMap1->ExecScript(sJs);
                }
#endif
            }
        }
        __finally
        {
            ItsLinkManager->FLists.UnLock();
        }
        FRAMEOpenMap1->ExecScript("makeEditLinkLayer()");
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TCTVM100M::OpenMapCallback(TObject *ASender, const String AFunc, const String AArgs, const bool AFirst)
{
    try
    {
        if (AFirst)
        {
            //지도를 편집모드로 변경
            sJs.printf(L"setEditMode(%d, 1)", FLayerIdx);
            FRAMEOpenMap1->ExecScript(sJs);

            //편집레이어 생성
            FRAMEOpenMap1->ExecScript("initEditLayer()");

            //배경링크레이어 생성
            UpdateEditLink();

            //시설물아이콘 등록
            UpdateIcon();
            sJs.printf(L"extentLayer(%d)", FLayerIdx);
            FRAMEOpenMap1->ExecScript(sJs);
            return;
        }


        if (AFunc.Pos("onlinkclick"))
        {
            TStringList *pStringList = new TStringList;
            pStringList->CommaText = AArgs;

            try
            {
                String sLyrType = pStringList->Strings[0];
                String sLinkId  = pStringList->Strings[1];
                if (EditMode)
                {
                    EdTmpLinkId->Text = sLinkId;
                }
            }
            catch(Exception &e)
            {
            }
            delete pStringList;
        }
        if (AFunc.Pos("onfacilitydragend"))
        {
            TStringList *pStringList = new TStringList;
            pStringList->CommaText = AArgs;

            try
            {
                String sNmbr      = pStringList->Strings[0];
                String sCenterLat = pStringList->Strings[1];
                String sCenterLng = pStringList->Strings[2];
                SePosX->Value = sCenterLat.ToDouble();
                SePosY->Value = sCenterLng.ToDouble();
            }
            catch(Exception &e)
            {
            }
            delete pStringList;
        }
    }
    catch(Exception &exception)
    {
    }
}
//---------------------------------------------------------------------------

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

    int nMemPtr = FRAMECctvList1->TvList->DataController->Values[nRow][FRAMECctvList1->Column99->Index];
    TCctvCtlr* pObj = (TCctvCtlr*)nMemPtr;
    DisplayInfo(pObj, true);
}
//---------------------------------------------------------------------------

void __fastcall TCTVM100M::BtnApplyClick(TObject *Sender)
{
    if (EditMode)
    {
        Ed_LINK_ID->Text = EdTmpLinkId->Text.Trim();
    }
}
//---------------------------------------------------------------------------

