/****************************************************************************
 * @source      :
 * @description : 
 ****************************************************************************
 * DATE         AUTHOR    DESCRIPTION
 * --------------------------------------------------------------------------
 * 2011/12/24   정승호    최초작성
 *
 ****************************************************************************/
//---------------------------------------------------------------------------

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

#include "FrmVmsLogDnldSubF.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 "cxEdit"
#pragma link "cxFilter"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxSplitter"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#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 "cxLabel"
#pragma resource "*.dfm"

//TFrmVmsLogDnldSub *FrmVmsLogDnldSub = NULL;

//---------------------------------------------------------------------------
__fastcall TFrmVmsLogDnldSub::TFrmVmsLogDnldSub(TComponent* Owner, HWND hHandle, String sFrom, String sTo, String sIdList)
    : TForm(Owner)
{           
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    //CMM_LoadForm(g_sFormsDir, this);

    FParent     = hHandle;
    FStDateTime = sFrom;
    FEdDateTime = sTo;
    FIdList     = sIdList;
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDnldSub::FormInit()
{
    ADOQry->Connection = ITSDb_GetConnection();
    m_pGDC = TvList->DataController;

    TvList->OptionsView->NoDataToDisplayInfoText = Caption + ":  " + lblText1->Caption;//조회결과가 없습니다.";
    LblSearch->Caption = lblText2->Caption + FStDateTime.SubString(1, 10) + " ~ " + FEdDateTime.SubString(1, 10);
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmVmsLogDnldSub::CommClose()
{
    //CMM_SaveForm(g_sFormsDir, this);
    //FrmVmsLogSub = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDnldSub::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false; 
    SelHistory();
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDnldSub::ChkExpandClick(TObject *Sender)
{
    CMM_ExpandCollapseChk(TvList, ChkExpand->Checked);
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDnldSub::SelHistory()
{
    TSqlCursor sqlCrs;

    String sQry;
    TADOQuery *pADO = ADOQry;

    sQry = "SELECT A.*, B.VMS_CTLR_ID, B.VMS_NM         \r\n"
           "  FROM TB_VMS_DOWNLOAD A,                   \r\n"
           "       TB_VMS_CTLR     B                    \r\n"
           " WHERE B.VMS_CTLR_NMBR IN(" + FIdList + ")  \r\n"
           "   AND A.RGST_DT BETWEEN :p01 AND :p02      \r\n"
           "   AND A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR    \r\n";

    try
    {
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", FStDateTime);
        ITSDb_SQLBind(pADO, "p02", FEdDateTime);
        ITSDb_SQLOpen(pADO);
    }
    catch(EDatabaseError &E)
    {
        DBERRORMSG("History Qeury", String(E.ClassName()), E.Message, sQry);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        DBERRORMSG("History Qeury", String(e.ClassName()), e.Message, sQry);
        throw Exception(String(e.ClassName()) + e.Message);
    }
    DspHistory();
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDnldSub::DspHistory()
{
    TADOQuery *pADO = ADOQry;

    try
    {
    	TcxGridChartSeries *pChart = NULL;

        CMM_ClearGridTableView(TvList);

        int nRow = 0;
        int nDataCnt = 0;
        try {
            TvList->BeginUpdate(lsimImmediate);

            nDataCnt = pADO->RecordCount;
            m_pGDC->RecordCount = nDataCnt;

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                m_pGDC->Values[nRow][Col00->Index] = pADO->FieldByName("VMS_CTLR_NMBR")->AsString;
                m_pGDC->Values[nRow][Col01->Index] = pADO->FieldByName("VMS_CTLR_ID")->AsString;
                m_pGDC->Values[nRow][Col02->Index] = pADO->FieldByName("VMS_NM")->AsString;
                m_pGDC->Values[nRow][Col03->Index] = ITSUtil_StrToDateTime(pADO->FieldByName("RGST_DT")->AsString).FormatString(STR_DATETIME);

                String sFileType = pADO->FieldByName("FILETYPE")->AsString;
                //다운로드파일유형(0:Local폼,1:이미지심볼,2:기본스케쥴)
                if      (sFileType == "0") sFileType = lblFileTp0->Caption;//"Local폼";
                else if (sFileType == "1") sFileType = lblFileTp1->Caption;//"이미지심볼";
                else if (sFileType == "2") sFileType = lblFileTp2->Caption;//"기본스케쥴";

                m_pGDC->Values[nRow][Col04->Index] = sFileType;

                m_pGDC->Values[nRow][Col05->Index] = pADO->FieldByName("FILEID")->AsString;

                m_pGDC->Values[nRow][Col06->Index] = ITSUtil_StrToDateTime(pADO->FieldByName("SENDSTARTDATE")->AsString).FormatString(STR_DATETIME);
                m_pGDC->Values[nRow][Col07->Index] = ITSUtil_StrToDateTime(pADO->FieldByName("SENDENDDATE")->AsString).FormatString(STR_DATETIME);
                m_pGDC->Values[nRow][Col08->Index] = pADO->FieldByName("SENDRESULT")->AsInteger == 1 ? lblSucc->Caption : lblFai->Caption;//"성공" : "실패";

                String sStep = pADO->FieldByName("SENDSTEP")->AsString;
                if      (sStep == "0") sStep = lblStep0->Caption;//"대기";
                else if (sStep == "1") sStep = lblStep1->Caption;//"전송중";
                else                   sStep = lblStep2->Caption;//"전송완료";

                m_pGDC->Values[nRow][Col09->Index] = sStep;
                m_pGDC->Values[nRow][Col10->Index] = pADO->FieldByName("USER_ID")->AsString;
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
            }

            TvList->EndUpdate();
            //CxList->SetFocus();
            LblRecords->Caption = FormatFloat("##,##0", m_pGDC->RecordCount) + lblCnt->Caption;//" 건";
        }
    }
    catch(EDatabaseError &E)
    {
        DBERRORMSG("History Results", String(E.ClassName()), E.Message, pADO->SQL->Text);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        DBERRORMSG("History Results", String(e.ClassName()), e.Message, pADO->SQL->Text);
        throw Exception(String(e.ClassName()) + e.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDnldSub::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle = Caption;

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

void __fastcall TFrmVmsLogDnldSub::FormClose(TObject *Sender, TCloseAction &Action)
{
    POST_MSG(FParent, WM_SUBFORM_CLOSE, 0, 0);
    CommClose();
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDnldSub::FormDestroy(TObject *Sender)
{
    //
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogDnldSub::Col08CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    try
    {
        TColor tColor;
        String sVmsMode = AViewInfo->GridRecord->DisplayTexts[AViewInfo->Item->Index];
        tColor = ACanvas->Brush->Color;
        if (sVmsMode == lblFai->Caption)//"실패")
            tColor = clRed;
        //else                    tColor = clLime;

        ACanvas->SetBrushColor(tColor);
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

