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


#pragma hdrstop

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

#include "VMSM500MF.h"
#include "VMSM500MSRCF.h"

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

#pragma package(smart_init)

//폼유형
void __fastcall TVMSM500M::FillVmsFormType(TcxComboBox *ACombo)
{
    String     sQry;
    TADOQuery *pADO = NULL;

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

    sQry = "SELECT VMS_FORM_TYPE_CD AS CD,   \r\n"
           "       VMS_FORM_TYPE_NM AS NM,   \r\n"
           "       USE_YN                    \r\n"
           "  FROM TB_VMS_FORM_TYPE          \r\n"
           " ORDER BY CD ASC                 \r\n";

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

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                String sUse = pADO->FieldByName("USE_YN")->AsString;
                if (sUse != "Y") continue;

                String sCd = pADO->FieldByName("CD")->AsString;
                String sNm = pADO->FieldByName("NM")->AsString;
    			ACombo->Properties->Items->Add(" [" + sCd + "] " + sNm);
            }
            ACombo->ItemIndex = 0;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVMSM500M::FillVmsFormType", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            DBERRORMSG("TVMSM500M::FillVmsFormType", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
}
//---------------------------------------------------------------------------
//표출방법
void __fastcall TVMSM500M::FillVmsFormDispMode(TcxComboBox *ACombo)
{
    String     sQry;
    TADOQuery *pADO = NULL;

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

    sQry = "SELECT VMS_FORM_DSPL_MTHD_CD AS CD,  \r\n"
           "       VMS_FORM_DSPL_MTHD_NM AS NM,  \r\n"
           "       USE_YN                        \r\n"
           "  FROM TB_VMS_FORM_DSPL_MTHD         \r\n"
           " ORDER BY CD ASC                     \r\n";

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

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                String sUse = pADO->FieldByName("USE_YN")->AsString;
                if (sUse != "Y") continue;

                String sCd = pADO->FieldByName("CD")->AsString;
                String sNm = pADO->FieldByName("NM")->AsString;
    			ACombo->Properties->Items->Add(" [" + sCd + "] " + sNm);
            }
            ACombo->ItemIndex = 0;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVMSM500M::FillVmsFormDispMode", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            DBERRORMSG("TVMSM500M::FillVmsFormDispMode", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
}
//---------------------------------------------------------------------------

//표출방향
void __fastcall TVMSM500M::FillVmsFormDispDir(TcxComboBox *ACombo)
{
    String     sQry;
    TADOQuery *pADO = NULL;

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

    sQry = "SELECT VMS_FORM_DSPL_DRCT_CD AS CD,  \r\n"
           "       VMS_FORM_DSPL_DRCT_NM AS NM,  \r\n"
           "       USE_YN                        \r\n"
           "  FROM TB_VMS_FORM_DSPL_DRCT         \r\n"
           " ORDER BY CD ASC                     \r\n";

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

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                String sUse = pADO->FieldByName("USE_YN")->AsString;
                if (sUse != "Y") continue;

                String sCd = pADO->FieldByName("CD")->AsString;
                String sNm = pADO->FieldByName("NM")->AsString;
    			ACombo->Properties->Items->Add(" [" + sCd + "] " + sNm);
            }
            ACombo->ItemIndex = 0;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVMSM500M::FillVmsFormDispDir", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            DBERRORMSG("TVMSM500M::FillVmsFormDispDir", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
}
//---------------------------------------------------------------------------

