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

#pragma hdrstop

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

#include "VMSM800MF.h"
#include "VMSM800MSRCF.h"

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

#pragma package(smart_init)

void __fastcall TVMSM800M::LoadVmsScheduleInfo(String AVmsNmbr)
{
    int        ii;
    String     sQry;
    TADOQuery *pADO = NULL;

    for (ii = 0; ii < MAX_FORM_SCHED; ii++)
    {
        if (FListSch[ii]) CMM_ClearGridTableView(FListSch[ii]);
    }

    sQry = "SELECT A.*, B.VMS_FORM_IMAG, B.VMS_TYPE_CD, B.VMS_FORM_NM,  \r\n"
           "       B.VMS_FORM_COLR_CD, B.VMS_FORM_TYPE_CD               \r\n"
           "  FROM TB_VMS_DSPL_SCH A,                                   \r\n"
           "       TB_VMS_FORM     B                                    \r\n"
           " WHERE A.VMS_CTLR_NMBR     = :p01                           \r\n"
           "   AND A.VMS_FORM_ID       = B.VMS_FORM_ID                  \r\n"
           "   AND A.TRFC_SITU_TYPE_CD = '6'                            \r\n"
           " ORDER BY A.PHASE                                           \r\n";

    try
    {
        for (ii = 0; ii < MAX_FORM_SCHED; ii++)
        {
            if (FListSch[ii]) FListSch[ii]->BeginUpdate();
        }

        TcxDataController *pDcSch;
        int nRow;

        try
        {
            int nRow;

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

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

            for( ; !pADO->Eof; pADO->Next())
            {
                int nFormType = pADO->FieldByName("TRFC_SITU_TYPE_CD")->AsString.Trim().ToIntDef(-1);
                switch(nFormType)
                {
                //case enFormBase:   //	, '	기본스케줄
                //    break;
                default: continue;
                }
                if (!FListSch[nFormType]) continue;

                pDcSch = FListSch[nFormType]->DataController;

                nRow = pDcSch->AppendRecord();

                String sStrDate = pADO->FieldByName("DSPL_STRT_HH")->AsString;
                String sEndDate = pADO->FieldByName("DSPL_END_HH")->AsString;

                pDcSch->Values[nRow][0] = pADO->FieldByName("VMS_FORM_ID")->AsString;
                pDcSch->Values[nRow][1] = pADO->FieldByName("VMS_FORM_IMAG")->AsVariant;
                pDcSch->Values[nRow][2] = pADO->FieldByName("FRST_VMS_IFSC_ID")->AsString;
                pDcSch->Values[nRow][3] = pADO->FieldByName("DSPL_HH")->AsString;
                pDcSch->Values[nRow][4] = ITSUtil_FormatStr(sStrDate, STR_HHNN);;
                pDcSch->Values[nRow][5] = ITSUtil_FormatStr(sEndDate, STR_HHNN);;
                pDcSch->Values[nRow][6] = pADO->FieldByName("USE_YN")->AsString;
                pDcSch->Values[nRow][7] = sStrDate;
                pDcSch->Values[nRow][8] = sEndDate;
                pDcSch->Values[nRow][ 9] = pADO->FieldByName("FRST_VMS_IFSC_ID")->AsString;
                pDcSch->Values[nRow][10] = pADO->FieldByName("SECD_VMS_IFSC_ID")->AsString;
                pDcSch->Values[nRow][11] = pADO->FieldByName("THIR_VMS_IFSC_ID")->AsString;
                pDcSch->Values[nRow][12] = pADO->FieldByName("FOUR_VMS_IFSC_ID")->AsString;
                pDcSch->Values[nRow][13] = pADO->FieldByName("VMS_TYPE_CD")->AsString;
                pDcSch->Values[nRow][14] = pADO->FieldByName("VMS_FORM_COLR_CD")->AsString;
                pDcSch->Values[nRow][15] = pADO->FieldByName("VMS_FORM_TYPE_CD")->AsString;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS Default Schedule Query", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS Default Schedule Query", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        for (ii = 0; ii < MAX_FORM_SCHED; ii++)
        {
            if (FListSch[ii]) FListSch[ii]->EndUpdate();
        }
    }
}
//---------------------------------------------------------------------------

bool __fastcall TVMSM800M::LoadVmsFormList(String AFormSizeCd)
{
    int ii;
    for (ii = 0; ii < MAX_FORM_SCHED; ii++)
    {
        if (FListFrm[ii])
        {
            CMM_ClearGridTableView(FListFrm[ii]);
        }
    }

    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.*                                 \r\n"
           "  FROM TB_VMS_FORM A                       \r\n"
           " WHERE A.VMS_TYPE_CD      = :p01           \r\n"
           "   AND A.VMS_FORM_TYPE_CD IN (40, 70, 80)  \r\n"
           " ORDER BY A.VMS_FORM_ID                    \r\n";

    try
    {
        for (ii = 0; ii < MAX_FORM_SCHED; ii++)
        {
            if (FListFrm[ii]) FListFrm[ii]->BeginUpdate();
        }

        TcxDataController *pGDC;
        int nRow;
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

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

            int nFormIdx;
            for( ; !pADO->Eof; pADO->Next())
            {
                int    sFormTypeCd = pADO->FieldByName("VMS_FORM_TYPE_CD")->AsInteger;
                String sFormId     = pADO->FieldByName("VMS_FORM_ID")->AsString;

                switch(sFormTypeCd)
                {
                //case 40://	, '	홍보문안
                //    nFormIdx = enFormBase; break;
                default: continue;
                }
                if (!FListFrm[nFormIdx]) continue;

                pGDC = FListFrm[nFormIdx]->DataController;
                nRow = pGDC->AppendRecord();
                pGDC->Values[nRow][0] = sFormId;
                pGDC->Values[nRow][1] = pADO->FieldByName("VMS_FORM_IMAG")->AsVariant;
                pGDC->Values[nRow][2] = String(sFormTypeCd);
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS Form Query", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS Form Query", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        for (ii = 0; ii < MAX_FORM_SCHED; ii++)
        {
            if (FListFrm[ii]) FListFrm[ii]->EndUpdate();
        }
    }
    return true;
}
//---------------------------------------------------------------------------

