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

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

#include "IDB0090MF.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"

TIDB0090M *IDB0090M = NULL;

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

    FTitle = Caption;//"운영자정보관리";
    FDbUpdate = false;
}
//---------------------------------------------------------------------------

void __fastcall TIDB0090M::CommClose()
{
    try
    {
        if (ItsUserManager)
        {
            delete ItsUserManager;
            ItsUserManager = NULL;
        }

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

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

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

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

    if (!ItsUserManager)
    {
        ItsUserManager = new TItsUserManager();
    }

    InitColumnProperties();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0090M::InitColumnProperties()
{
    FInitCombo1 = "";
    FInitCombo2 = "";

    String sQry;
    TADOQuery *pADO = NULL;

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


            sQry = "SELECT GROP_ID, GROP_NM  \r\n"
//                   "  FROM TB_USER_GROP_INFR \r\n"
                   "  FROM TB_USERGROP_INFR \r\n"
                   " WHERE DEL_YN = 'N'      \r\n"
                   " ORDER BY GROP_ID        \r\n";
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
            pADO->Prepared = true;
			pADO->Open();

            //그룹
    		TcxComboBoxProperties *pcxColumn12 = (TcxComboBoxProperties *)Column12->Properties;
            pcxColumn12->Items->Clear();
            for( ; !pADO->Eof; pADO->Next())
            {
                String sCode = pADO->FieldByName("GROP_ID")->AsString;    //코드
                String sName = pADO->FieldByName("GROP_NM")->AsString;    //코드명

    			pcxColumn12->Items->Add("[" + sCode + "] " + sName);
                if (FInitCombo1 == "")
                {
                    FInitCombo1 = "[" + sCode + "] " + sName;
                }
            }

            //시스템
            sQry = "SELECT OPER_SYST_ID, OPER_SYST_NAME \r\n"
                   "  FROM TB_OPERSYST                  \r\n"
                   " ORDER BY OPER_SYST_ID              \r\n";
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
            pADO->Prepared = true;
			pADO->Open();

            //그룹
    		TcxComboBoxProperties *pcxColumn13 = (TcxComboBoxProperties *)Column13->Properties;
            pcxColumn13->Items->Clear();
            for( ; !pADO->Eof; pADO->Next())
            {
                String sCode = pADO->FieldByName("OPER_SYST_ID")->AsString;    //코드
                String sName = pADO->FieldByName("OPER_SYST_NAME")->AsString;    //코드명

    			pcxColumn13->Items->Add("[" + sCode + "] " + sName);
                if (FInitCombo2 == "")
                {
                    FInitCombo2 = "[" + sCode + "] " + sName;
                }
            }
        }
        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;
        }
    }
}
//---------------------------------------------------------------------------

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

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

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

