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

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

#include "FrmSmsFailF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#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 "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "FRAME_NodeListF"
#pragma link "cxTextEdit"
#pragma link "cxDropDownEdit"
#pragma link "cxMaskEdit"
#pragma link "cxSpinEdit"
#pragma resource "*.dfm"

TFrmSmsFail *FrmSmsFail = NULL;
//---------------------------------------------------------------------------
__fastcall TFrmSmsFail::TFrmSmsFail(TComponent* Owner)
    : TForm(Owner)
{
    ITSSkin_Load(this);
    //CMM_LoadForm(g_sFormsDir, this);

    FUpdated = false;

    InitCode();
}
//---------------------------------------------------------------------------

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

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

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

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

void __fastcall TFrmSmsFail::InitForm()
{
    CbFTY->ItemIndex = -1;
    CbFFA->ItemIndex = -1;
    CbFSS->ItemIndex = -1;

    DtHH->Time = StrToDateTime("00:00");
    //SeCYCL->Value = 1;

    if (FDbMode == enJobSave)
    {   //신규등록
        CbFTY->ItemIndex = 0;
        CbFFA->ItemIndex = 0;
        CbFSS->ItemIndex = 0;
        CbYN->ItemIndex  = 0;
    }
    else
    {   //수정
        String sFSS = EdFSS->Text.Trim();
        if (sFSS == "FSS01")
        {   //즉시 발송
        }
        else
        if (sFSS == "FSS02")
        {   //일정주기(분)로 발송
            //SeCYCL->Value = 1;
        }
        else
        if (sFSS == "FSS03")
        {   //지정된 시간 발송
            String sHH = EdHH->Text.Trim();
            DtHH->Time = ITSUtil_StrToDateTime(sHH);
        }

        APP_SetCode(CbFTY, EdFTY->Text);
        APP_SetCode(CbFFA, EdFFA->Text);
        APP_SetCode(CbFSS, EdFSS->Text);
    }
}
//---------------------------------------------------------------------------

