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

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

#include "IDB0110MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#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 "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

TIDB0110M *IDB0110M = NULL;
extern LOGININFO *g_pLOGIN;
//---------------------------------------------------------------------------

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

    FTitle = Caption;//"기관정보관리";
    MyItsOrganManager = new TItsOrganManager();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::CommClose()
{
    try
    {
        if (MyItsOrganManager)
        {
            delete MyItsOrganManager;
            MyItsOrganManager = NULL;
        }

        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::FormShow(TObject *Sender)
{
    Refresh();
    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::FormInit()
{
    m_pGDC = TvList->DataController;

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

    FCodeCOT = ItsCodeManager->FLists.Find("COT");

    InitListCombo();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::InitListCombo()
{
    FInitCombo1 = "";

    TcxComboBoxProperties *pcxColumn03 = (TcxComboBoxProperties *)Column03->Properties;
    pcxColumn03->Items->Clear();
    if (FCodeCOT)
    {
		FOR_STL(TItsSubCode *, pSubCode, FCodeCOT->FSubLists)
		{
			pcxColumn03->Items->Add("[" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
            if (FInitCombo1 == "") FInitCombo1 = "[" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM;
		}
    }
    else
    {
        //pcxColumn03->Items->Add("[0] 기타");
        pcxColumn03->Items->Add("[0] ETC");
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    BtnSearchClick((TObject*)BtnSearch);   
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::BtnSearchClick(TObject *Sender)
{
    Application->ProcessMessages();
    TSqlCursor sqlCrs((TControl*)BtnSearch);

    RefreshData();
    CxList->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::RefreshData()
{
    MyItsOrganManager->LoadFromDb();
    SelListData();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::SelListData()
{
    FUpdate = false;
    CMM_ClearGridTableView(TvList);

    try
    {
        TvList->BeginUpdate();

        FOR_STL(TItsOrgan*, pObj, MyItsOrganManager->FLists)
        {
            pObj->EditMode = DB_INIT;   // 미리초기화해준다.(삭제된것)
            pObj->Completed = true;
            if (pObj->DEL_YN == "Y" || pObj->MEM_DEL == "Y") continue;

            AddObjectItem(pObj, "-");
        }
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        //CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------

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

void __fastcall TIDB0110M::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"기관정보";

    CMM_ExportToExcelFile(sTitle, pGrid, pView, this);
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::BtnInsertClick(TObject *Sender)
{
    int nMsgType = MB_OK|MB_ICONERROR|MB_APPLMODAL;
#if 0
	String sId = InputBox(lblInput->Caption,//"관리번호",
                          lblInQry->Caption,//"관리번호를 입력하세요(숫자).",
                          lblInput->Caption);//"최대10자리" );

    sId = sId.Trim();
	if (sId == lblInput->Caption //"관리번호"
     || sId == "")
	{
		return;
	}
	if (AnsiString(sId).Length() > 20)
	{
        Application->MessageBox(lblDigitErr->Caption.c_str(),//L"관리번호는 10Byte를 초과할 수 없습니다.",
                                FTitle.c_str(),
                                nMsgType);
		return;
	}
#else
    String sId;
    if (!MyItsOrganManager->GetNextOrganId(sId))
    {
        Application->MessageBox(FrmLang->lblMemErr->Caption.c_str(),//L"신규 기관 정보를 생성할 수 없습니다.[메모리오류]",
                                FTitle.c_str(), nMsgType);
        return;
    }
#endif

    TItsOrgan *pDb = (TItsOrgan *)MyItsOrganManager->FLists.Find(sId);
    if (pDb)
    {
        String sErr = FrmLang->lbIdErr->Caption + "[" + sId + "]";
        Application->MessageBox(sErr.c_str(),//("[" + sId + "]는 사용할 수 없는 관리번호 입니다.").c_str(),
                                FTitle.c_str(), nMsgType);
        return;
    }

    try
    {
        TvList->BeginUpdate();

        TItsOrgan *pObj = new TItsOrgan();
        if (!pObj)
        {
            Application->MessageBox(FrmLang->lblMemErr->Caption.c_str(),//L"신규 기관 정보를 생성할 수 없습니다.[메모리오류]",
                                    FTitle.c_str(), nMsgType);
            return;
        }
        //pObj->Clear();

        pObj->CNCT_INTN_NMBR      = sId;                                    //NUMBER(10)	N			연계 기관 관리 번호
        pObj->CNCT_INTN_NM        = "";                                     //VARCHAR2(50)	Y			연계 기관 명
        pObj->CNCT_INTN_TYPE_CD   = ITSUtil_GetCode(FInitCombo1);           //VARCHAR2(7)	Y			연계 기관 유형 코드
        pObj->CNCT_INTN_TEL       = "";                                     //VARCHAR2(128)	Y			연계 기관 전화번호
        pObj->CNCT_INTN_ADDR      = "";                                     //VARCHAR2(200)	Y			연계 기관 주소
        pObj->RMRK                = "";                                     //VARCHAR2(600)	Y			비고
        pObj->DEL_YN              = "N";                                    //CHAR(1)	Y	'N'		삭제 여부

        pObj->Completed    = false;
        pObj->EditMode     = DB_NEW;
        pObj->MEM_DEL      = "N";

        MyItsOrganManager->FLists.Push(pObj->CNCT_INTN_NMBR, pObj);

        AddObjectItem(pObj, FrmLang->lblNew->Caption);//"신규");
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

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

    int nMemPtr = m_pGDC->Values[nIdx][Column99->Index];
    TItsOrgan *pObj = (TItsOrgan*)nMemPtr;
    pObj->EditMode = DB_DELETE;
    m_pGDC->Values[nIdx][Column00->Index] = FrmLang->lblDelete->Caption;//"삭제";
}
//---------------------------------------------------------------------------

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

void __fastcall TIDB0110M::BtnEditClick(TObject *Sender)
{
    TListFocus tvFocus(TvList);

    if (BtnEdit->Caption == FrmLang->lblCancel->Caption)//"취소")
    {
        CxList->SetFocus();
        Application->ProcessMessages();
        SaveData();
    }

    FrmLang->ITSSkin_ButtonClick(BtnSearch, BtnEdit, BtnInsert, BtnDelete, BtnApply);
    ChangeEditMode(BtnEdit->Down);

    RefreshData();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::ChangeEditMode(bool AEdit)
{
    bool bEditing = AEdit;

    try 
    {
        TvList->BeginUpdate();
        
        Column00->Visible = bEditing;
    
        Column02->Options->Editing = bEditing;
        Column03->Options->Editing = bEditing;
        Column04->Options->Editing = bEditing;
        Column05->Options->Editing = bEditing;
        Column06->Options->Editing = bEditing;
    }
    __finally 
    {
        TvList->EndUpdate();
        if (bEditing)
        {
            BtnEdit->Caption = FrmLang->lblCancel->Caption;//"취소";
            BtnEdit->Hint    = FrmLang->lblCancel->Hint;//데이터 편집 취소";
        }
        else
        {
            BtnEdit->Caption = FrmLang->lblEdit->Caption;//"편집";
            BtnEdit->Hint    = FrmLang->lblEdit->Hint;//데이터 편집";
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::TvListColumnPropertiesValidate(TObject *Sender, Variant &DisplayValue, TCaption &ErrorText, bool &Error)
{
    if (TvList->Controller->FocusedRow->IsFilterRow) return;

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

    int nMemPtr = TvList->DataController->Values[nRow][Column99->Index];
    TItsOrgan *pObj = (TItsOrgan*)nMemPtr;
    if (pObj->Completed == false)
    {
        //신규이거나 삭제인 경우 편집 내용에 따른 변화가 필요없다.
        return;
    }

    String CNCT_INTN_NM      = VarToStr(m_pGDC->Values[nRow][Column02->Index]);
    String CNCT_INTN_TYPE_CD = VarToStr(m_pGDC->Values[nRow][Column03->Index]);
    String CNCT_INTN_TEL     = VarToStr(m_pGDC->Values[nRow][Column04->Index]);
    String CNCT_INTN_ADDR    = VarToStr(m_pGDC->Values[nRow][Column05->Index]);
    String RMRK              = VarToStr(m_pGDC->Values[nRow][Column06->Index]);

    TcxGridColumn *pColumn = TvList->Controller->FocusedColumn;
    if      (pColumn == Column02) CNCT_INTN_NM      = DisplayValue;
    else if (pColumn == Column03) CNCT_INTN_TYPE_CD = DisplayValue;
    else if (pColumn == Column04) CNCT_INTN_TEL     = DisplayValue;
    else if (pColumn == Column05) CNCT_INTN_ADDR    = DisplayValue;
    else if (pColumn == Column06) RMRK              = DisplayValue;

    CNCT_INTN_TYPE_CD = ITSUtil_GetCode(CNCT_INTN_TYPE_CD);

    if (pObj->CNCT_INTN_NM      != CNCT_INTN_NM      ||
        pObj->CNCT_INTN_TYPE_CD != CNCT_INTN_TYPE_CD ||
        pObj->CNCT_INTN_TEL     != CNCT_INTN_TEL     ||
        pObj->CNCT_INTN_ADDR    != CNCT_INTN_ADDR    ||
        pObj->RMRK              != RMRK               )
    {
        m_pGDC->Values[nRow][Column00->Index] = FrmLang->lblModify->Caption;//"수정";
        pObj->EditMode = DB_UPDATE;
    }
    else
    {
        TvList->DataController->Values[nRow][Column00->Index] = "-";
        pObj->EditMode = DB_INIT;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::BtnBestFitClick(TObject *Sender)
{
    try
    {
        TvList->BeginUpdate();
		TvList->ApplyBestFit(NULL, false, false);
	}
	__finally
	{
        TvList->EndUpdate();
	}
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::ChkAutoWidthPropertiesChange(TObject *Sender)
{
    try
    {
        TvList->BeginUpdate();
		TvList->OptionsView->ColumnAutoWidth = ChkAutoWidth->Checked;
        BtnBestFit->Visible = !ChkAutoWidth->Checked;
	}
	__finally
	{
        TvList->EndUpdate();
	}
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::MnuDeleteCancelClick(TObject *Sender)
{
    //    
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::BtnApplyClick(TObject *Sender)
{
    FUpdate = true;
    BtnEdit->Down = false;
    BtnEdit->Click();
}
//---------------------------------------------------------------------------

bool __fastcall TIDB0110M::AddObjectItem(TItsOrgan *pObj, String AMode)
{
    TItsSubCode *pSubCode;
    int          nRow;
    try
    {
        nRow = m_pGDC->AppendRecord();
        pObj->GridIndex = nRow;

        m_pGDC->Values[nRow][Column00->Index] = AMode;

        m_pGDC->Values[nRow][Column01->Index] = pObj->CNCT_INTN_NMBR;
        m_pGDC->Values[nRow][Column02->Index] = pObj->CNCT_INTN_NM;

        String sCOTName = "[" + pObj->CNCT_INTN_TYPE_CD + "] ";
        if (FCodeCOT)
        {
            pSubCode = FCodeCOT->FSubLists.Find(pObj->CNCT_INTN_TYPE_CD);
            if (pSubCode) sCOTName = sCOTName + pSubCode->CMMN_CD_KOR_NM;
        }
        if (sCOTName == "[] ") sCOTName = "";
        m_pGDC->Values[nRow][Column03->Index] = sCOTName;

        m_pGDC->Values[nRow][Column04->Index] = pObj->CNCT_INTN_TEL;
        m_pGDC->Values[nRow][Column05->Index] = pObj->CNCT_INTN_ADDR;
        m_pGDC->Values[nRow][Column06->Index] = pObj->RMRK;

        m_pGDC->Values[nRow][Column88->Index] = pObj->DEL_YN;

        m_pGDC->Values[nRow][Column99->Index] = (int)pObj;

        return true;
    }
    catch(...)
    {
    }
    return false;
}
//---------------------------------------------------------------------------

bool __fastcall TIDB0110M::IsUpdate()
{
    FOR_STL(TItsOrgan*, pObj, MyItsOrganManager->FLists)
    {
        if (pObj->Completed && pObj->EditMode != DB_INIT)
            return true;

        if (!pObj->Completed && pObj->EditMode == DB_NEW)
            return true;
    }
    return false;
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::SaveData()
{
    if (!IsUpdate()) return;

    if (!FUpdate)
    {
        if (Application->MessageBox(FrmLang->lblEidtConfirm->Caption.c_str(),//L"변경된 정보가 있습니다.\r\n변경된 정보를 저장 하시겠습니까?",
                                    FTitle.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;
    }

    TADOQuery *pADOI = NULL;
    TADOQuery *pADOU = NULL;
    TADOQuery *pADOD = NULL;
	String sQryI;
	String sQryU;
	String sQryD;
    int    nEditMode;
    try
    {
#if 0
        sQryI = "INSERT INTO TB_CNCT_INTN_INFR (               \r\n"
                "            CNCT_INTN_NMBR,    CNCT_INTN_NM,  \r\n"
                "            CNCT_INTN_TYPE_CD, CNCT_INTN_TEL, \r\n"
                "            CNCT_INTN_ADDR,    RMRK,          \r\n"
                "            DEL_YN  )                         \r\n"
                "   VALUES (                                   \r\n"
                "           :p01, :p02,                        \r\n"
                "           :p03, :p04,                        \r\n"
                "           :p05, :p06,                        \r\n"
                "           'N' )                              \r\n";
        sQryU = "UPDATE TB_CNCT_INTN_INFR          \r\n"
                "   SET CNCT_INTN_NM      = :p02,  \r\n"
                "       CNCT_INTN_TYPE_CD = :p03,  \r\n"
                "       CNCT_INTN_TEL     = :p04,  \r\n"
                "       CNCT_INTN_ADDR    = :p05,  \r\n"
                "       RMRK              = :p06   \r\n"
                " WHERE CNCT_INTN_NMBR = :p01      \r\n";

        sQryD = "UPDATE TB_CNCT_INTN_INFR          \r\n"
                "   SET DEL_YN         = 'Y',      \r\n"
                " WHERE CNCT_INTN_NMBR = :p01      \r\n";
#else
    //구미, 기존테이블 사용 CNCT_INTN_NMBR, CNCT_INTN_MNGM_NMBR
        sQryI = "INSERT INTO TB_CNCT_INTN (               \r\n"
                "            CNCT_INTN_MNGM_NMBR,    CNCT_INTN_NM,  \r\n"
                "            CNCT_INTN_TYPE_CD, CNCT_INTN_TEL, \r\n"
                "            CNCT_INTN_ADDR,    RMRK,          \r\n"
                "            DEL_YN  )                         \r\n"
                "   VALUES (                                   \r\n"
                "           :p01, :p02,                        \r\n"
                "           :p03, :p04,                        \r\n"
                "           :p05, :p06,                        \r\n"
                "           'N' )                              \r\n";

        sQryU = "UPDATE TB_CNCT_INTN          \r\n"
                "   SET CNCT_INTN_NM      = :p02,  \r\n"
                "       CNCT_INTN_TYPE_CD = :p03,  \r\n"
                "       CNCT_INTN_TEL     = :p04,  \r\n"
                "       CNCT_INTN_ADDR    = :p05,  \r\n"
                "       RMRK              = :p06   \r\n"
                " WHERE CNCT_INTN_MNGM_NMBR = :p01      \r\n";

        sQryD = "UPDATE TB_CNCT_INTN          \r\n"
                "   SET DEL_YN         = 'Y',      \r\n"
                " WHERE CNCT_INTN_MNGM_NMBR = :p01      \r\n";
#endif

        int nRow;
        TvList->BeginUpdate();
        try
        {
            pADOI = new TADOQuery(NULL);
			pADOI->Close();
            pADOI->Connection = ITSDb_GetConnection();

            pADOU = new TADOQuery(NULL);
			pADOU->Close();
            pADOU->Connection = ITSDb_GetConnection();

            pADOD = new TADOQuery(NULL);
			pADOD->Close();
            pADOD->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADOI, sQryI);
            ITSDb_SQLText(pADOU, sQryU);
            ITSDb_SQLText(pADOD, sQryD);

            ITSDb_GetConnection()->BeginTrans();

            FOR_STL(TItsOrgan*, pObj, MyItsOrganManager->FLists)
            {
                if (pObj->GridIndex < 0) continue;

                if (pObj->Completed && pObj->EditMode == DB_INIT)
                    continue;

                if (!pObj->Completed && pObj->EditMode != DB_NEW)
                    continue;

                nRow = pObj->GridIndex;

                String CNCT_INTN_NM      = VarToStr(m_pGDC->Values[nRow][Column02->Index]);
                String CNCT_INTN_TYPE_CD = VarToStr(m_pGDC->Values[nRow][Column03->Index]);
                String CNCT_INTN_TEL     = VarToStr(m_pGDC->Values[nRow][Column04->Index]);
                String CNCT_INTN_ADDR    = VarToStr(m_pGDC->Values[nRow][Column05->Index]);
                String RMRK              = VarToStr(m_pGDC->Values[nRow][Column06->Index]);

                CNCT_INTN_TYPE_CD = ITSUtil_GetCode(CNCT_INTN_TYPE_CD);

                //pObj->CNCT_INTN_NMBR = pObj->CNCT_INTN_NMBR;
                pObj->CNCT_INTN_NM      = CNCT_INTN_NM;
                pObj->CNCT_INTN_TYPE_CD = CNCT_INTN_TYPE_CD;
                pObj->CNCT_INTN_TEL     = CNCT_INTN_TEL;
                pObj->CNCT_INTN_ADDR    = CNCT_INTN_ADDR;
                pObj->RMRK              = RMRK;
                pObj->DEL_YN            = pObj->DEL_YN;

                nEditMode = pObj->EditMode;
                if (pObj->Completed)
                {
                    if (pObj->EditMode == DB_DELETE)
                    {
                        //delete
                        ITSDb_SQLBind(pADOD, "p01", pObj->CNCT_INTN_NMBR);
                        ITSDb_SQLExec(pADOD);
                        pObj->DEL_YN  = "Y";
                        pObj->MEM_DEL = "Y";
                    }
                    else
                    if (pObj->EditMode == DB_UPDATE)
                    {
                        //update
                        ITSDb_SQLBind(pADOU, "p01", pObj->CNCT_INTN_NMBR);
                        ITSDb_SQLBind(pADOU, "p02", pObj->CNCT_INTN_NM);
                        ITSDb_SQLBind(pADOU, "p03", pObj->CNCT_INTN_TYPE_CD);
                        ITSDb_SQLBind(pADOU, "p04", pObj->CNCT_INTN_TEL);
                        ITSDb_SQLBind(pADOU, "p05", pObj->CNCT_INTN_ADDR);
                        ITSDb_SQLBind(pADOU, "p06", pObj->RMRK);
                        ITSDb_SQLExec(pADOU);
                        pObj->DEL_YN  = "N";
                        pObj->MEM_DEL = "N";
                    }
                }
                else
                {
                    if (pObj->EditMode == DB_NEW)
                    {
                        //insert
                        ITSDb_SQLBind(pADOI, "p01", pObj->CNCT_INTN_NMBR);
                        ITSDb_SQLBind(pADOI, "p02", pObj->CNCT_INTN_NM);
                        ITSDb_SQLBind(pADOI, "p03", pObj->CNCT_INTN_TYPE_CD);
                        ITSDb_SQLBind(pADOI, "p04", pObj->CNCT_INTN_TEL);
                        ITSDb_SQLBind(pADOI, "p05", pObj->CNCT_INTN_ADDR);
                        ITSDb_SQLBind(pADOI, "p06", pObj->RMRK);
                        ITSDb_SQLExec(pADOI);
                        pObj->DEL_YN  = "N";
                        pObj->MEM_DEL = "N";
                    }
                }
                pObj->Completed = true;
            }
            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);
            if (nEditMode == DB_DELETE)
                DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQryD);
            else
            if (nEditMode == DB_UPDATE)
                DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQryU);
            else
            if (nEditMode == DB_NEW)
                DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQryI);
            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);
            if (nEditMode == DB_DELETE)
                DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQryD);
            else
            if (nEditMode == DB_UPDATE)
                DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQryU);
            else
            if (nEditMode == DB_NEW)
                DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQryI);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADOI)
        {
            pADOI->Close();
            delete pADOI;
        }
        if (pADOU)
        {
            pADOU->Close();
            delete pADOU;
        }
        if (pADOD)
        {
            pADOD->Close();
            delete pADOD;
        }
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::FormCloseQuery(TObject *Sender, bool &CanClose)
{
    SaveData();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0110M::TvListKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if (Key == VK_DELETE)
	{
		if (BtnDelete->Enabled) BtnDeleteClick(Sender);
	}
}
//---------------------------------------------------------------------------

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

void __fastcall TIDB0110M::TvListCustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo)
    {
        String sType = AViewInfo->GridRecord->DisplayTexts[Column00->Index];
        if (sType == FrmLang->lblModify->Caption)//"수정")
        {
            ACanvas->Brush->Color = clOlive;
        }
        else if (sType == FrmLang->lblDelete->Caption)//"삭제")
        {
            ACanvas->Brush->Color = clRed;
        }
        else if (sType == FrmLang->lblNew->Caption)//"신규")
        {
            ACanvas->Brush->Color = clLime;
            if (AViewInfo->Focused) ACanvas->Font->Color = clBlack;
        }
    }
}
//---------------------------------------------------------------------------

