/****************************************************************************
 * @source      : FrmVmsOpMainF.cpp
 * @description : 어플리케이션 메인폼
 ****************************************************************************
 * DATE         AUTHOR    DESCRIPTION
 * --------------------------------------------------------------------------
 * 2011/11/22   정승호    최초작성
 *
 ****************************************************************************/
//---------------------------------------------------------------------------

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#pragma hdrstop

#include "FrmLangEditF.h"
#include "CDSAppLangF.h"

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

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxHint"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSpinEdit"
#pragma link "cxTextEdit"
#pragma link "dxBar"
#pragma link "dxCustomHint"
#pragma link "dxRibbon"
#pragma link "dxRibbonGallery"
#pragma link "dxRibbonSkins"
#pragma link "dxRibbonStatusBar"
#pragma link "dxScreenTip"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinChooserGallery"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinsdxBarPainter"
#pragma link "dxSkinsdxRibbonPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinsForm"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "dxStatusBar"
#pragma link "dxTabbedMDI"
#pragma link "cxButtons"
#pragma link "cxCalendar"
#pragma link "cxDropDownEdit"
#pragma link "cxGroupBox"
#pragma link "cxSplitter"
#pragma link "cxTimeEdit"
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxFilter"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxStyles"
#pragma link "cxMemo"
#pragma resource "*.dfm"

TFrmLangEdit *FrmLangEdit = NULL;

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

__fastcall TFrmLangEdit::TFrmLangEdit(TComponent* Owner)
    : TForm(Owner)//TdxCustomRibbonForm(Owner)//TForm(Owner)
{
    DoubleBuffered = true;

    g_AppCfg.lMainWinHandle = (long)Handle;

    CMM_SetFormPosition(g_AppCfg.bSaveForm);

    ITSSkin_Initialize(Application, "EN");
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    SetColorScheme(g_AppCfg.sSkinName);

    AppLangManager = new TAppLangManager();
}
//---------------------------------------------------------------------------

