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

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

#include "IFM00401F.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_ChartF"
#pragma link "cxButtons"
#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 "cxGridBandedTableView"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

TIFM00401 *IFM00401 = NULL;

//---------------------------------------------------------------------------
__fastcall TIFM00401::TIFM00401(TComponent* Owner, HWND hHandle, String sFrom, String sTo, String sIdList)
    : TForm(Owner)
{           
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

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

__fastcall TIFM00401::~TIFM00401(void)
{
}
//--------------------------------------------------------------------------

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

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<시설물장애 이력 정보가 없습니다.>";
	//TvList->ViewInfo->GroupByBoxViewInfo->Text = "그룹핑할 필드의 헤더를 이곳으로 드래그 하세요";
    LblSearch->Caption = FrmLang->lblQryCond->Caption + FStDateTime.SubString(1, 10) + " ~ " + FEdDateTime.SubString(1, 10);
}
//---------------------------------------------------------------------------

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

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

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

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

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

    String sQry;
    TADOQuery *pADO = ADOQry;

#if 0
    sQry = "SELECT X.FCLT_TYPE,                            \r\n"
           "       X.FCLT_LCTN,                            \r\n"
           "       X.FCLT_ID,                              \r\n"
           "       X.FAIL_HS_SEQ,                          \r\n"
           "       X.EQPM_TYPE_CD,                         \r\n"
           "       X.OCRR_DT,                              \r\n"
           "       X.RCPR_NM,                              \r\n"
           "       X.ADTN_DT,                              \r\n"
           "       X.ADTN_NM,                              \r\n"
           "       X.CHNG_NM,                              \r\n"
           "       X.CHNG_DT,                              \r\n"
           "       X.FAIL_OCRR_CD,                         \r\n"
           "       X.FAIL_TYPE,                            \r\n"
           "       X.ETC_FAIL_CONT,                        \r\n"
           "       NVL(Y.FAIL_HS_SEQ, 'X') AS RPAR_HS,     \r\n"
           "       Y.WRKR_NM,                              \r\n"
           "       Y.WORK_STRT_DT,                         \r\n"
           "       Y.WORK_END_DT,                          \r\n"
           "       Y.WORK_FINS_YN,                         \r\n"
           "       Y.FAIL_HNDL_TYPE,                       \r\n"
           "       Y.ETC_HNDL_CONT,                        \r\n"
           "       Y.ADTN_WRKR_NM,                         \r\n"
           "       Y.ADTN_WORK_STRT_DT,                    \r\n"
           "       Y.ADTN_WORK_END_DT,                     \r\n"
           "       Y.DEL_YN                                \r\n"
           "  FROM (SELECT B.FCLT_TYPE, B.FCLT_LCTN, A.*   \r\n"
           "          FROM TB_FCLT_FAIL_HS      A,         \r\n"
           "               TB_FCLT_INFR         B          \r\n"
           "         WHERE A.OCRR_DT BETWEEN :p01 AND :p02 \r\n"
           "           AND A.FCLT_ID = B.FCLT_ID           \r\n"
           "       ) X,                                    \r\n"
           "       TB_FCLT_FAIL_RPAR_HS Y                  \r\n"
           " WHERE X.FAIL_HS_SEQ = Y.FAIL_HS_SEQ(+)        \r\n"
           "   AND Y.DEL_YN <> 'Y'                         \r\n"
           " ORDER BY X.FCLT_TYPE, X.OCRR_DT               \r\n";