void __fastcall TIDB0090M::RefreshData()
{
    ItsUserManager->LoadFromDb();
    SelListData();
}
//---------------------------------------------------------------------------

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

    try
    {
        TvList->BeginUpdate();

        FOR_STL(TItsUser*, pObj, ItsUserManager->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 TIDB0090M::BtnCloseClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0090M::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"운영자정보";

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

void __fastcall TIDB0090M::BtnInsertClick(TObject *Sender)
{
    int nMsgType = MB_OK|MB_ICONERROR|MB_APPLMODAL;

	String sId = InputBox(lblInput->Caption,//"운영자 ID",
                          lblInQry->Caption,//"신규 운영자 ID를 입력하세요.",
                          lblInput->Caption);//"최대20자리" );

    sId = sId.Trim();
	if (sId == lblInput->Caption //"운영자 ID"
     || sId == "")
	{
		return;
	}
	if (AnsiString(sId).Length() > 20)
	{
        Application->MessageBox(lblDigitErr->Caption.c_str(),//L"운영자 ID는  20Byte를 초과할 수 없습니다.",
                                FTitle.c_str(),
                                nMsgType);
		return;
	}

    TItsUser *pDb = (TItsUser *)ItsUserManager->FLists.Find(sId);
    if (pDb)
    {
        String sErr = FrmLang->lbIdErr->Caption + "[" + sId + "]";
        Application->MessageBox(sErr.c_str(),//("[" + sId + "]는 사용할 수 없는 운영자 ID 입니다.").c_str(),
                                FTitle.c_str(), nMsgType);
        return;
    }

    try
    {
        TvList->BeginUpdate();

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

        pObj->USER_ID      = sId;  //VARCHAR2(20)	N			사용자ID
        pObj->PWD          = "";  //VARCHAR2(64)	Y			암호
        pObj->NAME         = "";  //VARCHAR2(30)	Y			성명
        pObj->COMP         = "";  //VARCHAR2(50)	Y			회사
        pObj->TEL          = "";  //VARCHAR2(128)	Y			전화번호
        pObj->ADDR         = "";  //VARCHAR2(200)	Y			주소
        pObj->RESD_NMBR    = "";  //VARCHAR2(14)	Y			주민번호
        pObj->MOBILE       = "";  //VARCHAR2(128)	Y			이동전화
        pObj->EMAL         = "";  //VARCHAR2(128)	Y			이메일
        pObj->DEL_YN       = "N"; //CHAR(1)	Y	'N'		삭제 여부
        pObj->RGSTYMD      = Now().FormatString("YYYYMMDDHHNNSS");  //VARCHAR2(14)	Y			등록일자
        pObj->CRCTYMD      = Now().FormatString("YYYYMMDDHHNNSS");  //VARCHAR2(14)	Y			수정일자
        pObj->HINT_QUES    = "";  //VARCHAR2(7)	Y			힌트질문
        pObj->HINT_ANS     = "";  //VARCHAR2(100)	Y			힌트질문답
        pObj->GROP_ID      = ITSUtil_GetCode(FInitCombo1);  //VARCHAR2(30)	Y			그룹ID
        pObj->OPER_SYST_ID = ITSUtil_GetCode(FInitCombo2);  //VARCHAR2(30)	Y			운영시스템ID

        pObj->GROP_NM      = FInitCombo1;
        pObj->OPER_SYST_NM = FInitCombo2;

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

        ItsUserManager->FLists.Push(pObj->USER_ID, 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 TIDB0090M::BtnDeleteClick(TObject *Sender)
{
    int nIdx = m_pGDC->FocusedRecordIndex;
    if (nIdx < 0) return;

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

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

void __fastcall TIDB0090M::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 TIDB0090M::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;
        Column07->Options->Editing = bEditing;
        Column08->Options->Editing = bEditing;
        Column09->Options->Editing = bEditing;
        Column10->Options->Editing = bEditing;
        Column11->Options->Editing = bEditing;
        Column12->Options->Editing = bEditing;
        Column13->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 TIDB0090M::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];
    TItsUser *pObj = (TItsUser*)nMemPtr;
    if (pObj->Completed == false)
    {
        //신규이거나 삭제인 경우 편집 내용에 따른 변화가 필요없다.
        return;
    }

    String PWD_TMP      = "";
    String PWD_ORG      = VarToStr(m_pGDC->Values[nRow][Column16->Index]);
    String PWD          = VarToStr(m_pGDC->Values[nRow][Column02->Index]);
    PWD_TMP = PWD_ORG;

    String NAME         = VarToStr(m_pGDC->Values[nRow][Column03->Index]);
    String COMP         = VarToStr(m_pGDC->Values[nRow][Column04->Index]);
    String TEL          = VarToStr(m_pGDC->Values[nRow][Column05->Index]);
    String ADDR         = VarToStr(m_pGDC->Values[nRow][Column06->Index]);
    String RESD_NMBR    = "";//VarToStr(m_pGDC->Values[nRow][Column07->Index]);
    String MOBILE       = VarToStr(m_pGDC->Values[nRow][Column08->Index]);
    String EMAL         = VarToStr(m_pGDC->Values[nRow][Column09->Index]);
    String HINT_QUES    = VarToStr(m_pGDC->Values[nRow][Column10->Index]);
    String HINT_ANS     = VarToStr(m_pGDC->Values[nRow][Column11->Index]);
    String GROP_ID      = VarToStr(m_pGDC->Values[nRow][Column12->Index]);
    String OPER_SYST_ID = VarToStr(m_pGDC->Values[nRow][Column13->Index]);

    TcxGridColumn *pColumn = TvList->Controller->FocusedColumn;
    if      (pColumn == Column02)
    {
        PWD = DisplayValue;
        PWD_TMP = String(ITSSHA256_Encrpyt(PWD));
    }
    else if (pColumn == Column03) NAME         = DisplayValue;
    else if (pColumn == Column04) COMP         = DisplayValue;
    else if (pColumn == Column05) TEL          = DisplayValue;
    else if (pColumn == Column06) ADDR         = DisplayValue;
    //else if (pColumn == Column07) RESD_NMBR    = DisplayValue;
    else if (pColumn == Column08) MOBILE       = DisplayValue;
    else if (pColumn == Column09) EMAL         = DisplayValue;
    else if (pColumn == Column10) HINT_QUES    = DisplayValue;
    else if (pColumn == Column11) HINT_ANS     = DisplayValue;
    else if (pColumn == Column12) GROP_ID      = DisplayValue;
    else if (pColumn == Column13) OPER_SYST_ID = DisplayValue;

    GROP_ID      = ITSUtil_GetCode(GROP_ID);
    OPER_SYST_ID = ITSUtil_GetCode(OPER_SYST_ID);

    if (PWD_ORG            != PWD_TMP     ||
        pObj->NAME         != NAME        ||
        pObj->COMP         != COMP        ||
        pObj->TEL          != TEL         ||
        pObj->ADDR         != ADDR        ||
        //pObj->RESD_NMBR    != RESD_NMBR   ||
        pObj->MOBILE       != MOBILE      ||
        pObj->EMAL         != EMAL        ||
        pObj->HINT_QUES    != HINT_QUES   ||
        pObj->HINT_ANS     != HINT_ANS    ||
        pObj->GROP_ID      != GROP_ID     ||
        pObj->OPER_SYST_ID != OPER_SYST_ID )
    {
        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 TIDB0090M::BtnBestFitClick(TObject *Sender)
{
    try
    {
        TvList->BeginUpdate();
		TvList->ApplyBestFit(NULL, false, false);
	}
	__finally
	{
        TvList->EndUpdate();
	}
}
//---------------------------------------------------------------------------

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

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

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

bool __fastcall TIDB0090M::AddObjectItem(TItsUser *pObj, String AMode)
{
    int nRow;
    try
    {
        nRow = m_pGDC->AppendRecord();
        pObj->GridIndex = nRow;

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

        m_pGDC->Values[nRow][Column01->Index] = pObj->USER_ID;
        m_pGDC->Values[nRow][Column02->Index] = "";   //pObj->PWD;
        m_pGDC->Values[nRow][Column16->Index] = pObj->PWD;
        m_pGDC->Values[nRow][Column03->Index] = pObj->NAME;
        m_pGDC->Values[nRow][Column04->Index] = pObj->COMP;
        m_pGDC->Values[nRow][Column05->Index] = pObj->TEL;
        m_pGDC->Values[nRow][Column06->Index] = pObj->ADDR;
        m_pGDC->Values[nRow][Column07->Index] = pObj->RESD_NMBR;
        m_pGDC->Values[nRow][Column08->Index] = pObj->MOBILE;
        m_pGDC->Values[nRow][Column09->Index] = pObj->EMAL;
        m_pGDC->Values[nRow][Column10->Index] = pObj->HINT_QUES;
        m_pGDC->Values[nRow][Column11->Index] = pObj->HINT_ANS;
        m_pGDC->Values[nRow][Column12->Index] = pObj->GROP_NM;
        m_pGDC->Values[nRow][Column13->Index] = pObj->OPER_SYST_NM;

        if (!pObj->RGSTYMD.IsEmpty())
        m_pGDC->Values[nRow][Column14->Index] = ITSUtil_StrToTime(pObj->RGSTYMD);

        if (!pObj->CRCTYMD.IsEmpty())
        m_pGDC->Values[nRow][Column15->Index] = ITSUtil_StrToTime(pObj->CRCTYMD);

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

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

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

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

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

void __fastcall TIDB0090M::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
    {
        sQryI = "INSERT INTO TB_USER_INFR (                                  \r\n"
                "            USER_ID, PWD, NAME, COMP, TEL, ADDR,            \r\n"
                "            MOBILE, EMAL,                                   \r\n"
                "            DEL_YN,                                         \r\n"
                "            RGSTYMD,                                        \r\n"
                "            CRCTYMD,                                        \r\n"
                "            HINT_QUES, HINT_ANS, GROP_ID, OPER_SYST_ID)     \r\n"
                "   VALUES (                                                 \r\n"
                "           :p01, :p02, :p03, :p04, :p05, :p06,              \r\n"
                "           :p08, :p09,                                \r\n"
                "           'N',                                             \r\n"
                "           TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS'),            \r\n"
                "           TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS'),            \r\n"
                "           :p13, :p14, :p15, :p16 )                         \r\n";

        sQryU = "UPDATE TB_USER_INFR                                         \r\n"
                "   SET PWD          = :p02,                                 \r\n"
                "       NAME         = :p03,                                 \r\n"
                "       COMP         = :p04,                                 \r\n"
                "       TEL          = :p05,                                 \r\n"
                "       ADDR         = :p06,                                 \r\n"
                "       MOBILE       = :p08,                                 \r\n"
                "       EMAL         = :p09,                                 \r\n"
                "       CRCTYMD      = TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS'), \r\n"
                "       HINT_QUES    = :p13,                                 \r\n"
                "       HINT_ANS     = :p14,                                 \r\n"
                "       GROP_ID      = :p15,                                 \r\n"
                "       OPER_SYST_ID = :p16                                  \r\n"
                " WHERE USER_ID      = :p01                                  \r\n";

        sQryD = "UPDATE TB_USER_INFR                                         \r\n"
                "   SET CRCTYMD      = TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS'), \r\n"
                "       DEL_YN       = 'Y'                                   \r\n"
                " WHERE USER_ID      = :p01                                  \r\n";

        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(TItsUser*, pObj, ItsUserManager->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 PWD_ORG      = VarToStr(m_pGDC->Values[nRow][Column16->Index]);
                String PWD          = VarToStr(m_pGDC->Values[nRow][Column02->Index]);
                if (PWD != "" || PWD != PWD_ORG)
                {
                    PWD = GetPassword(PWD);
                }
                else
                {
                    PWD = PWD_ORG;
                }

                String NAME         = VarToStr(m_pGDC->Values[nRow][Column03->Index]);
                String COMP         = VarToStr(m_pGDC->Values[nRow][Column04->Index]);
                String TEL          = VarToStr(m_pGDC->Values[nRow][Column05->Index]);
                String ADDR         = VarToStr(m_pGDC->Values[nRow][Column06->Index]);
                String RESD_NMBR    = "";//VarToStr(m_pGDC->Values[nRow][Column07->Index]);
                String MOBILE       = VarToStr(m_pGDC->Values[nRow][Column08->Index]);
                String EMAL         = VarToStr(m_pGDC->Values[nRow][Column09->Index]);
                String HINT_QUES    = VarToStr(m_pGDC->Values[nRow][Column10->Index]);
                String HINT_ANS     = VarToStr(m_pGDC->Values[nRow][Column11->Index]);
                String GROP_ID      = VarToStr(m_pGDC->Values[nRow][Column12->Index]);
                String OPER_SYST_ID = VarToStr(m_pGDC->Values[nRow][Column13->Index]);

                GROP_ID      = ITSUtil_GetCode(GROP_ID);
                OPER_SYST_ID = ITSUtil_GetCode(OPER_SYST_ID);

                //pObj->USER_ID      = pObj->USER_ID;

                pObj->PWD          = PWD;
                pObj->NAME         = NAME;
                pObj->COMP         = COMP;
                pObj->TEL          = TEL;
                pObj->ADDR         = ADDR;
                pObj->RESD_NMBR    = RESD_NMBR;
                pObj->MOBILE       = MOBILE;
                pObj->EMAL         = EMAL;
                //pObj->DEL_YN       = pObj->DEL_YN;  //CHAR(1)	Y	'N'		삭제 여부
                pObj->RGSTYMD      = Now().FormatString("YYYYMMDDHHNNSS");
                pObj->CRCTYMD      = Now().FormatString("YYYYMMDDHHNNSS");
                pObj->HINT_QUES    = HINT_QUES;
                pObj->HINT_ANS     = HINT_ANS;
                pObj->GROP_ID      = GROP_ID;
                pObj->OPER_SYST_ID = OPER_SYST_ID;

                nEditMode = pObj->EditMode;
                if (pObj->Completed)
                {
                    if (pObj->EditMode == DB_DELETE)
                    {
                        //delete
                        ITSDb_SQLBind(pADOD, "p01", pObj->USER_ID);
                        ITSDb_SQLExec(pADOD);
                        pObj->DEL_YN  = "Y";
                        pObj->MEM_DEL = "Y";
                    }
                    else
                    if (pObj->EditMode == DB_UPDATE)
                    {

                        ITSDb_SQLBind(pADOU, "p01", pObj->USER_ID);
                        ITSDb_SQLBind(pADOU, "p02", pObj->PWD);
                        ITSDb_SQLBind(pADOU, "p03", pObj->NAME);
                        ITSDb_SQLBind(pADOU, "p04", pObj->COMP);
                        ITSDb_SQLBind(pADOU, "p05", pObj->TEL);
                        ITSDb_SQLBind(pADOU, "p06", pObj->ADDR);
                        //ITSDb_SQLBind(pADOU, "p07", pObj->RESD_NMBR);
                        ITSDb_SQLBind(pADOU, "p08", pObj->MOBILE);
                        ITSDb_SQLBind(pADOU, "p09", pObj->EMAL);
                        //ITSDb_SQLBind(pADOU, "p10", pObj->DEL_YN);
                        //ITSDb_SQLBind(pADOU, "p11", pObj->RGSTYMD);
                        //ITSDb_SQLBind(pADOU, "p12", pObj->CRCTYMD);
                        ITSDb_SQLBind(pADOU, "p13", pObj->HINT_QUES);
                        ITSDb_SQLBind(pADOU, "p14", pObj->HINT_ANS);
                        ITSDb_SQLBind(pADOU, "p15", pObj->GROP_ID);
                        ITSDb_SQLBind(pADOU, "p16", pObj->OPER_SYST_ID);
                        ITSDb_SQLExec(pADOU);
                        pObj->DEL_YN  = "N";
                        pObj->MEM_DEL = "N";
                    }
                }
                else
                {
                    if (pObj->EditMode == DB_NEW)
                    {
                        ITSDb_SQLBind(pADOI, "p01", pObj->USER_ID);
                        ITSDb_SQLBind(pADOI, "p02", pObj->PWD);
                        ITSDb_SQLBind(pADOI, "p03", pObj->NAME);
                        ITSDb_SQLBind(pADOI, "p04", pObj->COMP);
                        ITSDb_SQLBind(pADOI, "p05", pObj->TEL);
                        ITSDb_SQLBind(pADOI, "p06", pObj->ADDR);
                        //ITSDb_SQLBind(pADOI, "p07", pObj->RESD_NMBR);
                        ITSDb_SQLBind(pADOI, "p08", pObj->MOBILE);
                        ITSDb_SQLBind(pADOI, "p09", pObj->EMAL);
                        //ITSDb_SQLBind(pADOI, "p10", pObj->DEL_YN);
                        //ITSDb_SQLBind(pADOI, "p11", pObj->RGSTYMD);
                        //ITSDb_SQLBind(pADOI, "p12", pObj->CRCTYMD);
                        ITSDb_SQLBind(pADOI, "p13", pObj->HINT_QUES);
                        ITSDb_SQLBind(pADOI, "p14", pObj->HINT_ANS);
                        ITSDb_SQLBind(pADOI, "p15", pObj->GROP_ID);
                        ITSDb_SQLBind(pADOI, "p16", pObj->OPER_SYST_ID);
                        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);
            FDbUpdate = true;
        }
        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();
    }
}
//---------------------------------------------------------------------------

String __fastcall TIDB0090M::GetPassword(String APswd)
{
    int    nRes;
    String sEncPswd = "";
    String sQry;

#if 1
#if 0
    sQry = "SELECT SCP.HASH_B64('71', :p01) AS PWD FROM DUAL \r\n";
#else
    sQry = "SELECT :p01 AS PWD FROM DUAL \r\n";
#endif

    TADOQuery *pADO = NULL;
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
            pADO->Connection = ITSDb_GetConnection();
            pADO->Close();
            pADO->SQL->Text = sQry;
            pADO->Parameters->ParamByName("p01")->Value = APswd;
            pADO->Prepared = true;
            pADO->Open();
            if (!pADO->Eof)
            {
                pADO->First();
                sEncPswd = pADO->FieldByName("PWD")->AsString;
            }
            pADO->Close();
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName())+E.Message);
        }
        catch (Exception &exception)
        {
            throw Exception(String(exception.ClassName())+exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
#else
    sEncPswd = String(ITSSHA256_Encrpyt(AnsiString(APswd)));
#endif

    return sEncPswd;
}
//---------------------------------------------------------------------------

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

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

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

void __fastcall TIDB0090M::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;
        }
    }
}
//---------------------------------------------------------------------------