bool __fastcall TFrmSmsFail::CheckField()
{
    String sId   = EdId->Text.Trim();
    String sFTY  = APP_GetCode(CbFTY);
    String sFFA  = APP_GetCode(CbFFA);
    String sFSS  = APP_GetCode(CbFSS);
    String sDesc = EdDesc->Text.Trim();
    String sCYCL = VarToStr(SeCYCL->Value);
	String sHH   = DtHH->Time.FormatString("hhnn");
	String sYN   = CbYN->Text.Trim();

	if (sId == "")
    {
        Application->MessageBox(L"ID를 확인 하세요.", Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return false;
    }
	if (CbFTY->ItemIndex < 0)
    {
        Application->MessageBox(L"시설물유형을 확인 하세요.", Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return false;
    }
	if (CbFFA->ItemIndex < 0)
    {
        Application->MessageBox(L"장애유형을 확인 하세요.", Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return false;
    }
	if (CbFSS->ItemIndex < 0)
    {
        Application->MessageBox(L"SMS 송신방식을 확인 하세요.", Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return false;
    }
	if (CbYN->ItemIndex < 0)
    {
        Application->MessageBox(L"SMS 송신여부를 확인 하세요.", Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return false;
    }

    return true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmSmsFail::BtnSaveClick(TObject *Sender)
{
    String sMsg;

    String sId    = EdId->Text.Trim();
    String sFTY   = APP_GetCode(CbFTY);
    String sFFA   = APP_GetCode(CbFFA);
    String sFSS   = APP_GetCode(CbFSS);
    String sDesc  = EdDesc->Text.Trim();
    String sCYCL  = VarToStr(SeCYCL->Value);
	String sHH    = DtHH->Time.FormatString("hhnn");
	String sYN    = CbYN->Text.Trim();
    String sRegDt = Now().FormatString("yyyymmddhhnnss");//EdRegDt->Text.Trim();

    EdRegDt->Text = sRegDt;

    if (sFSS == "FSS01")
    {   //즉시 발송
        sCYCL = "";
	    sHH   = "";
    }
    else
    if (sFSS == "FSS02")
    {   //일정주기(분)로 발송
	    sHH   = "";
    }
    else
    if (sFSS == "FSS03")
    {   //지정된 시간 발송
        sCYCL = "";
	    sHH   = Now().FormatString("YYYYMMDD") + sHH + "00";
    }

    if (!CheckField()) return;

    if (FDbMode == enJobSave)
        sMsg = "SMS 장애유형 정보를 등록 하시겠습까?";
    else
        sMsg = "SMS 장애유형 정보를 수정 하시겠습까?";

    if (Application->MessageBox(sMsg.c_str(), Caption.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

    String sQry;
    TADOQuery *pADO = NULL;

	sQry = "MERGE INTO TB_SMS_FAIL_STUP L                       \r\n"
           "USING (SELECT :p01 AS SMS_FAIL_ID,                  \r\n"
           "              :p02 AS SMS_FCLT_TYPE,                \r\n"
           "              :p03 AS SMS_FAIL_TYPE,                \r\n"
           "              :p04 AS SMS_FAIL_EXPL,                \r\n"
           "              :p05 AS SMS_SEND_STYL,                \r\n"
           "              :p06 AS SEND_CYCL_STYL,               \r\n"
           "              :p07 AS SEND_HH_STYL,                 \r\n"
           "              :p08 AS SEND_YN,                      \r\n"
           "              :p09 AS RGST_DT                       \r\n"
           "         FROM DUAL                                  \r\n"
           "      ) M                                           \r\n"
           "ON (L.SMS_FAIL_ID = M.SMS_FAIL_ID)                  \r\n"
           "WHEN MATCHED THEN                                   \r\n"
           "    UPDATE SET L.SMS_FCLT_TYPE  = M.SMS_FCLT_TYPE,  \r\n"
           "               L.SMS_FAIL_TYPE  = M.SMS_FAIL_TYPE,  \r\n"
           "               L.SMS_FAIL_EXPL  = M.SMS_FAIL_EXPL,  \r\n"
           "               L.SMS_SEND_STYL  = M.SMS_SEND_STYL,  \r\n"
           "               L.SEND_CYCL_STYL = M.SEND_CYCL_STYL, \r\n"
           "               L.SEND_HH_STYL   = M.SEND_HH_STYL,   \r\n"
           "               L.SEND_YN        = M.SEND_YN,        \r\n"
           "               L.RGST_DT        = M.RGST_DT         \r\n"
           "WHEN NOT MATCHED THEN                               \r\n"
           "    INSERT (L.SMS_FAIL_ID,                          \r\n"
           "            L.SMS_FCLT_TYPE,                        \r\n"
           "            L.SMS_FAIL_TYPE,                        \r\n"
           "            L.SMS_FAIL_EXPL,                        \r\n"
           "            L.SMS_SEND_STYL,                        \r\n"
           "            L.SEND_CYCL_STYL,                       \r\n"
           "            L.SEND_HH_STYL,                         \r\n"
           "            L.SEND_YN,                              \r\n"
           "            L.RGST_DT)                              \r\n"
           "    VALUES (M.SMS_FAIL_ID,                          \r\n"
           "            M.SMS_FCLT_TYPE,                        \r\n"
           "            M.SMS_FAIL_TYPE,                        \r\n"
           "            M.SMS_FAIL_EXPL,                        \r\n"
           "            M.SMS_SEND_STYL,                        \r\n"
           "            M.SEND_CYCL_STYL,                       \r\n"
           "            M.SEND_HH_STYL,                         \r\n"
           "            M.SEND_YN,                              \r\n"
           "            M.RGST_DT)                              \r\n";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);

            ITSDb_SQLBind(pADO, "p01", sId);
            ITSDb_SQLBind(pADO, "p02", sFTY);
            ITSDb_SQLBind(pADO, "p03", sFFA);
            ITSDb_SQLBind(pADO, "p04", sDesc);
            ITSDb_SQLBind(pADO, "p05", sFSS);
            ITSDb_SQLBind(pADO, "p06", sCYCL);
            ITSDb_SQLBind(pADO, "p07", sHH);
            ITSDb_SQLBind(pADO, "p08", sYN);
            ITSDb_SQLBind(pADO, "p09", sRegDt);

            EdFTY->Text   = sFTY;
            EdFFA->Text   = sFFA;
            EdFSS->Text   = sFSS;
            EdCycle->Text = sCYCL;
            EdHH->Text    = sHH;
            EdYN->Text    = sYN;
            EdRegDt->Text = sRegDt;

            ITSDb_GetConnection()->BeginTrans();
            ITSDb_SQLExec(pADO);
            ITSDb_GetConnection()->CommitTrans();
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            ShowMessage(String(E.ClassName()) + E.Message);
            return;
        }
        catch(Exception &exception)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            ShowMessage(String(exception.ClassName()) + exception.Message);
            return;
        }
        catch(...)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            ShowMessage("데이터 저장 중에 알수없는 DB 오류가 발생하였습니다.");
            return;
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    if (FDbMode == enJobSave)
        sMsg = "SMS 장애유형 정보를 등록 하였습니다.";
    else
        sMsg = "SMS 장애유형 정보를 수정 하였습니다.";

    Application->MessageBox(sMsg.c_str(), Caption.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

    FUpdated = true;
    Close();
}
//---------------------------------------------------------------------------

void __fastcall TFrmSmsFail::EdCallTelKeyPress(TObject *Sender, wchar_t &Key)
{
    if (Key > '9' || Key < '0')
    {
        if (Key != VK_BACK && Key != VK_TAB)
            Key = 0;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmSmsFail::InitCode()
{
    TItsCode *FCodeFTY;
    TItsCode *FCodeFFA;
    TItsCode *FCodeFSS;
    FCodeFTY = ItsCodeManager->FLists.Find("FTY");
    FCodeFFA = ItsCodeManager->FLists.Find("FFA");
    FCodeFSS = ItsCodeManager->FLists.Find("FSS");

    // 시설물유형
    CbFTY->Properties->Items->Clear();
	if (FCodeFTY)
	{
		FOR_STL(TItsSubCode *, pSubCode, FCodeFTY->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			CbFTY->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
    CbFTY->ItemIndex = 0;

    // 장애유형
    CbFFA->Properties->Items->Clear();
	if (FCodeFFA)
	{
		FOR_STL(TItsSubCode *, pSubCode, FCodeFFA->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			CbFFA->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
    CbFFA->ItemIndex = 0;

    // 송신방식
    CbFSS->Properties->Items->Clear();
	if (FCodeFSS)
	{
		FOR_STL(TItsSubCode *, pSubCode, FCodeFSS->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			CbFSS->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
    CbFSS->ItemIndex = 0;
}
//---------------------------------------------------------------------------

void __fastcall TFrmSmsFail::CbFSSPropertiesChange(TObject *Sender)
{
    String sFSS = APP_GetCode(CbFSS);
    if (sFSS == "FSS01")
    {   //즉시 발송
        SeCYCL->Enabled = false;
	    DtHH->Enabled = false;;
    }
    else
    if (sFSS == "FSS02")
    {   //일정주기(분)로 발송
        SeCYCL->Enabled = true;
	    DtHH->Enabled = false;;
    }
    else
    if (sFSS == "FSS03")
    {   //지정된 시간 발송
        SeCYCL->Enabled = false;
	    DtHH->Enabled = true;;
    }
}
//---------------------------------------------------------------------------