void __fastcall TFrmLangEdit::FormCreate(TObject *Sender)
{
    Application->ShowMainForm = false;

    try {
        Application->Icon->LoadFromResourceName(((unsigned int)HInstance), "MAINICON");
    } catch(...) { ShowMessage("LoadFromResourceName failed"); }

    Application->ShowMainForm = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmLangEdit::FormShow(TObject *Sender)
{
    Application->ProcessMessages();
    FormInit();
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmLangEdit::CommClose()
{
    try
    {
        ITSSkin_Term();
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmLangEdit::SetColorScheme(String ASkinName)
{
    dxSkinController1->SkinName = ASkinName;
    dxSkinController1->Refresh();
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmLangEdit::FormInit()
{
    m_pGDC = TvList->DataController;
    SelAppLanguage();
}
//---------------------------------------------------------------------------

void __fastcall TFrmLangEdit::SelAppLanguage()
{
    String     sQry;
    TADOQuery *pADO = NULL;

    CMM_ClearGridTableView(TvList);
    AppLangManager->FLists.RemoveAll();

    m_pGDC = TvList->DataController;

    sQry = "SELECT APP_NAME,                         \r\n"
           "       FORM_NAME,                        \r\n"
           "       FORM_PROP,                        \r\n"
           "       PROP_VALUE_KR,                    \r\n"
           "       PROP_VALUE_LC                     \r\n"
           "  FROM TB_APP_LANGSET                    \r\n"
           " ORDER BY APP_NAME, FORM_NAME, FORM_PROP \r\n";
    int nRow = 0;
    TcxDataController *pGDC = TvList->DataController;
    try
    {
        TvList->BeginUpdate(lsimImmediate);
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                TAppLang* pObj = new TAppLang();
                if (!pObj) continue;

                pObj->APP_NAME      = pADO->FieldByName("APP_NAME")->AsString;
                pObj->FORM_NAME     = pADO->FieldByName("FORM_NAME")->AsString;
                pObj->FORM_PROP     = pADO->FieldByName("FORM_PROP")->AsString;
                pObj->PROP_VALUE_KR = pADO->FieldByName("PROP_VALUE_KR")->AsString;
                pObj->PROP_VALUE_LC = pADO->FieldByName("PROP_VALUE_LC")->AsString;
                AppLangManager->Rows++;

                nRow = pGDC->AppendRecord();

                AppLangManager->FLists.Push(AppLangManager->Rows, pObj);

                pGDC->Values[nRow][Column00->Index] = "0";
                pGDC->Values[nRow][Column01->Index] = AppLangManager->Rows;
                pGDC->Values[nRow][Column02->Index] = pObj->APP_NAME;
                pGDC->Values[nRow][Column03->Index] = pObj->FORM_NAME;
                pGDC->Values[nRow][Column04->Index] = pObj->FORM_PROP;
                pGDC->Values[nRow][Column05->Index] = pObj->PROP_VALUE_KR;
                pGDC->Values[nRow][Column06->Index] = pObj->PROP_VALUE_LC;
                pGDC->Values[nRow][Column07->Index] = pObj->PROP_VALUE_KR;
                pGDC->Values[nRow][Column08->Index] = pObj->PROP_VALUE_LC;

                pGDC->Values[nRow][Column99->Index] = (int)pObj;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("App Language Query", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("App Language Query", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmLangEdit::BtnSearchClick(TObject *Sender)
{
    SelAppLanguage();
}
//---------------------------------------------------------------------------

void __fastcall TFrmLangEdit::TvListCustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo)
    {
        String sType = AViewInfo->GridRecord->DisplayTexts[Column00->Index];
        if (sType == "1")
        {
            ACanvas->Brush->Color = clLime;
            ACanvas->Font->Color = clBlack;
        }
    }
    //if (AViewInfo->Focused) ACanvas->Font->Color = clBlack;
}
//---------------------------------------------------------------------------

void __fastcall TFrmLangEdit::Column05PropertiesValidate(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];
    TAppLang *pObj = (TAppLang*)nMemPtr;
    if (!pObj)
    {
        return;
    }

    String PROP_VALUE_KR_C = VarToStr(m_pGDC->Values[nRow][Column05->Index]);
    String PROP_VALUE_LC_C = VarToStr(m_pGDC->Values[nRow][Column06->Index]);

    String PROP_VALUE_KR_O = VarToStr(m_pGDC->Values[nRow][Column07->Index]);
    String PROP_VALUE_LC_O = VarToStr(m_pGDC->Values[nRow][Column08->Index]);

    TcxGridColumn *pColumn = TvList->Controller->FocusedColumn;
    if (pColumn == Column05)
        PROP_VALUE_KR_C = DisplayValue;
    else
    if (pColumn == Column06)
        PROP_VALUE_LC_C = DisplayValue;

    if (PROP_VALUE_KR_C != PROP_VALUE_KR_O     ||
        PROP_VALUE_LC_C != PROP_VALUE_LC_O )
    {
        m_pGDC->Values[nRow][Column00->Index] = "1";

        pObj->PROP_VALUE_KR = PROP_VALUE_KR_C;
        pObj->PROP_VALUE_LC = PROP_VALUE_LC_C;
        pObj->Update = true;
    }
    else
    {
        m_pGDC->Values[nRow][Column00->Index] = "0";

        pObj->PROP_VALUE_KR = PROP_VALUE_KR_O;
        pObj->PROP_VALUE_LC = PROP_VALUE_LC_O;
        pObj->Update = false;
    }
}
//---------------------------------------------------------------------------

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

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

void __fastcall TFrmLangEdit::BtnSaveClick(TObject *Sender)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO  TB_APP_LANGSET L                                                    \r\n"
           "USING (SELECT :p01 AS APP_NAME,                                                 \r\n"
           "              :p02 AS FORM_NAME,                                                \r\n"
           "              :p03 AS FORM_PROP,                                                \r\n"
           "              :p04 AS PROP_VALUE_KR,                                            \r\n"
           "              :p05 AS PROP_VALUE_LC                                             \r\n"
           "         FROM DUAL) M                                                           \r\n"
           "   ON (    L.APP_NAME  = M.APP_NAME                                             \r\n"
           "       AND L.FORM_NAME = M.FORM_NAME                                            \r\n"
           "       AND L.FORM_PROP = M.FORM_PROP)                                           \r\n"
           " WHEN MATCHED THEN                                                              \r\n"
           "      UPDATE SET L.PROP_VALUE_KR = M.PROP_VALUE_KR,                             \r\n"
           "                 L.PROP_VALUE_LC = M.PROP_VALUE_LC                              \r\n"
           " WHEN NOT MATCHED THEN                                                          \r\n"
           "INSERT (L.APP_NAME, L.FORM_NAME, L.FORM_PROP, L.PROP_VALUE_KR, L.PROP_VALUE_LC) \r\n"
           "VALUES (M.APP_NAME, M.FORM_NAME, M.FORM_PROP, M.PROP_VALUE_KR, M.PROP_VALUE_LC) \r\n";

    //m_pGDC = TvList->DataController;
    try
    {
        TvList->BeginUpdate(lsimImmediate);
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Connection->BeginTrans();

            for(int ii = 0; ii < m_pGDC->RecordCount; ii++)
            {
                String sUpdate = m_pGDC->Values[ii][Column99->Index];
                if (sUpdate == "0") continue;

                int nMemPtr = TvList->DataController->Values[ii][Column99->Index];
                TAppLang *pObj = (TAppLang*)nMemPtr;
                if (!pObj)
                {
                    continue;
                }
                pObj->Update = false;

                pADO->Parameters->ParamByName("p01")->Value = pObj->APP_NAME;
                pADO->Parameters->ParamByName("p02")->Value = pObj->FORM_NAME;
                pADO->Parameters->ParamByName("p03")->Value = pObj->FORM_PROP;
                pADO->Parameters->ParamByName("p04")->Value = pObj->PROP_VALUE_KR;
                pADO->Parameters->ParamByName("p05")->Value = pObj->PROP_VALUE_LC;
                pADO->ExecSQL();

                m_pGDC->Values[ii][Column00->Index] = "0";
                m_pGDC->Values[ii][Column05->Index] = pObj->PROP_VALUE_KR;
                m_pGDC->Values[ii][Column06->Index] = pObj->PROP_VALUE_LC;
                m_pGDC->Values[ii][Column07->Index] = pObj->PROP_VALUE_KR;
                m_pGDC->Values[ii][Column08->Index] = pObj->PROP_VALUE_LC;
            }
            pADO->Connection->CommitTrans();
        }
        catch(EDatabaseError &E)
        {
            pADO->Connection->RollbackTrans();
            DBERRORMSG("App Language Update", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            pADO->Connection->RollbackTrans();
            DBERRORMSG("App Language Update", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmLangEdit::TvListDataControllerFilterChanged(TObject *Sender)
{
    CMM_SetFilterLike(TvList);
}
//---------------------------------------------------------------------------

void __fastcall TFrmLangEdit::BtnSaveAllClick(TObject *Sender)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    String sColumn = CboType->ItemIndex == 0 ? "PROP_VALUE_LC" : "PROP_VALUE_KR";
    String sFrom   = EdFrom->Text;
    String sTo     = EdTo->Text;

    sQry.printf(L"UPDATE TB_APP_LANGSET \r\n"
                 "   SET %s = '%s'      \r\n"
                 " WHERE %s = '%s'      \r\n",
                 sColumn.c_str(), sTo.c_str(),
                 sColumn.c_str(), sFrom.c_str());

    //m_pGDC = TvList->DataController;
    try
    {
        TvList->BeginUpdate(lsimImmediate);
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Connection->BeginTrans();
            pADO->ExecSQL();
            pADO->Connection->CommitTrans();
        }
        catch(EDatabaseError &E)
        {
            pADO->Connection->RollbackTrans();
            DBERRORMSG("App Language Update All", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            pADO->Connection->RollbackTrans();
            DBERRORMSG("App Language Update All", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
    }

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

void __fastcall TFrmLangEdit::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"VMS통신상태";

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

