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

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

#include "FrmSmsUserF.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 resource "*.dfm"

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

    FUpdated = false;
}
//---------------------------------------------------------------------------

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

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

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

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

bool __fastcall TFrmSmsUser::CheckField()
{
    String sId      = EdId->Text.Trim();
    String sName    = EdName->Text.Trim();
    String sCallTel = EdCallTel->Text.Trim();
    String sBackTel = EdBackTel->Text.Trim();
    String sDesc    = EdDesc->Text.Trim();

	if (sId == "")
    {
        Application->MessageBox(L"ID를 확인 하세요.", Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return false;
    }
	if (sName == "")
    {
        Application->MessageBox(L"사용자명을 확인 하세요.", Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return false;
    }
	if (sCallTel == "")
    {
        Application->MessageBox(L"전화번호를 확인 하세요.", Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return false;
    }
	if (sBackTel == "")
    {
        Application->MessageBox(L"회신 전화번호를 확인 하세요.", Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return false;
    }
    return true;
}
//---------------------------------------------------------------------------

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

    String sId      = EdId->Text.Trim();
    String sName    = EdName->Text.Trim();
    String sCallTel = EdCallTel->Text.Trim();
    String sBackTel = EdBackTel->Text.Trim();
    String sDesc    = EdDesc->Text.Trim();
    String sRegDt   = Now().FormatString("yyyymmddhhnnss");//EdRegDt->Text.Trim();

    EdRegDt->Text = sRegDt;

    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_USER L                          \r\n"
           "USING (SELECT :p01 AS SMS_USER_ID,                \r\n"
           "              :p02 AS SMS_USER_NM,                \r\n"
           "              :p03 AS SMS_USER_TEL,               \r\n"
           "              :p04 AS SMS_RPLY_TEL,               \r\n"
           "              :p05 AS SMS_USER_EXPL,              \r\n"
           "              :p06 AS RGST_DT                     \r\n"
           "         FROM DUAL                                \r\n"
           "      ) M                                         \r\n"
           "ON (L.SMS_USER_ID = M.SMS_USER_ID)                \r\n"
           "WHEN MATCHED THEN                                 \r\n"
           "    UPDATE SET L.SMS_USER_NM   = M.SMS_USER_NM,   \r\n"
           "               L.SMS_USER_TEL  = M.SMS_USER_TEL,  \r\n"
           "               L.SMS_RPLY_TEL  = M.SMS_RPLY_TEL,  \r\n"
           "               L.SMS_USER_EXPL = M.SMS_USER_EXPL, \r\n"
           "               L.RGST_DT       = M.RGST_DT        \r\n"
           "WHEN NOT MATCHED THEN                             \r\n"
           "    INSERT (L.SMS_USER_ID,                        \r\n"
           "            L.SMS_USER_NM,                        \r\n"
           "            L.SMS_USER_TEL,                       \r\n"
           "            L.SMS_RPLY_TEL,                       \r\n"
           "            L.SMS_USER_EXPL,                      \r\n"
           "            L.RGST_DT)                            \r\n"
           "    VALUES (M.SMS_USER_ID,                        \r\n"
           "            M.SMS_USER_NM,                        \r\n"
           "            M.SMS_USER_TEL,                       \r\n"
           "            M.SMS_RPLY_TEL,                       \r\n"
           "            M.SMS_USER_EXPL,                      \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", sName);
            ITSDb_SQLBind(pADO, "p03", sCallTel);
            ITSDb_SQLBind(pADO, "p04", sBackTel);
            ITSDb_SQLBind(pADO, "p05", sDesc);
            ITSDb_SQLBind(pADO, "p06", sRegDt);

            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 TFrmSmsUser::EdCallTelKeyPress(TObject *Sender, wchar_t &Key)
{
    if (Key > '9' || Key < '0')
    {
        if (Key != VK_BACK && Key != VK_TAB && Key != '-')
            Key = 0;
    }
}
//---------------------------------------------------------------------------