//VMS Font
void __fastcall TVMSM500M::FillVmsFont(TcxComboBox *ACombo)
{
    String     sQry;
    TADOQuery *pADO = NULL;

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

    sQry = "SELECT VMS_FONT_NAME_CD AS CD,   \r\n"
           "       VMS_FONT_NAME_NM AS NM,   \r\n"
           "       USE_YN                    \r\n"
           "  FROM TB_VMS_FONT_NAME          \r\n"
           " ORDER BY CD ASC                 \r\n";

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

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                String sUse = pADO->FieldByName("USE_YN")->AsString;
                if (sUse != "Y") continue;

                String sCd = pADO->FieldByName("CD")->AsString;
                String sNm = pADO->FieldByName("NM")->AsString;
    			ACombo->Properties->Items->Add(" [" + sCd + "] " + sNm);
            }
            ACombo->ItemIndex = 0;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVMSM500M::FillVmsFont", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            DBERRORMSG("TVMSM500M::FillVmsFont", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSM500M::LoadFormObjectInfo(String AFormId, TVmsFormDesigner *ADesigner, String AFormType)
{
    ADesigner->Clear();

    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.*,                                       \r\n"
           "       C.VMS_FONT_NAME_NM,                        \r\n"
           "       B.IMAG_DATA AS SYMB_IMG,                   \r\n"
           "       B.SYMB_EXPL AS SYMB_IMG_NM                 \r\n"
           "  FROM TB_VMS_FORM_OBJECT  A,                     \r\n"
           "       TB_VMS_SYMB_LIB     B,                     \r\n"
           "       TB_VMS_FONT_NAME    C                      \r\n"
           " WHERE A.VMS_FORM_ID      = :p01                  \r\n"
           "   AND A.SYMB_LIB_NMBR    = B.SYMB_LIB_NMBR(+)    \r\n"
           "   AND A.VMS_FONT_NAME_CD = C.VMS_FONT_NAME_CD(+) \r\n"
           " ORDER BY A.VMS_FORM_OBJECT_ID ASC                \r\n";

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

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", AFormId);
            ITSDb_SQLOpen(pADO);

            int    nDrawType;
            String sObjKind;
            for( ; !pADO->Eof; pADO->Next())
            {
                int nFormId = pADO->FieldByName("VMS_FORM_ID")->AsInteger;
                int nObjId  = pADO->FieldByName("VMS_FORM_OBJECT_ID")->AsInteger;

                int nObjType = pADO->FieldByName("VMS_FORM_OBJECT_TYPE_CD")->AsInteger;
                if (nObjType ==   1 || //이미지 심볼
                    nObjType ==   2 || //파일이미지
                    nObjType ==   3 || //소통정보이미지
                    nObjType ==  17 || //소통이미지1
                    nObjType ==  27 || //소통이미지2
                    nObjType ==  37 || //소통이미지3
                    nObjType ==  47 || //소통이미지4
                    nObjType == 200 || //동영상
                    nObjType == 300 || //스트리밍영상
                    nObjType == 406 || //@통합대기등급 이미지
                    nObjType == 407 || //@미세먼지등급 이미지
                    nObjType == 408 || //@초미세먼지등급 이미지
                    nObjType == 413    //@오존등급이미지
                    )
                {
                    if (nObjType == 3) sObjKind = OBJKIND_IMAGE;
                    else               sObjKind = OBJKIND_IMAGEID;

                    nDrawType = 1;  //이미지그리기
                }
                else
                {
                    sObjKind  = OBJKIND_TEXT;  //문자열
                    nDrawType = 0;  //문자열그리기
                }

                int    nBlinking = pADO->FieldByName("VMS_DSPL_BLINKING")->AsInteger;
                int    nPosX     = pADO->FieldByName("VMS_DSPL_XCRDN")->AsInteger;
                int    nPosY     = pADO->FieldByName("VMS_DSPL_YCRDN")->AsInteger;
                int    nWidth    = pADO->FieldByName("VMS_DSPL_WIDTH")->AsInteger;
                int    nHeight   = pADO->FieldByName("VMS_DSPL_HEIGHT")->AsInteger;

                TVmsFormObject *pObj = ADesigner->AddObject((VmsObjectDrawType)nDrawType);
                if (!pObj) continue;

                if (nObjType == 200 || nObjType == 300)
                {
                    pObj->EditMode = false;
                }
                String sIfscId  = pADO->FieldByName("VMS_IFSC_ID")->AsString;
                String sFillCd  = pADO->FieldByName("TRFC_FILL_CD")->AsString;

                pObj->ObjSeq      = nObjId;
                pObj->ObjId       = nObjId;
                pObj->ObjKind     = sObjKind;

                pObj->Tag         = sIfscId;
                if (AFormType == "15")
                {
                    if (nObjType == 0 || nObjType == 1 || nObjType == 2)
                    {
                        pObj->Tag = sFillCd;
                    }
                }

                pObj->ObjType     = pADO->FieldByName("VMS_FORM_OBJECT_TYPE_CD")->AsString;
                pObj->ObjSize     = pADO->FieldByName("VMS_DSPL_SIZE")->AsInteger;
                pObj->BkColorCode = pADO->FieldByName("VMS_DSPL_BKCOLOR")->AsString;
                pObj->Blink       = nBlinking == 1 ? true : false;

                pObj->ImageId     = pADO->FieldByName("SYMB_LIB_NMBR")->AsString;
                pObj->Text        = pADO->FieldByName("VMS_DSPL_TXT")->AsString;
                pObj->RunText     = pObj->Text;

                pObj->SetPos(nPosX, nPosY);
                if (pObj->ObjKind == OBJKIND_TEXT)    // 문자열
                {
                    int nFontBold       = pADO->FieldByName("VMS_FONT_BOLD")->AsInteger;

                    pObj->FontName      = pADO->FieldByName("VMS_FONT_NAME_NM")->AsString;
                    pObj->FontNameCode  = pADO->FieldByName("VMS_FONT_NAME_CD")->AsString;
                    pObj->FontColorCode = pADO->FieldByName("VMS_FONT_COLR_CD")->AsString;
                    pObj->FontSize      = pADO->FieldByName("VMS_FONT_SIZE")->AsInteger;
                    pObj->FontBold      = nFontBold == 1 ? true : false;
                    pObj->TextAlign     = pADO->FieldByName("VMS_FONT_ALIGN")->AsInteger;
                    //pObj->SetPos(nPosX, nPosY);
                    //pObj->SetSize(pADO->FieldByName("VMS_DSPL_WIDTH")->AsInteger, pADO->FieldByName("VMS_DSPL_HEIGHT")->AsInteger);
                }
                else //if (sObjKind == "1" || sObjKind == "2")    // 이미지(1), 이미지 ID(2)
                {
                    if (pObj->ObjKind == OBJKIND_IMAGEID)
                    {
                        if (pObj->Text.Trim() == "")
                        {
                            pObj->Text = pObj->ImageId;
                        }
                    }

                    TStream *pStream = NULL;
                    pStream = pADO->CreateBlobStream(pADO->FieldByName("VMS_DSPL_FIGR"), bmRead);

                    try
                    {
                        if (pStream && pStream->Size > 0)
                        {
                            Graphics::TBitmap *Bitmap = pObj->GetBitmap();
                            pStream->Position = 0;
                            Bitmap->LoadFromStream(pStream);

#ifdef IMAGE_RESIZE
#if 0
                            if (nObjType == 1 || nObjType == 2)
                            {
                                pObj->GetImage()->AutoSize= false;
                                pObj->GetImage()->Stretch = true;
                                pObj->GetImage()->Width  = nWidth;
                                pObj->GetImage()->Height = nHeight;
                                pObj->SetSize(nWidth, nHeight);
                                pObj->SetPos(nPosX, nPosY);
                            }
                            else
                            {
                                pObj->SetSize(Bitmap->Width, Bitmap->Height);
                            }
#else
                            switch(nObjType)
                            {
                            case   1: //심볼
                            case   2: //이미지
                            case   3: //소통정보배경이미지
                            case  17: //소통정보이미지1
                            case  27: //소통정보이미지2
                            case  37: //소통정보이미지3
                            case  47: //소통정보이미지4
                            case 167: //@우회소통정보이미지
                            case 200: //동영상이미지
                            case 300: //스트리밍영상이미지
                            case 406: //@통합대기등급 이미지
                            case 407: //@미세먼지등급 이미지
                            case 408: //@초미세먼지등급 이미지
                            case 413: //@오존등급이미지
                                pObj->GetImage()->AutoSize= false;
                                pObj->GetImage()->Stretch = true;
                                pObj->GetImage()->Width  = nWidth;
                                pObj->GetImage()->Height = nHeight;
                                pObj->SetSize(nWidth, nHeight);
                                pObj->SetPos(nPosX, nPosY);
                                break;
                            default:
                                pObj->SetSize(Bitmap->Width, Bitmap->Height);
                                pObj->SetPos(nPosX, nPosY); //TODO
                                break;
                            }
#endif
#else
                            pObj->SetSize(Bitmap->Width, Bitmap->Height);
                            pObj->SetPos(nPosX, nPosY);
#endif
                        }
                    }
                    __finally
                    {
                        SAFE_DELETE(pStream);
                    }
                }

                //pObj->SetPos(nPosX, nPosY);
                //if (pObj->Left < 0) pObj->Left = 0;
                //if (pObj->Top  < 0) pObj->Top  = 0;
                pObj->Show();
                pObj->BringToFront();
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVMSM500M::LoadFormObjectInfo", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            DBERRORMSG("TVMSM500M::LoadFormObjectInfo", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSM500M::LoadVmsForm()
{
    FormClear();

    int nFocusedIdx = TvList->DataController->FocusedRecordIndex;
    CMM_ClearGridTableView(TvList);
    ClearSymbloList();

    TVmsSizeType *pVmsSizeType = APP_GetVmsSizeTypeObject(CbSizeType);
    if (pVmsSizeType == NULL) return;

    String sWhereQry = "";
    sWhereQry = " WHERE A.VMS_FORM_ID = B.VMS_FORM_ID(+) \r\n";
    if (pVmsSizeType->TypeCd != "ALL")
    {
        //sWhereQry = " WHERE A.VMS_TYPE_CD = '" + pVmsSizeType->TypeCd + "' \r\n";
        sWhereQry += "   AND A.VMS_TYPE_CD = '" + pVmsSizeType->TypeCd + "' \r\n";
    }

    FVmsTypeCd = pVmsSizeType->TypeCd;
    FVmsWidth  = pVmsSizeType->Width;
    FVmsHeight = pVmsSizeType->Height;
    FVmsCols   = pVmsSizeType->Cols;
    FVmsRows   = pVmsSizeType->Rows;

    ImgForm->Width  = FVmsWidth;
    ImgForm->Height = FVmsHeight;
    ImgForm->Picture->Bitmap->Width  = FVmsWidth;
    ImgForm->Picture->Bitmap->Height = FVmsHeight;

    {
        PnlBack->Left = 5;
        PnlBack->Top  = 5;
        PnlBack->Width = FVmsWidth+4;
        PnlBack->Height= FVmsHeight+4;
    }

    {
        PnlEdtBack->Width = FVmsWidth+4;
        PnlEdtBack->Height= FVmsHeight+4;

        PnlRePreview->Width = FVmsWidth+4;
        PnlRePreview->Height= FVmsHeight+4;
        PnlRePreview->Color = clWhite;

        ImgEdtPreview->Left = 2;
        ImgEdtPreview->Top  = 2;
        ImgEdtPreview->Width = FVmsWidth;
        ImgEdtPreview->Height= FVmsHeight;
        ImgEdtPreview->Picture->Bitmap->Width = FVmsWidth;
        ImgEdtPreview->Picture->Bitmap->Height = FVmsHeight;
        m_pEdtDesigner->DrawPreviewModuleBack(ImgEdtPreview, clBlack, FVmsRows, FVmsCols);
#if 0
#if 0
        ReForm->Width = FVmsWidth+22+4;
        ReForm->Height= FVmsHeight+22+4;
#else
        ReForm->Width = FVmsWidth+4;
        ReForm->Height= FVmsHeight+4;
#endif
#endif
        ReForm->Color = clBlack;

        m_pEdtDesigner->Clear();
        m_pEdtDesigner->SizeType = FVmsTypeCd;
        m_pEdtDesigner->SetSize(FVmsWidth, FVmsHeight);
        m_pEdtDesigner->SetPos(0, 0);
        m_pEdtDesigner->BkColorCode = "0";

        m_pEdtDesigner->EditMode = true;
        m_pEdtDesigner->Show();
    }

    String sVmsSizeNm = pVmsSizeType->TypeNm;

    String sQry;
    TADOQuery *pADO = NULL;

#if 0
    sQry = "SELECT A.VMS_FORM_ID, A.VMS_FORM_NM, A.VMS_FORM_EXPL,      \r\n"
           "       A.VMS_FORM_TYPE_CD, A.VMS_TYPE_CD,                  \r\n"
           "       (SELECT VMS_TYPE_NM                                 \r\n"
           "          FROM TB_VMS_TYPE                                 \r\n"
           "         WHERE VMS_TYPE_CD = A.VMS_TYPE_CD                 \r\n"
           "           AND USE_YN = 'Y') AS VMS_SIZE_TYPE_NM,          \r\n"
           "       (SELECT VMS_FORM_TYPE_NM                            \r\n"
           "          FROM TB_VMS_FORM_TYPE                            \r\n"
           "         WHERE USE_YN = 'Y'                                \r\n"
           "           AND VMS_FORM_TYPE_CD = A.VMS_FORM_TYPE_CD)      \r\n"
           "       AS FORM_TYPE_NM,                                    \r\n"
           "       A.VMS_FORM_COLR_CD,                                 \r\n"
           "       A.VMS_FORM_DSPL_DRCT_CD,                            \r\n"
           "       A.VMS_FORM_DSPL_MTHD_CD,                            \r\n"
           "       A.VMS_FORM_IMAG                                     \r\n"
           "  FROM TB_VMS_FORM A                                       \r\n";
    sQry += sWhereQry;
    sQry +=" ORDER BY A.VMS_TYPE_CD DESC, A.VMS_FORM_ID ASC            \r\n";
#else
    sQry = "SELECT A.VMS_FORM_ID, A.VMS_FORM_NM, A.VMS_FORM_EXPL,                                                \r\n"
           "       A.VMS_FORM_TYPE_CD, A.VMS_TYPE_CD,                                                            \r\n"
           "       (SELECT VMS_TYPE_NM                                                                           \r\n"
           "          FROM TB_VMS_TYPE                                                                           \r\n"
           "         WHERE VMS_TYPE_CD = A.VMS_TYPE_CD                                                           \r\n"
           "           AND USE_YN = 'Y') AS VMS_SIZE_TYPE_NM,                                                    \r\n"
           "       (SELECT VMS_FORM_TYPE_NM                                                                      \r\n"
           "          FROM TB_VMS_FORM_TYPE                                                                      \r\n"
           "         WHERE USE_YN = 'Y'                                                                          \r\n"
           "           AND VMS_FORM_TYPE_CD = A.VMS_FORM_TYPE_CD)                                                \r\n"
           "       AS FORM_TYPE_NM,                                                                              \r\n"
           "       A.VMS_FORM_COLR_CD,                                                                           \r\n"
           "       A.VMS_FORM_DSPL_DRCT_CD,                                                                      \r\n"
           "       A.VMS_FORM_DSPL_MTHD_CD,                                                                      \r\n"
           "       A.VMS_FORM_IMAG,                                                                              \r\n"
           "       B.VMS_DSPL_TXT                                                                                \r\n"
           "  FROM TB_VMS_FORM A,                                                                                \r\n"
           "       (SELECT VMS_FORM_ID,                                                                          \r\n"
           "               LISTAGG(VMS_DSPL_TXT, ' ') WITHIN GROUP (ORDER BY VMS_FORM_OBJECT_ID) AS VMS_DSPL_TXT \r\n"
           "          FROM TB_VMS_FORM_OBJECT                                                                    \r\n"
           "         WHERE VMS_FORM_OBJECT_TYPE_CD = '0'                                                         \r\n"
           "         GROUP BY VMS_FORM_ID) B                                                                     \r\n";
    sQry += sWhereQry;
    sQry +=" ORDER BY A.VMS_TYPE_CD DESC, A.VMS_FORM_ID ASC                                                      \r\n";
#endif

    String sFirstFormId = "";
    try
    {
        int nRow;
        TcxDataController *pGDC = TvList->DataController;
        TvList->BeginUpdate();
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);
            //ITSDb_SQLBind(pADO, "p01", sVmsTypeCd);
            ITSDb_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                nRow = pGDC->AppendRecord();
                String sFormId = pADO->FieldByName("VMS_FORM_ID")->AsString;
                if (sFirstFormId == "") sFirstFormId = sFormId;

                pGDC->Values[nRow][Col01->Index] = sFormId;          //폼ID
                pGDC->Values[nRow][Col02->Index] = pADO->FieldByName("VMS_FORM_NM")->AsVariant;             //폼명칭
                pGDC->Values[nRow][Col03->Index] = pADO->FieldByName("VMS_FORM_EXPL")->AsString;            //폼설명
                pGDC->Values[nRow][Col04->Index] = pADO->FieldByName("FORM_TYPE_NM")->AsString;             //폼종류
                pGDC->Values[nRow][Col05->Index] = pADO->FieldByName("VMS_SIZE_TYPE_NM")->AsString;         //폼크기
                pGDC->Values[nRow][Col06->Index] = pADO->FieldByName("VMS_FORM_DSPL_MTHD_CD")->AsString;    //표출방법코드-
                pGDC->Values[nRow][Col07->Index] = pADO->FieldByName("VMS_FORM_DSPL_DRCT_CD")->AsString;    //표출방향코드-
                pGDC->Values[nRow][Col08->Index] = pADO->FieldByName("VMS_FORM_COLR_CD")->AsString;         //배경색코드-
                pGDC->Values[nRow][Col09->Index] = pADO->FieldByName("VMS_FORM_TYPE_CD")->AsString;         //폼종류코드-
                pGDC->Values[nRow][Col10->Index] = pADO->FieldByName("VMS_TYPE_CD")->AsString;              //폼크기코드-
                pGDC->Values[nRow][Col12->Index] = pADO->FieldByName("VMS_DSPL_TXT")->AsString;             //폼표출문자

				Graphics::TBitmap *bitmap = new Graphics::TBitmap;
				try {
                bitmap->LoadFromStream(pADO->CreateBlobStream(pADO->FieldByName("VMS_FORM_IMAG"), bmRead));
				} catch(Exception &e) {
                    //throw Exception(String(e.ClassName()) + e.Message);
                }
                FListSymb->Add(bitmap);
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVMSM500M::LoadVmsForm", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVMSM500M::LoadVmsForm", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
        if (TvList->DataController->RecordCount > 0)
        {
            //CMM_SetGridRow(CxList, TvList, sFirstFormId, Col01->Index);
            //DisplayInfo();
        }
    }
}
//---------------------------------------------------------------------------

bool __fastcall TVMSM500M::SaveVmsForm(String AVALID_YN)
{
    String sQry;
    TADOQuery *pADO = NULL;

    String sFormId    = EdFormId->Text;
    String sVmsType   = FVmsTypeCd;
    String sFormType  = APP_GetCode(CbFormKind);
    String sName      = EdFormName->Text.Trim();
    String sExpl      = EdFormDesc->Text.Trim();
    String sFormColor = String(CbFormColor->ItemIndex);
    String sDispDir   = APP_GetCode(CbFormDispDir);
    String sDispMode  = APP_GetCode(CbFormDispMode);
    String sUpdtDt    = Now().FormatString("yyyymmddhhnnss");

    sQry = "MERGE INTO TB_VMS_FORM L                                         \r\n"
           "USING (SELECT :p01 AS VMS_FORM_ID,                               \r\n"
           "              :p02 AS VMS_TYPE_CD,                               \r\n"
           "              :p03 AS VMS_FORM_TYPE_CD,                          \r\n"
           "              :p04 AS VMS_FORM_NM,                               \r\n"
           "              :p05 AS VMS_FORM_EXPL,                             \r\n"
           "              :p06 AS VMS_FORM_COLR_CD,                          \r\n"
           "              :p07 AS VMS_FORM_DSPL_DRCT_CD,                     \r\n"
           "              :p08 AS VMS_FORM_DSPL_MTHD_CD,                     \r\n"
           "              :p09 AS UPDT_DT,                                   \r\n"
           "              :p10 AS VALID_YN                                   \r\n"
           "         FROM DUAL) M                                            \r\n"
           "        ON (L.VMS_FORM_ID = M.VMS_FORM_ID)                       \r\n"
           "WHEN MATCHED                                                     \r\n"
           "THEN                                                             \r\n"
           "   UPDATE SET L.VMS_FORM_NM           = M.VMS_FORM_NM,           \r\n"
           "              L.VMS_FORM_EXPL         = M.VMS_FORM_EXPL,         \r\n"
           "              L.VMS_FORM_COLR_CD      = M.VMS_FORM_COLR_CD,      \r\n"
           "              L.VMS_FORM_DSPL_DRCT_CD = M.VMS_FORM_DSPL_DRCT_CD, \r\n"
           "              L.VMS_FORM_DSPL_MTHD_CD = M.VMS_FORM_DSPL_MTHD_CD, \r\n"
           "              L.UPDT_DT               = M.UPDT_DT,               \r\n"
           "              L.VALID_YN              = M.VALID_YN               \r\n"
           "WHEN NOT MATCHED                                                 \r\n"
           "THEN                                                             \r\n"
           "   INSERT (VMS_FORM_ID,                                          \r\n"
           "           VMS_TYPE_CD,                                          \r\n"
           "           VMS_FORM_TYPE_CD,                                     \r\n"
           "           VMS_FORM_COLR_CD,                                     \r\n"
           "           VMS_FORM_NM,                                          \r\n"
           "           VMS_FORM_EXPL,                                        \r\n"
           "           VMS_FORM_DSPL_DRCT_CD,                                \r\n"
           "           VMS_FORM_DSPL_MTHD_CD,                                \r\n"
           "           UPDT_DT,                                              \r\n"
           "           VALID_YN )                                            \r\n"
           "   VALUES (M.VMS_FORM_ID,                                        \r\n"
           "           M.VMS_TYPE_CD,                                        \r\n"
           "           M.VMS_FORM_TYPE_CD,                                   \r\n"
           "           M.VMS_FORM_COLR_CD,                                   \r\n"
           "           M.VMS_FORM_NM,                                        \r\n"
           "           M.VMS_FORM_EXPL,                                      \r\n"
           "           M.VMS_FORM_DSPL_DRCT_CD,                              \r\n"
           "           M.VMS_FORM_DSPL_MTHD_CD,                              \r\n"
           "           M.UPDT_DT,                                            \r\n"
           "           M.VALID_YN )                                          \r\n";

	TMemoryStream *dataStream = new TMemoryStream();
   	ImgForm->Picture->Bitmap->SaveToStream(dataStream);
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);

            ITSDb_SQLBind(pADO, "p01", sFormId);
            ITSDb_SQLBind(pADO, "p02", sVmsType);
            ITSDb_SQLBind(pADO, "p03", sFormType);
            ITSDb_SQLBind(pADO, "p04", sName);
            ITSDb_SQLBind(pADO, "p05", sExpl);
            ITSDb_SQLBind(pADO, "p06", sFormColor);
            ITSDb_SQLBind(pADO, "p07", sDispDir);
            ITSDb_SQLBind(pADO, "p08", sDispMode);
            ITSDb_SQLBind(pADO, "p09", sUpdtDt);
            ITSDb_SQLBind(pADO, "p10", AVALID_YN);
			ITSDb_SQLExec(pADO);

    sQry = "UPDATE TB_VMS_FORM          \r\n"
           "   SET VMS_FORM_IMAG = :p02 \r\n"
           " WHERE VMS_FORM_ID   = :p01 \r\n";

            ITSDb_SQLText(pADO, sQry);

            ITSDb_SQLBind(pADO, "p01", sFormId);
            if (dataStream->Size == 0)
            {
    		    ITSDb_SQLBind(pADO, "p01", "");
			}
            else
            {
    		    pADO->Parameters->ParamByName("p02")->LoadFromStream(dataStream, ftBlob);
            }
            ITSDb_SQLExec(pADO);

            if (AVALID_YN == "N")
            {
                //폼 유효성이 N 인 경우 해당폼을 사용하는 스케쥴의 사용여부를 모두 N으로 업데이트 한다.
    sQry = "UPDATE TB_VMS_DSPL_SCH      \r\n"
           "   SET USE_YN      = 'N'    \r\n"
           " WHERE VMS_FORM_ID = :p01   \r\n";

                ITSDb_SQLText(pADO, sQry);
                ITSDb_SQLBind(pADO, "p01", sFormId);
                ITSDb_SQLExec(pADO);
            }

            CDSVmsFormManager->AddVmsForm(sFormId, AVALID_YN);

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TVMSM500M::SaveVmsForm", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TVMSM500M::SaveVmsForm", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        if (dataStream) delete dataStream;
        dataStream = NULL;
    }

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

