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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "WindowMsgF.h"
#include "AppGlobalF.h"
#include "ITSLangTransF.h"
#include "ITS_OPLibF.h"
#pragma hdrstop

#include "FRAME_IncidentRegistF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxCalendar"
#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 "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSplitter"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"
TFRAMEIncidentRegist *FRAMEIncidentRegist;
//---------------------------------------------------------------------------
__fastcall TFRAMEIncidentRegist::TFRAMEIncidentRegist(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load((TForm*)this);

    FTitle = Caption;//"신규 돌발 등록";

#if 1
    CxList2->Visible = false;
    cxTabSheet2->TabVisible = false;
    cxTabSheet2->Visible = false;
    cxPageControl1->Properties->HideTabs = true;
#endif
    cxPageControl1->Properties->ActivePage = cxTabSheet1;

    FIncidentId = "";
    FOcrrType   = 0;
    FInsert     = false;

    //m_pGDC = TvList->DataController;
    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<현재 등록된 돌발정보가 없습니다>";

    FCodeIST  = ItsCodeManager->FLists.Find("IST");  //돌발상황 원본 코드 : IST
    FCodeLTCU = ItsCodeManager->FLists.Find("LTCU");//LTCU"); //소통 등급 코드 : LTCU
    FCodeICM  = ItsCodeManager->FLists.Find("ICM");  //돌발상황 확인 방법 코드 : ICM
    FCodeIDTU = ItsCodeManager->FLists.Find("IDTU"); //돌발상황 유형 분류 코드 : IDTU

    FCodeACTU = ItsCodeManager->FLists.Find("ACTU"); //돌발상황 유형 코드 : ACTU, CRTU, EVTU, WETU 중 하나
    FCodeCRTU = ItsCodeManager->FLists.Find("CRTU"); //돌발상황 유형 코드 : ACTU, CRTU, EVTU, WETU 중 하나
    FCodeEVTU = ItsCodeManager->FLists.Find("EVTU"); //돌발상황 유형 코드 : ACTU, CRTU, EVTU, WETU 중 하나
    FCodeWETU = ItsCodeManager->FLists.Find("WETU"); //돌발상황 유형 코드 : ACTU, CRTU, EVTU, WETU 중 하나
    FCodeCNTU = ItsCodeManager->FLists.Find("CNTU"); //돌발상황 유형 코드 : ACTU, CRTU, EVTU, WETU 중 하나
    FCodeCATU = ItsCodeManager->FLists.Find("CATU"); //돌발상황 유형 코드 : ACTU, CRTU, EVTU, WETU 중 하나

    FCodeSRLU = ItsCodeManager->FLists.Find("SRLU"); //돌발상황 심각도 코드 : SRLU
    FCodePRTU = ItsCodeManager->FLists.Find("PRTU"); //통행 제한 유형 코드 : PRTU
    FCodeRSL  = ItsCodeManager->FLists.Find("RSL");  //대응 수준 코드 : RSL
    FCodeISS  = ItsCodeManager->FLists.Find("ISS");  //돌발상황 진행 단계 코드 : ISS
    FCodeICM  = ItsCodeManager->FLists.Find("ICM");  //돌발상황 종료 확인 방법 코드 : ICM
    FCodeDFW  = ItsCodeManager->FLists.Find("DFW");  //기상 코드 : DFW
    FCodeVIT  = ItsCodeManager->FLists.Find("VIT");  //VMS 돌발 유형 코드 : VIT
    FCodeIDT  = ItsCodeManager->FLists.Find("IDT");  //VMS 돌발 유형 세부 코드 : IDT
    FCodeEDT  = ItsCodeManager->FLists.Find("EDT");  //VMS 돌발 유형 세부 코드 : EDT

    InitForm();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEIncidentRegist::InitForm()
{
	DtINCD_CNFM_DT->Date = Now();
	DtINCD_STRT_DT->Date = Now();
	DtINCD_END_PRAR_DT->Date = Now() + ( 1 / 24 );

    CbINCD_ORGN_CD->Properties->Items->Clear();
	if (FCodeIST)
	{
		FOR_STL(TItsSubCode *, pSubCode, FCodeIST->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			CbINCD_ORGN_CD->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
    CbINCD_ORGN_CD->ItemIndex = 0;

    CbCMTR_GRAD_CD->Properties->Items->Clear();
	if (FCodeLTCU)
	{
		FOR_STL(TItsSubCode *, pSubCode, FCodeLTCU->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
		    CbCMTR_GRAD_CD->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
    CbCMTR_GRAD_CD->ItemIndex = 0;

    CbINCD_CNFM_MTHD_CD->Properties->Items->Clear();
	if (FCodeICM)
	{
		FOR_STL(TItsSubCode *, pSubCode, FCodeICM->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
		    CbINCD_CNFM_MTHD_CD->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
    CbINCD_CNFM_MTHD_CD->ItemIndex = 0;

    CbINCD_SERN_CD->Properties->Items->Clear();
	if (FCodeSRLU)
	{
		FOR_STL(TItsSubCode *, pSubCode, FCodeSRLU->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
		    CbINCD_SERN_CD->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
    CbINCD_SERN_CD->ItemIndex = 0;

    CbINCD_TYPE_CLSF_CD->Properties->Items->Clear();
	if (FCodeIDTU)
	{
		FOR_STL(TItsSubCode *, pSubCode, FCodeIDTU->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
		    CbINCD_TYPE_CLSF_CD->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
    CbINCD_TYPE_CLSF_CD->ItemIndex = 0;
    CbINCD_TYPE_CLSF_CDClick((TObject *)CbINCD_TYPE_CLSF_CD);
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEIncidentRegist::UpdateList()
{
    TItsSubCode *pSubCode;

    CMM_ClearGridTableView(TvList);

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

        try
        {
            ItsIncidentManager->FLists.Lock();
            FOR_STL(TItsIncident *, pObj, ItsIncidentManager->FLists)
            {
                if (pObj->INCD_PRGR_STEP_CD != "ISS1") continue;
                if (pObj->INCD_ORGN_CD != "IST5" && pObj->INCD_ORGN_CD != "IST3") continue; //IST5:도로교통공단,  IST3:연계

                nRow = pGDC->AppendRecord();

                String sIST  = "";
                String sLTCU = "";
                String sICM  = "";
                String sSRLU = "";
                String sIDTU = "";
                String sACTU = "";
                String sISS  = "";
                String sDFW  = "";
                String sRSL  = "";
                String sPRTU = "";

                if (pObj->INCD_ORGN_CD      != "") sIST  = " [" + pObj->INCD_ORGN_CD + "] ";
                if (pObj->CMTR_GRAD_CD      != "") sLTCU = " [" + pObj->CMTR_GRAD_CD + "] ";
                if (pObj->INCD_CNFM_MTHD_CD != "") sICM  = " [" + pObj->INCD_CNFM_MTHD_CD + "] ";
                if (pObj->INCD_SERN_CD      != "") sSRLU = " [" + pObj->INCD_SERN_CD + "] ";
                if (pObj->INCD_TYPE_CLSF_CD != "") sIDTU = " [" + pObj->INCD_TYPE_CLSF_CD + "] ";
                if (pObj->INCD_TYPE_CD      != "") sACTU = " [" + pObj->INCD_TYPE_CD + "] ";
                if (pObj->INCD_PRGR_STEP_CD != "") sISS  = " [" + pObj->INCD_PRGR_STEP_CD + "] ";
                if (pObj->WTHR_CD           != "") sDFW  = " [" + pObj->WTHR_CD + "] ";
                if (pObj->RESP_LVL_CD       != "") sRSL  = " [" + pObj->RESP_LVL_CD + "] ";
                if (pObj->TRVL_LMNT_TYPE_CD != "") sPRTU = " [" + pObj->TRVL_LMNT_TYPE_CD + "] ";

                if (FCodeIST)
                {
                    pSubCode = FCodeIST->FSubLists.Find(pObj->INCD_ORGN_CD);
                    if (pSubCode) sIST = sIST + pSubCode->CMMN_CD_KOR_NM;
                }
                if (FCodeLTCU)
                {
                    pSubCode = FCodeLTCU->FSubLists.Find(pObj->CMTR_GRAD_CD);
                    if (pSubCode) sLTCU = sLTCU + pSubCode->CMMN_CD_KOR_NM;
                }
                if (FCodeICM)
                {
                    pSubCode = FCodeICM->FSubLists.Find(pObj->INCD_CNFM_MTHD_CD);
                    if (pSubCode) sICM = sICM + pSubCode->CMMN_CD_KOR_NM;
                }
                if (FCodeSRLU)
                {
                    pSubCode = FCodeSRLU->FSubLists.Find(pObj->INCD_SERN_CD);
                    if (pSubCode) sSRLU = sSRLU + pSubCode->CMMN_CD_KOR_NM;
                }
                if (FCodeIDTU)
                {
                    pSubCode = FCodeIDTU->FSubLists.Find(pObj->INCD_TYPE_CLSF_CD);
                    if (pSubCode) sIDTU = sIDTU + pSubCode->CMMN_CD_KOR_NM;
                }

                if (!pObj->INCD_TYPE_CLSF_CD.IsEmpty() && !pObj->INCD_TYPE_CD.IsEmpty())
                {
                    if (pObj->INCD_TYPE_CLSF_CD == "1")
                    {
                        if (FCodeACTU)
                        {
                            pSubCode = FCodeACTU->FSubLists.Find(pObj->INCD_TYPE_CD);
                            if (pSubCode) sACTU = sACTU + pSubCode->CMMN_CD_KOR_NM;
                        }
                    }
                    else if (pObj->INCD_TYPE_CLSF_CD == "2")
                    {
                        if (FCodeCRTU)
                        {
                            pSubCode = FCodeCRTU->FSubLists.Find(pObj->INCD_TYPE_CD);
                            if (pSubCode) sACTU = sACTU + pSubCode->CMMN_CD_KOR_NM;
                        }
                    }
                    else if (pObj->INCD_TYPE_CLSF_CD == "3")
                    {
                        if (FCodeEVTU)
                        {
                            pSubCode = FCodeEVTU->FSubLists.Find(pObj->INCD_TYPE_CD);
                            if (pSubCode) sACTU = sACTU + pSubCode->CMMN_CD_KOR_NM;
                        }
                    }
                    else if (pObj->INCD_TYPE_CLSF_CD == "4")
                    {
                        if (FCodeWETU)
                        {
                            pSubCode = FCodeWETU->FSubLists.Find(pObj->INCD_TYPE_CD);
                            if (pSubCode) sACTU = sACTU + pSubCode->CMMN_CD_KOR_NM;
                        }
                    }
                    else if (pObj->INCD_TYPE_CLSF_CD == "5")
                    {
                        if (FCodeCNTU)
                        {
                            pSubCode = FCodeCNTU->FSubLists.Find(pObj->INCD_TYPE_CD);
                            if (pSubCode) sACTU = sACTU + pSubCode->CMMN_CD_KOR_NM;
                        }
                    }
                    else if (pObj->INCD_TYPE_CLSF_CD == "6")
                    {
                        if (FCodeCATU)
                        {
                            pSubCode = FCodeCATU->FSubLists.Find(pObj->INCD_TYPE_CD);
                            if (pSubCode) sACTU = sACTU + pSubCode->CMMN_CD_KOR_NM;
                        }
                    }
                }

                if (FCodeISS)
                {
                    pSubCode = FCodeISS->FSubLists.Find(pObj->INCD_PRGR_STEP_CD);
                    if (pSubCode) sISS = sISS + pSubCode->CMMN_CD_KOR_NM;
                }

                if (FCodeDFW)
                {
                    pSubCode = FCodeISS->FSubLists.Find(pObj->WTHR_CD);
                    if (pSubCode) sDFW = sDFW + pSubCode->CMMN_CD_KOR_NM;
                }

                if (FCodeRSL)
                {
                    pSubCode = FCodeRSL->FSubLists.Find(pObj->RESP_LVL_CD);
                    if (pSubCode) sRSL = sRSL + pSubCode->CMMN_CD_KOR_NM;
                }

                if (FCodePRTU)
                {
                    pSubCode = FCodePRTU->FSubLists.Find(pObj->TRVL_LMNT_TYPE_CD);
                    if (pSubCode) sPRTU = sPRTU + pSubCode->CMMN_CD_KOR_NM;
                }

                pGDC->Values[nRow][Column01->Index] = pObj->INCD_OCRR_ID;                //돌발상황 발생 ID

                pGDC->Values[nRow][Column02->Index] = ITSUtil_StrToDateTime(pObj->RGST_DT);                     //등록 일시

                pGDC->Values[nRow][Column03->Index] = sISS;//pObj->INCD_PRGR_STEP_CD;    //돌발상황 진행 단계 코드 : ISS
                pGDC->Values[nRow][Column04->Index] = sIST;//pObj->INCD_ORGN_CD;         //돌발상황 원본 코드 : IST
                pGDC->Values[nRow][Column05->Index] = pObj->OCRR_LCTN_NM;                //발생 위치 명
                pGDC->Values[nRow][Column06->Index] = pObj->LINK_ID;                     //돌발발생 LINK ID
                pGDC->Values[nRow][Column07->Index] = pObj->X_CRDN;                      //X 좌표
                pGDC->Values[nRow][Column08->Index] = pObj->Y_CRDN;                      //Y 좌표
                pGDC->Values[nRow][Column09->Index] = pObj->INCD_EXPL;                   //돌발상황 설명
                pGDC->Values[nRow][Column10->Index] = sLTCU;//pObj->CMTR_GRAD_CD;        //소통 등급 코드 : LTCU
                pGDC->Values[nRow][Column11->Index] = pObj->IFMR_NM;                     //제보자 명
                pGDC->Values[nRow][Column12->Index] = sICM;//pObj->INCD_CNFM_MTHD_CD;    //돌발상황 확인 방법 코드 : ICM
                pGDC->Values[nRow][Column13->Index] = sSRLU;//pObj->INCD_SERN_CD;        //돌발상황 심각도 코드 : SRLU
                pGDC->Values[nRow][Column14->Index] = pObj->INCD_TITL;                   //돌발상황 제목
                pGDC->Values[nRow][Column15->Index] = sIDTU;//pObj->INCD_TYPE_CLSF_CD;   //돌발상황 유형 분류 코드 : IDTU
                pGDC->Values[nRow][Column16->Index] = sACTU;//pObj->INCD_TYPE_CD;        //돌발상황 유형 코드 : ACTU, CRTU, EVTU, WETU 중 하나

                pGDC->Values[nRow][Column17->Index] = ITSUtil_StrToDateTime(pObj->INCD_STRT_DT);                //돌발상황 시작 일시
                pGDC->Values[nRow][Column18->Index] = ITSUtil_StrToDateTime(pObj->INCD_END_PRAR_DT);            //돌발상황 종료 예정 일시

                pGDC->Values[nRow][Column19->Index] = pObj->INCD_OCRR_LANE;              //돌발상황 발생 차로
                pGDC->Values[nRow][Column20->Index] = pObj->INCD_CLSR_LANE;              //돌발상황 차단 차로
                pGDC->Values[nRow][Column21->Index] = pObj->DELY_LNGT;                   //지체 길이
                pGDC->Values[nRow][Column22->Index] = pObj->INCD_LNGT;                   //돌발상황 길이
                pGDC->Values[nRow][Column23->Index] = pObj->SMLT_VHCL_UNUM;              //소형 차량 대수
                pGDC->Values[nRow][Column24->Index] = pObj->MDLT_VHCL_UNUM;              //중대형 차량 대수
                pGDC->Values[nRow][Column25->Index] = pObj->INJR_PNUM;                   //부상자 인원수
                pGDC->Values[nRow][Column26->Index] = pObj->FTLT_PNUM;                   //사망자 인원수
                pGDC->Values[nRow][Column27->Index] = sDFW;//pObj->WTHR_CD;              //기상 코드 : DFW
                pGDC->Values[nRow][Column28->Index] = sRSL;//pObj->RESP_LVL_CD;          //대응 수준 코드 : RSL
                pGDC->Values[nRow][Column29->Index] = pObj->UTIS_CNCT_YN;                //UTIS 연계 여부
                pGDC->Values[nRow][Column30->Index] = pObj->TRUB_VHCL_YN;                //고장 차량 여부
                pGDC->Values[nRow][Column31->Index] = pObj->FALT_YN;                     //낙하물 여부
                pGDC->Values[nRow][Column32->Index] = pObj->FRWT_YN;                     //화재위험물 여부
                pGDC->Values[nRow][Column33->Index] = sPRTU;//pObj->TRVL_LMNT_TYPE_CD;   //통행 제한 유형 코드 : PRTU
                pGDC->Values[nRow][Column34->Index] = pObj->VMS_DSPL_YN;                 //VMS 표출 여부
                pGDC->Values[nRow][Column35->Index] = ITSUtil_StrToDateTime(pObj->INCD_CNFM_DT);                 //VMS 표출 여부

                pGDC->Values[nRow][Column99->Index] = (int)pObj;
            }
        }
        __finally
        {
            ItsIncidentManager->FLists.UnLock();
        }
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

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

void __fastcall TFRAMEIncidentRegist::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    //String            sTitle= "UTIS(CNS) 돌발정보";
    String            sTitle= lblFileName->Caption;//"외부연계기관 돌발정보";

    if (cxPageControl1->Properties->ActivePage != cxTabSheet1)
    {
        pGrid = CxList2;
        pView = TvList2;
        sTitle= lblFileName1->Caption;//"자동감지 돌발정보";
    }
    CMM_ExportToExcelFile(sTitle, pGrid, pView, (TForm*)this);
#if 0
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= "등록된 돌발정보";

    CMM_ExportToExcelFile(sTitle, pGrid, pView, (TForm*)this);
#endif
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEIncidentRegist::CbINCD_TYPE_CLSF_CDClick(TObject *Sender)
{
	if (CbINCD_TYPE_CLSF_CD->ItemIndex < 0) return;

	CbINCD_TYPE_CD->Properties->Items->Clear();

    String sCode = ITSUtil_GetCode(CbINCD_TYPE_CLSF_CD->Text);
	if (sCode == "1")
	{
		if (FCodeACTU)
		{
			FOR_STL(TItsSubCode *, pSubCode, FCodeACTU->FSubLists)
			{
				if (pSubCode->USE_YN == "N") continue;
				CbINCD_TYPE_CD->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
			}
		}
	}
	else
    if (sCode == "2")
	{
		if (FCodeCRTU)
		{
			FOR_STL(TItsSubCode *, pSubCode, FCodeCRTU->FSubLists)
			{
				if (pSubCode->USE_YN == "N") continue;
				CbINCD_TYPE_CD->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
			}
		}
	}
	else
    if (sCode == "3")
	{
		if (FCodeEVTU)
		{
			FOR_STL(TItsSubCode *, pSubCode, FCodeEVTU->FSubLists)
			{
				if (pSubCode->USE_YN == "N") continue;
				CbINCD_TYPE_CD->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
			}
		}
	}
	else
    if (sCode == "4")
	{
		if (FCodeWETU)
		{
			FOR_STL(TItsSubCode *, pSubCode, FCodeWETU->FSubLists)
			{
				if (pSubCode->USE_YN == "N") continue;
				CbINCD_TYPE_CD->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
			}
		}
	}
	else
    if (sCode == "5")
	{
		if (FCodeCNTU)
		{
			FOR_STL(TItsSubCode *, pSubCode, FCodeCNTU->FSubLists)
			{
				if (pSubCode->USE_YN == "N") continue;
				CbINCD_TYPE_CD->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
			}
		}
	}
	else
    if (sCode == "6")
	{
		if (FCodeCATU)
		{
			FOR_STL(TItsSubCode *, pSubCode, FCodeCATU->FSubLists)
			{
				if (pSubCode->USE_YN == "N") continue;
				CbINCD_TYPE_CD->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
			}
		}
	}
	CbINCD_TYPE_CD->ItemIndex = 0;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEIncidentRegist::TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
    InitScreen();

    TcxDataController *pGDC = TvList->DataController;
	int nRow = pGDC->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

    TItsSubCode *pSubCode;

    //int nMemPtr = pGDC->Values[nRow][Column99->Index];
    //TItsIncident *pObj = (TItsIncident *)nMemPtr;

    try
    {
        String sINCD_OCRR_ID = pGDC->Values[nRow][Column01->Index];

        TItsIncident *pObj = ItsIncidentManager->FLists.Find(sINCD_OCRR_ID);
        if (!pObj) return;

        String sIST  = "";
        if (pObj->INCD_ORGN_CD != "") sIST = " [" + pObj->INCD_ORGN_CD + "] ";
        String sLTCU= "";
        if (pObj->CMTR_GRAD_CD != "") sLTCU = " [" + pObj->CMTR_GRAD_CD + "] ";
        String sICM = "";
        if (pObj->INCD_CNFM_MTHD_CD != "") sICM  = " [" + pObj->INCD_CNFM_MTHD_CD + "] ";
        String sSRLU= "";
        if (pObj->INCD_SERN_CD != "") sSRLU = " [" + pObj->INCD_SERN_CD + "] ";
        String sIDTU = "";
        if (pObj->INCD_TYPE_CLSF_CD != "") sIDTU = " [" + pObj->INCD_TYPE_CLSF_CD + "] ";
        String sACTU = "";
        if (pObj->INCD_TYPE_CD != "") sACTU = " [" + pObj->INCD_TYPE_CD + "] ";
        String sISS  = "";
        if (pObj->INCD_PRGR_STEP_CD != "") sISS = " [" + pObj->INCD_PRGR_STEP_CD + "] ";
        String sDFW  = "";
        if (pObj->WTHR_CD != "") sDFW = " [" + pObj->WTHR_CD + "] ";
        String sRSL  = "";
        if (pObj->RESP_LVL_CD != "") sRSL = " [" + pObj->RESP_LVL_CD + "] ";
        String sPRTU = "";
        if (pObj->TRVL_LMNT_TYPE_CD != "") sPRTU = " [" + pObj->TRVL_LMNT_TYPE_CD + "] ";

        if (FCodeIST)
        {
            pSubCode = FCodeIST->FSubLists.Find(pObj->INCD_ORGN_CD);
            if (pSubCode) sIST = sIST + pSubCode->CMMN_CD_KOR_NM;
        }
        if (FCodeLTCU)
        {
            pSubCode = FCodeLTCU->FSubLists.Find(pObj->CMTR_GRAD_CD);
            if (pSubCode) sLTCU = sLTCU + pSubCode->CMMN_CD_KOR_NM;
        }
        if (FCodeICM)
        {
            pSubCode = FCodeICM->FSubLists.Find(pObj->INCD_CNFM_MTHD_CD);
            if (pSubCode) sICM = sICM + pSubCode->CMMN_CD_KOR_NM;
        }
        if (FCodeSRLU)
        {
            pSubCode = FCodeSRLU->FSubLists.Find(pObj->INCD_SERN_CD);
            if (pSubCode) sSRLU = sSRLU + pSubCode->CMMN_CD_KOR_NM;
        }
        if (FCodeIDTU)
        {
            pSubCode = FCodeIDTU->FSubLists.Find(pObj->INCD_TYPE_CLSF_CD);
            if (pSubCode) sIDTU = sIDTU + pSubCode->CMMN_CD_KOR_NM;
        }

        if (!pObj->INCD_TYPE_CLSF_CD.IsEmpty() && !pObj->INCD_TYPE_CD.IsEmpty())
        {
            if (pObj->INCD_TYPE_CLSF_CD == "1")
            {
                if (FCodeACTU)
                {
                    pSubCode = FCodeACTU->FSubLists.Find(pObj->INCD_TYPE_CD);
                    if (pSubCode) sACTU = sACTU + pSubCode->CMMN_CD_KOR_NM;
                }
            }
            else if (pObj->INCD_TYPE_CLSF_CD == "2")
            {
                if (FCodeCRTU)
                {
                    pSubCode = FCodeCRTU->FSubLists.Find(pObj->INCD_TYPE_CD);
                    if (pSubCode) sACTU = sACTU + pSubCode->CMMN_CD_KOR_NM;
                }
            }
            else if (pObj->INCD_TYPE_CLSF_CD == "3")
            {
                if (FCodeEVTU)
                {
                    pSubCode = FCodeEVTU->FSubLists.Find(pObj->INCD_TYPE_CD);
                    if (pSubCode) sACTU = sACTU + pSubCode->CMMN_CD_KOR_NM;
                }
            }
            else if (pObj->INCD_TYPE_CLSF_CD == "4")
            {
                if (FCodeWETU)
                {
                    pSubCode = FCodeWETU->FSubLists.Find(pObj->INCD_TYPE_CD);
                    if (pSubCode) sACTU = sACTU + pSubCode->CMMN_CD_KOR_NM;
                }
            }
            else if (pObj->INCD_TYPE_CLSF_CD == "5")
            {
                if (FCodeCNTU)
                {
                    pSubCode = FCodeCNTU->FSubLists.Find(pObj->INCD_TYPE_CD);
                    if (pSubCode) sACTU = sACTU + pSubCode->CMMN_CD_KOR_NM;
                }
            }
            else if (pObj->INCD_TYPE_CLSF_CD == "6")
            {
                if (FCodeCATU)
                {
                    pSubCode = FCodeCATU->FSubLists.Find(pObj->INCD_TYPE_CD);
                    if (pSubCode) sACTU = sACTU + pSubCode->CMMN_CD_KOR_NM;
                }
            }
        }

        if (FCodeISS)
        {
            pSubCode = FCodeISS->FSubLists.Find(pObj->INCD_PRGR_STEP_CD);
            if (pSubCode) sISS = sISS + pSubCode->CMMN_CD_KOR_NM;
        }

        if (FCodeDFW)
        {
            pSubCode = FCodeISS->FSubLists.Find(pObj->WTHR_CD);
            if (pSubCode) sDFW = sDFW + pSubCode->CMMN_CD_KOR_NM;
        }

        if (FCodeRSL)
        {
            pSubCode = FCodeRSL->FSubLists.Find(pObj->RESP_LVL_CD);
            if (pSubCode) sRSL = sRSL + pSubCode->CMMN_CD_KOR_NM;
        }

        if (FCodePRTU)
        {
            pSubCode = FCodePRTU->FSubLists.Find(pObj->TRVL_LMNT_TYPE_CD);
            if (pSubCode) sPRTU = sPRTU + pSubCode->CMMN_CD_KOR_NM;
        }
        EdINCD_TITL->Text           = pObj->INCD_TITL;
        EdINCD_OCRR_ID->Text		= pObj->INCD_OCRR_ID;
        EdRGST_DT->Text			    = ITSUtil_StrToDateTime(pObj->RGST_DT);
        CbINCD_ORGN_CD->Text		= sIST;
        EdOCRR_LCTN_NM->Text		= pObj->OCRR_LCTN_NM;
        EdINCD_EXPL->Text			= pObj->INCD_EXPL;
        CbCMTR_GRAD_CD->Text		= sLTCU;
        EdIFMR_NM->Text			    = pObj->IFMR_NM;
        CbINCD_CNFM_MTHD_CD->Text	= sICM;
        CbINCD_SERN_CD->Text		= sSRLU;
        CbINCD_TYPE_CLSF_CD->Text	= sIDTU;
        CbINCD_TYPE_CLSF_CDClick((TObject *)CbINCD_TYPE_CLSF_CD);
        CbINCD_TYPE_CD->Text		= sACTU;
        DtINCD_CNFM_DT->Date		= ITSUtil_StrToDateTime(pObj->INCD_CNFM_DT);
        DtINCD_STRT_DT->Date		= ITSUtil_StrToDateTime(pObj->INCD_STRT_DT);
        DtINCD_END_PRAR_DT->Date	= ITSUtil_StrToDateTime(pObj->INCD_END_PRAR_DT);

        FInsert = false;
        BtnMissInfo->Enabled = true;
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEIncidentRegist::BtnSaveClick(TObject *Sender)
{
    String sIncidentId = EdINCD_OCRR_ID->Text.Trim();

	if (EdOCRR_LCTN_NM->Text.Trim().IsEmpty())
	{
        Application->MessageBox(L"★[발생위치설명] 항목은 필수 입력 항목입니다.\r\입력후 진행해 주세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        if (EdOCRR_LCTN_NM->CanFocus()) EdOCRR_LCTN_NM->SetFocus();
		return;
	}
	if (EdINCD_EXPL->Text.Trim().IsEmpty())
	{
        Application->MessageBox(L"★[돌발상황설명] 항목은 필수 입력 항목입니다.\r\입력후 진행해 주세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        if (EdINCD_EXPL->CanFocus()) EdINCD_EXPL->SetFocus();
		return;
	}
	if (DtINCD_CNFM_DT->EditValue.IsEmpty())
	{
        Application->MessageBox(L"★[돌발확인시각] 항목은 필수 입력 항목입니다.\r\입력후 진행해 주세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        if (DtINCD_CNFM_DT->CanFocus()) DtINCD_CNFM_DT->SetFocus();
		return;
	}
	if (EdINCD_TITL->Text.Trim().IsEmpty())
	{
        Application->MessageBox(L"★[돌발제목] 항목은 필수 입력 항목입니다.\r\입력후 진행해 주세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        if (EdINCD_TITL->CanFocus()) EdINCD_TITL->SetFocus();
		return;
	}
	if (EdINCD_OCRR_ID->Text.Trim().IsEmpty())
	{
        Application->MessageBox(L"★[돌발 ID] 항목은 필수 입력 항목입니다.\r\입력후 진행해 주세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        if (EdINCD_OCRR_ID->CanFocus()) EdINCD_OCRR_ID->SetFocus();
		return;
	}

    String sStartTm = DtINCD_STRT_DT->Date.FormatString("YYYYMMDDHHNNSS");
    String sEndTm   = DtINCD_END_PRAR_DT->Date.FormatString("YYYYMMDDHHNNSS");
    if (sStartTm >= sEndTm)
    {
        Application->MessageBox(L"돌발종료예정시각이 돌발시작시각과 같거나 큽니다.\r\n확인 후 진행해 주세요",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        if (DtINCD_STRT_DT->CanFocus()) DtINCD_STRT_DT->SetFocus();
        return;
    }

    if (!FIncidentId.IsEmpty())
    {
        if (FIncidentId == sIncidentId) return;
    }

    if (Application->MessageBox(L"돌발 정보를 확정 하시겠습니까?",
                                FTitle.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

#if 0
    if (!ItsIncidentManager->GetNextIncidentId(CenterId, sIncidentId))
    {
        Application->MessageBox(L"신규 돌발 ID를 조회하지 못하였습니다.\r\n데이터베이스 관리자에게 확인 후에 작업하세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
    }
#endif

    bool bResult;
    if (FInsert)
    {
        bResult = InsertToDb(sIncidentId,
                             ITSUtil_GetCode(CbINCD_ORGN_CD->Text),
                             EdOCRR_LCTN_NM->Text.Trim(),
                             EdINCD_EXPL->Text.Trim(),
                             ITSUtil_GetCode(CbCMTR_GRAD_CD->Text),
                             EdIFMR_NM->Text.Trim(),
                             ITSUtil_GetCode(CbINCD_CNFM_MTHD_CD->Text),
                             ITSUtil_GetCode(CbINCD_SERN_CD->Text),
                             ITSUtil_GetCode(CbINCD_TYPE_CLSF_CD->Text),
                             ITSUtil_GetCode(CbINCD_TYPE_CD->Text),
                             DtINCD_CNFM_DT->Date.FormatString("YYYYMMDDHHNNSS"),
                             DtINCD_STRT_DT->Date.FormatString("YYYYMMDDHHNNSS"),
                             DtINCD_END_PRAR_DT->Date.FormatString("YYYYMMDDHHNNSS"),
                             EdINCD_TITL->Text.Trim());
    }
    else
    {
        bResult = UpdateToDb(sIncidentId,
                             ITSUtil_GetCode(CbINCD_ORGN_CD->Text),
                             EdOCRR_LCTN_NM->Text.Trim(),
                             EdINCD_EXPL->Text.Trim(),
                             ITSUtil_GetCode(CbCMTR_GRAD_CD->Text),
                             EdIFMR_NM->Text.Trim(),
                             ITSUtil_GetCode(CbINCD_CNFM_MTHD_CD->Text),
                             ITSUtil_GetCode(CbINCD_SERN_CD->Text),
                             ITSUtil_GetCode(CbINCD_TYPE_CLSF_CD->Text),
                             ITSUtil_GetCode(CbINCD_TYPE_CD->Text),
                             DtINCD_CNFM_DT->Date.FormatString("YYYYMMDDHHNNSS"),
                             DtINCD_STRT_DT->Date.FormatString("YYYYMMDDHHNNSS"),
                             DtINCD_END_PRAR_DT->Date.FormatString("YYYYMMDDHHNNSS"),
                             EdINCD_TITL->Text.Trim());
    }

    if (bResult)
    {
        if (FOcrrType == 1)
        {
            UpdateAutoIncident(EdLinkId->Text, EdRegDt->Text, "Y");
            ItsIncidentManager->LoadFromAutoIncident();
            ItsIncidentManager->LoadFromDb();
            UpdateAutoList();
        }
        else
        {
            ItsIncidentManager->LoadFromDb();
            UpdateList();
            //CMM_SetGridFocusRow(CxList, TvList, sIncidentId, Column01->Index);
        }
        BtnNewClick((TObject *)BtnNew);
    }

    InitScreen();
}
//---------------------------------------------------------------------------

bool __fastcall TFRAMEIncidentRegist::InsertToDb(String AINCD_OCRR_ID,      //돌발ID
                                                 String AINCD_ORGN_CD,      //돌발상황 원본 코드 : IST
                                                 String AOCRR_LCTN_NM,      //발생 위치 명
                                                 String AINCD_EXPL,         //돌발상황 설명
                                                 String ACMTR_GRAD_CD,      //소통 등급 코드 : LTCU
                                                 String AIFMR_NM,           //제보자 명
                                                 String AINCD_CNFM_MTHD_CD, //돌발상황 확인 방법 코드 : ICM
                                                 String AINCD_SERN_CD,      //돌발상황 심각도 코드 : SRLU
                                                 String AINCD_TYPE_CLSF_CD, //돌발상황 유형 분류 코드 : IDTU
                                                 String AINCD_TYPE_CD,      //돌발상황 유형 코드 : ACTU, CRTU, EVTU, WETU 중 하나
                                                 String AINCD_CNFM_DT,      //돌발상황 확인 일시
                                                 String AINCD_STRT_DT,      //돌발상황 시작 일시
                                                 String AINCD_END_PRAR_DT,  //돌발상황 종료 예정 일시
                                                 String AINCD_TITL)         //돌발제목
{
    String     sQry;
    TADOQuery *pADO = NULL;
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_GetConnection()->BeginTrans();

            sQry = "INSERT INTO TB_INCD_OCRR(INCD_OCRR_ID, RGST_DT, INCD_ORGN_CD, OCRR_LCTN_NM,               \r\n"
                   "                         INCD_EXPL, CMTR_GRAD_CD, IFMR_NM, INCD_CNFM_MTHD_CD,             \r\n"
                   "                         INCD_CNFM_DT, INCD_STRT_DT, INCD_END_PRAR_DT, INCD_TYPE_CLSF_CD, \r\n"
                   "                         INCD_TYPE_CD, INCD_SERN_CD, INCD_PRGR_STEP_CD, INCD_TITL )       \r\n"
                   "                  VALUES(:p01, :p02, :p03, :p04,                                          \r\n"
                   "                         :p05, :p06, :p07, :p08,                                          \r\n"
                   "                         :p09, :p10, :p11, :p12,                                          \r\n"
                   "                         :p13, :p14, 'ISS2', :p15 )                                       \r\n";

            ITSDb_SQLText(pADO, sQry);

            ITSDb_SQLBind(pADO, "p01", AINCD_OCRR_ID);
            ITSDb_SQLBind(pADO, "p02", Now().FormatString("YYYYMMDDHHNNSS"));
            ITSDb_SQLBind(pADO, "p03", AINCD_ORGN_CD);
            ITSDb_SQLBind(pADO, "p04", AOCRR_LCTN_NM);
            ITSDb_SQLBind(pADO, "p05", AINCD_EXPL);
            ITSDb_SQLBind(pADO, "p06", ACMTR_GRAD_CD);
            ITSDb_SQLBind(pADO, "p07", AIFMR_NM);
            ITSDb_SQLBind(pADO, "p08", AINCD_CNFM_MTHD_CD);
            ITSDb_SQLBind(pADO, "p09", AINCD_CNFM_DT);
            ITSDb_SQLBind(pADO, "p10", AINCD_STRT_DT);
            ITSDb_SQLBind(pADO, "p11", AINCD_END_PRAR_DT);
            ITSDb_SQLBind(pADO, "p12", AINCD_TYPE_CLSF_CD);
            ITSDb_SQLBind(pADO, "p13", AINCD_TYPE_CD);
            ITSDb_SQLBind(pADO, "p14", AINCD_SERN_CD);
            ITSDb_SQLBind(pADO, "p15", AINCD_TITL);

            ITSDb_SQLExec(pADO);

            ITSDb_GetConnection()->CommitTrans();

            Application->MessageBox(FrmLang->lblDbSave->Caption.c_str(),//L"돌발 확정 정보를 저장 하였습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

bool __fastcall TFRAMEIncidentRegist::UpdateToDb(String AINCD_OCRR_ID,      //돌발ID
                                                 String AINCD_ORGN_CD,      //돌발상황 원본 코드 : IST
                                                 String AOCRR_LCTN_NM,      //발생 위치 명
                                                 String AINCD_EXPL,         //돌발상황 설명
                                                 String ACMTR_GRAD_CD,      //소통 등급 코드 : LTCU
                                                 String AIFMR_NM,           //제보자 명
                                                 String AINCD_CNFM_MTHD_CD, //돌발상황 확인 방법 코드 : ICM
                                                 String AINCD_SERN_CD,      //돌발상황 심각도 코드 : SRLU
                                                 String AINCD_TYPE_CLSF_CD, //돌발상황 유형 분류 코드 : IDTU
                                                 String AINCD_TYPE_CD,      //돌발상황 유형 코드 : ACTU, CRTU, EVTU, WETU 중 하나
                                                 String AINCD_CNFM_DT,      //돌발상황 확인 일시
                                                 String AINCD_STRT_DT,      //돌발상황 시작 일시
                                                 String AINCD_END_PRAR_DT,  //돌발상황 종료 예정 일시
                                                 String AINCD_TITL)         //돌발제목
{
    String     sQry;
    TADOQuery *pADO = NULL;
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_GetConnection()->BeginTrans();

            sQry = "UPDATE TB_INCD_OCRR                \r\n"
                   "   SET RGST_DT           = :p02,   \r\n"
                   "       INCD_ORGN_CD      = :p03,   \r\n"
                   "       OCRR_LCTN_NM      = :p04,   \r\n"
                   "       INCD_EXPL         = :p05,   \r\n"
                   "       CMTR_GRAD_CD      = :p06,   \r\n"
                   "       IFMR_NM           = :p07,   \r\n"
                   "       INCD_CNFM_MTHD_CD = :p08,   \r\n"
                   "       INCD_CNFM_DT      = :p09,   \r\n"
                   "       INCD_STRT_DT      = :p10,   \r\n"
                   "       INCD_END_PRAR_DT  = :p11,   \r\n"
                   "       INCD_TYPE_CLSF_CD = :p12,   \r\n"
                   "       INCD_TYPE_CD      = :p13,   \r\n"
                   "       INCD_SERN_CD      = :p14,   \r\n"
                   "       INCD_PRGR_STEP_CD = 'ISS2', \r\n"
                   "       INCD_TITL         = :p15    \r\n"
                   " WHERE INCD_OCRR_ID      = :p01    \r\n";

            ITSDb_SQLText(pADO, sQry);

            ITSDb_SQLBind(pADO, "p01", AINCD_OCRR_ID);
            ITSDb_SQLBind(pADO, "p02", Now().FormatString("YYYYMMDDHHNNSS"));
            ITSDb_SQLBind(pADO, "p03", AINCD_ORGN_CD);
            ITSDb_SQLBind(pADO, "p04", AOCRR_LCTN_NM);
            ITSDb_SQLBind(pADO, "p05", AINCD_EXPL);
            ITSDb_SQLBind(pADO, "p06", ACMTR_GRAD_CD);
            ITSDb_SQLBind(pADO, "p07", AIFMR_NM);
            ITSDb_SQLBind(pADO, "p08", AINCD_CNFM_MTHD_CD);
            ITSDb_SQLBind(pADO, "p09", AINCD_CNFM_DT);
            ITSDb_SQLBind(pADO, "p10", AINCD_STRT_DT);
            ITSDb_SQLBind(pADO, "p11", AINCD_END_PRAR_DT);
            ITSDb_SQLBind(pADO, "p12", AINCD_TYPE_CLSF_CD);
            ITSDb_SQLBind(pADO, "p13", AINCD_TYPE_CD);
            ITSDb_SQLBind(pADO, "p14", AINCD_SERN_CD);
            ITSDb_SQLBind(pADO, "p15", AINCD_TITL);

            ITSDb_SQLExec(pADO);

            ITSDb_GetConnection()->CommitTrans();

            Application->MessageBox(FrmLang->lblDbSave->Caption.c_str(),//L"돌발 확정 정보를 저장 하였습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEIncidentRegist::InitScreen()
{
    FInsert = false;
    FOcrrType = 0;
    BtnMissInfo->Enabled = false;

    String sNow = Now().FormatString("YYYYMMDDHHNNSS");

    EdINCD_OCRR_ID->Text				= "";
    EdRGST_DT->Text                     = ITSUtil_StrToDateTime(sNow);
    EdRegDt->Text                       = sNow;
    CbINCD_ORGN_CD->ItemIndex           = 0;
    EdOCRR_LCTN_NM->Text				= "";
    //EdLINK_ID->Text					    = "";
    EdLinkId->Text                      = "";
    //SePosX->Value						= 0;
    //SePosY->Value						= 0;
    EdINCD_EXPL->Text					= "";
    CbCMTR_GRAD_CD->ItemIndex           = 0;
    EdIFMR_NM->Text					    = "";
    CbINCD_CNFM_MTHD_CD->ItemIndex      = 0;
    CbINCD_SERN_CD->ItemIndex           = 0;
    EdINCD_TITL->Text					= "";
    CbINCD_TYPE_CLSF_CD->ItemIndex      = 0;
    CbINCD_TYPE_CD->ItemIndex           = 0;
    DtINCD_CNFM_DT->Date                = Now();
    DtINCD_STRT_DT->Date				= Now();
    DtINCD_END_PRAR_DT->Date			= Now() + ( 1 / 24 );
    //ChkINCD_OCRR_LANE_Extra->Checked	= false;
    //ChkINCD_OCRR_LANE_1->Checked		= false;
    //ChkINCD_OCRR_LANE_2->Checked		= false;
    //ChkINCD_OCRR_LANE_3->Checked		= false;
    //ChkINCD_OCRR_LANE_4->Checked		= false;
    //ChkINCD_OCRR_LANE_5->Checked		= false;
    //ChkINCD_CLSR_LANE_Extra->Checked	= false;
    //ChkINCD_CLSR_LANE_1->Checked		= false;
    //ChkINCD_CLSR_LANE_2->Checked		= false;
    //ChkINCD_CLSR_LANE_3->Checked		= false;
    //ChkINCD_CLSR_LANE_4->Checked		= false;
    //ChkINCD_CLSR_LANE_5->Checked		= false;
    //SeDELY_LNGT->Value					= 0;
    //SeINCD_LNGT->Value					= 0;
    //SeSMLT_VHCL_UNUM->Value			    = 0;
    //SeMDLT_VHCL_UNUM->Value			    = 0;
    //SeINJR_PNUM->Value					= 0;
    //SeFTLT_PNUM->Value					= 0;
    //CbWTHR_CD->Text					    = "";
    //CbRESP_LVL_CD->Text				    = "";
    //ChkUTIS_CNCT_YN->Checked			= false;
    //ChkTRUB_VHCL_YN->Checked			= false;
    //ChkFALT_YN->Checked					= false;
    //ChkFRWT_YN->Checked					= false;
    //CbTRVL_LMNT_TYPE_CD->Text			= "";
    //ChkVMS_DSPL_YN->Checked				= false;
    //EdSTRT_LCTN_NM->Text				= "";
    //EdEND_LCTN_NM->Text				    = "";
    //CbVMS_INCD_TYPE_CD->Text			= "";
    //CbVMS_INCD_DETL_TYPE_CD->Text		= "";
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEIncidentRegist::UpdateAutoList()
{
    ItsIncidentManager->LoadFromVmsIncident();
    ItsIncidentManager->LoadFromAutoIncident();

    CMM_ClearGridTableView(TvList2);
    TcxDataController *pGDC = TvList2->DataController;

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

        try
        {
            ItsIncidentManager->FAutoLists.Lock();
            FOR_STL(TItsAutoIncident *, pObj, ItsIncidentManager->FAutoLists)
            {
                if (pObj->REAL_YN == "Y") continue;

                nRow = pGDC->AppendRecord();

                pGDC->Values[nRow][ColumnA01->Index] = pObj->LINK_ID;                           //링크ID
                pGDC->Values[nRow][ColumnA02->Index] = ITSUtil_StrToDateTime(pObj->DETC_DT);    //검지 일시
                pGDC->Values[nRow][ColumnA03->Index] = pObj->CLCT_SPED_AVRG;                    //수집 속도 평균
                pGDC->Values[nRow][ColumnA04->Index] = pObj->ESTM_SPED_AVRG;                    //예상 속도 평균
                pGDC->Values[nRow][ColumnA05->Index] = pObj->REAL_YN;                           //사실 여부
                pGDC->Values[nRow][ColumnA06->Index] = pObj->LINK_ID;                           //링크ID
                pGDC->Values[nRow][ColumnA07->Index] = pObj->ROAD_NAME;                         //도로명
                pGDC->Values[nRow][ColumnA08->Index] = pObj->STNAME;                            //시점명
                pGDC->Values[nRow][ColumnA09->Index] = pObj->EDNAME;                            //종점명

                pGDC->Values[nRow][ColumnA99->Index] = (int)pObj;
            }
        }
        __finally
        {
            ItsIncidentManager->FAutoLists.UnLock();
        }
    }
    __finally
    {
        TvList2->EndUpdate();
        TvList2->DataController->GotoFirst();
        TvList2->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEIncidentRegist::TvList2CellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
    InitScreen();

    TcxDataController *pGDC = TvList2->DataController;
	int nRow = pGDC->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

    String sIncidentId = "";
    if (!ItsIncidentManager->GetNextIncidentId(CenterId, sIncidentId)) return;

    int nMemPtr = pGDC->Values[nRow][ColumnA99->Index];
    TItsAutoIncident *pObj = (TItsAutoIncident *)nMemPtr;

    if (pObj)
    {                                   //[1100003] 송정IC램프->송정앞
        EdINCD_OCRR_ID->Text        = sIncidentId;
        EdRGST_DT->Text             = ITSUtil_StrToDateTime(pObj->DETC_DT);
        EdRegDt->Text               = pObj->DETC_DT;
        CbINCD_ORGN_CD->Text        = "";
        EdOCRR_LCTN_NM->Text        = pObj->ROAD_NAME;
        EdLinkId->Text              = pObj->LINK_ID;
        //EdLINK_ID->Text             = "[" + pObj->LINK_ID + "] " + pObj->STNAME + "→" + pObj->EDNAME;
        //EdLinkId->Text              = pObj->LINK_ID;
        //if (pObj->X_CRDN.IsEmpty()) pObj->X_CRDN = "0.0";
        //SePosX->Value               = StrToFloat(pObj->X_CRDN);
        //if (pObj->Y_CRDN.IsEmpty()) pObj->Y_CRDN = "0.0";
        //SePosY->Value               = StrToFloat(pObj->Y_CRDN);
        EdINCD_EXPL->Text           = "";
        CbCMTR_GRAD_CD->Text        = "";
        EdIFMR_NM->Text             = "";
        CbINCD_CNFM_MTHD_CD->Text   = "";
        CbINCD_SERN_CD->Text        = "";
        EdINCD_TITL->Text           = "";
        CbINCD_TYPE_CLSF_CD->Text   = "";
        CbINCD_TYPE_CD->Text        = "";
        DtINCD_STRT_DT->Date        = Now();
        DtINCD_END_PRAR_DT->Date    = Now() + 1;

        //ChkVMS_DSPL_YN->Checked     = false;
        //EdSTRT_LCTN_NM->Text        = "";
        //EdEND_LCTN_NM->Text         = "";
        //CbVMS_INCD_TYPE_CD->Text    = "";
        //CbVMS_INCD_DETL_TYPE_CD->Text = "";

        FInsert = true;
        FOcrrType = 1;
        BtnMissInfo->Enabled = true;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEIncidentRegist::BtnNewClick(TObject *Sender)
{
    InitScreen();

    if (BtnNew->Tag == 0)
    {
        String sIncidentId = "";
        if (ItsIncidentManager->GetNextIncidentId(CenterId, sIncidentId))
        {
            cxPageControl1->Enabled = false;
            BtnMissInfo->Enabled = false;
            BtnNew->Tag = 1;
            BtnNew->Caption = "등록취소";

            EdINCD_OCRR_ID->Text = sIncidentId;
            EdLinkId->Text = sIncidentId;
            FInsert = true;
        }
    }
    else
    {
        cxPageControl1->Enabled = true;
        BtnNew->Tag = 0;
        BtnNew->Caption = "신규등록";
        FInsert = false;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEIncidentRegist::BtnMissInfoClick(TObject *Sender)
{
	if (EdINCD_OCRR_ID->Text.IsEmpty())
	{
        Application->MessageBox(L"돌발 오보 판정\r\n돌발 구간을 먼저 선택하세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
	}

    if (Application->MessageBox(L"돌발 오보 판정\r\n현재 돌발 정보를 오보로 판정 하시겠습니까?",
                                FTitle.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

	if (FOcrrType == 1)
	{
        UpdateAutoIncident(EdLinkId->Text, EdRegDt->Text, "X");
		ItsIncidentManager->LoadFromAutoIncident();
		UpdateAutoList();
	}
    else
    {
        TItsIncident *pObj = ItsIncidentManager->FLists.Find(EdINCD_OCRR_ID->Text);
        if (!pObj) return;

        if (UpdateMissIncident(pObj->INCD_OCRR_ID))
        {
            pObj->INCD_PRGR_STEP_CD = "ISS4"; // 오보판정

            UpdateList();
            InitScreen();
        }
    }

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

    Application->ProcessMessages();
    POST_MSG(Application->MainForm->Handle, WM_DATABASE_REFRESH, WP_MSG_02, 0);
}
//---------------------------------------------------------------------------

bool __fastcall TFRAMEIncidentRegist::UpdateMissIncident(String AINCD_OCRR_ID)
{
    String     sQry;
    TADOQuery *pADO = NULL;
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_GetConnection()->BeginTrans();

            sQry = "UPDATE TB_INCD_OCRR                \r\n"
                   "   SET INCD_PRGR_STEP_CD = 'ISS4'  \r\n"
				   " WHERE INCD_OCRR_ID = :p01         \r\n";

            ITSDb_SQLText(pADO, sQry);

            ITSDb_SQLBind(pADO, "p01", AINCD_OCRR_ID);

            ITSDb_SQLExec(pADO);

            ITSDb_GetConnection()->CommitTrans();

            Application->MessageBox(FrmLang->lblDbSave->Caption.c_str(),//L"돌발정보를 오보 판정 돌발 정보로 저장 하였습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

bool __fastcall TFRAMEIncidentRegist::UpdateAutoIncident(String ALINK_ID, String ARGST_DT, String AREAL_YN)
{
    String     sQry;
    TADOQuery *pADO = NULL;
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_GetConnection()->BeginTrans();

            sQry = "UPDATE TB_INCD_ESTM_SECT \r\n"
				   "   SET REAL_YN = :p03    \r\n"
                   " WHERE LINK_ID = :p01    \r\n"
                   "   AND DETC_DT = :p02    \r\n";

            ITSDb_SQLText(pADO, sQry);

            ITSDb_SQLBind(pADO, "p01", ALINK_ID);
            ITSDb_SQLBind(pADO, "p02", ARGST_DT);
            ITSDb_SQLBind(pADO, "p03", AREAL_YN);

            ITSDb_SQLExec(pADO);

            ITSDb_GetConnection()->CommitTrans();

            //Application->MessageBox(FrmLang->lblDbSave->Caption.c_str(),//L"돌발정보를 오보 판정 돌발 정보로 저장 하였습니다.",
            //                          FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEIncidentRegist::BtnMoveGisClick(TObject *Sender)
{
    if (EdLinkId->Text.IsEmpty()) return;
    g_SelLink.pForm = NULL;
    g_SelLink.Level = 1;
    g_SelLink.Id    = EdLinkId->Text;
    POST_MSG(Application->MainForm->Handle, WM_PLUGINFORM, WP_MSG_15, LP_MSG_15);
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEIncidentRegist::cxPageControl1PageChanging(TObject *Sender,
          TcxTabSheet *NewPage, bool &AllowChange)
{
    if (NewPage == cxTabSheet1)
    {
        cxGroupBox1->Caption = "▶ 외부연계기관 돌발목록 (목록에서 더블클릭으로 돌발정보 선택)";
    }
    else
    {
        cxGroupBox1->Caption = "▶ 자동 돌발목록 (목록에서 더블클릭으로 돌발정보 선택)";
    }
}
//---------------------------------------------------------------------------

