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

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

#include "FRAME_FacilityFailActionF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxCalendar"
#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 "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#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 "cxDropDownEdit"
#pragma resource "*.dfm"

extern LOGININFO *g_pLOGIN;

//---------------------------------------------------------------------------
__fastcall TFRAMEFacilityFailAction::TFRAMEFacilityFailAction(TComponent* Owner)
    : TFrame(Owner)
{
    ITSSkin_Load((TForm*)this);

    FTitle = "시설물 장애 내역 조치";
    m_pGDC = TvList->DataController;
    TvList->OptionsView->NoDataToDisplayInfoText = "<시설물 장애 내역처리 정보가 없습니다>";

    FFacilityFail = NULL;
    FUpdate       = false;
    FKeySeq       = -1;

    FCode = new TFacilityCodeManager();
    FCode->LoadHndlTypeDb();

    //작업완료여부
	TcxComboBoxProperties *pcxCboProperties05 = (TcxComboBoxProperties*)Column05->Properties;
    pcxCboProperties05->Items->Clear();
	pcxCboProperties05->Items->Add("Y");
	pcxCboProperties05->Items->Add("N");

	pcxHndlType = (TcxComboBoxProperties*)Column06->Properties;
    pcxHndlType->Items->Clear();
    FOR_STL(TFacilityHndlType *, pCode, FCode->FHndlLists)
    {
        pcxHndlType->Items->Add("[" + pCode->FAIL_HNDL_CD + "] " + pCode->FAIL_HNDL_CD_NM);
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityFailAction::UpdateCheck()
{
    if (FFacilityFail)
    {
        if (BtnEdit->Down)
        {
            FUpdate = false;
            BtnEdit->Down = false;
            BtnEdit->Click();
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityFailAction::UpdateList(TItsFacilityFail *AFacilityFail)
{
    FUpdate = false;

    FFacilityFail = AFacilityFail;

    LblFacility->Caption = "---";
    CMM_ClearGridTableView(TvList);

    if (!FFacilityFail) return;
    LblFacility->Caption = "[" + FFacilityFail->FCLT_ID + "] [" + FFacilityFail->FAIL_HS_SEQ + "] " + FFacilityFail->ETC_FAIL_CONT;

    try
    {
        TvList->BeginUpdate();

        ItsFacilityFailManager->SelDbFailAction(FFacilityFail);
        FOR_STL(TItsFacilityFailAction*, pObj, FFacilityFail->FLists)
        {
            pObj->EditMode = DB_INIT;   //미리 초기화시켜야함
            pObj->Completed = true;
            if (pObj->DEL_YN == "Y") continue;

            if (pObj->FAIL_HS_SEQ != FFacilityFail->FAIL_HS_SEQ) continue;

            AddObjectItem(pObj, "-");
        }
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        //CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------

bool __fastcall TFRAMEFacilityFailAction::AddObjectItem(TItsFacilityFailAction *pObj, String AMode)
{
    int nRow;
    try
    {
        nRow = m_pGDC->AppendRecord();
        pObj->GridIndex = nRow;

        m_pGDC->Values[nRow][Column00->Index] = AMode;

        m_pGDC->Values[nRow][Column01->Index] = pObj->FAIL_HS_SEQ;                              //'장애 이력 일련번호
        m_pGDC->Values[nRow][Column02->Index] = pObj->WRKR_NM;                                  //'작업자 명

        if (!pObj->WORK_STRT_DT.IsEmpty())
        m_pGDC->Values[nRow][Column03->Index] = ITSUtil_StrToDateTime(pObj->WORK_STRT_DT);      //'작업 시작 일시

        if (!pObj->WORK_END_DT.IsEmpty())
        m_pGDC->Values[nRow][Column04->Index] = ITSUtil_StrToDateTime(pObj->WORK_END_DT);       //'작업 종료 일시

        m_pGDC->Values[nRow][Column05->Index] = pObj->WORK_FINS_YN;                             //'작업 완료 여부

        String sHndlType = "[" + pObj->FAIL_HNDL_TYPE + "] ";
        TFacilityHndlType *pHndl = FCode->FHndlLists.Find(pObj->FAIL_HNDL_TYPE);
        if (pHndl) sHndlType = sHndlType + pHndl->FAIL_HNDL_CD_NM;
        else       sHndlType = pcxHndlType->Items->Strings[0];

        m_pGDC->Values[nRow][Column06->Index] = sHndlType;                                      //'작업 완료 여부

        m_pGDC->Values[nRow][Column07->Index] = pObj->ETC_HNDL_CONT;                            //'기타 처리 내용
        m_pGDC->Values[nRow][Column08->Index] = pObj->ADTN_WRKR_NM;                             //'추가 작업자 명

        if (!pObj->ADTN_WORK_STRT_DT.IsEmpty())
        m_pGDC->Values[nRow][Column09->Index] = ITSUtil_StrToDateTime(pObj->ADTN_WORK_STRT_DT); //'추가 작업 시작 일시

        if (!pObj->ADTN_WORK_END_DT.IsEmpty())
        m_pGDC->Values[nRow][Column10->Index] = ITSUtil_StrToDateTime(pObj->ADTN_WORK_END_DT);  //'추가 작업 종료 일시

        m_pGDC->Values[nRow][Column99->Index] = (int)pObj;

        return true;
    }
    catch(...)
    {
    }
    return false;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityFailAction::TvListDataControllerFilterChanged(TObject *Sender)
{
    LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityFailAction::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= "시설물장애처리내역";

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

void __fastcall TFRAMEFacilityFailAction::BtnEditClick(TObject *Sender)
{
    if (!FFacilityFail)
    {
        Application->MessageBox(L"시설물 장애내역 정보를 먼저 선택하세요.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
    }

    TListFocus tvFocus(TvList);

    if (BtnEdit->Caption == "취소")
    {
        CxList->SetFocus();
        Application->ProcessMessages();
        SaveData();
    }

    ITSSkin_ButtonClick(BtnSearch, BtnEdit, BtnInsert, BtnDelete, BtnApply);
    ChangeEditMode(BtnEdit->Down);

    UpdateList(FFacilityFail);
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityFailAction::ChangeEditMode(bool AEdit)
{
    bool bEditing = AEdit;

    try
    {
        TvList->BeginUpdate();

        Column00->Visible = bEditing;

        //Column01->Options->Editing = bEditing;  //장애 이력 일련번호
        Column02->Options->Editing = bEditing;  //작업자 명
        Column03->Options->Editing = bEditing;  //작업 시작 일시
        Column04->Options->Editing = bEditing;  //작업 종료 일시
        Column05->Options->Editing = bEditing;  //작업 완료 여부
        Column06->Options->Editing = bEditing;  //장애 처리 유형
        Column07->Options->Editing = bEditing;  //기타 처리 내용
        Column08->Options->Editing = bEditing;  //추가 작업자 명
        Column09->Options->Editing = bEditing;  //추가 작업 시작 일시
        Column10->Options->Editing = bEditing;  //추가 작업 종료 일시

        if (bEditing)
        {
            if (FFacilityFail)
            {
                //장애조치내역 중복 허용할 경우 이곳을 주석으로 처리하면 됨
                if (FFacilityFail->FLists.Size() > 0)
                    BtnInsert->Enabled = false;
            }
        }
    }
    __finally
    {
        TvList->EndUpdate();
        if (bEditing)
        {
            BtnEdit->Caption = "취소";
            BtnEdit->Hint    = "데이터 편집 취소";
        }
        else
        {
            BtnEdit->Caption = "편집";
            BtnEdit->Hint    = "데이터 편집";
        }
    }
}
//---------------------------------------------------------------------------

bool __fastcall TFRAMEFacilityFailAction::GetNewId(String &ANewId)
{
    String sQry;
    TADOQuery *pADO = NULL;
    String sNowDate = Now().FormatString("YYYYMMDDHHNNSS");

    ANewId = "";
    ANewId = sNowDate;
    return true;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityFailAction::BtnInsertClick(TObject *Sender)
{
    int nMsgType = MB_OK|MB_ICONERROR|MB_APPLMODAL;

    if (!FFacilityFail)
    {
        Application->MessageBox(L"시설물 장애 내역 정보를 먼저 선택하세요.", FTitle.c_str(), nMsgType);
		return;
    }

    //String sNewId = "";
    //GetNewId(sNewId);

    try
    {
        TvList->BeginUpdate();

        TItsFacilityFailAction *pObj = new TItsFacilityFailAction();
        pObj->Clear();

        pObj->FAIL_HS_SEQ       = FFacilityFail->FAIL_HS_SEQ;           // 장애 이력 일련번호
        pObj->WRKR_NM           = g_pLOGIN->sUserId;                    // 작업자 명
        pObj->WORK_STRT_DT      = Now().FormatString("yyyymmddhhnnss"); // 작업 시작 일시
        pObj->WORK_END_DT       = Now().FormatString("yyyymmddhhnnss"); // 작업 종료 일시
        pObj->WORK_FINS_YN      = "N";                                  //작업 완료 여부
        pObj->FAIL_HNDL_TYPE    = pcxHndlType->Items->Strings[0];       // 장애 처리 유형
        pObj->ETC_HNDL_CONT     = "";                                   // 기타 처리 내용
        pObj->ADTN_WRKR_NM      = "";                                   // 추가 작업자 명
        pObj->ADTN_WORK_STRT_DT = Now().FormatString("yyyymmddhhnnss"); // 추가 작업 시작 일시
        pObj->ADTN_WORK_END_DT  = Now().FormatString("yyyymmddhhnnss"); // 추가 작업 종료 일시
        pObj->DEL_YN            = "N";                                  // 삭제 여부

        FFacilityFail->DataCnt++;
        FFacilityFail->FLists.Push(String(FFacilityFail->DataCnt), pObj);
        //FFacilityFail->FLists.Push(pObj->FAIL_HS_SEQ, pObj);

        pObj->Completed = false;
        pObj->EditMode = DB_NEW;

        AddObjectItem(pObj, "신규");
        Application->ProcessMessages();
        //Sleep(1000);    //생성시각을 유니크하게 하기위함.
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityFailAction::BtnDeleteClick(TObject *Sender)
{
    int nIdx = m_pGDC->FocusedRecordIndex;
    if (nIdx < 0) return;

    int nMemPtr = m_pGDC->Values[nIdx][Column99->Index];
    TItsFacilityFailAction *pObj = (TItsFacilityFailAction*)nMemPtr;
    pObj->EditMode = DB_DELETE;
    m_pGDC->Values[nIdx][Column00->Index] = "삭제";
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityFailAction::BtnApplyClick(TObject *Sender)
{
    FUpdate = true;
    BtnEdit->Down = false;
    BtnEdit->Click();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityFailAction::ChkAutoWidthPropertiesChange(TObject *Sender)
{
    try
    {
        TvList->BeginUpdate();
		TvList->OptionsView->ColumnAutoWidth = ChkAutoWidth->Checked;
        BtnBestFit->Visible = !ChkAutoWidth->Checked;
	}
	__finally
	{
        TvList->EndUpdate();
	}
}
//---------------------------------------------------------------------------

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


bool __fastcall TFRAMEFacilityFailAction::IsUpdate()
{
    if (!FFacilityFail) return false;

    FOR_STL(TItsFacilityFailAction*, pObj, FFacilityFail->FLists)
    {
        if (pObj->Completed && pObj->EditMode != DB_INIT)
            return true;

        if (!pObj->Completed && pObj->EditMode == DB_NEW)
            return true;
    }
    return false;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityFailAction::SaveData()
{
    if (!IsUpdate()) return;

    if (!FUpdate)
    {
        if (Application->MessageBox(L"변경된 정보가 있습니다.\r\n변경된 정보를 저장 하시겠습니까?",
                                    FTitle.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES)
        {
            //추가했던것 삭제
            FOR_STL(TItsFacilityFailAction*, pObj, FFacilityFail->FLists)
            {
                if (pObj->EditMode == DB_NEW)
                    FFacilityFail->FLists.Remove(pObj->FAIL_HS_SEQ);
            }
            return;
        }
    }

    TADOQuery *pADOI = NULL;
    TADOQuery *pADOU = NULL;
    TADOQuery *pADOD = NULL;
	String sQryI;
	String sQryU;
	String sQryD;
    try
    {
        sQryI = "INSERT INTO TB_FCLT_FAIL_RPAR_HS (FAIL_HS_SEQ,      WRKR_NM,      WORK_STRT_DT,      \r\n"
                "                                  WORK_END_DT,      WORK_FINS_YN, FAIL_HNDL_TYPE,    \r\n"
                "                                  ETC_HNDL_CONT,    ADTN_WRKR_NM, ADTN_WORK_STRT_DT, \r\n"
                "                                  ADTN_WORK_END_DT, DEL_YN)                          \r\n"
                "                           VALUES(:p01, :p02, :p03,                                  \r\n"
                "                                  :p04, :p05, :p06,                                  \r\n"
                "                                  :p07, :p08, :p09,                                  \r\n"
                "                                  :p10, 'N')                                         \r\n";

        sQryU = "UPDATE TB_FCLT_FAIL_RPAR_HS        \r\n"
                "   SET  WRKR_NM           = :p02,  \r\n"
                "        WORK_STRT_DT      = :p03,  \r\n"
                "        WORK_END_DT       = :p04,  \r\n"
                "        WORK_FINS_YN      = :p05,  \r\n"
                "        FAIL_HNDL_TYPE    = :p06,  \r\n"
                "        ETC_HNDL_CONT     = :p07,  \r\n"
                "        ADTN_WRKR_NM      = :p08,  \r\n"
                "        ADTN_WORK_STRT_DT = :p09,  \r\n"
                "        ADTN_WORK_END_DT  = :p10   \r\n"
                " WHERE FAIL_HS_SEQ = :p01          \r\n";

        sQryD = "DELETE TB_FCLT_FAIL_RPAR_HS \r\n"
                " WHERE FAIL_HS_SEQ = :p01   \r\n";

        int nRow;
        TvList->BeginUpdate();
        try
        {
            pADOI = new TADOQuery(NULL);
			pADOI->Close();
            pADOI->Connection = ITSDb_GetConnection();

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

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

            ITSDb_SQLText(pADOI, sQryI);
            ITSDb_SQLText(pADOU, sQryU);
            ITSDb_SQLText(pADOD, sQryD);

            ITSDb_GetConnection()->BeginTrans();

            FOR_STL(TItsFacilityFailAction*, pObj, FFacilityFail->FLists)
            {
                if (pObj->GridIndex < 0) continue;

                if (pObj->Completed && pObj->EditMode == DB_INIT)
                    continue;

                if (!pObj->Completed && pObj->EditMode != DB_NEW)
                    continue;

                nRow = pObj->GridIndex;

                String FAIL_HS_SEQ       = pObj->FAIL_HS_SEQ;        // 장애 이력 일련번호
                String WRKR_NM           = pObj->WRKR_NM;            // 작업자 명
                String WORK_STRT_DT      = pObj->WORK_STRT_DT;       // 작업 시작 일시
                String WORK_END_DT       = pObj->WORK_END_DT;        // 작업 종료 일시
                String WORK_FINS_YN      = pObj->WORK_FINS_YN;       // 작업 완료 여부
                String FAIL_HNDL_TYPE    = pObj->FAIL_HNDL_TYPE;     // 장애 처리 유형
                String ETC_HNDL_CONT     = pObj->ETC_HNDL_CONT;      // 기타 처리 내용
                String ADTN_WRKR_NM      = pObj->ADTN_WRKR_NM;       // 추가 작업자 명
                String ADTN_WORK_STRT_DT = pObj->ADTN_WORK_STRT_DT;  // 추가 작업 시작 일시
                String ADTN_WORK_END_DT  = pObj->ADTN_WORK_END_DT;   // 추가 작업 종료 일시
                String DEL_YN            = pObj->DEL_YN;             // 삭제 여부

                WRKR_NM           = VarToStr(m_pGDC->Values[nRow][Column02->Index]);            // 작업자 명
                WORK_STRT_DT      = "";       // 작업 시작 일시
                WORK_END_DT       = "";        // 작업 종료 일시
                WORK_FINS_YN      = VarToStr(m_pGDC->Values[nRow][Column05->Index]);       // 작업 완료 여부
                FAIL_HNDL_TYPE    = VarToStr(m_pGDC->Values[nRow][Column06->Index]);     // 장애 처리 유형
                ETC_HNDL_CONT     = VarToStr(m_pGDC->Values[nRow][Column07->Index]);      // 기타 처리 내용
                ADTN_WRKR_NM      = VarToStr(m_pGDC->Values[nRow][Column08->Index]);       // 추가 작업자 명
                ADTN_WORK_STRT_DT = "";  // 추가 작업 시작 일시
                ADTN_WORK_END_DT  = "";   // 추가 작업 종료 일시

                if (!VarIsNull(m_pGDC->Values[nRow][Column03->Index]))
                {
                    TDateTime dtTmp = m_pGDC->Values[nRow][Column03->Index];
                    WORK_STRT_DT = dtTmp.FormatString("YYYYMMDDHHNNSS");
                }
                if (!VarIsNull(m_pGDC->Values[nRow][Column04->Index]))
                {
                    TDateTime dtTmp = m_pGDC->Values[nRow][Column04->Index];
                    WORK_END_DT = dtTmp.FormatString("YYYYMMDDHHNNSS");
                }
                if (!VarIsNull(m_pGDC->Values[nRow][Column09->Index]))
                {
                    TDateTime dtTmp = m_pGDC->Values[nRow][Column10->Index];
                    ADTN_WORK_STRT_DT = dtTmp.FormatString("YYYYMMDDHHNNSS");
                }
                if (!VarIsNull(m_pGDC->Values[nRow][Column06->Index]))
                {
                    TDateTime dtTmp = m_pGDC->Values[nRow][Column10->Index];
                    ADTN_WORK_END_DT = dtTmp.FormatString("YYYYMMDDHHNNSS");
                }
                FAIL_HNDL_TYPE = ITSUtil_GetCode(FAIL_HNDL_TYPE);

                pObj->WRKR_NM           = WRKR_NM;            // 작업자 명
                pObj->WORK_STRT_DT      = WORK_STRT_DT;       // 작업 시작 일시
                pObj->WORK_END_DT       = WORK_END_DT;        // 작업 종료 일시
                pObj->WORK_FINS_YN      = WORK_FINS_YN;       // 작업 완료 여부
                pObj->FAIL_HNDL_TYPE    = FAIL_HNDL_TYPE;     // 장애 처리 유형
                pObj->ETC_HNDL_CONT     = ETC_HNDL_CONT;      // 기타 처리 내용
                pObj->ADTN_WRKR_NM      = ADTN_WRKR_NM;       // 추가 작업자 명
                pObj->ADTN_WORK_STRT_DT = ADTN_WORK_STRT_DT;  // 추가 작업 시작 일시
                pObj->ADTN_WORK_END_DT  = ADTN_WORK_END_DT;   // 추가 작업 종료 일시

                if (pObj->Completed)
                {
                    if (pObj->EditMode == DB_DELETE)
                    {
                        //delete
                        ITSDb_SQLBind(pADOD, "p01", pObj->FAIL_HS_SEQ);
                        ITSDb_SQLExec(pADOD);
                        pObj->DEL_YN = "Y";
                        FFacilityFail->ActionCnt--;
                    }
                    else
                    if (pObj->EditMode == DB_UPDATE)
                    {
                        //update
                        ITSDb_SQLBind(pADOU, "p01", pObj->FAIL_HS_SEQ);
                        ITSDb_SQLBind(pADOU, "p02", pObj->WRKR_NM);
                        ITSDb_SQLBind(pADOU, "p03", pObj->WORK_STRT_DT);
                        ITSDb_SQLBind(pADOU, "p04", pObj->WORK_END_DT);
                        ITSDb_SQLBind(pADOU, "p05", pObj->WORK_FINS_YN);
                        ITSDb_SQLBind(pADOU, "p06", pObj->FAIL_HNDL_TYPE);
                        ITSDb_SQLBind(pADOU, "p07", pObj->ETC_HNDL_CONT);
                        ITSDb_SQLBind(pADOU, "p08", pObj->ADTN_WRKR_NM);
                        ITSDb_SQLBind(pADOU, "p09", pObj->ADTN_WORK_STRT_DT);
                        ITSDb_SQLBind(pADOU, "p10", pObj->ADTN_WORK_END_DT);

                        ITSDb_SQLExec(pADOU);
                    }
                }
                else
                {
                    if (pObj->EditMode == DB_NEW)
                    {
                        //insert
                        ITSDb_SQLBind(pADOI, "p01", pObj->FAIL_HS_SEQ);
                        ITSDb_SQLBind(pADOI, "p02", pObj->WRKR_NM);
                        ITSDb_SQLBind(pADOI, "p03", pObj->WORK_STRT_DT);
                        ITSDb_SQLBind(pADOI, "p04", pObj->WORK_END_DT);
                        ITSDb_SQLBind(pADOI, "p05", pObj->WORK_FINS_YN);
                        ITSDb_SQLBind(pADOI, "p06", pObj->FAIL_HNDL_TYPE);
                        ITSDb_SQLBind(pADOI, "p07", pObj->ETC_HNDL_CONT);
                        ITSDb_SQLBind(pADOI, "p08", pObj->ADTN_WRKR_NM);
                        ITSDb_SQLBind(pADOI, "p09", pObj->ADTN_WORK_STRT_DT);
                        ITSDb_SQLBind(pADOI, "p10", pObj->ADTN_WORK_END_DT);

                        ITSDb_SQLExec(pADOI);

                        FFacilityFail->DataCnt++;
                        FFacilityFail->FLists.Push(String(FFacilityFail->DataCnt), pObj);
                        //FFacilityFail->FLists.Push(pObj->FAIL_HS_SEQ, pObj);
                        FFacilityFail->ActionCnt++;
                    }
                }
                pObj->Completed = true;
            }
            ITSDb_GetConnection()->CommitTrans();
            Application->MessageBox(L"시설물 장애 조치 정보를 저장 하였습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage("데이터 작업 중에 알수없는 DB 오류가 발생하였습니다.");
        }
    }
    __finally
    {
        if (pADOI)
        {
            pADOI->Close();
            delete pADOI;
        }
        if (pADOU)
        {
            pADOU->Close();
            delete pADOU;
        }
        if (pADOD)
        {
            pADOD->Close();
            delete pADOD;
        }
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityFailAction::ColumnPropertiesValidate(TObject *Sender, Variant &DisplayValue, TCaption &ErrorText,
          bool &Error)
{
    if (TvList->Controller->FocusedRow->IsFilterRow) return;

    int nRow = m_pGDC->FocusedRecordIndex;
    if (nRow < 0) return;

    int nMemPtr = m_pGDC->Values[nRow][Column99->Index];
    TItsFacilityFailAction *pObj = (TItsFacilityFailAction*)nMemPtr;
    if (pObj->Completed == false)
    {
        //신규이거나 삭제인 경우 편집 내용에 따른 변화가 필요없다.
        return;
    }

    String WRKR_NM           = VarToStr(m_pGDC->Values[nRow][Column02->Index]);  // 작업자 명
    String WORK_STRT_DT      = "";                                               // 작업 시작 일시
    String WORK_END_DT       = "";                                               // 작업 종료 일시
    String WORK_FINS_YN      = VarToStr(m_pGDC->Values[nRow][Column05->Index]);  // 작업 완료 여부
    String FAIL_HNDL_TYPE    = VarToStr(m_pGDC->Values[nRow][Column06->Index]);  // 장애 처리 유형
    String ETC_HNDL_CONT     = VarToStr(m_pGDC->Values[nRow][Column07->Index]);  // 기타 처리 내용
    String ADTN_WRKR_NM      = VarToStr(m_pGDC->Values[nRow][Column08->Index]);  // 추가 작업자 명
    String ADTN_WORK_STRT_DT = "";                                               // 추가 작업 시작 일시
    String ADTN_WORK_END_DT  = "";                                               // 추가 작업 종료 일시


    if (!VarIsNull(m_pGDC->Values[nRow][Column03->Index]))
    {
        TDateTime dtTmp = m_pGDC->Values[nRow][Column03->Index];
        WORK_STRT_DT = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    if (!VarIsNull(m_pGDC->Values[nRow][Column04->Index]))
    {
        TDateTime dtTmp = m_pGDC->Values[nRow][Column04->Index];
        WORK_END_DT = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    if (!VarIsNull(m_pGDC->Values[nRow][Column09->Index]))
    {
        TDateTime dtTmp = m_pGDC->Values[nRow][Column10->Index];
        ADTN_WORK_STRT_DT = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    if (!VarIsNull(m_pGDC->Values[nRow][Column06->Index]))
    {
        TDateTime dtTmp = m_pGDC->Values[nRow][Column10->Index];
        ADTN_WORK_END_DT = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }

    TcxGridColumn *pColumn = TvList->Controller->FocusedColumn;

    if      (pColumn == Column02) WRKR_NM = DisplayValue;
    else if (pColumn == Column03)
    {
        TDateTime dtTmp = DisplayValue;
        WORK_STRT_DT = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    else if (pColumn == Column04)
    {
        TDateTime dtTmp = DisplayValue;
        WORK_END_DT = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    else if (pColumn == Column05) WORK_FINS_YN   = DisplayValue;
    else if (pColumn == Column06) FAIL_HNDL_TYPE = DisplayValue;
    else if (pColumn == Column07) ETC_HNDL_CONT  = DisplayValue;
    else if (pColumn == Column08) ADTN_WRKR_NM   = DisplayValue;
    else if (pColumn == Column09)
    {
        TDateTime dtTmp = DisplayValue;
        ADTN_WORK_STRT_DT = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    else if (pColumn == Column10)
    {
        TDateTime dtTmp = DisplayValue;
        ADTN_WORK_END_DT = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    FAIL_HNDL_TYPE = ITSUtil_GetCode(FAIL_HNDL_TYPE);

    if (WRKR_NM           != pObj->WRKR_NM           ||   // 작업자 명
        WORK_STRT_DT      != pObj->WORK_STRT_DT      ||   // 작업 시작 일시
        WORK_END_DT       != pObj->WORK_END_DT       ||   // 작업 종료 일시
        WORK_FINS_YN      != pObj->WORK_FINS_YN      ||   // 작업 완료 여부
        FAIL_HNDL_TYPE    != pObj->FAIL_HNDL_TYPE    ||   // 장애 처리 유형
        ETC_HNDL_CONT     != pObj->ETC_HNDL_CONT     ||   // 기타 처리 내용
        ADTN_WRKR_NM      != pObj->ADTN_WRKR_NM      ||   // 추가 작업자 명
        ADTN_WORK_STRT_DT != pObj->ADTN_WORK_STRT_DT ||   // 추가 작업 시작 일시
        ADTN_WORK_END_DT  != pObj->ADTN_WORK_END_DT   )   // 추가 작업 종료 일시
    {
        m_pGDC->Values[nRow][Column00->Index] = "수정";
        pObj->EditMode = DB_UPDATE;
    }
    else
    {
        m_pGDC->Values[nRow][Column00->Index] = "-";
        pObj->EditMode = DB_INIT;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityFailAction::TvListCustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo)
    {
        String sType = AViewInfo->GridRecord->DisplayTexts[Column00->Index];
        if (sType == "수정")
        {
            ACanvas->Brush->Color = clOlive;
        }
        else if (sType == "삭제")
        {
            ACanvas->Brush->Color = clRed;
        }
        else if (sType == "신규")
        {
            ACanvas->Brush->Color = clLime;
            if (AViewInfo->Focused) ACanvas->Font->Color = clBlack;
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityFailAction::TvListKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if (Key == VK_DELETE)
	{
		if (BtnDelete->Enabled) BtnDeleteClick(Sender);
	}
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityFailAction::OnCloseQuery(bool &CanClose)
{
    SaveData();
    try {
        if (FCode) delete FCode;
        FCode = NULL;
    } catch(...) {}
}
//---------------------------------------------------------------------------