#endif
    sQry = "SELECT X.FCLT_TYPE,                                 \r\n"
           "       X.FCLT_LCTN,                                 \r\n"
           "       X.FCLT_ID,                                   \r\n"
           "       X.FAIL_HS_SEQ,                               \r\n"
           "       X.EQPM_TYPE_CD,                              \r\n"
           "       (SELECT EQPM_TYPE_CD_NM                      \r\n"
           "          FROM TB_FAIL_EQPM_TYPE                    \r\n"
           "         WHERE EQPM_TYPE_CD = X.EQPM_TYPE_CD )      \r\n"
           "       AS EQPM_TYPE_CD_NM,                          \r\n"
           "       X.OCRR_DT,                                   \r\n"
           "       X.RCPR_NM,                                   \r\n"
           "       X.ADTN_DT,                                   \r\n"
           "       X.ADTN_NM,                                   \r\n"
           "       X.CHNG_NM,                                   \r\n"
           "       X.CHNG_DT,                                   \r\n"
           "       X.FAIL_OCRR_CD,                              \r\n"
           "       (SELECT FAIL_OCRR_CD_NM                      \r\n"
           "          FROM TB_FAIL_OCRR_DVSN                    \r\n"
           "         WHERE FAIL_OCRR_CD = X.FAIL_OCRR_CD)       \r\n"
           "       AS FAIL_OCRR_CD_NM,                          \r\n"
           "       X.FAIL_TYPE,                                 \r\n"
           "       (SELECT A.FAIL_CD_NM                         \r\n"
           "          FROM TB_FAIL_TYPE    A,                   \r\n"
           "               TB_FAIL_DETL_CD B                    \r\n"
           "         WHERE B.EQPM_TYPE_CD = X.EQPM_TYPE_CD      \r\n"
           "           AND B.FAIL_OCRR_CD = X.FAIL_OCRR_CD      \r\n"
           "           AND B.FAIL_CD      = X.FAIL_TYPE         \r\n"
           "           AND A.FAIL_CD      = B.FAIL_CD )         \r\n"
           "       AS FAIL_CD_NM,                               \r\n"
           "       X.ETC_FAIL_CONT,                             \r\n"
           "       NVL(Y.FAIL_HS_SEQ, 'X') AS RPAR_HS,          \r\n"
           "       Y.WRKR_NM,                                   \r\n"
           "       Y.WORK_STRT_DT,                              \r\n"
           "       Y.WORK_END_DT,                               \r\n"
           "       Y.WORK_FINS_YN,                              \r\n"
           "       Y.FAIL_HNDL_TYPE,                            \r\n"
           "       (SELECT FAIL_HNDL_CD_NM                      \r\n"
           "          FROM TB_FAIL_HNDL_TYPE                    \r\n"
           "         WHERE FAIL_HNDL_CD = Y.FAIL_HNDL_TYPE)     \r\n"
           "       AS FAIL_HNDL_CD_NM,                          \r\n"
           "       Y.ETC_HNDL_CONT,                             \r\n"
           "       Y.ADTN_WRKR_NM,                              \r\n"
           "       Y.ADTN_WORK_STRT_DT,                         \r\n"
           "       Y.ADTN_WORK_END_DT,                          \r\n"
           "       Y.DEL_YN                                     \r\n"
           "  FROM (SELECT B.FCLT_TYPE, B.FCLT_LCTN, A.*        \r\n"
           "          FROM TB_FCLT_FAIL_HS      A,              \r\n"
           "               TB_FCLT_INFR         B               \r\n"
           "         WHERE A.OCRR_DT BETWEEN :p01 AND :p02      \r\n"
           "           AND A.FCLT_ID = B.FCLT_ID                \r\n"
           "       ) X,                                         \r\n"
           "       (SELECT *                                    \r\n"
           "          FROM TB_FCLT_FAIL_RPAR_HS C               \r\n"
           "         WHERE C.FAIL_HS_SEQ IN                     \r\n"
           "               (SELECT FAIL_HS_SEQ                  \r\n"
           "                  FROM TB_FCLT_FAIL_HS              \r\n"
           "                 WHERE OCRR_DT                      \r\n"
           "               BETWEEN :p03 AND :p04)               \r\n"
           "           AND C.DEL_YN <> 'Y'                      \r\n"
           "       ) Y                                          \r\n"
           " WHERE X.FAIL_HS_SEQ = Y.FAIL_HS_SEQ(+)             \r\n"
           " ORDER BY X.FCLT_TYPE, X.OCRR_DT                    \r\n";


    try
    {
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", FStDateTime);
        ITSDb_SQLBind(pADO, "p02", FEdDateTime);
        ITSDb_SQLBind(pADO, "p03", FStDateTime);
        ITSDb_SQLBind(pADO, "p04", FEdDateTime);
        ITSDb_SQLOpen(pADO);

        DspHistory();
    }
    catch(EDatabaseError &E)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQry);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQry);
        throw Exception(String(e.ClassName()) + e.Message);
    }
}
//---------------------------------------------------------------------------

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

    try
    {
    	TcxGridChartSeries *pChart = NULL;

        CMM_ClearGridTableView(TvList);

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

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

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                m_pGDC->Values[nRow][TvListColumn1->Index] = pADO->FieldByName("FCLT_TYPE")->AsString;      //시설물유형
                m_pGDC->Values[nRow][TvListColumn2->Index] = pADO->FieldByName("FCLT_ID")->AsString;        //시설물아이디
                m_pGDC->Values[nRow][TvListColumn3->Index] = pADO->FieldByName("FCLT_LCTN")->AsString;      //시설물위치명
                m_pGDC->Values[nRow][TvListColumn4->Index] = pADO->FieldByName("FAIL_HS_SEQ")->AsString;    //일련번호
                //m_pGDC->Values[nRow][TvListColumn5->Index] = pADO->FieldByName("EQPM_TYPE_CD")->AsString;   //장비유형
                m_pGDC->Values[nRow][TvListColumn5->Index] = pADO->FieldByName("EQPM_TYPE_CD_NM")->AsString;   //장비유형

                m_pGDC->Values[nRow][TvListColumn6->Index] = ITSUtil_StrToTime(pADO->FieldByName("OCRR_DT")->AsString);        //발생일시
                m_pGDC->Values[nRow][TvListColumn7->Index] = pADO->FieldByName("RCPR_NM")->AsString;        //접수자명
                m_pGDC->Values[nRow][TvListColumn8->Index] = ITSUtil_StrToTime(pADO->FieldByName("ADTN_DT")->AsString);        //등록일시
                m_pGDC->Values[nRow][TvListColumn9->Index] = pADO->FieldByName("ADTN_NM")->AsString;        //등록자
                m_pGDC->Values[nRow][TvListColumn10->Index] = pADO->FieldByName("CHNG_NM")->AsString;       //변경자
                m_pGDC->Values[nRow][TvListColumn11->Index] = ITSUtil_StrToTime(pADO->FieldByName("CHNG_DT")->AsString);       //변경일시
                //m_pGDC->Values[nRow][TvListColumn12->Index] = pADO->FieldByName("FAIL_OCRR_CD")->AsString;  //장애발생코드
                //m_pGDC->Values[nRow][TvListColumn13->Index] = pADO->FieldByName("FAIL_TYPE")->AsString;     //장애유형
                m_pGDC->Values[nRow][TvListColumn12->Index] = pADO->FieldByName("FAIL_OCRR_CD_NM")->AsString;  //장애발생코드
                m_pGDC->Values[nRow][TvListColumn13->Index] = pADO->FieldByName("FAIL_CD_NM")->AsString;     //장애유형
                m_pGDC->Values[nRow][TvListColumn14->Index] = pADO->FieldByName("ETC_FAIL_CONT")->AsString; //장애내용

                String sRparHs = pADO->FieldByName("RPAR_HS")->AsString;
                //TODO
                //if (sRparHs != "X")
                {
                    m_pGDC->Values[nRow][TvListColumn15->Index] = pADO->FieldByName("WRKR_NM")->AsString;           //작업자
                    m_pGDC->Values[nRow][TvListColumn16->Index] = ITSUtil_StrToTime(pADO->FieldByName("WORK_STRT_DT")->AsString);      //작업시작일시
                    m_pGDC->Values[nRow][TvListColumn17->Index] = ITSUtil_StrToTime(pADO->FieldByName("WORK_END_DT")->AsString);       //작업종료일시
                    m_pGDC->Values[nRow][TvListColumn18->Index] = pADO->FieldByName("WORK_FINS_YN")->AsString;      //작업완료여부
                    //m_pGDC->Values[nRow][TvListColumn19->Index] = pADO->FieldByName("FAIL_HNDL_TYPE")->AsString;    //장애처리유형
                    m_pGDC->Values[nRow][TvListColumn19->Index] = pADO->FieldByName("FAIL_HNDL_CD_NM")->AsString;    //장애처리유형
                    m_pGDC->Values[nRow][TvListColumn20->Index] = pADO->FieldByName("ETC_HNDL_CONT")->AsString;     //기타처리내용
                    m_pGDC->Values[nRow][TvListColumn21->Index] = pADO->FieldByName("ADTN_WRKR_NM")->AsString;      //추가작업자
                    m_pGDC->Values[nRow][TvListColumn22->Index] = ITSUtil_StrToTime(pADO->FieldByName("ADTN_WORK_STRT_DT")->AsString); //추가작업일시
                    m_pGDC->Values[nRow][TvListColumn23->Index] = ITSUtil_StrToTime(pADO->FieldByName("ADTN_WORK_END_DT")->AsString);  //추가작업종료일시
                    //m_pGDC->Values[nRow][TvListColumn24->Index] = pADO->FieldByName("DEL_YN")->AsString;            //삭제여부
                }
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
            }

            TvList->EndUpdate();
            //CxList->SetFocus();
            LblRecords->Caption = FormatFloat("##,##0", m_pGDC->RecordCount) + FrmLang->lblEA->Caption;//" 건";

            String sEnd = FrmLang->lblQrySel->Caption + " [" + LblRecords->Caption + "]";//"데이터 " + LblRecords->Caption + " 이 조회 되었습니다.";
            Application->MessageBox(sEnd.c_str(),
                                    FrmLang->lblQryEnd->Caption.c_str(),//L"데이터 조회 완료",
                                    MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        }
    }
    catch(EDatabaseError &E)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(E.ClassName()), E.Message, pADO->SQL->Text);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(e.ClassName()), e.Message, pADO->SQL->Text);
        throw Exception(String(e.ClassName()) + e.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TIFM00401::OnMessage(TMessage &Msg)
{
    switch (Msg.Msg)
    {
    case WM_PARAM_DATABASE:
        if (WP_DB_SELECT_OK == Msg.WParam)
        {
            //ShowMessage("Select Ok");
        }
        break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIFM00401::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"시설물장애이력";

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

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

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

