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

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

#include "VMSM620MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalendar"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxDropDownEdit"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSpinEdit"
#pragma link "cxSplitter"
#pragma link "cxTextEdit"
#pragma link "cxTimeEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#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 "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "cxRadioGroup"
#pragma link "cxClasses"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxDBData"
#pragma link "cxFilter"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridDBTableView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxStyles"
#pragma link "cxCheckBox"
#pragma link "cxImage"
#pragma link "cxButtonEdit"
#pragma link "cxBlobEdit"
#pragma resource "*.dfm"

TVMSM620M *VMSM620M = NULL;
//---------------------------------------------------------------------------

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

    FUpdate= false;
    FTitle = Caption;//"VMS 객체이미지 선택";

	FListSymb = new TList;

    EditMode = false;
    m_bSelected = false;

    TvList->OptionsView->DataRowHeight  = 0;
    TvList->OptionsView->CellAutoHeight = true;
    TvList->OptionsView->Indicator      = true;

    TvList->OptionsCustomize->ColumnMoving  = false;
    TvList->OptionsCustomize->ColumnSorting = false;

    TvList->Columns[0]->Width = 50;
    TvList->Columns[0]->Options->HorzSizing = false;
}
//---------------------------------------------------------------------------

/*
 * 닫기버튼 이나 x버튼 클릭시 공통으로 처리하는 로직이들어간다..
 * Form과 DataModule class를 delete시킨다.
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TVMSM620M::CommClose()
{
    try
    {
        ClearSymbloList();
        SAFE_DELETE(FListSymb);
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

/*
 * Form을 보여줄때 호출되는 event 메서드이다.
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TVMSM620M::FormShow(TObject *Sender)
{
    Refresh();

    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

/*
 * form 초기화
 *
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TVMSM620M::FormInit()
{
	EdNmbr->Text = "";

    ImgPreView->Picture->Bitmap->FreeImage();
    ImgPreView->Picture->Bitmap = NULL;

    FNewDb = false;
}
//---------------------------------------------------------------------------

/*
 * Form이 Show되고 난 후 최초 1회 수행되는 타이머 이벤트
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TVMSM620M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;

    LoadImageSymbol();
}
//---------------------------------------------------------------------------

/*
 * Close 버튼 클릭 이벤트 핸들러
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TVMSM620M::BtnCloseClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------

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

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

void __fastcall TVMSM620M::DisplayInfo()
{
    FormInit();

	int nRow = TvList->DataController->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

	TcxGridDataController *pDc = TvList->DataController;

	nRow = pDc->FocusedRecordIndex;
    if (nRow < FListSymb->Count)
    {
        Graphics::TBitmap *bitmap = (Graphics::TBitmap*)FListSymb->Items[nRow];
        ImgPreView->Picture->Bitmap->Assign(bitmap);
    }

    EdNmbr->Text  = VarToStr(pDc->Values[nRow][ColLink1->Index]);
}
//---------------------------------------------------------------------------

void __fastcall TVMSM620M::ClearSymbloList()
{
	for(int ii = 0; ii < FListSymb->Count; ii++)
	{
		Graphics::TBitmap *bitmap = (Graphics::TBitmap *)FListSymb->Items[ii];
		delete bitmap;
	}
	FListSymb->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TVMSM620M::LoadImageSymbol()
{
    FormInit();

    CMM_ClearGridTableView(TvList);
    ClearSymbloList();

    String sQry;
    TADOQuery *pADO = NULL;

    if (FImageType == 1)    //이미지 심볼
    {
        sQry = "SELECT SYMB_LIB_NMBR,                       \r\n"
               "       IMAG_DATA,                           \r\n"
               "       SYMB_EXPL, SYMB_FILE_NM, SYMB_TYPE   \r\n"
               "  FROM TB_VMS_SYMB_LIB                      \r\n"
               " WHERE SYMB_LIB_NMBR BETWEEN 6000 AND 6999  \r\n"
               "   AND DEL_YN = 'N'                         \r\n"
               " ORDER BY SYMB_LIB_NMBR                     \r\n";
    }
    else
    if (FImageType == 2)    //소통정보이미지
    {
        sQry = "SELECT SYMB_LIB_NMBR,                       \r\n"
               "       IMAG_DATA,                           \r\n"
               "       SYMB_EXPL, SYMB_FILE_NM, SYMB_TYPE   \r\n"
               "  FROM TB_VMS_SYMB_LIB                      \r\n"
               " WHERE SYMB_LIB_NMBR BETWEEN 8000 AND 8999  \r\n"
               "   AND DEL_YN = 'N'                         \r\n"
               " ORDER BY SYMB_LIB_NMBR                     \r\n";
    }
    else
    if (FImageType == 3)    //소통정보배경이미지
    {
        sQry = "SELECT SYMB_LIB_NMBR,                       \r\n"
               "       IMAG_DATA,                           \r\n"
               "       SYMB_EXPL, SYMB_FILE_NM, SYMB_TYPE   \r\n"
               "  FROM TB_VMS_SYMB_LIB                      \r\n"
               " WHERE SYMB_LIB_NMBR BETWEEN 7000 AND 7999  \r\n"
               "   AND DEL_YN = 'N'                         \r\n"
               " ORDER BY SYMB_LIB_NMBR                     \r\n";
    }
    else
    if (FImageType == 4)    //동영상, 5: 스트리밍영상
    {
        sQry = "SELECT SYMB_LIB_NMBR,                       \r\n"
               "       IMAG_DATA,                           \r\n"
               "       SYMB_EXPL, SYMB_FILE_NM, SYMB_TYPE   \r\n"
               "  FROM TB_VMS_SYMB_LIB                      \r\n"
               " WHERE SYMB_LIB_NMBR BETWEEN 5000 AND 5999  \r\n"
               "   AND DEL_YN = 'N'                         \r\n"
               " ORDER BY SYMB_LIB_NMBR                     \r\n";
    }
    else return;

    int nRowHeight = 0;
    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_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                nRow = pGDC->AppendRecord();
                pGDC->Values[nRow][ColLink1->Index] = pADO->FieldByName("SYMB_LIB_NMBR")->AsString;
                pGDC->Values[nRow][ColLink2->Index] = pADO->FieldByName("IMAG_DATA")->AsVariant;
                pGDC->Values[nRow][ColLink3->Index] = pADO->FieldByName("SYMB_EXPL")->AsString;
                pGDC->Values[nRow][ColLink4->Index] = pADO->FieldByName("SYMB_TYPE")->AsString;

#if 1
    /* 이미지를 가지고 가는 곳에서 이것을 참조하기 때문에 주석으로 막지 말자.... */
				Graphics::TBitmap *bitmap = new Graphics::TBitmap;
				bitmap->LoadFromStream(pADO->CreateBlobStream(pADO->FieldByName("IMAG_DATA"), bmRead));
				FListSymb->Add(bitmap);
                if (bitmap->Height+2 > nRowHeight) nRowHeight = bitmap->Height+3;
#endif
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVMSM620M::LoadImageSymbol", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVMSM620M::LoadImageSymbol", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        //TvList->OptionsView->DataRowHeight = nRowHeight;
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSM620M::TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift,
          bool &AHandled)
{
	if (!ACellViewInfo) return;

    DisplayInfo();
    Sleep(200);
    BtnSelImageClick((TObject*)BtnSelImage);
}
//---------------------------------------------------------------------------

void __fastcall TVMSM620M::TvListFocusedRecordChanged(TcxCustomGridTableView *Sender,
          TcxCustomGridRecord *APrevFocusedRecord, TcxCustomGridRecord *AFocusedRecord,
          bool ANewItemRecordFocusingChanged)
{
    if (!AFocusedRecord) return;
    DisplayInfo();
}
//---------------------------------------------------------------------------

void __fastcall TVMSM620M::BtnSelImageClick(TObject *Sender)
{
    if (EdNmbr->Text == "")
    {
        Application->MessageBox(lblText1->Caption.c_str(),//L"VMS 객체이미지 선택\r\nVMS 객체이미지를 목록에서 선택하세요.",
                                FTitle.c_str(),
                                MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }
    m_bSelected = true;
    Close();
}
//---------------------------------------------------------------------------

