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

#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 "IDB0080MF.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 "cxGroupBox"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxSplitter"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "cxLabel"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

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

    m_enJob1 = enJobNone;
    m_enJob2 = enJobNone;
}
//---------------------------------------------------------------------------

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

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

void __fastcall TIDB0080M::FormInit()
{
    m_pGDC1 = TvList1->DataController;
    m_pGDC2 = TvList2->DataController;

    TvList1->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<대표코드 정보가 없습니다>";
    TvList2->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<세부코드 정보가 없습니다>";

    EdId1->Enabled = false;
    EdId2->Enabled = false;

    FormClear1();
    FormClear2();
    ChangeButtonType1(1);
    ChangeButtonType2(1);
}
//---------------------------------------------------------------------------

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

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

    FormClear1();
    FormClear2();

    RefreshData();
    CxList1->SetFocus();

    ChangeButtonType1(1);
    ChangeButtonType2(1);
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::RefreshData()
{
    SelListData1();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::FormClear1()
{
    m_enJob1 = enJobNone;

    EdId1->Clear();
    EdMsgCd->Clear();
    EdKName1->Clear();
    EdEName1->Clear();
    EdRemark1->Clear();
    EdSysCd->Clear();
    CbUseYn1->ItemIndex = 0;

    EdCmmnClsfCd->Clear();
    EdCmmnClsfCdNm->Clear();
    
    FCode = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::FormClear2()
{
    m_enJob2 = enJobNone;

    EdId2->Clear();
    EdCdAbbr->Clear();
    EdKName2->Clear();
    EdEName2->Clear();
    EdRemark2->Clear();
    EdAtrb1->Clear();
    EdAtrb2->Clear();
    CbUseYn2->ItemIndex = 0;
    
    FSubCode = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::ChangeButtonType1(int nBtnType)
{
    if (nBtnType == 1)
    {
        BtnSave1->Visible = false;
        BtnCancel1->Visible = false;

        BtnInsert1->Visible = true;
        if (TvList1->ViewData->RecordCount > 0)
        {
            BtnEdit1->Visible = true;
            BtnDelete1->Visible = true;
        }
        else
        {
            BtnEdit1->Visible = false;
            BtnDelete1->Visible = false;
        }

        BtnSearch->Enabled = true;
        CxList1->Enabled = true;
        cxGroupBox1->Enabled = false;
    }
    else
    {
        BtnSave1->Visible = true;
        BtnCancel1->Visible = true;

        BtnInsert1->Visible = false;
        BtnEdit1->Visible = false;
        BtnDelete1->Visible = false;

        BtnSearch->Enabled = false;
        CxList1->Enabled = false;
        cxGroupBox1->Enabled = true;
    }
}
//---------------------------------------------------------------------------
void __fastcall TIDB0080M::ChangeButtonType2(int nBtnType)
{
    if (nBtnType == 1)
    {
        BtnSave2->Visible = false;
        BtnCancel2->Visible = false;

        BtnInsert2->Visible = true;
        if (TvList2->ViewData->RecordCount > 0)
        {
            BtnEdit2->Visible = true;
            BtnDelete2->Visible = true;
        }
        else
        {
            BtnEdit2->Visible = false;
            BtnDelete2->Visible = false;
        }

        CxList2->Enabled = true;
        cxGroupBox2->Enabled = false;
    }
    else
    {
        BtnSave2->Visible = true;
        BtnCancel2->Visible = true;

        BtnInsert2->Visible = false;
        BtnEdit2->Visible = false;
        BtnDelete2->Visible = false;

        CxList2->Enabled = false;

        CxList2->Enabled = false;
        cxGroupBox2->Enabled = true;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::SelListData1()
{
    CMM_ClearGridTableView(TvList1);
    CMM_ClearGridTableView(TvList2);

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

        FOR_STL(TItsCode *, pCode, ItsCodeManager->FLists)
        {
            nRow = m_pGDC1->AppendRecord();

            m_pGDC1->Values[nRow][MColumn01->Index] = "-";                         //편집모드
            m_pGDC1->Values[nRow][MColumn02->Index] = pCode->CMMN_CLSF_CD;         //공통 분류 코드
            m_pGDC1->Values[nRow][MColumn03->Index] = pCode->MSG_TYPE_CD;          //메시지 유형 코드
            m_pGDC1->Values[nRow][MColumn04->Index] = pCode->CMMN_CLSF_KOR_NM;     //공통 분류 한글 명
            m_pGDC1->Values[nRow][MColumn05->Index] = pCode->CMMN_CLSF_ENGL_NM;    //공통 분류 영문 명
            m_pGDC1->Values[nRow][MColumn06->Index] = pCode->USE_SYST_CD;          //사용 시스템 코드
            m_pGDC1->Values[nRow][MColumn07->Index] = pCode->RMRK;                 //비고
            m_pGDC1->Values[nRow][MColumn08->Index] = pCode->USE_YN;               //사용 여부
            m_pGDC1->Values[nRow][MColumn98->Index] = (int)pCode;
        }
    }
    __finally
    {
        TvList1->EndUpdate();
        TvList1->DataController->GotoFirst();
        TvList1->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::SelListData2()
{
    CMM_ClearGridTableView(TvList2);

    if (TvList1->ViewData->RecordCount <= 0) return;
    int nIndex = m_pGDC1->FocusedRecordIndex;
    if (nIndex < 0) return;

    int nMemPtr = m_pGDC1->Values[nIndex][MColumn98->Index];
    if (nMemPtr == 0) return;

    TItsCode *pCode = (TItsCode *)nMemPtr;
    int nRow = 0;
    try
    {
        TvList2->BeginUpdate();

        FOR_STL(TItsSubCode *, pSubCode, pCode->FSubLists)
        {
            nRow = m_pGDC2->AppendRecord();

            m_pGDC2->Values[nRow][SColumn01->Index] = "-";                         //편집모드
            m_pGDC2->Values[nRow][SColumn02->Index] = pSubCode->CMMN_CD;
            m_pGDC2->Values[nRow][SColumn03->Index] = pSubCode->CMMN_CD_KOR_NM;
            m_pGDC2->Values[nRow][SColumn04->Index] = pSubCode->CMMN_CD_ENGL_NM;
            m_pGDC2->Values[nRow][SColumn05->Index] = pSubCode->CMMN_CD_ABBR;
            m_pGDC2->Values[nRow][SColumn06->Index] = pSubCode->RMRK;
            m_pGDC2->Values[nRow][SColumn07->Index] = pSubCode->ATRB1;
            m_pGDC2->Values[nRow][SColumn08->Index] = pSubCode->ATRB2;
            m_pGDC2->Values[nRow][SColumn09->Index] = pSubCode->USE_YN;
            m_pGDC2->Values[nRow][SColumn98->Index] = (int)pSubCode;
        }
    }
    __finally
    {
        TvList2->EndUpdate();
        TvList2->DataController->GotoFirst();
        TvList2->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

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

void __fastcall TIDB0080M::BtnSaveClick(TObject *Sender)
{
    bool   bMemDel = true;
    String sMsgTitle, sMsgString;

    TcxButton *pBtn = (TcxButton*)Sender;

    if (pBtn->Tag == 0)
    {
        TItsCode *pCode = new TItsCode();

        try
        {
            pCode->CMMN_CLSF_CD      = EdId1->Text.Trim();
            pCode->MSG_TYPE_CD       = EdMsgCd->Text.Trim();
            pCode->CMMN_CLSF_KOR_NM  = EdKName1->Text.Trim();
            pCode->CMMN_CLSF_ENGL_NM = EdEName1->Text.Trim();
            pCode->USE_SYST_CD       = EdSysCd->Text.Trim();
            pCode->RMRK              = EdRemark1->Text.Trim();
            pCode->USE_YN            = (CbUseYn1->ItemIndex == 0) ? "Y" : "N";
            
            if (m_enJob1 == enJobSave) sMsgTitle  = cxLabel1->Caption;//"대표코드 정보 등록";
            else                       sMsgTitle  = cxLabel2->Caption;//"대표코드 정보 수정";

            if (MergeCmmnClsfCd(pCode))
            {
                if (m_enJob1 == enJobSave)
                {
    				ItsCodeManager->FLists.Push(pCode->CMMN_CLSF_CD, pCode);
                    pCode->Completed = true;
                    bMemDel = false;
                }
                else
                {
                    //FCode->FCMMN_CLSF_CD      = EdId1->Text.Trim();
                    FCode->MSG_TYPE_CD       = EdMsgCd->Text.Trim();
                    FCode->CMMN_CLSF_KOR_NM  = EdKName1->Text.Trim();
                    FCode->CMMN_CLSF_ENGL_NM = EdEName1->Text.Trim();
                    FCode->USE_SYST_CD       = EdSysCd->Text.Trim();
                    FCode->RMRK              = EdRemark1->Text.Trim();
                    FCode->USE_YN            = (CbUseYn1->ItemIndex == 0) ? "Y" : "N";
                }
                RefreshData();
                CMM_SetGridFocusRow(CxList1, TvList1, pCode->CMMN_CLSF_CD, MColumn02->Index);
                DisplayListData1();
                ChangeButtonType1(1);
                Application->MessageBox(sMsgTitle.c_str(), sMsgTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
            }
        }
        __finally
        {
            if (bMemDel)
            {
                if (pCode) delete pCode;
            }
        }
    }
    else
    if (pBtn->Tag == 1)
    {
        TItsSubCode *pSubCode = new TItsSubCode();
        
        try
        {
            pSubCode->CMMN_CD         = EdId2->Text.Trim();
            pSubCode->CMMN_CLSF_CD    = FCode->CMMN_CLSF_CD;
            pSubCode->CMMN_CD_KOR_NM  = EdKName2->Text.Trim();
            pSubCode->CMMN_CD_ENGL_NM = EdEName2->Text.Trim();
            pSubCode->CMMN_CD_ABBR    = EdCdAbbr->Text.Trim();
            pSubCode->RMRK            = EdRemark2->Text.Trim();
            pSubCode->ATRB1           = EdAtrb1->Text.Trim();
            pSubCode->ATRB2           = EdAtrb2->Text.Trim();
            pSubCode->USE_YN          = (CbUseYn2->ItemIndex == 0) ? "Y" : "N";

            if (m_enJob2 == enJobSave) sMsgTitle  = cxLabel3->Caption;//"세부코드 정보 등록";
            else                       sMsgTitle  = cxLabel4->Caption;//"세부코드 정보 수정";

            if (MergeCmmnCd(pSubCode))
            {
                if (m_enJob2 == enJobSave)
                {
    				FCode->FSubLists.Push(pSubCode->CMMN_CD, pSubCode);
                    pSubCode->Completed = true;
                    bMemDel = false;
                }
                else
                {
                    //FSubCode->CMMN_CD         = EdId2->Text.Trim();
                    //FSubCode->CMMN_CLSF_CD    = FCode->CMMN_CLSF_CD;
                    FSubCode->CMMN_CD_KOR_NM  = EdKName2->Text.Trim();
                    FSubCode->CMMN_CD_ENGL_NM = EdEName2->Text.Trim();
                    FSubCode->CMMN_CD_ABBR    = EdCdAbbr->Text.Trim();
                    FSubCode->RMRK            = EdRemark2->Text.Trim();
                    FSubCode->ATRB1           = EdAtrb1->Text.Trim();
                    FSubCode->ATRB2           = EdAtrb2->Text.Trim();
                    FSubCode->USE_YN          = (CbUseYn2->ItemIndex == 0) ? "Y" : "N";
                }
                SelListData2();
                CMM_SetGridFocusRow(CxList2, TvList2, pSubCode->CMMN_CD, SColumn02->Index);
                DisplayListData2();
                ChangeButtonType2(1);
                Application->MessageBox(sMsgTitle.c_str(), sMsgTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
            }
        }
        __finally
        {
            if (bMemDel)
            {
                if (pSubCode) delete pSubCode;
            }
        }
    }
}
//---------------------------------------------------------------------------

bool __fastcall TIDB0080M::MergeCmmnClsfCd(TItsCode *pCode)
{
    bool   bResult = false;
    String sQry;
    TADOQuery *pADO = NULL;

	sQry = "MERGE INTO TB_CMMN_CLSF_CD L                                \r\n"
           "USING (SELECT :p01 AS CMMN_CLSF_CD,                         \r\n"
           "              :p02 AS MSG_TYPE_CD,                          \r\n"
           "              :p03 AS CMMN_CLSF_KOR_NM,                     \r\n"
           "              :p04 AS CMMN_CLSF_ENGL_NM,                    \r\n"
           "              :p05 AS USE_SYST_CD,                          \r\n"
           "              :p06 AS RMRK,                                 \r\n"
           "              :p07 AS USE_YN                                \r\n"
           "         FROM DUAL                                          \r\n"
           "      ) M                                                   \r\n"
           "ON (L.CMMN_CLSF_CD = M.CMMN_CLSF_CD)                        \r\n"
           "WHEN MATCHED THEN                                           \r\n"
           "    UPDATE SET L.MSG_TYPE_CD       = M.MSG_TYPE_CD,         \r\n"
           "               L.CMMN_CLSF_KOR_NM  = M.CMMN_CLSF_KOR_NM,    \r\n"
           "               L.CMMN_CLSF_ENGL_NM = M.CMMN_CLSF_ENGL_NM,   \r\n"
           "               L.USE_SYST_CD       = M.USE_SYST_CD,         \r\n"
           "               L.RMRK              = M.RMRK,                \r\n"
           "               L.USE_YN            = M.USE_YN               \r\n"
           "WHEN NOT MATCHED THEN                                       \r\n"
           "    INSERT (L.CMMN_CLSF_CD,                                 \r\n"
           "            L.MSG_TYPE_CD,                                  \r\n"
           "            L.CMMN_CLSF_KOR_NM,                             \r\n"
           "            L.CMMN_CLSF_ENGL_NM,                            \r\n"
           "            L.USE_SYST_CD,                                  \r\n"
           "            L.RMRK,                                         \r\n"
           "            L.USE_YN)                                       \r\n"
           "    VALUES (M.CMMN_CLSF_CD,                                 \r\n"
           "            M.MSG_TYPE_CD,                                  \r\n"
           "            M.CMMN_CLSF_KOR_NM,                             \r\n"
           "            M.CMMN_CLSF_ENGL_NM,                            \r\n"
           "            M.USE_SYST_CD,                                  \r\n"
           "            M.RMRK,                                         \r\n"
           "            M.USE_YN)                                       \r\n";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);

            ITSDb_SQLBind(pADO, "p01", pCode->CMMN_CLSF_CD);
            ITSDb_SQLBind(pADO, "p02", pCode->MSG_TYPE_CD);
            ITSDb_SQLBind(pADO, "p03", pCode->CMMN_CLSF_KOR_NM);
            ITSDb_SQLBind(pADO, "p04", pCode->CMMN_CLSF_ENGL_NM);
            ITSDb_SQLBind(pADO, "p05", pCode->USE_SYST_CD);
            ITSDb_SQLBind(pADO, "p06", pCode->RMRK);
            ITSDb_SQLBind(pADO, "p07", pCode->USE_YN);

            ITSDb_GetConnection()->BeginTrans();
            ITSDb_SQLExec(pADO);
            ITSDb_GetConnection()->CommitTrans();

            bResult = true;
        }
        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 bResult;
}
//---------------------------------------------------------------------------
bool __fastcall TIDB0080M::MergeCmmnCd(TItsSubCode *pSubCode)
{
    bool   bResult = false;
    String sQry;
    TADOQuery *pADO = NULL;

	sQry = "MERGE INTO TB_CMMN_CD L                                  \r\n"
           "USING (SELECT :p01 AS CMMN_CD,                           \r\n"
           "              :p02 AS CMMN_CLSF_CD,                      \r\n"
           "              :p03 AS CMMN_CD_KOR_NM,                    \r\n"
           "              :p04 AS CMMN_CD_ENGL_NM,                   \r\n"
           "              :p05 AS CMMN_CD_ABBR,                      \r\n"
           "              :p06 AS RMRK,                              \r\n"
           "              :p07 AS ATRB1,                             \r\n"
           "              :p08 AS ATRB2,                             \r\n"
           "              :p09 AS USE_YN                             \r\n"
           "         FROM DUAL                                       \r\n"
           "      ) M                                                \r\n"
           "ON (    L.CMMN_CD      = M.CMMN_CD                       \r\n"
           "    AND L.CMMN_CLSF_CD = M.CMMN_CLSF_CD)                 \r\n"
           "WHEN MATCHED THEN                                        \r\n"
           "    UPDATE SET L.CMMN_CD_KOR_NM = M.CMMN_CD_KOR_NM,      \r\n"
           "               L.CMMN_CD_ENGL_NM    = M.CMMN_CD_ENGL_NM, \r\n"
           "               L.CMMN_CD_ABBR = M.CMMN_CD_ABBR,          \r\n"
           "               L.RMRK = M.RMRK,                          \r\n"
           "               L.ATRB1 = M.ATRB1,                        \r\n"
           "               L.ATRB2 = M.ATRB2,                        \r\n"
           "               L.USE_YN = M.USE_YN                       \r\n"
           "WHEN NOT MATCHED THEN                                    \r\n"
           "    INSERT (L.CMMN_CD,                                   \r\n"
           "            L.CMMN_CLSF_CD,                              \r\n"
           "            L.CMMN_CD_KOR_NM,                            \r\n"
           "            L.CMMN_CD_ENGL_NM,                           \r\n"
           "            L.CMMN_CD_ABBR,                              \r\n"
           "            L.RMRK,                                      \r\n"
           "            L.ATRB1,                                     \r\n"
           "            L.ATRB2,                                     \r\n"
           "            L.USE_YN)                                    \r\n"
           "    VALUES (M.CMMN_CD,                                   \r\n"
           "            M.CMMN_CLSF_CD,                              \r\n"
           "            M.CMMN_CD_KOR_NM,                            \r\n"
           "            M.CMMN_CD_ENGL_NM,                           \r\n"
           "            M.CMMN_CD_ABBR,                              \r\n"
           "            M.RMRK,                                      \r\n"
           "            M.ATRB1,                                     \r\n"
           "            M.ATRB2,                                     \r\n"
           "            M.USE_YN)                                    \r\n";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
            
            ITSDb_SQLText(pADO, sQry);

            ITSDb_SQLBind(pADO, "p01", pSubCode->CMMN_CD);
            ITSDb_SQLBind(pADO, "p02", pSubCode->CMMN_CLSF_CD);
            ITSDb_SQLBind(pADO, "p03", pSubCode->CMMN_CD_KOR_NM);
            ITSDb_SQLBind(pADO, "p04", pSubCode->CMMN_CD_ENGL_NM);
            ITSDb_SQLBind(pADO, "p05", pSubCode->CMMN_CD_ABBR);
            ITSDb_SQLBind(pADO, "p06", pSubCode->RMRK);
            ITSDb_SQLBind(pADO, "p07", pSubCode->ATRB1);
            ITSDb_SQLBind(pADO, "p08", pSubCode->ATRB2);
            ITSDb_SQLBind(pADO, "p09", pSubCode->USE_YN);

            ITSDb_GetConnection()->BeginTrans();
            ITSDb_SQLExec(pADO);
            ITSDb_GetConnection()->CommitTrans();

            bResult = true;
        }
        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 bResult;
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::BtnCancelClick(TObject *Sender)
{
    TcxButton *pBtn = (TcxButton*)Sender;
    if (pBtn->Tag == 0)
    {
        ChangeButtonType1(1);
        m_enJob1 = enJobNone;
        DisplayListData1();
    }
    else
    if (pBtn->Tag == 1)
    {
        ChangeButtonType2(1);
        m_enJob2 = enJobNone;
        DisplayListData2();
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::BtnInsertClick(TObject *Sender)
{
    String sErrMsg = "";
    String sId     = "";

    TcxButton *pBtn = (TcxButton*)Sender;
    if (pBtn->Tag == 0)
    {
        sId = InputBox(cxLabel5->Caption,//"대표코드",
                       cxLabel6->Caption,//"새로운 대표코드를 입력하세요.\r\n입력후에는 수정이 불가능합니다.",
                       cxLabel7->Caption);//"최대5자리");
        sId.Trim();
        if (sId.IsEmpty() || AnsiString(sId).Length() > 5 || sId == cxLabel7->Caption)
        {
            Application->MessageBox(cxLabel8->Caption.c_str(),//L"5자리를 초과하였거나 값이 없습니다.",
                                    cxLabel9->Caption.c_str(),//L"대표코드 추가 오류",
                                    MB_OK|MB_ICONERROR|MB_APPLMODAL);
        	return;
        }
        
        TItsCode *pCode = ItsCodeManager->FLists.Find(sId);
        if (pCode)
        {
            sErrMsg = cxLabel10->Caption + "[" + sId + "]";// 코드는 이미 사용 중인 코드입니다.\r\n다른 코드를 입력하세요.";
            Application->MessageBox(sErrMsg.c_str(),
                                    cxLabel9->Caption.c_str(),//L"대표코드 추가 오류",
                                    MB_OK|MB_ICONERROR|MB_APPLMODAL);
        	return;
        }

        ChangeButtonType1(2);
        FormClear1();
        FormClear2();
        EdId1->Text = sId;
        m_enJob1 = enJobSave;
    }
    else
    if (pBtn->Tag == 1)
    {
        if (m_pGDC1->FocusedRecordIndex < 0 || !FCode)
        {
            Application->MessageBox(cxLabel16->Caption.c_str(),//L"대표코드를 선택후 사용해주십시오.",
                                    cxLabel15->Caption.c_str(),//L"세부코드 추가 오류",
                                    MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }

        sId = InputBox(cxLabel11->Caption,//"세부코드"
                       cxLabel12->Caption,//"새로운 세부코드를 입력하세요.\r\n입력후에는 수정이 불가능합니다.",
                       cxLabel13->Caption);//"최대7자리");
        sId.Trim();
        if (sId.IsEmpty() || AnsiString(sId).Length() > 7 || sId == cxLabel13->Caption)
        {
            Application->MessageBox(cxLabel14->Caption.c_str(),//L"7자리를 초과하였거나 값이 없습니다.",
                                    cxLabel15->Caption.c_str(),//L"세부코드 추가 오류",
                                    MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }
        
        TItsSubCode *pSubCode = FCode->FSubLists.Find(sId);
        if (pSubCode)
        {
            sErrMsg = cxLabel10->Caption + "[" + sId + "]";// 코드는 이미 사용 중인 코드입니다.\r\n다른 코드를 입력하세요.";
            Application->MessageBox(sErrMsg.c_str(),
                                    cxLabel15->Caption.c_str(),//L"세부코드 추가 오류",
                                    MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }        

        ChangeButtonType2(2);
        FormClear2();
        EdId2->Text = sId;
        m_enJob2 = enJobSave;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::BtnEditClick(TObject *Sender)
{
    TcxButton *pBtn = (TcxButton*)Sender;
    if (pBtn->Tag == 0)
    {
        ChangeButtonType1(2);
        m_enJob1 = enJobEdit;
    }
    else
    if (pBtn->Tag == 1)
    {
        ChangeButtonType2(2);
        m_enJob2 = enJobEdit;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::BtnDeleteClick(TObject *Sender)
{
    TcxButton *pBtn = (TcxButton*)Sender;
    if (pBtn->Tag == 0)
    {
        DeleteData1();
    }
    else
    if (pBtn->Tag == 1)
    {
        DeleteData2();
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::DeleteData1()
{
    TADOQuery *pADO = NULL;
	String sQry;
    String sMsgString;

    if (TvList1->ViewData->RecordCount <= 0) return;
    if (TvList1->DataController->FocusedRecordIndex < 0) return;
    if (!FCode)
    {
        return;
    }
    sMsgString = cxLabel18->Caption;//"대표코드 정보를 삭제 하시겠습니까?\r\n세부코드 정보도 함께 삭제 됩니다." ;
    if (Application->MessageBox(sMsgString.c_str(),
                                cxLabel17->Caption.c_str(),//L"코드 정보 삭제",
                                MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

    String sCode = FCode->CMMN_CLSF_CD;

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
            
            ITSDb_GetConnection()->BeginTrans();
            // 세부코드 삭제
        	sQry = "DELETE FROM TB_CMMN_CD     \r\n"
                   " WHERE CMMN_CLSF_CD = :p01 \r\n";

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", sCode);
            ITSDb_SQLExec(pADO);

            // 대분류 코드 삭제
        	sQry = "DELETE FROM TB_CMMN_CLSF_CD \r\n"
                   " WHERE CMMN_CLSF_CD = :p01  \r\n";

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", sCode);
            ITSDb_SQLExec(pADO);
            ITSDb_GetConnection()->CommitTrans();

            ItsCodeManager->FLists.Remove(sCode);
            Application->MessageBox(cxLabel19->Caption.c_str(),//L"대표코드와 세부코드 정보를 삭제하였습니다.",
                                    cxLabel17->Caption.c_str(),//L"코드 정보 삭제",
                                    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;
        }
    }

    BtnSearchClick(NULL);
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::DeleteData2()
{
    TADOQuery *pADO = NULL;
	String sQry;
    String sMsgString;

    if (TvList2->ViewData->RecordCount <= 0) return;
    if (TvList2->DataController->FocusedRecordIndex < 0) return;
    if (!FCode || !FSubCode)
    {
        return;
    }

    sMsgString = cxLabel21->Caption;//"세부코드 정보를 삭제 하시겠습니까?" ;
    if (Application->MessageBox(sMsgString.c_str(),
                                cxLabel20->Caption.c_str(),//L"세부코드 정보 삭제",
                                MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

    String sSubCode = FSubCode->CMMN_CD;
    String sCode    = FSubCode->CMMN_CLSF_CD;

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
            
            // 세부코드 삭제
        	sQry = "DELETE FROM TB_CMMN_CD     \r\n"
                   " WHERE CMMN_CD      = :p01 \r\n"
                   "   AND CMMN_CLSF_CD = :p02 \r\n";

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", sSubCode);
            ITSDb_SQLBind(pADO, "p02", sCode);
            ITSDb_SQLExec(pADO);
            ITSDb_GetConnection()->CommitTrans();

            ITSDb_GetConnection()->BeginTrans();
            FCode->FSubLists.Remove(sSubCode);
            Application->MessageBox(cxLabel22->Caption.c_str(),//L"세부코드 정보를 삭제하였습니다.",
                                    cxLabel20->Caption.c_str(),//L"세부코드 정보 삭제",
                                    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;
        }
    }

    SelListData2();
    DisplayListData2();
    ChangeButtonType2(1);
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::TvList1FocusedRecordChanged(TcxCustomGridTableView *Sender, TcxCustomGridRecord *APrevFocusedRecord,
          TcxCustomGridRecord *AFocusedRecord, bool ANewItemRecordFocusingChanged)
{
    DisplayListData1();
    SelListData2();
    ChangeButtonType2(1);
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::TvList2FocusedRecordChanged(TcxCustomGridTableView *Sender, TcxCustomGridRecord *APrevFocusedRecord,
          TcxCustomGridRecord *AFocusedRecord, bool ANewItemRecordFocusingChanged)
{
    DisplayListData2();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::DisplayListData1()
{
    int ii;

    FormClear1();

    if (TvList1->ViewData->RecordCount <= 0) return;
    int nIndex = m_pGDC1->FocusedRecordIndex;
    if (nIndex < 0) return;

    int nMemPtr = m_pGDC1->Values[nIndex][MColumn98->Index];
    if (nMemPtr == 0) return;

    FCode = (TItsCode *)nMemPtr;

    try
    {
            EdId1->Text         = FCode->CMMN_CLSF_CD;
            EdMsgCd->Text       = FCode->MSG_TYPE_CD;
            EdKName1->Text      = FCode->CMMN_CLSF_KOR_NM;
            EdEName1->Text      = FCode->CMMN_CLSF_ENGL_NM;
            EdRemark1->Text     = FCode->RMRK;
            EdSysCd->Text       = FCode->USE_SYST_CD;
            CbUseYn1->ItemIndex = FCode->USE_YN == "Y" ? 0 : 1;

            EdCmmnClsfCd->Text  = FCode->CMMN_CLSF_CD;
            EdCmmnClsfCdNm->Text= FCode->CMMN_CLSF_KOR_NM;
    }
    catch(Exception &e)
    {
        Application->MessageBox((e.Message + " - " +__FILE__+ " - " +__LINE__).c_str(), Caption.c_str(), MB_OK);
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::DisplayListData2()
{
    FormClear2();

    if (TvList2->ViewData->RecordCount <= 0) return;
    int nIndex = m_pGDC2->FocusedRecordIndex;
    if (nIndex < 0) return;

    int nMemPtr = m_pGDC2->Values[nIndex][SColumn98->Index];
    if (nMemPtr == 0) return;

    FSubCode = (TItsSubCode *)nMemPtr;

    try
    {
        EdId2->Text         = FSubCode->CMMN_CD;
        EdCdAbbr->Text      = FSubCode->CMMN_CD_ABBR;
        EdKName2->Text      = FSubCode->CMMN_CD_KOR_NM;
        EdEName2->Text      = FSubCode->CMMN_CD_ENGL_NM;
        EdRemark2->Text     = FSubCode->RMRK;
        EdAtrb1->Text       = FSubCode->ATRB1;
        EdAtrb2->Text       = FSubCode->ATRB2;
        CbUseYn2->ItemIndex = FSubCode->USE_YN == "Y" ? 0 : 1;
    }
    catch(Exception &e)
    {
        Application->MessageBox((e.Message + " - " +__FILE__+ " - " +__LINE__).c_str(), Caption.c_str(), MB_OK);
    }
}
//---------------------------------------------------------------------------

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


void __fastcall TIDB0080M::TvList1DataControllerFilterChanged(TObject *Sender)
{
    CMM_SetFilterLike(TvList1);
}
//---------------------------------------------------------------------------

void __fastcall TIDB0080M::TvList2DataControllerFilterChanged(TObject *Sender)
{
    CMM_SetFilterLike(TvList2);
}
//---------------------------------------------------------------------------

