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

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

#include "PIS0010MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#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 "cxTimeEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "FRAME_ParkingListF"
#pragma link "FRAME_OpenMapF"
#pragma resource "*.dfm"

TPIS0010M *PIS0010M = NULL;
//---------------------------------------------------------------------------

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

__fastcall TPIS0010M::TPIS0010M(TComponent* Owner)
    : TForm(Owner)
{
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FUpdate= false;
    FTitle = "주차장 관리";

    MapStart = false;

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

    FRAMEParkingList1->Column02->Visible = true;
    FRAMEParkingList1->Column03->Visible = true;
    //FRAMEParkingList1->Column05->Visible = true;
    FRAMEParkingList1->Column06->Visible = true;
    //FRAMEParkingList1->Column07->Visible = true;
    //FRAMEParkingList1->Column08->Visible = true;
    //FRAMEParkingList1->Column09->Visible = true;
    FRAMEParkingList1->Column10->Visible = true;
    FRAMEParkingList1->Column13->Visible = true;

    m_pGDC = FRAMEParkingList1->TvList->DataController;
    TvLink->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<주차장 층정보>";

    FLayerIdx = LyrTp_Park;
    FRAMEOpenMap1->LayerIdx = FLayerIdx;
    FRAMEOpenMap1->MapCallbackFunc = TPIS0010M_MapCallback;

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

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

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

    FRAMEOpenMap1->OpenMap();

    TcxComboBox *pCombo = CbPRLT_OPER_DVSN_CD;
    pCombo->Properties->Items->Clear();
    TItsCode *pCode = ItsCodeManager->FLists.Find("POT");
    if (pCode)
    {
		FOR_STL(TItsSubCode *, pSubCode, pCode->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			pCombo->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
    }
	pCombo->ItemIndex = 0;

    pCombo = CbPRLT_TYPE_DVSN_CD;
    pCombo->Properties->Items->Clear();
    pCode = ItsCodeManager->FLists.Find("PFT");
    if (pCode)
    {
		FOR_STL(TItsSubCode *, pSubCode, pCode->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			pCombo->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
    }
	pCombo->ItemIndex = 0;

    pCombo = PARK_NDSY_CD;
    pCombo->Properties->Items->Clear();
    pCode = ItsCodeManager->FLists.Find("PND");
    if (pCode)
    {
		FOR_STL(TItsSubCode *, pSubCode, pCode->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			pCombo->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
    }
	pCombo->ItemIndex = 0;

    pCombo = PARK_OPER_DAY_CD;
    pCombo->Properties->Items->Clear();
    pCode = ItsCodeManager->FLists.Find("POD");
    if (pCode)
    {
		FOR_STL(TItsSubCode *, pSubCode, pCode->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			pCombo->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
    }
	pCombo->ItemIndex = 0;

    pCombo = PARK_OPDR_CD;
    pCombo->Properties->Items->Clear();
    pCode = ItsCodeManager->FLists.Find("POP");
    if (pCode)
    {
		FOR_STL(TItsSubCode *, pSubCode, pCode->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			pCombo->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
    }
	pCombo->ItemIndex = 0;

    pCombo = PARK_FARE_IMPS_CD;
    pCombo->Properties->Items->Clear();
    pCode = ItsCodeManager->FLists.Find("PFS");
    if (pCode)
    {
		FOR_STL(TItsSubCode *, pSubCode, pCode->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			pCombo->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
    }
	pCombo->ItemIndex = 0;

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

void __fastcall TPIS0010M::FormInit()
{
	EdNmbr->Text = "";
    EdPRLT_CTLR_ID->Text   = "";
    EdPRLT_NM->Text        = "";

    EdPRLT_CTLR_IP->Text   = "";
    EdPRLT_CTLR_PORT->Text   = "";
    //EdPRLT_CTLR_LOCAL_NO->Text   = "";
    CbPRLT_OPER_DVSN_CD->ItemIndex = 0;
    CbPRLT_TYPE_DVSN_CD->ItemIndex = 0;

    EdISTL_LCTN_NM->Text   = "";
    EdPRLT_TEL->Text       = "";
    EdISTL_LCTN_ADDR->Text = "";

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

    SeCTLR_CLCT_CYCL->Value = 60;
    SeCTLR_STTS_CYCL->Value = 60;
    SeWHOL_NPLS->Value      = 0;
    SeWHOL_NPLS->Value      = 0;
    SeGNRL_NPLS->Value      = 0;
    SeLGVH_NPLS->Value      = 0;
    SeHVVH_NPLS->Value      = 0;
    SeEMVH_NPLS->Value      = 0;
    SeHNDC_NPLS->Value      = 0;
    SeWMON_NPLS->Value      = 0;
    SeELVH_NPLS->Value      = 0;
    SeETC_NPLS->Value       = 0;


    CbREAL_DATA_YN->ItemIndex = 1;
    EdPRLT_CTLR_IP->Text   = "";
    EdPRLT_CTLR_PORT->Text = "";
    SeCNGT_NPLS->Value     = 10;

    PRLT_MNGM_INTN_NM->Text  = "";
    PRLT_MNGM_INTN_TEL->Text = "";

    MoPRLT_CHARGE_DESC->Lines->Clear();
    MoPRLT_OPER_DESC->Lines->Clear();
    MoPRLT_ETC_DESC->Lines->Clear();

    VALD_YN->ItemIndex           = 0;

    PARK_NDSY_CD->ItemIndex      = 0;
    PARK_OPER_DAY_CD->ItemIndex  = 0;
    PARK_OPDR_CD->ItemIndex      = 0;
    PARK_FARE_IMPS_CD->ItemIndex = 0;
    PARK_APNT_OFFR_YN->ItemIndex = 0;

	PARK_WKDY_STRT_HH->Clear();
    PARK_WKDY_STRT_HH->EditValue = Now().FormatString("hh:nn");
    PARK_WKDY_STRT_HH->EditValue = "00:00";

	PARK_WKDY_END_HH->Clear();
    PARK_WKDY_END_HH->EditValue = Now().FormatString("hh:nn");
    PARK_WKDY_END_HH->EditValue = "00:00";

	PARK_LHDY_STRT_HH->Clear();
    PARK_LHDY_STRT_HH->EditValue = Now().FormatString("hh:nn");
    PARK_LHDY_STRT_HH->EditValue = "00:00";

	PARK_LHDY_END_HH->Clear();
    PARK_LHDY_END_HH->EditValue = Now().FormatString("hh:nn");
    PARK_LHDY_END_HH->EditValue = "00:00";

    CMM_ClearGridTableView(TvLink);

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

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

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

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

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

    FRAMEParkingList1->OnCloseQuery(CanClose);
}
//---------------------------------------------------------------------------

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

    if (!AObj) return;

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

    EdNmbr->Text            = AObj->PRLT_CTLR_NMBR;
    EdPRLT_CTLR_ID->Text    = AObj->PRLT_CTLR_ID;
    EdPRLT_NM->Text         = AObj->PRLT_NM;
    EdISTL_LCTN_NM->Text    = AObj->ISTL_LCTN_NM;
    EdISTL_LCTN_ADDR->Text  = AObj->ISTL_LCTN_ADDR;
    EdPRLT_TEL->Text        = AObj->PRLT_TEL;
    EdPRLT_CTLR_IP->Text    = AObj->PRLT_CTLR_IP;
    SeCNGT_NPLS->Value      = AObj->CNGT_NPLS;
    EdPRLT_CTLR_PORT->Text  = String(AObj->PRLT_CTLR_PORT);
    //PRLT_MNGM_INTN_NM->Text  = AObj->PRLT_MNGM_INTN_NM;
    //PRLT_MNGM_INTN_TEL->Text = AObj->PRLT_MNGM_INTN_TEL;

    SePosX->Value         = StrToFloatDef(AObj->X_CRDN, 0);
    SePosY->Value         = StrToFloatDef(AObj->Y_CRDN, 0);

    SeCTLR_CLCT_CYCL->Value = AObj->CTLR_CLCT_CYCL;
    SeCTLR_STTS_CYCL->Value = AObj->CTLR_STTS_CYCL;
    SeWHOL_NPLS->Value      = AObj->WHOL_NPLS;
    SeGNRL_NPLS->Value      = AObj->GNRL_NPLS;
    SeLGVH_NPLS->Value      = AObj->LGVH_NPLS;
    SeHVVH_NPLS->Value      = AObj->HVVH_NPLS;
    SeEMVH_NPLS->Value      = AObj->EMVH_NPLS;
    SeHNDC_NPLS->Value      = AObj->HNDC_NPLS;
    SeWMON_NPLS->Value      = AObj->WMON_NPLS;
    SeELVH_NPLS->Value      = AObj->ELVH_NPLS;
    SeETC_NPLS->Value       = AObj->ETC_NPLS;

    CMM_SetComboBoxItem(CbPRLT_OPER_DVSN_CD, AObj->PRLT_OPER_DVSN_CD);
    CMM_SetComboBoxItem(CbPRLT_TYPE_DVSN_CD, AObj->PRLT_TYPE_DVSN_CD);
    //CMM_SetComboBoxItem(VALD_YN,           AObj->VALD_YN);
    CMM_SetComboBoxItem(CbREAL_DATA_YN,      AObj->REAL_DATA_YN);
    //CMM_SetComboBoxItem(PARK_NDSY_CD,      AObj->PARK_NDSY_CD);
    //CMM_SetComboBoxItem(PARK_OPER_DAY_CD,  AObj->PARK_OPER_DAY_CD);
    //CMM_SetComboBoxItem(PARK_OPDR_CD,      AObj->PARK_OPDR_CD);
    //CMM_SetComboBoxItem(PARK_FARE_IMPS_CD, AObj->PARK_FARE_IMPS_CD);
    //CMM_SetComboBoxItem(PARK_APNT_OFFR_YN, AObj->PARK_APNT_OFFR_YN);

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

    MoPRLT_CHARGE_DESC->Lines->Text = AObj->PRLT_CHARGE_DESC;
    MoPRLT_OPER_DESC->Lines->Text   = AObj->PRLT_OPER_DESC;
    MoPRLT_ETC_DESC->Lines->Text    = AObj->PRLT_ETC_DESC;

    ChangeEditMode(3);
}
//---------------------------------------------------------------------------

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

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

    String arrObj = "";
    String sTemp;

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

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

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

void __fastcall TPIS0010M::BtnEditClick(TObject *Sender)
{
    String sId = EdNmbr->Text;
    TPrltCtlr *pObj = PrltCtlrManager->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->PRLT_CTLR_NMBR.c_str());
            FRAMEOpenMap1->ExecScript(sJs);
        }
    }
    else
    {
        //편집모드 취소
        ChangeEditMode(1);

        if (pObj)
        {
            CMM_SetGridRow(FRAMEParkingList1->CxList, FRAMEParkingList1->TvList, sId, FRAMEParkingList1->Column01->Index);
            DisplayInfo(pObj, false);

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

void __fastcall TPIS0010M::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;//데이터 편집 취소";
        }
        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;
        }
        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)
    {
        FRAMEParkingList1->CxList->Enabled = false;
        GrpInfo->Enabled = true;
    }
    else
    {
        FRAMEParkingList1->CxList->Enabled = true;
        GrpInfo->Enabled = false;
    }

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

void __fastcall TPIS0010M::BtnInsertClick(TObject *Sender)
{
    FormInit();
    //신규아이디를 얻어와서 화면을 초기화한다.
    int nNmbr, nId;
    if (!PrltCtlrManager->GetNextPrltId(nNmbr, nId))
    {
        Application->MessageBox(L"새로운 주차장 정보를 생성하지 못하였습니다.\r\n프로그램 종료후에 다시 시도해 보세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }

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

    SePosX->Value = FRAMEOpenMap1->CenterX;
    SePosY->Value = FRAMEOpenMap1->CenterY;

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

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

void __fastcall TPIS0010M::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(L"삭제할 주차장정보를 목록에서 선택하세요.\r\n목록에서 더블클릭하여 선택합니다.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }
    String sMsg;
    sMsg = "ID: " + sId;
    sMsg+= "\r\n선택하신 주차장 정보를 삭제하시겠습니까?";

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

	TPrltCtlr *pObj = PrltCtlrManager->FLists.Find(sId);
	if (pObj)
	{
		if (PrltCtlrManager->DeletePrlt(pObj))
		{
            pObj->DEL_YN = "Y";
            FRAMEParkingList1->UpdateList();
            UpdateIcon();
		}

        Application->MessageBox(L"주차장 정보삭제\r\n주차장 정보를 삭제 하였습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

        FormInit();
        ChangeEditMode(1);

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

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

    String sNmbr = EdNmbr->Text;
    int    nNmbr = sNmbr.ToInt();
    String sId = EdPRLT_CTLR_ID->Text.Trim();

    if (CbREAL_DATA_YN->ItemIndex == 0 && !ITSUtil_CheckIpAddress(EdPRLT_CTLR_IP->Text.Trim()))
    {
        Application->MessageBox(L"제어기 IP가 유효하지 않습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
    }

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

        if (pTmpObj->PRLT_CTLR_ID == sId)
        {
            Application->MessageBox(L"주차장 ID가 이미 사용 중입니다. 주차장 ID를 확인하세요.",
                                    FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }
    }
    PrltCtlrManager->FLists.UnLock();

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

    if (pObj)
    {
        pObj->PRLT_CTLR_NMBR    = sNmbr;
        pObj->PRLT_CTLR_ID      = sId;
        pObj->PRLT_NM           = EdPRLT_NM->Text.Trim();
        pObj->PRLT_CTLR_IP      = EdPRLT_CTLR_IP->Text.Trim();
        pObj->CNGT_NPLS         = SeCNGT_NPLS->Value;
        pObj->PRLT_CTLR_PORT    = EdPRLT_CTLR_PORT->Text.Trim().ToIntDef(0);
        pObj->PRLT_CTLR_LOCAL_NO= 0;

        pObj->PRLT_OPER_DVSN_CD = APP_GetCode(CbPRLT_OPER_DVSN_CD);
        pObj->PRLT_TYPE_DVSN_CD = APP_GetCode(CbPRLT_TYPE_DVSN_CD);
        pObj->ISTL_LCTN_NM      = EdISTL_LCTN_NM->Text.Trim();
        //pObj->VALD_YN           = APP_GetCode(VALD_YN);
        pObj->X_CRDN            = SePosX->Value;
        pObj->Y_CRDN            = SePosY->Value;
        pObj->PRLT_TEL          = EdPRLT_TEL->Text.Trim();
        pObj->ISTL_LCTN_ADDR    = EdISTL_LCTN_ADDR->Text.Trim();
        pObj->WHOL_NPLS         = SeWHOL_NPLS->Value;
        pObj->GNRL_NPLS         = SeGNRL_NPLS->Value;
        pObj->LGVH_NPLS         = SeLGVH_NPLS->Value;
        pObj->HVVH_NPLS         = SeHVVH_NPLS->Value;
        pObj->EMVH_NPLS         = SeEMVH_NPLS->Value;
        pObj->HNDC_NPLS         = SeHNDC_NPLS->Value;
        pObj->WMON_NPLS         = SeWMON_NPLS->Value;
        pObj->ELVH_NPLS         = SeELVH_NPLS->Value;
        pObj->ETC_NPLS          = SeETC_NPLS->Value;
        pObj->REAL_DATA_YN      = APP_GetCode(CbREAL_DATA_YN);
        pObj->CTLR_CLCT_CYCL    = SeCTLR_CLCT_CYCL->Value;
        pObj->CTLR_STTS_CYCL    = SeCTLR_STTS_CYCL->Value;
        pObj->PRLT_CHARGE_DESC  = MoPRLT_CHARGE_DESC->Lines->Text.Trim();
        pObj->PRLT_OPER_DESC    = MoPRLT_OPER_DESC->Lines->Text.Trim();
        pObj->PRLT_ETC_DESC     = MoPRLT_ETC_DESC->Lines->Text.Trim();

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

        bool bResult;
        if (FNewDb)
        {
            bResult = PrltCtlrManager->InsertPrlt(pObj);
            if (bResult)
            {
                PrltCtlrManager->FLists.Push(sNmbr, pObj);
            }
		}
        else
		{
            bResult = PrltCtlrManager->UpdatePrlt(pObj);
		}

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

            FUpdate = true;

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

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

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

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

void __fastcall TPIS0010M::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);

            //시설물아이콘 등록
            UpdateIcon();

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

        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 TPIS0010M::BtnListDeleteClick(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();
}
//---------------------------------------------------------------------------

bool __fastcall TPIS0010M::FindParkingFloor(String ANmbr)
{
    try {
        TvLink->BeginUpdate();
        for (int ii = 0; ii < TvLink->DataController->RecordCount; ii++)
        {
            String sNmbr = VarToStr(TvLink->DataController->Values[ii][ColLink2->Index]);
            if (sNmbr == "") continue;
            if (sNmbr == ANmbr) return true;
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }
    return false;
}
//---------------------------------------------------------------------------

void __fastcall TPIS0010M::BtnListAddClick(TObject *Sender)
{
	String sId = InputBox("층번호", "신규 주차장 층번호를 입력하세요(최대 20Byte).", "층번호" );

    sId = sId.Trim();
	if (sId == "층번호" || sId == "")
	{
		return;
	}
	if (AnsiString(sId).Length() > 20)
	{
        Application->MessageBox(L"주차장 층번호는 20Byte를 초과할 수 없습니다.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
	}
    if (FindParkingFloor(sId))
    {
        Application->MessageBox(L"이미 등록되어 있는 층번호 입니다.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
    }

    int nRow = TvLink->DataController->AppendRecord();
    TvLink->DataController->Values[nRow][ColLink2->Index] = sId;
}
//---------------------------------------------------------------------------