bool __fastcall TVMSM500M::CheckVmsFormUse(String AFormId)
{
    String sQry;
    TADOQuery *pADO = NULL;

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

#if 0
    sQry = "SELECT COUNT(1) AS CNT       \r\n"
           "  FROM TB_VMS_PASV_MSG_DETL  \r\n"
           " WHERE VMS_FORM_ID = :p01    \r\n";

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", AFormId);
            ITSDb_SQLOpen(pADO);
            if (pADO->FieldByName("CNT")->AsInteger > 0)
            {
                Application->MessageBox(L"VMS 폼 사용확인\r\n삭제할 VMS 폼 정보가 VMS 고정메시지 스케쥴에 사용되고 있습니다.\r\n먼저 해당 정보를 삭제해주시기 바랍니다.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
                return false;
            }
#endif

#if 1
    sQry = "SELECT VMS_SCH_TYPE, COUNT(1) AS CNT \r\n"
           "  FROM TB_VMS_DSPL_SCH               \r\n"
           " WHERE VMS_FORM_ID = :p01            \r\n"
           " GROUP BY VMS_SCH_TYPE               \r\n";

            String sSchedule = "";

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", AFormId);
            ITSDb_SQLOpen(pADO);
            for( ; !pADO->Eof; pADO->Next())
            {
                if (pADO->FieldByName("CNT")->AsInteger > 0)
                {
                    String VMS_SCH_TYPE = pADO->FieldByName("VMS_SCH_TYPE")->AsString;
                    if (VMS_SCH_TYPE == "A")
                    {
                        if (sSchedule != "") sSchedule = sSchedule + ", 자동";
                        else                 sSchedule = "자동";
                    }
                    else
                    if (VMS_SCH_TYPE == "F")
                    {
                        if (sSchedule != "") sSchedule = sSchedule + ", 고정";
                        else                 sSchedule = "고정";
                    }
                    else
                    if (VMS_SCH_TYPE == "B")
                    {
                        if (sSchedule != "") sSchedule = sSchedule + ", 기본";
                        else                 sSchedule = "기본";
                    }
                }
            }
            if (sSchedule != "")
            {
                String sMsg;
                sMsg = "VMS 폼 사용확인\r\n삭제할 VMS 폼 정보가 [" + sSchedule + "] 스케쥴에 사용되고 있습니다.\r\n먼저 해당 정보를 삭제해주시기 바랍니다.";
                Application->MessageBox(sMsg.c_str(), FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
                return false;
            }
#else
    sQry = "SELECT COUNT(1) AS CNT      \r\n"
           "  FROM TB_VMS_DSPL_SCH      \r\n"
           " WHERE VMS_FORM_ID = :p01   \r\n";

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", AFormId);
            ITSDb_SQLOpen(pADO);
            if (pADO->FieldByName("CNT")->AsInteger > 0)
            {
                //Application->MessageBox(L"VMS 폼 사용확인\r\n삭제할 VMS 폼 정보가 VMS 자동 스케쥴에 사용되고 있습니다.\r\n먼저 해당 정보를 삭제해주시기 바랍니다.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
                Application->MessageBox(L"VMS 폼 사용확인\r\n삭제할 VMS 폼 정보가 VMS 스케쥴에 사용되고 있습니다.\r\n먼저 해당 정보를 삭제해주시기 바랍니다.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
                return false;
            }
#endif
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVMSM500M::CheckVmsFormUse", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVMSM500M::CheckVmsFormUse", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return true;
}
//---------------------------------------------------------------------------

bool __fastcall TVMSM500M::DelVmsForm(String AFormId)
{
    String sQry;
    TADOQuery *pADO = NULL;

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

    sQry = "DELETE TB_VMS_FORM_OBJECT   \r\n"
           " WHERE VMS_FORM_ID = :p01   \r\n";

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", AFormId);
			pADO->ExecSQL();

    sQry = "DELETE TB_VMS_FORM        \r\n"
           " WHERE VMS_FORM_ID = :p01 \r\n";

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", AFormId);
			pADO->ExecSQL();
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVMSM500M::DelVmsForm", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVMSM500M::DelVmsForm", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return true;
}
//---------------------------------------------------------------------------

bool __fastcall TVMSM500M::GetNextVmsFormId(int &AFormId)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT NVL(MAX(VMS_FORM_ID), 9999)+1 AS NEWID FROM TB_VMS_FORM \r\n";

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

			pADO->Open();
            AFormId = pADO->FieldByName("NEWID")->AsInteger;
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TVMSM500M::GetNextVmsFormId", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TVMSM500M::GetNextVmsFormId", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

