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

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

#include "PRK0010MF.h"
//#include "PRK0011MF.h"
//#include "PRK0012MF.h"
//#include "PRK0013MF.h"
//#include "PRK0014MF.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_OpenMapF"
#pragma link "dxSkinMcSkin"
#pragma link "cxCalc"
#pragma resource "*.dfm"

TPRK0010M *PRK0010M = NULL;
//---------------------------------------------------------------------------

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

__fastcall TPRK0010M::TPRK0010M(TComponent* Owner)
    : TForm(Owner)
{
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);
    Position = poOwnerFormCenter;

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

    m_pGDC = TvList->DataController;

    MapStart = false;

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

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

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

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

    FRAMEOpenMap1->OpenMap();

    TcxComboBox *pCombo = NULL;

    // 주차장 입출구 형태
    pCombo = PRK_PLCE_ENTRC_EXTTY;
    pCombo->Properties->Items->Clear();
    TItsCode *pCode = ItsCodeManager->FLists.Find("PRKEX");
    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 = PARKING_TY;
    pCombo->Properties->Items->Clear();
    pCode = ItsCodeManager->FLists.Find("PRKTY");
    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 = PARKING_US;
    pCombo->Properties->Items->Clear();
    pCode = ItsCodeManager->FLists.Find("PRKUS");
    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 = AR_LEVEL_SE;
    pCombo->Properties->Items->Clear();
    pCode = ItsCodeManager->FLists.Find("PRKSE");
    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 = NO_DRV_DAY_OPERTN_AT;
    pCombo->Properties->Items->Clear();
    pCode = ItsCodeManager->FLists.Find("PRKOP");
    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 = PARKING_CHRGE_EXCCLC_MTHD;
    pCombo->Properties->Items->Clear();
    pCode = ItsCodeManager->FLists.Find("PRKPT");
    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 = PARKING_RESVE_OPERTN_AT;
    pCombo->Properties->Items->Clear();
    pCode = ItsCodeManager->FLists.Find("PRKRV");
    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 TPRK0010M::FormInit()
{
    int ii;

    PRK_PLCE_NMBR->Text      = "";
    PRK_PLCE_MANAGE_NO->Text = "";
    PRK_PLCE_STTUS_VER->Text = "";
    PRK_PLCE_NM->Text        = "";
    PRK_PLCE_ADRES->Text     = "";
    PRK_PLCE_INSTL_DE->Text  = "";
    PRK_PLCE_ABL_DE->Text    = "";
    PRK_PLCE_ENTRC_NM->Text  = "";

    PRK_PLCE_ENTRC_LA->Value = 0;
    PRK_PLCE_ENTRC_LO->Value = 0;

    //PRK_PLCE_IMAGE_DATA	N	BLOB	Y			주차장 구조 이미지 - 주차장 구조 이미지 데이터
    try {
        PRK_PLCE_IMAGE_DATA->Picture->Bitmap->FreeImage();
        PRK_PLCE_IMAGE_DATA->Picture->Bitmap = NULL;
    } catch(Exception &e) {
    }

    PRK_PLCE_IMAGE_TYPE->Text         = "";
    PRK_PLCE_IMAGE_DC->Text           = "";
    SYS_NE->Text                      = "";
    PARKING_CNTRL_SYS_INSTL_DE->Text  = "";
    RELATION_LAW_AND_ORDINANCES->Text = "";

    // 이용가능 차량유형
    PRK_AVAIL_CARTYPE->Text = "";
    for (int ii = 0; ii <= 6; ii++) {
        TcxCheckBox *pCheckBox = (TcxCheckBox*)FindComponent("PRK_AVAIL_CARTYPE"+String(ii));
        if (pCheckBox) {
            pCheckBox->Checked = false;
        }
    }

    PRK_CMPRT_CO->Value = 0;

    COLCT_DEVICE_INSTL_AT->Checked = false;
    MASTER_CONTROLLER->Value = 0;
    SLAVE_CONTROLLER->Value = 0;
    COLCT_DEVICE_QTY->Value = 0;

    OPR_INFO->Text = "";
    MIN30_FEE->Value = 0;
    MIN10_FEE->Value = 0;
    DAY_FEE->Value = 0;
    MON_FEE->Value = 0;

    // 주차구획 단위정보 수집장치 정보
    COLCT_DEVICE_KND->Text = "";
    for (int ii = 0; ii <= 5; ii++) {
        TcxCheckBox *pCheckBox = (TcxCheckBox*)FindComponent("COLCT_DEVICE_KND"+String(ii));
        if (pCheckBox) {
            pCheckBox->Checked = false;
        }
    }

    COLCT_DEVICE_INSTL_DE->Text = "";

    CRM_PRV_CCTV_INSTL_AT->Checked = false;
    CRM_PRV_CCTV_INSTL_CO->Value = 0;

    //AObj->VMS_DSPL_NM;

    PRK_PLCE_OPR_VER->Text  = "";
    CMPT_INST_NM->Text      = "";
    OPER_DEPT_NM->Text      = "";
    OPER_PERSON_NM->Text    = "";
    OPER_CTT_PC_TEL->Text   = "";
    OPER_CTT_PC_EMAIL->Text = "";

    OPERTN_BS_FREE_TIME->Text = "";
    OPN_CODE->Checked = false;

    // 주차요금 지불수단
    PARKING_CHRGE_PYMNT_MN->Text = "";
    for (int ii = 1; ii <= 8; ii++) {
        TcxCheckBox *pCheckBox = (TcxCheckBox*)FindComponent("PARKING_CHRGE_PYMNT_MN"+String(ii));
        if (pCheckBox) {
            pCheckBox->Checked = false;
        }
    }

    PARTCLR_MATTER->Text = "";

    // 전용주차구역정보
    for (int ii = 0; ii <= 7; ii++) {
        TcxSpinEdit *pEdit = (TcxSpinEdit*)FindComponent("VHCTY_AR_SE"+String(ii));
        if (pEdit) {
            pEdit->Value = 0;
        }
    }

    // 주차요금
    PARKING_CHRGE_BS_TIME->Value         = 0;
    PARKING_CHRGE_BS_CHRGE->Value        = 0;
    PARKING_CHRGE_ADIT_UNIT_TIME->Value  = 0;
    PARKING_CHRGE_ADIT_UNIT_CHRGE->Value = 0;

    TcxComboBox *pCombo = NULL;

    // 주차장 입출구 형태
    pCombo = PRK_PLCE_ENTRC_EXTTY;
	pCombo->ItemIndex = 0;

    // 주차형식
    pCombo = PARKING_TY;
	pCombo->ItemIndex = 0;

    // 이용형태
    pCombo = PARKING_US;
	pCombo->ItemIndex = 0;

    // 급지구분
    pCombo = AR_LEVEL_SE;
	pCombo->ItemIndex = 0;

    // 주차 부제 시행여부
    pCombo = NO_DRV_DAY_OPERTN_AT;
	pCombo->ItemIndex = 0;

    // 주차요금 정산 방식
    pCombo = PARKING_CHRGE_EXCCLC_MTHD;
	pCombo->ItemIndex = 0;

    // 주차예약 시행여부
    pCombo = PARKING_RESVE_OPERTN_AT;
	pCombo->ItemIndex = 0;

    FChrges.Clear();
    FScheds.Clear();
    FFlrs.Clear();
    FPrvs.Clear();

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

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

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

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

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

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

    if (!AObj) return;

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

    if (AObj->FOPR->FChrges.Size() == 0) {
        //PrkPlceManager->LoadFromDbOprChrge(AObj);
    }
    if (AObj->FOPR->FScheds.Size() == 0) {
        //PrkPlceManager->LoadFromDbOprSched(AObj);
    }
    if (AObj->FFlrs.Size() == 0) {
        //PrkPlceManager->LoadFromDbFlr(AObj);
    }
    if (AObj->FPrvs.Size() == 0) {
        //PrkPlceManager->LoadFromDbPrv(AObj);
    }
    if (AObj->FDevs.Size() == 0) {
        //PrkPlceManager->LoadFromDbDev(AObj);
    }

    FChrges.Clear();
    FOR_STL(TPrkPlceOprChrge*, pTmpChrge, AObj->FOPR->FChrges) {
        TPrkPlceOprChrge* pChrge = new TPrkPlceOprChrge();
        pChrge->PRK_PLCE_NMBR                 = pTmpChrge->PRK_PLCE_NMBR;
        pChrge->CHRGE                         = pTmpChrge->CHRGE;
        pChrge->PARKING_CHRGE_BS_TIME         = pTmpChrge->PARKING_CHRGE_BS_TIME;
        pChrge->PARKING_CHRGE_BS_CHRGE        = pTmpChrge->PARKING_CHRGE_BS_CHRGE;
        pChrge->PARKING_CHRGE_ADIT_UNIT_TIME  = pTmpChrge->PARKING_CHRGE_ADIT_UNIT_TIME;
        pChrge->PARKING_CHRGE_ADIT_UNIT_CHRGE = pTmpChrge->PARKING_CHRGE_ADIT_UNIT_CHRGE;

        FChrges.Push(pChrge->CHRGE, pChrge);
    }

    FScheds.Clear();
    FOR_STL(TPrkPlceOprSched*, pTmpSched, AObj->FOPR->FScheds) {
        TPrkPlceOprSched* pSched = new TPrkPlceOprSched();
        pSched->PRK_PLCE_NMBR         = pTmpSched->PRK_PLCE_NMBR;
        pSched->SCHED                 = pTmpSched->SCHED;
        pSched->OPERTN_START_TIME     = pTmpSched->OPERTN_START_TIME;
        pSched->OPERTN_END_TIME       = pTmpSched->OPERTN_END_TIME;
        pSched->PARKING_CHRGE_LEVY_AT = pTmpSched->PARKING_CHRGE_LEVY_AT;

        FScheds.Push(pSched->SCHED, pSched);
    }

    FFlrs.Clear();
    FOR_STL(TPrkPlceFlr*, pTmpFlr, AObj->FFlrs) {
        TPrkPlceFlr* pFlr = new TPrkPlceFlr();
        pFlr->PRK_PLCE_NMBR           = pTmpFlr->PRK_PLCE_NMBR;
        pFlr->FLR_NO                  = pTmpFlr->FLR_NO;
        pFlr->FLR_PRK_CMPRT_CO        = pTmpFlr->FLR_PRK_CMPRT_CO;
        //pFlr->FLR_REMNDR_PRK_CMPRT_CO = pTmpFlr->FLR_REMNDR_PRK_CMPRT_CO;
        //pFlr->DATA_COLCT_TIME         = pTmpFlr->DATA_COLCT_TIME;

        FFlrs.Push(pFlr->FLR_NO, pFlr);
    }

    FPrvs.Clear();
    FOR_STL(TPrkPlcePrv*, pTmpPrv, AObj->FPrvs) {
        TPrkPlcePrv* pPrv = new TPrkPlcePrv();
        pPrv->PRK_PLCE_NMBR                = pTmpPrv->PRK_PLCE_NMBR;
        pPrv->VHCTY_AR_SE                  = pTmpPrv->VHCTY_AR_SE;
        pPrv->VHCTY_AR_PRK_CMPRT_CO        = pTmpPrv->VHCTY_AR_PRK_CMPRT_CO;
        //pPrv->VHCTY_AR_REMNDR_PRK_CMPRT_CO = pTmpPrv->VHCTY_AR_REMNDR_PRK_CMPRT_CO;

        FPrvs.Push(pPrv->VHCTY_AR_SE, pPrv);
    }

    PRK_PLCE_NMBR->Text      = AObj->CTLR_NMBR;
    PRK_PLCE_MANAGE_NO->Text = AObj->CTLR_ID;
    PRK_PLCE_STTUS_VER->Text = AObj->PRK_PLCE_STTUS_VER;
    PRK_PLCE_NM->Text        = AObj->NAME;
    PRK_PLCE_ADRES->Text     = AObj->ISTL_LCTN_ADDR;
    PRK_PLCE_INSTL_DE->Text  = AObj->PRK_PLCE_INSTL_DE;
    PRK_PLCE_ABL_DE->Text    = AObj->PRK_PLCE_ABL_DE;
    PRK_PLCE_ENTRC_NM->Text  = AObj->PRK_PLCE_ENTRC_NM;

    PRK_PLCE_ENTRC_LA->Value = StrToFloatDef(AObj->Y_CRDN, 0);
    PRK_PLCE_ENTRC_LO->Value = StrToFloatDef(AObj->X_CRDN, 0);

    OPR_INFO->Text   = AObj->OPR_INFO;
    MIN30_FEE->Value = AObj->MIN30_FEE;
    MIN10_FEE->Value = AObj->MIN10_FEE;
    DAY_FEE->Value   = AObj->DAY_FEE;
    MON_FEE->Value   = AObj->MON_FEE;


    CMM_SetComboBoxItem(PRK_PLCE_ENTRC_EXTTY, AObj->PRK_PLCE_ENTRC_EXTTY);
    CMM_SetComboBoxItem(PARKING_TY, AObj->PARKING_TY);
    CMM_SetComboBoxItem(PARKING_US, AObj->PARKING_US);
    CMM_SetComboBoxItem(AR_LEVEL_SE, AObj->AR_LEVEL_SE);

    //PRK_PLCE_IMAGE_DATA	N	BLOB	Y			주차장 구조 이미지 - 주차장 구조 이미지 데이터

    PRK_PLCE_IMAGE_TYPE->Text         = AObj->PRK_PLCE_IMAGE_TYPE;
    PRK_PLCE_IMAGE_DC->Text           = AObj->PRK_PLCE_IMAGE_DC;
    SYS_NE->Text                      = AObj->SYS_NE;
    PARKING_CNTRL_SYS_INSTL_DE->Text  = AObj->PARKING_CNTRL_SYS_INSTL_DE;
    RELATION_LAW_AND_ORDINANCES->Text = AObj->RELATION_LAW_AND_ORDINANCES;

    // 이용가능 차량유형
    PRK_AVAIL_CARTYPE->Text = AObj->PRK_AVAIL_CARTYPE;
    for (int ii = 0; ii <= 6; ii++) {
        String sCheck = AObj->PRK_AVAIL_CARTYPE.SubString(ii+1, 1);
        TcxCheckBox *pCheckBox = (TcxCheckBox*)FindComponent("PRK_AVAIL_CARTYPE"+String(ii));
        if (pCheckBox) {
            pCheckBox->Checked = sCheck == "1" ? true : false;
        }
    }

    PRK_CMPRT_CO->Value = AObj->PRK_CMPRT_CO;

    COLCT_DEVICE_INSTL_AT->Checked = AObj->COLCT_DEVICE_INSTL_AT == 1 ? true : false;
    MASTER_CONTROLLER->Value = AObj->MASTER_CONTROLLER;
    SLAVE_CONTROLLER->Value  = AObj->SLAVE_CONTROLLER;
    COLCT_DEVICE_QTY->Value  = AObj->COLCT_DEVICE_QTY;
    //COLCT_DEVICE_KND	N	VARCHAR2(6)	Y			주차구획 단위정보 수집장치 정보 - 주차정보 수집장치 종류 - 아래 유형 조합해서 생성됌 - 예시) 지자기설치 시 (010000), 영상/레이더 혼합 설치시(101000)
    // 주차구획 단위정보 수집장치 정보
    COLCT_DEVICE_KND->Text = AObj->COLCT_DEVICE_KND;
    for (int ii = 0; ii <= 5; ii++) {
        String sCheck = AObj->COLCT_DEVICE_KND.SubString(ii+1, 1);
        TcxCheckBox *pCheckBox = (TcxCheckBox*)FindComponent("COLCT_DEVICE_KND"+String(ii));
        if (pCheckBox) {
            pCheckBox->Checked = sCheck == "1" ? true : false;
        }
    }

    COLCT_DEVICE_INSTL_DE->Text = AObj->COLCT_DEVICE_INSTL_DE;

    CRM_PRV_CCTV_INSTL_AT->Checked = AObj->CRM_PRV_CCTV_INSTL_AT == 1 ? true : false;
    CRM_PRV_CCTV_INSTL_CO->Value = AObj->CRM_PRV_CCTV_INSTL_CO;

    CMM_SetComboBoxItem(PIS_TYPE, AObj->TYPE_CD);

    AObj->VMS_DSPL_NM;

    PRK_PLCE_OPR_VER->Text  = AObj->FOPR->PRK_PLCE_OPR_VER;
    CMPT_INST_NM->Text      = AObj->FOPR->CMPT_INST_NM;
    OPER_DEPT_NM->Text      = AObj->FOPR->OPER_DEPT_NM;
    OPER_PERSON_NM->Text    = AObj->FOPR->OPER_PERSON_NM;
    OPER_CTT_PC_TEL->Text   = AObj->FOPR->OPER_CTT_PC_TEL;
    OPER_CTT_PC_EMAIL->Text = AObj->FOPR->OPER_CTT_PC_EMAIL;

    CMM_SetComboBoxItem(NO_DRV_DAY_OPERTN_AT, AObj->FOPR->NO_DRV_DAY_OPERTN_AT);

    OPERTN_BS_FREE_TIME->Text = AObj->FOPR->OPERTN_BS_FREE_TIME;
    OPN_CODE->Checked = AObj->FOPR->OPN_CODE == 1 ? true : false;

    CMM_SetComboBoxItem(PARKING_CHRGE_EXCCLC_MTHD, AObj->FOPR->PARKING_CHRGE_EXCCLC_MTHD);

    //PARKING_CHRGE_PYMNT_MN	N	VARCHAR2(8)	Y			주차요금 지불수단 - 아래 유형 조합해서 생성됌 - 예시) 현금/신용카드 혼합결제 시 (11000000), 핀테크 결제 시 (00000100), 1: 현금, 2: 신용카드, 3: 교통카드, 4: 전자지불(하이패스), 5: 휴대폰소액결제, 6: 핀테크(OO페이), 7: MaaS 통합결제, 9: 기타
    // 주차요금 지불수단
    PARKING_CHRGE_PYMNT_MN->Text = AObj->FOPR->PARKING_CHRGE_PYMNT_MN;
    for (int ii = 1; ii <= 8; ii++) {
        String sCheck = AObj->FOPR->PARKING_CHRGE_PYMNT_MN.SubString(ii, 1);
        TcxCheckBox *pCheckBox = (TcxCheckBox*)FindComponent("PARKING_CHRGE_PYMNT_MN"+String(ii));
        if (pCheckBox) {
            pCheckBox->Checked = sCheck == "1" ? true : false;
        }
    }

    CMM_SetComboBoxItem(PARKING_RESVE_OPERTN_AT, AObj->FOPR->PARKING_RESVE_OPERTN_AT);

    PARTCLR_MATTER->Text = AObj->FOPR->PARTCLR_MATTER;

    // 전용주차구역정보
    FOR_STL(TPrkPlcePrv*, pPrv, FPrvs) {
        TcxSpinEdit *pArSe = (TcxSpinEdit*)FindComponent("VHCTY_AR_SE"+String(pPrv->VHCTY_AR_SE));
        if (pArSe) {
            pArSe->Value = pPrv->VHCTY_AR_PRK_CMPRT_CO;
        }
    }

    // 주차요금
    TPrkPlceOprChrge* pChrge = FChrges.Find(1); // 일반요금
    if (pChrge) {
        PARKING_CHRGE_BS_TIME->Value         = pChrge->PARKING_CHRGE_BS_TIME;
        PARKING_CHRGE_BS_CHRGE->Value        = pChrge->PARKING_CHRGE_BS_CHRGE;
        PARKING_CHRGE_ADIT_UNIT_TIME->Value  = pChrge->PARKING_CHRGE_ADIT_UNIT_TIME;
        PARKING_CHRGE_ADIT_UNIT_CHRGE->Value = pChrge->PARKING_CHRGE_ADIT_UNIT_CHRGE;
    }

    // 운영시간

    // 층정보
    //////////

    //LoadPrkPlceImage(AObj->CTLR_NMBR);

    ChangeEditMode(3);
}
//---------------------------------------------------------------------------
void __fastcall TPRK0010M::UpdateList()
{
    CMM_ClearGridTableView(TvList);

    int nRow = 0;
    try
    {
        TvList->BeginUpdate();

        TItsSubCode *pSubCode;
        try
        {
            PrkPlceManager->FLists.Lock();
            FOR_STL(TPrkPlce*, pObj, PrkPlceManager->FLists)
            {
                if (pObj->DEL_YN != "N") continue;

                nRow = m_pGDC->AppendRecord();

                m_pGDC->Values[nRow][ColumnSel->Index] = false;                 //선택
                m_pGDC->Values[nRow][Column00->Index]  = "-";                   //편집
                m_pGDC->Values[nRow][Column01->Index]  = pObj->CTLR_NMBR;
                m_pGDC->Values[nRow][Column11->Index]  = pObj->CTLR_ID;
                m_pGDC->Values[nRow][Column04->Index]  = pObj->NAME;
                m_pGDC->Values[nRow][Column10->Index]  = pObj->PRK_CMPRT_CO;

                // 층정보
                String sFlrs = "";
                FOR_STL(TPrkPlceFlr*, pFlr, pObj->FFlrs)
                {
                    if (sFlrs == "") {
                        sFlrs = pFlr->FLR_NO;
                    } else {
                        sFlrs += ", " + pFlr->FLR_NO;
                    }
                }
                m_pGDC->Values[nRow][Column12->Index] = sFlrs;

                m_pGDC->Values[nRow][Column13->Index] = "사용";
                m_pGDC->Values[nRow][Column99->Index] = (int)pObj;

            }
        }
        __finally
        {
            PrkPlceManager->FLists.UnLock();
        }
    }
    __finally
    {
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        //CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------
void __fastcall TPRK0010M::UpdateIcon()
{
    if (!FRAMEOpenMap1->MapStart) return;

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

    String arrObj = "";
    String sTemp;

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

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

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

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

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

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

void __fastcall TPRK0010M::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)
    {
        CxList->Enabled = false;
        GrpInfo->Enabled = true;
    }
    else
    {
        CxList->Enabled = true;
        GrpInfo->Enabled = false;
    }

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

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

    PRK_PLCE_NMBR->Text = String(nNmbr);
    //PRK_PLCE_ENTRC_LA->Value = FRAMEOpenMap1->CenterX;
    //PRK_PLCE_ENTRC_LO->Value = FRAMEOpenMap1->CenterY;
    PRK_PLCE_ENTRC_LA->Value = FRAMEOpenMap1->CenterY;
    PRK_PLCE_ENTRC_LO->Value = FRAMEOpenMap1->CenterX;


    //지도에 새로운 시설물 아이콘 추가하면서 편집모드로 변경
    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 TPRK0010M::BtnDeleteClick(TObject *Sender)
{
    if (!FRAMEOpenMap1->MapStart) return;

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

    String sId = PRK_PLCE_NMBR->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;

	TPrkPlce *pObj = PrkPlceManager->FLists.Find(sId);
	if (pObj)
	{
		if (PrkPlceManager->DeletePrkPlce(pObj))
		{
            pObj->DEL_YN = "Y";
            UpdateList();
            UpdateIcon();
		}

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

        FormInit();
        ChangeEditMode(1);

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

void __fastcall TPRK0010M::BtnSaveClick(TObject *Sender)
{
	if (PRK_PLCE_NMBR->Text.IsEmpty()             ||
        PRK_PLCE_MANAGE_NO->Text.Trim().IsEmpty() ||
        PRK_PLCE_NM->Text.Trim().IsEmpty()        ||
        PRK_PLCE_ENTRC_LA->Value == 0             ||
        PRK_PLCE_ENTRC_LO->Value == 0             ||
        PIS_TYPE->ItemIndex < 0   )
	{
        Application->MessageBox(L"[*]항목은 필수 입력 항목입니다.\r\n모두 입력후 진행해 주세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
	}

    String sNmbr = PRK_PLCE_NMBR->Text;
    int    nNmbr = sNmbr.ToInt();
    String sId = PRK_PLCE_MANAGE_NO->Text.Trim();

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

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

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

    if (pObj)
    {
        pObj->CTLR_NMBR                   = PRK_PLCE_NMBR->Text;
        pObj->CTLR_ID                     = PRK_PLCE_MANAGE_NO->Text;
        pObj->PRK_PLCE_STTUS_VER          = PRK_PLCE_STTUS_VER->Text;
        pObj->NAME                        = PRK_PLCE_NM->Text;
        pObj->VMS_DSPL_NM                 = pObj->NAME;
        pObj->ISTL_LCTN_ADDR              = PRK_PLCE_ADRES->Text;
        pObj->PRK_PLCE_INSTL_DE           = PRK_PLCE_INSTL_DE->Text;
        pObj->PRK_PLCE_ABL_DE             = PRK_PLCE_ABL_DE->Text;
        pObj->PRK_PLCE_ENTRC_NM           = PRK_PLCE_ENTRC_NM->Text;
        pObj->Y_CRDN                      = PRK_PLCE_ENTRC_LA->Value;
        pObj->X_CRDN                      = PRK_PLCE_ENTRC_LO->Value;
        pObj->PRK_PLCE_ENTRC_EXTTY        = APP_GetCode(PRK_PLCE_ENTRC_EXTTY).ToIntDef(0);
        pObj->PARKING_TY                  = APP_GetCode(PARKING_TY).ToIntDef(0);
        pObj->PARKING_US                  = APP_GetCode(PARKING_US).ToIntDef(0);
        pObj->AR_LEVEL_SE                 = APP_GetCode(AR_LEVEL_SE).ToIntDef(0);
        //pObj->PRK_PLCE_IMAGE_DATA         = PRK_PLCE_IMAGE_DATA;
        pObj->PRK_PLCE_IMAGE_TYPE         = PRK_PLCE_IMAGE_TYPE->Text.ToIntDef(0);
        pObj->PRK_PLCE_IMAGE_DC           = PRK_PLCE_IMAGE_DC->Text;
        pObj->SYS_NE                      = SYS_NE->Text;
        pObj->PARKING_CNTRL_SYS_INSTL_DE  = PARKING_CNTRL_SYS_INSTL_DE->Text;
        pObj->RELATION_LAW_AND_ORDINANCES = RELATION_LAW_AND_ORDINANCES->Text;

        // 이용가능 차량유형
        String prkAvailCarType = "";
        for (int ii = 0; ii <= 6; ii++) {
            TcxCheckBox *pCheckBox = (TcxCheckBox*)FindComponent("PRK_AVAIL_CARTYPE"+String(ii));
            if (pCheckBox) {
                prkAvailCarType += pCheckBox->Checked ? "1" : "0";
            }
        }
        pObj->PRK_AVAIL_CARTYPE           = prkAvailCarType;

        pObj->PRK_CMPRT_CO                = PRK_CMPRT_CO->Value;
        pObj->COLCT_DEVICE_INSTL_AT       = COLCT_DEVICE_INSTL_AT->Checked ? 1 : 0;
        pObj->MASTER_CONTROLLER           = MASTER_CONTROLLER->Value;
        pObj->SLAVE_CONTROLLER            = SLAVE_CONTROLLER->Value;
        pObj->COLCT_DEVICE_QTY            = COLCT_DEVICE_QTY->Value;

        pObj->OPR_INFO  = OPR_INFO->Text.Trim();
        pObj->MIN30_FEE = MIN30_FEE->Value;
        pObj->MIN10_FEE = MIN10_FEE->Value;
        pObj->DAY_FEE   = DAY_FEE->Value;
        pObj->MON_FEE   = MON_FEE->Value;


        // 주차구획 단위정보 수집장치 정보
        String colctDeviceKnd = "";
        for (int ii = 0; ii <= 5; ii++) {
            TcxCheckBox *pCheckBox = (TcxCheckBox*)FindComponent("COLCT_DEVICE_KND"+String(ii));
            if (pCheckBox) {
                colctDeviceKnd += pCheckBox->Checked ? "1" : "0";
            }
        }
        pObj->COLCT_DEVICE_KND            = colctDeviceKnd;

        pObj->COLCT_DEVICE_INSTL_DE       = COLCT_DEVICE_INSTL_DE->Text;
        pObj->CRM_PRV_CCTV_INSTL_AT       = CRM_PRV_CCTV_INSTL_AT->Checked ? 1 : 0;
        pObj->CRM_PRV_CCTV_INSTL_CO       = CRM_PRV_CCTV_INSTL_CO->Value;
        if (pObj->INFO_CREATE_TIME == "") {
            pObj->INFO_CREATE_TIME = Now().FormatString("YYYYMMDDHHNNSS");
        }
        //TODO
        pObj->INFO_CREATE_TIME = Now().FormatString("YYYYMMDDHHNNSS");

        pObj->TYPE_CD                    = APP_GetCode(PIS_TYPE);

        pObj->ISTL_LCTN_NM = pObj->NAME;
        pObj->X_CRDN       = pObj->X_CRDN;
        pObj->Y_CRDN       = pObj->Y_CRDN;

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

        // 운영정보 설정
        pObj->FOPR->PRK_PLCE_NMBR             = pObj->CTLR_NMBR;
        pObj->FOPR->PRK_PLCE_OPR_VER          = PRK_PLCE_OPR_VER->Text;
        pObj->FOPR->CMPT_INST_NM              = CMPT_INST_NM->Text;
        pObj->FOPR->OPER_DEPT_NM              = OPER_DEPT_NM->Text;
        pObj->FOPR->OPER_PERSON_NM            = OPER_PERSON_NM->Text;
        pObj->FOPR->OPER_CTT_PC_TEL           = OPER_CTT_PC_TEL->Text;
        pObj->FOPR->OPER_CTT_PC_EMAIL         = OPER_CTT_PC_EMAIL->Text;
        pObj->FOPR->NO_DRV_DAY_OPERTN_AT      = APP_GetCode(NO_DRV_DAY_OPERTN_AT).ToIntDef(0);
        pObj->FOPR->OPERTN_BS_FREE_TIME       = OPERTN_BS_FREE_TIME->Text;
        pObj->FOPR->OPN_CODE                  = OPN_CODE->Checked ? 1 : 0;
        pObj->FOPR->PARKING_CHRGE_EXCCLC_MTHD = APP_GetCode(PARKING_CHRGE_EXCCLC_MTHD).ToIntDef(0);

        // // 주차요금 지불수단
        String sChrgePymnt = "";
        for (int ii = 1; ii <= 8; ii++) {
            TcxCheckBox *pCheckBox = (TcxCheckBox*)FindComponent("PARKING_CHRGE_PYMNT_MN"+String(ii));
            if (pCheckBox) {
                sChrgePymnt += pCheckBox->Checked ? "1" : "0";
            }
        }
        pObj->FOPR->PARKING_CHRGE_PYMNT_MN    = sChrgePymnt;

        pObj->FOPR->PARKING_RESVE_OPERTN_AT   = APP_GetCode(PARKING_RESVE_OPERTN_AT).ToIntDef(0);
        pObj->FOPR->PARTCLR_MATTER            = PARTCLR_MATTER->Text;
        if (pObj->FOPR->INFO_CREATE_TIME == "") {
            pObj->FOPR->INFO_CREATE_TIME = Now().FormatString("YYYYMMDDHHNNSS");
        }

        // 전용주차구역정보
        for (int ii = 0; ii <= 7; ii++) {
            int VHCTY_AR_PRK_CMPRT_CO = 0;
            TcxSpinEdit *pEdit = (TcxSpinEdit*)FindComponent("VHCTY_AR_SE"+String(ii));
            if (pEdit) {
                VHCTY_AR_PRK_CMPRT_CO = pEdit->Value;
            }

            TPrkPlcePrv *pPrv = pObj->FPrvs.Find(ii);
            if (pPrv) {
                pPrv->VHCTY_AR_PRK_CMPRT_CO = VHCTY_AR_PRK_CMPRT_CO;
            }
            else {
                pPrv = new TPrkPlcePrv();
                pPrv->PRK_PLCE_NMBR                = pObj->CTLR_NMBR;
                pPrv->VHCTY_AR_SE                  = ii;
                pPrv->VHCTY_AR_PRK_CMPRT_CO        = VHCTY_AR_PRK_CMPRT_CO;
                pPrv->VHCTY_AR_REMNDR_PRK_CMPRT_CO = 0;

                pObj->FPrvs.Push(pPrv->VHCTY_AR_SE, pPrv);
            }
        }


        bool bResult;
        if (FNewDb)
        {
            bResult = PrkPlceManager->InsertPrkPlce(pObj);
            if (bResult)
            {
                PrkPlceManager->FLists.Push(sNmbr, pObj);
            }
		}
        else
		{
            bResult = PrkPlceManager->UpdatePrkPlce(pObj);
		}

        if (bResult)
        {
            //UpdatePrkPlceImage(pObj);
            //MergePrkPlcePrv(pObj);
            //MergePrkPlceFlr(pObj);
            //MergePrkPlceFlrDev(pObj);
            //MergePrkPlceOprChrge(pObj);
            //MergePrkPlceOprSched(pObj);

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

            FUpdate = true;

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

void __fastcall TPRK0010M::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];
                //PRK_PLCE_ENTRC_LA->Value = sCenterLat.ToDouble();
                //PRK_PLCE_ENTRC_LO->Value = sCenterLng.ToDouble();
                PRK_PLCE_ENTRC_LO->Value = sCenterLat.ToDouble();
                PRK_PLCE_ENTRC_LA->Value = sCenterLng.ToDouble();
            }
            catch(Exception &e)
            {
            }
            delete pStringList;
        }
    }
    catch(Exception &exception)
    {
    }
}
//---------------------------------------------------------------------------

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

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

void __fastcall TPRK0010M::BtnPRK_PLCE_IMAGE_DATAClick(TObject *Sender)
{
#if 0
    String sId = PRK_PLCE_NMBR->Text;
    TPrkPlce *pObj = PrkPlceManager->FLists.Find(sId);
    if (pObj == NULL) {
        return;
    }

    TPRK0011M *pSelForm = NULL;
    try
    {
        try {
            pSelForm = new TPRK0011M(this, PRK_PLCE_IMAGE_DATA, PRK_PLCE_IMAGE_TYPE->Text);
            pSelForm->ShowModal();
            if (pSelForm->FSelected)
            {
                try {
                    PRK_PLCE_IMAGE_DATA->Picture->Bitmap->FreeImage();
                    PRK_PLCE_IMAGE_DATA->Picture->Bitmap = NULL;

                    PRK_PLCE_IMAGE_DATA->Picture->Assign(pSelForm->PRK_PLCE_IMAGE_DATA->Picture);
                    PRK_PLCE_IMAGE_TYPE->Text = pSelForm->FPRK_PLCE_IMAGE_TYPE;
                } catch(Exception &e) {}
            }
        } catch(...) { }
    }
    __finally
    {
        if (pSelForm) delete pSelForm;
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TPRK0010M::BtnOPR_CHRGEClick(TObject *Sender)
{
#if 0
    String sId = PRK_PLCE_NMBR->Text;
    TPrkPlce *pObj = PrkPlceManager->FLists.Find(sId);
    if (pObj == NULL) {
        return;
    }

    TPRK0012M *pSelForm = NULL;
    try
    {
        try {
            pSelForm = new TPRK0012M(this, pObj);
            FOR_STL(TPrkPlceOprChrge*, pChrge, FChrges) {
                TcxSpinEdit *pBsTime = (TcxSpinEdit*)pSelForm->FindComponent("BS_TIME" + String(pChrge->CHRGE));
                TcxSpinEdit *pBsChrge = (TcxSpinEdit*)pSelForm->FindComponent("BS_CHRGE" + String(pChrge->CHRGE));
                TcxSpinEdit *pAddTime = (TcxSpinEdit*)pSelForm->FindComponent("ADIT_UNIT_TIME" + String(pChrge->CHRGE));
                TcxSpinEdit *pAddChrge = (TcxSpinEdit*)pSelForm->FindComponent("ADIT_UNIT_CHRGE" + String(pChrge->CHRGE));

                if (pBsTime) pBsTime->Value = pChrge->PARKING_CHRGE_BS_TIME;
                if (pBsChrge) pBsChrge->Value = pChrge->PARKING_CHRGE_BS_CHRGE;
                if (pAddTime) pAddTime->Value  = pChrge->PARKING_CHRGE_ADIT_UNIT_TIME;
                if (pAddChrge) pAddChrge->Value = pChrge->PARKING_CHRGE_ADIT_UNIT_CHRGE;
            }
            pSelForm->ShowModal();
            if (pSelForm->FSelected)
            {
                FChrges.Clear();
                for (int ii = 1; ii <= 4; ii++) {
                    TcxSpinEdit *pBsTime = (TcxSpinEdit*)pSelForm->FindComponent("BS_TIME" + String(ii));
                    TcxSpinEdit *pBsChrge = (TcxSpinEdit*)pSelForm->FindComponent("BS_CHRGE" + String(ii));
                    TcxSpinEdit *pAddTime = (TcxSpinEdit*)pSelForm->FindComponent("ADIT_UNIT_TIME" + String(ii));
                    TcxSpinEdit *pAddChrge = (TcxSpinEdit*)pSelForm->FindComponent("ADIT_UNIT_CHRGE" + String(ii));

                    if (pBsTime && pBsChrge && pAddTime && pAddChrge) {
                        if (pBsTime->Value > 0 || pBsChrge->Value > 0 || pAddTime->Value > 0 || pAddChrge->Value > 0) {
                            TPrkPlceOprChrge* pChrge = new TPrkPlceOprChrge();
                            pChrge->PRK_PLCE_NMBR                 = sId;
                            pChrge->CHRGE                         = ii;
                            pChrge->PARKING_CHRGE_BS_TIME         = pBsTime->Value;
                            pChrge->PARKING_CHRGE_BS_CHRGE        = pBsChrge->Value;
                            pChrge->PARKING_CHRGE_ADIT_UNIT_TIME  = pAddTime->Value;
                            pChrge->PARKING_CHRGE_ADIT_UNIT_CHRGE = pAddChrge->Value;

                            FChrges.Push(pChrge->CHRGE, pChrge);
                            if (ii == 1) // 일반요금
                            {
                                PARKING_CHRGE_BS_TIME->Value         = pChrge->PARKING_CHRGE_BS_TIME;
                                PARKING_CHRGE_BS_CHRGE->Value        = pChrge->PARKING_CHRGE_BS_CHRGE;
                                PARKING_CHRGE_ADIT_UNIT_TIME->Value  = pChrge->PARKING_CHRGE_ADIT_UNIT_TIME;
                                PARKING_CHRGE_ADIT_UNIT_CHRGE->Value = pChrge->PARKING_CHRGE_ADIT_UNIT_CHRGE;
                            }
                        }
                    }
                }
            }
        } catch(...) { }
    }
    __finally
    {
        if (pSelForm) delete pSelForm;
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TPRK0010M::BtnOPR_SCHEDClick(TObject *Sender)
{
#if 0
    String sId = PRK_PLCE_NMBR->Text;
    TPrkPlce *pObj = PrkPlceManager->FLists.Find(sId);
    if (pObj == NULL) {
        return;
    }

    TPRK0013M *pSelForm = NULL;
    try
    {
        try {
            pSelForm = new TPRK0013M(this, pObj);

            TItsCode *pCode = ItsCodeManager->FLists.Find("PRKCH");
            if (pCode)
            {
                for (int ii = 1; ii <= 8; ii++) {
                    TcxComboBox *pCombo = (TcxComboBox*)pSelForm->FindComponent("CHRGE_LEVY_AT" + String(ii));
                    if (pCombo) {
                        pCombo->Properties->Items->Clear();
                        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 = pCode->FSubLists.Size() - 1;
                    }
                }
            }
            FOR_STL(TPrkPlceOprSched*, pSched, FScheds) {
                TcxTextEdit *pStartTime = (TcxTextEdit*)pSelForm->FindComponent("START_TIME" + String(pSched->SCHED));
                TcxTextEdit *pEndTime = (TcxTextEdit*)pSelForm->FindComponent("END_TIME" + String(pSched->SCHED));
                TcxComboBox *pLevyAt = (TcxComboBox*)pSelForm->FindComponent("CHRGE_LEVY_AT" + String(pSched->SCHED));

                if (pStartTime) pStartTime->Text = pSched->OPERTN_START_TIME;
                if (pEndTime) pEndTime->Text = pSched->OPERTN_END_TIME;
                if (pLevyAt) CMM_SetComboBoxItem(pLevyAt, pSched->PARKING_CHRGE_LEVY_AT);
            }

            pSelForm->ShowModal();
            if (pSelForm->FSelected)
            {
                FScheds.Clear();
                for (int ii = 1; ii <= 8; ii++) {
                    TcxTextEdit *pStartTime = (TcxTextEdit*)pSelForm->FindComponent("START_TIME" + String(ii));
                    TcxTextEdit *pEndTime = (TcxTextEdit*)pSelForm->FindComponent("END_TIME" + String(ii));
                    TcxComboBox *pLevyAt = (TcxComboBox*)pSelForm->FindComponent("CHRGE_LEVY_AT" + String(ii));

                    if (pStartTime && pEndTime && pLevyAt) {
                        String sLevyAt = APP_GetCode(pLevyAt);
                        if (sLevyAt == "9") {
                            continue;   // 사용안함
                        }
                        TPrkPlceOprSched* pSched = new TPrkPlceOprSched();
                        pSched->PRK_PLCE_NMBR         = sId;
                        pSched->SCHED                 = ii;
                        pSched->OPERTN_START_TIME     = pStartTime->Text;
                        pSched->OPERTN_END_TIME       = pEndTime->Text;
                        pSched->PARKING_CHRGE_LEVY_AT = sLevyAt.ToIntDef(0);

                        FScheds.Push(pSched->SCHED, pSched);
                    }
                }
            }
        } catch(...) { }
    }
    __finally
    {
        if (pSelForm) delete pSelForm;
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TPRK0010M::BtnFLR_NOClick(TObject *Sender)
{
#if 0
    String sId = PRK_PLCE_NMBR->Text;
    TPrkPlce *pObj = PrkPlceManager->FLists.Find(sId);
    if (pObj == NULL) {
        return;
    }

    TPRK0014M *pSelForm = NULL;
    try
    {
        try {
            pSelForm = new TPRK0014M(this, pObj);

            CMM_ClearGridTableView(pSelForm->TvFlrNo);
            int nRow = 0;
            try
            {
                TcxDataController *pGDC = pSelForm->TvFlrNo->DataController;
                pSelForm->TvFlrNo->BeginUpdate();
                FOR_STL(TPrkPlceFlr*, pObj, FFlrs)
                {
                    nRow = pGDC->AppendRecord();

                    pGDC->Values[nRow][pSelForm->Col02->Index]  = pObj->FLR_NO;
                    pGDC->Values[nRow][pSelForm->Col03->Index]  = pObj->FLR_PRK_CMPRT_CO;
                }
            }
            __finally
            {
                pSelForm->TvFlrNo->EndUpdate();
                pSelForm->TvFlrNo->DataController->GotoFirst();
                pSelForm->TvFlrNo->DataController->FocusedRecordIndex = 0;
            }
            pSelForm->ShowModal();
            if (pSelForm->FSelected)
            {
                FFlrs.Clear();
                try {
                    pSelForm->TvFlrNo->BeginUpdate();
                    for (int ii = 0; ii < pSelForm->TvFlrNo->DataController->RecordCount; ii++)
                    {
                        String sFlrNo = VarToStr(pSelForm->TvFlrNo->DataController->Values[ii][pSelForm->Col02->Index]);
                        String sFlrNoCo = VarToStr(pSelForm->TvFlrNo->DataController->Values[ii][pSelForm->Col03->Index]);

                        TPrkPlceFlr* pFlr = new TPrkPlceFlr();
                        pFlr->PRK_PLCE_NMBR    = sId;
                        pFlr->FLR_NO           = sFlrNo;
                        pFlr->FLR_PRK_CMPRT_CO = sFlrNoCo.ToIntDef(0);

                        FFlrs.Push(pFlr->FLR_NO, pFlr);
                    }
                }
                __finally
                {
                    pSelForm->TvFlrNo->EndUpdate();
                }
            }
        } catch(...) { }
    }
    __finally
    {
        if (pSelForm) delete pSelForm;
    }
#endif
}
//---------------------------------------------------------------------------
void __fastcall TPRK0010M::LoadPrkPlceImage(String APrkNmbr)
{
    String sQry;
    TADOQuery *pADO = NULL;

    try {
        PRK_PLCE_IMAGE_DATA->Picture->Bitmap->FreeImage();
        PRK_PLCE_IMAGE_DATA->Picture->Bitmap = NULL;
    } catch(Exception &e) {}

    sQry = "SELECT PRK_PLCE_IMAGE_TYPE,     \r\n"
           "       PRK_PLCE_IMAGE_DATA      \r\n"
           "  FROM TB_PRK_PLCE              \r\n"
           " WHERE PRK_PLCE_NMBR = :p01     \r\n";

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", APrkNmbr);
            ITSDb_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                //int PRK_PLCE_IMAGE_TYPE = pADO->FieldByName("PRK_PLCE_IMAGE_TYPE")->AsInteger;
                //TMemoryStream *pImgStream = new TMemoryStream();
                //pImgStream->LoadFromStream(pADO->CreateBlobStream(pADO->FieldByName("PRK_PLCE_IMAGE_DATA"), bmRead));

                //AssignPrkPlceImage(PRK_PLCE_IMAGE_TYPE, pImgStream);
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TPRK0010M::LoadPrkPlceImage", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TPRK0010M::LoadPrkPlceImage", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
}
//---------------------------------------------------------------------------
bool __fastcall TPRK0010M::AssignPrkPlceImage(int AImageType, TMemoryStream *AImgStream)
{
    if (AImgStream == NULL || AImgStream->Size <= 30) {
        return false;
    }
    try {
        try {
            if (AImageType == 3) {
                TdxPNGImage *img = new TdxPNGImage;
                img->LoadFromStream(AImgStream);

                PRK_PLCE_IMAGE_DATA->Picture->Bitmap->Assign(img);
                PRK_PLCE_IMAGE_DATA->Picture->Bitmap->PixelFormat = pf24bit;

                SAFE_DELETE(img);
            }
            else
            if (AImageType == 2) {
                TJPEGImage *img = new TJPEGImage();
                img->LoadFromStream(AImgStream);

                PRK_PLCE_IMAGE_DATA->Picture->Bitmap->Assign(img);
                PRK_PLCE_IMAGE_DATA->Picture->Bitmap->PixelFormat = pf24bit;

                SAFE_DELETE(img);
            }
            else
            if (AImageType == 0) {
                Graphics::TBitmap *img = new Graphics::TBitmap;
                img->LoadFromStream(AImgStream);

                PRK_PLCE_IMAGE_DATA->Picture->Bitmap->Assign(img);

                SAFE_DELETE(img);
            }
            else {
                return false;
            }
        } catch(Exception &e) {
            return false;
        }
    } __finally {
        SAFE_DELETE(AImgStream);
    }
    return true;
}
//---------------------------------------------------------------------------
bool __fastcall TPRK0010M::UpdatePrkPlceImage(TPrkPlce *AObj)
{
    String sQry;
    TADOQuery *pADO = NULL;

	TMemoryStream *dataStream = new TMemoryStream();
   	PRK_PLCE_IMAGE_DATA->Picture->Bitmap->SaveToStream(dataStream);
    if (dataStream == NULL || dataStream->Size <= 32) {
        return true;
    }

    sQry = "UPDATE TB_PRK_PLCE                  \r\n"
           "   SET PRK_PLCE_IMAGE_DATA = :p02   \r\n"
           " WHERE PRK_PLCE_NMBR  = :p01        \r\n";

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", AObj->CTLR_NMBR);
    		pADO->Parameters->ParamByName("p02")->LoadFromStream(dataStream, ftBlob);
			pADO->ExecSQL();
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TPRK0010M::UpdatePrkPlceImage", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TPRK0010M::UpdatePrkPlceImage", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        if (dataStream) delete dataStream;
        dataStream = NULL;
    }
    return false;
}
//---------------------------------------------------------------------------
bool __fastcall TPRK0010M::MergePrkPlcePrv(TPrkPlce *AObj)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_PRK_PLCE_PRV L                                        \r\n"
           "USING (SELECT                                                       \r\n"
           "              :p01 AS PRK_PLCE_NMBR,                                \r\n"
           "              :p02 AS VHCTY_AR_SE,                                  \r\n"
           "              :p03 AS VHCTY_AR_PRK_CMPRT_CO,                        \r\n"
           "              0 AS VHCTY_AR_REMNDR_PRK_CMPRT_CO                     \r\n"
           "         FROM DUAL) M                                               \r\n"
           "        ON (L.PRK_PLCE_NMBR = M.PRK_PLCE_NMBR                       \r\n"
           "       AND  L.VHCTY_AR_SE   = M.VHCTY_AR_SE )                       \r\n"
           "WHEN MATCHED THEN                                                   \r\n"
           "   UPDATE SET                                                       \r\n"
           "              L.VHCTY_AR_PRK_CMPRT_CO = M.VHCTY_AR_PRK_CMPRT_CO     \r\n"
           "WHEN NOT MATCHED THEN                                               \r\n"
           "   INSERT (                                                         \r\n"
           "           PRK_PLCE_NMBR,                                           \r\n"
           "           VHCTY_AR_SE,                                             \r\n"
           "           VHCTY_AR_PRK_CMPRT_CO,                                   \r\n"
           "           VHCTY_AR_REMNDR_PRK_CMPRT_CO )                           \r\n"
           "   VALUES (                                                         \r\n"
           "           M.PRK_PLCE_NMBR,                                         \r\n"
           "           M.VHCTY_AR_SE,                                           \r\n"
           "           M.VHCTY_AR_PRK_CMPRT_CO,                                 \r\n"
           "           M.VHCTY_AR_REMNDR_PRK_CMPRT_CO )                         \r\n";

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();

			pADO->SQL->Text = sQry;

            FOR_STL(TPrkPlcePrv*, pPrv, AObj->FPrvs) {
                pADO->Parameters->ParamByName("p01")->Value = AObj->CTLR_NMBR;
                pADO->Parameters->ParamByName("p02")->Value = pPrv->VHCTY_AR_SE;
                pADO->Parameters->ParamByName("p03")->Value = pPrv->VHCTY_AR_PRK_CMPRT_CO;
                pADO->ExecSQL();
            }

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TPRK0010M::MergePrkPlcePrv", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TPRK0010M::MergePrkPlcePrv", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------
bool __fastcall TPRK0010M::MergePrkPlceFlr(TPrkPlce *AObj)
{
    String sQry;
    TADOQuery *pADO = NULL;

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();

            String sDelIds = "";
            FOR_STL(TPrkPlceFlr*, pFlr, FFlrs)
            {
                if (sDelIds == "") {
                    sDelIds = "'" + pFlr->FLR_NO + "'";
                } else {
                    sDelIds += ", '" + pFlr->FLR_NO + "'";
                }
            }
            if (sDelIds == "") {
    sQry = "DELETE TB_PRK_PLCE_FLR       \r\n"
           " WHERE PRK_PLCE_NMBR = :p01  \r\n";
            }
            else {
    sQry = "DELETE TB_PRK_PLCE_FLR       \r\n"
           " WHERE PRK_PLCE_NMBR = :p01  \r\n"
           "   AND FLR_NO NOT IN (" + sDelIds + ")\r\n";
            }
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->CTLR_NMBR;
			pADO->ExecSQL();

    sQry = "MERGE INTO TB_PRK_PLCE_FLR L                            \r\n"
           "USING (SELECT :p01 AS PRK_PLCE_NMBR,                    \r\n"
           "              :p02 AS FLR_NO,                           \r\n"
           "              :p03 AS FLR_PRK_CMPRT_CO                  \r\n"
           "         FROM DUAL) M                                   \r\n"
           "        ON (L.PRK_PLCE_NMBR = M.PRK_PLCE_NMBR           \r\n"
           "        AND L.FLR_NO        = M.FLR_NO)                 \r\n"
           "WHEN MATCHED THEN                                       \r\n"
           "   UPDATE SET L.FLR_PRK_CMPRT_CO = M.FLR_PRK_CMPRT_CO   \r\n"
           "WHEN NOT MATCHED THEN                                   \r\n"
           "   INSERT (PRK_PLCE_NMBR,                               \r\n"
           "           FLR_NO,                                      \r\n"
           "           FLR_PRK_CMPRT_CO,                            \r\n"
           "           FLR_REMNDR_PRK_CMPRT_CO )                    \r\n"
           "   VALUES (M.PRK_PLCE_NMBR,                             \r\n"
           "           M.FLR_NO,                                    \r\n"
           "           M.FLR_PRK_CMPRT_CO,                          \r\n"
           "           0 )                                          \r\n";

			pADO->SQL->Text = sQry;

            FOR_STL(TPrkPlceFlr*, pFlr, FFlrs)
            {
                pADO->Parameters->ParamByName("p01")->Value = pFlr->PRK_PLCE_NMBR;
                pADO->Parameters->ParamByName("p02")->Value = pFlr->FLR_NO;
                pADO->Parameters->ParamByName("p03")->Value = pFlr->FLR_PRK_CMPRT_CO;
                pADO->ExecSQL();
            }

            AObj->FFlrs.Clear();
            FOR_STL(TPrkPlceFlr*, pTmpFlr, FFlrs) {
                TPrkPlceFlr* pFlr = new TPrkPlceFlr();
                pFlr->PRK_PLCE_NMBR           = pTmpFlr->PRK_PLCE_NMBR;
                pFlr->FLR_NO                  = pTmpFlr->FLR_NO;
                pFlr->FLR_PRK_CMPRT_CO        = pTmpFlr->FLR_PRK_CMPRT_CO;

                AObj->FFlrs.Push(pFlr->FLR_NO, pFlr);
            }
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TPRK0010M::MergePrkPlceFlr", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TPRK0010M::MergePrkPlceFlr", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------
bool __fastcall TPRK0010M::MergePrkPlceFlrDev(TPrkPlce *AObj)
{
    String sQry;
    TADOQuery *pADO = NULL;

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();

            String sDelIds = "";
            FOR_STL(TPrkPlceFlr*, pFlr, FFlrs)
            {
                if (sDelIds == "") {
                    sDelIds = "'" + pFlr->FLR_NO + "'";
                } else {
                    sDelIds += ", '" + pFlr->FLR_NO + "'";
                }
            }

    sQry = "MERGE INTO TB_PRK_PLCE_FLR_DEV L                 \r\n"
           "USING (SELECT :p01 AS PRK_PLCE_NMBR,             \r\n"
           "              :p02 AS FLR_NO,                    \r\n"
           "              :p03 AS DEVICE_ID                  \r\n"
           "         FROM DUAL) M                            \r\n"
           "        ON (L.PRK_PLCE_NMBR = M.PRK_PLCE_NMBR    \r\n"
           "        AND L.FLR_NO        = M.FLR_NO           \r\n"
           "        AND L.DEVICE_ID     = M.DEVICE_ID)       \r\n"
           "WHEN NOT MATCHED THEN                            \r\n"
           "   INSERT (PRK_PLCE_NMBR,                        \r\n"
           "           FLR_NO,                               \r\n"
           "           DEVICE_ID )                           \r\n"
           "   VALUES (M.PRK_PLCE_NMBR,                      \r\n"
           "           M.FLR_NO,                             \r\n"
           "           M.DEVICE_ID )                         \r\n";

			pADO->SQL->Text = sQry;

            FOR_STL(TPrkPlceFlr*, pFlr, FFlrs)
            {
                for (int ii = 1; ii <= pFlr->FLR_PRK_CMPRT_CO; ii++) {
                    pADO->Parameters->ParamByName("p01")->Value = pFlr->PRK_PLCE_NMBR;
                    pADO->Parameters->ParamByName("p02")->Value = pFlr->FLR_NO;
                    pADO->Parameters->ParamByName("p03")->Value = ii;
                    pADO->ExecSQL();
                }
            }
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TPRK0010M::MergePrkPlceFlrDev", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TPRK0010M::MergePrkPlceFlrDev", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------
bool __fastcall TPRK0010M::MergePrkPlceOprChrge(TPrkPlce *AObj)
{
    String sQry;
    TADOQuery *pADO = NULL;

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();

    sQry = "DELETE TB_PRK_PLCE_OPR_CHRGE \r\n"
           " WHERE PRK_PLCE_NMBR = :p01  \r\n";

			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->CTLR_NMBR;
			pADO->ExecSQL();


    sQry = "MERGE INTO TB_PRK_PLCE_OPR_CHRGE L                                                \r\n"
           "USING (SELECT                                                                     \r\n"
           "              :p01 AS PRK_PLCE_NMBR,                                              \r\n"
           "              :p02 AS CHRGE,                                                      \r\n"
           "              :p03 AS PARKING_CHRGE_BS_TIME,                                      \r\n"
           "              :p04 AS PARKING_CHRGE_BS_CHRGE,                                     \r\n"
           "              :p05 AS PARKING_CHRGE_ADIT_UNIT_TIME,                               \r\n"
           "              :p06 AS PARKING_CHRGE_ADIT_UNIT_CHRGE                               \r\n"
           "         FROM DUAL) M                                                             \r\n"
           "        ON (L.PRK_PLCE_NMBR = M.PRK_PLCE_NMBR)                                    \r\n"
           "WHEN MATCHED THEN                                                                 \r\n"
           "   UPDATE SET                                                                     \r\n"
           "              L.CHRGE                         = M.CHRGE,                          \r\n"
           "              L.PARKING_CHRGE_BS_TIME         = M.PARKING_CHRGE_BS_TIME,          \r\n"
           "              L.PARKING_CHRGE_BS_CHRGE        = M.PARKING_CHRGE_BS_CHRGE,         \r\n"
           "              L.PARKING_CHRGE_ADIT_UNIT_TIME  = M.PARKING_CHRGE_ADIT_UNIT_TIME,   \r\n"
           "              L.PARKING_CHRGE_ADIT_UNIT_CHRGE = M.PARKING_CHRGE_ADIT_UNIT_CHRGE   \r\n"
           "WHEN NOT MATCHED THEN                                                             \r\n"
           "   INSERT (                                                                       \r\n"
           "           PRK_PLCE_NMBR,                                                         \r\n"
           "           CHRGE,                                                                 \r\n"
           "           PARKING_CHRGE_BS_TIME,                                                 \r\n"
           "           PARKING_CHRGE_BS_CHRGE,                                                \r\n"
           "           PARKING_CHRGE_ADIT_UNIT_TIME,                                          \r\n"
           "           PARKING_CHRGE_ADIT_UNIT_CHRGE )                                        \r\n"
           "   VALUES (                                                                       \r\n"
           "           M.PRK_PLCE_NMBR,                                                       \r\n"
           "           M.CHRGE,                                                               \r\n"
           "           M.PARKING_CHRGE_BS_TIME,                                               \r\n"
           "           M.PARKING_CHRGE_BS_CHRGE,                                              \r\n"
           "           M.PARKING_CHRGE_ADIT_UNIT_TIME,                                        \r\n"
           "           M.PARKING_CHRGE_ADIT_UNIT_CHRGE )                                      \r\n";

			pADO->SQL->Text = sQry;

            FOR_STL(TPrkPlceOprChrge*, pChrge, FChrges)
            {
                pADO->Parameters->ParamByName("p01")->Value = pChrge->PRK_PLCE_NMBR;
                pADO->Parameters->ParamByName("p02")->Value = pChrge->CHRGE;
                pADO->Parameters->ParamByName("p03")->Value = pChrge->PARKING_CHRGE_BS_TIME;
                pADO->Parameters->ParamByName("p04")->Value = pChrge->PARKING_CHRGE_BS_CHRGE;
                pADO->Parameters->ParamByName("p05")->Value = pChrge->PARKING_CHRGE_ADIT_UNIT_TIME;
                pADO->Parameters->ParamByName("p06")->Value = pChrge->PARKING_CHRGE_ADIT_UNIT_CHRGE;
                pADO->ExecSQL();
            }

            AObj->FOPR->FChrges.Clear();
            FOR_STL(TPrkPlceOprChrge*, pTmpChrge, FChrges) {
                TPrkPlceOprChrge* pChrge = new TPrkPlceOprChrge();
                pChrge->PRK_PLCE_NMBR                 = pTmpChrge->PRK_PLCE_NMBR;
                pChrge->CHRGE                         = pTmpChrge->CHRGE;
                pChrge->PARKING_CHRGE_BS_TIME         = pTmpChrge->PARKING_CHRGE_BS_TIME;
                pChrge->PARKING_CHRGE_BS_CHRGE        = pTmpChrge->PARKING_CHRGE_BS_CHRGE;
                pChrge->PARKING_CHRGE_ADIT_UNIT_TIME  = pTmpChrge->PARKING_CHRGE_ADIT_UNIT_TIME;
                pChrge->PARKING_CHRGE_ADIT_UNIT_CHRGE = pTmpChrge->PARKING_CHRGE_ADIT_UNIT_CHRGE;

                AObj->FOPR->FChrges.Push(pChrge->CHRGE, pChrge);
            }
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TPRK0010M::MergePrkPlceOprChrge", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TPRK0010M::MergePrkPlceOprChrge", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------
bool __fastcall TPRK0010M::MergePrkPlceOprSched(TPrkPlce *AObj)
{
    String sQry;
    TADOQuery *pADO = NULL;

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();

    sQry = "DELETE TB_PRK_PLCE_OPR_SCHED \r\n"
           " WHERE PRK_PLCE_NMBR = :p01  \r\n";

			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->CTLR_NMBR;
			pADO->ExecSQL();


    sQry = "MERGE INTO TB_PRK_PLCE_OPR_SCHED L                                \r\n"
           "USING (SELECT                                                     \r\n"
           "              :p01 AS PRK_PLCE_NMBR,                              \r\n"
           "              :p02 AS SCHED,                                      \r\n"
           "              :p03 AS OPERTN_START_TIME,                          \r\n"
           "              :p04 AS OPERTN_END_TIME,                            \r\n"
           "              :p05 AS PARKING_CHRGE_LEVY_AT                       \r\n"
           "         FROM DUAL) M                                             \r\n"
           "        ON (L.PRK_PLCE_NMBR = M.PRK_PLCE_NMBR)                    \r\n"
           "WHEN MATCHED THEN                                                 \r\n"
           "   UPDATE SET                                                     \r\n"
           "              L.SCHED                 = M.SCHED,                  \r\n"
           "              L.OPERTN_START_TIME     = M.OPERTN_START_TIME,      \r\n"
           "              L.OPERTN_END_TIME       = M.OPERTN_END_TIME,        \r\n"
           "              L.PARKING_CHRGE_LEVY_AT = M.PARKING_CHRGE_LEVY_AT   \r\n"
           "WHEN NOT MATCHED THEN                                             \r\n"
           "   INSERT (                                                       \r\n"
           "           PRK_PLCE_NMBR,                                         \r\n"
           "           SCHED,                                                 \r\n"
           "           OPERTN_START_TIME,                                     \r\n"
           "           OPERTN_END_TIME,                                       \r\n"
           "           PARKING_CHRGE_LEVY_AT )                                \r\n"
           "   VALUES (                                                       \r\n"
           "           M.PRK_PLCE_NMBR,                                       \r\n"
           "           M.SCHED,                                               \r\n"
           "           M.OPERTN_START_TIME,                                   \r\n"
           "           M.OPERTN_END_TIME,                                     \r\n"
           "           M.PARKING_CHRGE_LEVY_AT )                              \r\n";

			pADO->SQL->Text = sQry;

            FOR_STL(TPrkPlceOprSched*, pSched, FScheds)
            {
                pADO->Parameters->ParamByName("p01")->Value = pSched->PRK_PLCE_NMBR;
                pADO->Parameters->ParamByName("p02")->Value = pSched->SCHED;
                pADO->Parameters->ParamByName("p03")->Value = pSched->OPERTN_START_TIME;
                pADO->Parameters->ParamByName("p04")->Value = pSched->OPERTN_END_TIME;
                pADO->Parameters->ParamByName("p05")->Value = pSched->PARKING_CHRGE_LEVY_AT;
                pADO->ExecSQL();
            }

            AObj->FOPR->FScheds.Clear();
            FOR_STL(TPrkPlceOprSched*, pTmpSched, FScheds) {
                TPrkPlceOprSched* pSched = new TPrkPlceOprSched();
                pSched->PRK_PLCE_NMBR         = pTmpSched->PRK_PLCE_NMBR;
                pSched->SCHED                 = pTmpSched->SCHED;
                pSched->OPERTN_START_TIME     = pTmpSched->OPERTN_START_TIME;
                pSched->OPERTN_END_TIME       = pTmpSched->OPERTN_END_TIME;
                pSched->PARKING_CHRGE_LEVY_AT = pTmpSched->PARKING_CHRGE_LEVY_AT;

                AObj->FOPR->FScheds.Push(pSched->SCHED, pSched);
            }
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TPRK0010M::MergePrkPlceOprSched", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TPRK0010M::MergePrkPlceOprSched", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

