<?php
	require_once( "../inc/session.php" );
    require_once ($_SERVER['DOCUMENT_ROOT']."/inc/pgsql.inc");

	//$suid = $HTTP_COOKIE_VARS[SUID];
	$suid = $_COOKIE[SUID];

	if ($suid != "guru222")
	{
	    AlertBack("전체관리자만 접근할 수 있습니다.");
	    exit;
	}

	$mode = $_GET["mode"];
    $group_seq = isset($_GET['group']) ? $_GET['group'] : $_POST['group'];
	$group_name = $_GET["gname"];
	$phone = $_GET["phone"];

	$delete_btn = "";
	$which_word = "추가";
	if ($mode == "mod")
	{
		$objConn = new DB_Sql;
		$enCoding = "set client_encoding = 'uhc'";
		$objConn->query($enCoding);

		$strSql  = " SELECT member_name ";
		$strSql .= " FROM t_group_member ";
		$strSql .= " WHERE  member_cellphone = '$phone' AND group_seq = '$group_seq' ";

		$objConn->query($strSql);
		if($objConn->nf() != 0)
		{
			$objConn->next_record();
			$member_name = $objConn->f("member_name");
		}
		$objConn->close();
		$delete_btn = "<a href='javascript:delForm();'><img src='img/btn_del.gif' width='50' height='18' border='0'></a>";
		$which_word = "수정";
	}

    if($group_seq != "")
    {
        if(getenv("REQUEST_METHOD") == "GET")
        {
?>

<html>
<head>
<title>멤버<?=$which_word?></title>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
<link rel="stylesheet" href="img/style.css" type="text/css">
<script language='javascript'>
        function chkForm()
        {
            frm = document.info;
            if(frm.member_name.value == '')
            {
                alert('이름을 입력하세요!');
				frm.member_name.focus();
                return;
            }
            if(frm.member_cellphone.value == "")
            {
                alert('핸드폰번호를 입력하세요!');
				frm.member_cellphone.focus();
                return;
            }
            frm.submit();
		}

		function delForm()
        {
            frm = document.info;
            if(confirm('정말 삭제 하겠습니까?'))
            {
                frm.mode.value = "del";
                frm.member_cellphone.value = '<?=$phone?>';
                frm.submit();
            }
        }	
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="info" method="post" action="sms_member_add.html">
<table width="300" border="0" cellspacing="0" cellpadding="0">
  <tr> 
    <td style="padding-top:10" align="right">
      <table width="100%" border="0" cellpadding="0" cellspacing="0" class="ContentSubtitle">
        <tr> 
          <td width="10" height="25"><img src="img/icon_arrow.gif" width="16" height="10"></td>
          <td><?=$group_name?> 멤버<?=$which_word?></td>
        </tr>
      </table>
    </td>
  </tr>
  <tr> 
    <td> 
      <table border='0' width='300' cellpadding="3" cellspacing="1" bgcolor="e1e1e1">
        <tr> 
          <td width='80' align='center' bgcolor="#F6F5F2" class="txt"><font color="666666">이름</font></td>
          <td bgcolor="#FFFFFF" align='center' class="name_link"> 
            <input type="text" name="member_name" value="<?=$member_name?>">
          </td>
        </tr>
        <tr> 
          <td align='center' bgcolor="#F6F5F2" class="txt"><font color="666666">핸드폰</font></td>
          <td bgcolor="#FFFFFF" align='center' class="name_link"> 
            <input type="text" name="member_cellphone" value="<?=$phone?>">
			<input type="hidden" name="member_old_cellphone" value="<?=$phone?>">
			<input type="hidden" name="mode" value="<?=$mode?>">
			<input type='hidden' name='group' value='<?= $group_seq ?>'>
          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr> 
    <td style="padding-top:10" align="right"><a href="javascript:self.close();"><img src="img/btn_cancel.gif" width="50" height="18" border="0"></a>
	  <?=$delete_btn?> <a href="javascript:chkForm();"><img src="img/btn_add.gif" width="50" height="18" border="0"></a> 
    </td>
  </tr>
</table>
<br>
<div align='right'> </div>
</body>
</html>
<?php
        }else if(getenv("REQUEST_METHOD") == "POST")
        {
			$objConn = new DB_Sql;
	        $enCoding = "set client_encoding = 'uhc'";
	        $objConn->query($enCoding);
			
			$mode				= trim($_POST['mode']);
            $group_seq          = trim($_POST['group']);
            $member_name        = trim($_POST['member_name']);
			$member_old_cellphone   = trim(str_replace("-", "", $_POST['member_old_cellphone']));
            $member_cellphone   = trim(str_replace("-", "", $_POST['member_cellphone']));

			$strSql  = "SELECT COUNT(group_seq) AS cnt ";
            $strSql .= "FROM t_group_member ";
			$strSql .= "WHERE group_seq = ".$group_seq." AND member_cellphone = '".$member_cellphone."'";

            $objConn->query($strSql);
            if($objConn->nf() != 0)
            {
                $objConn->next_record();
                $cnt = $objConn->f("cnt");

                if($cnt == 0)
                {
					if ($mode == "add")
					{
	                    $strSql  = "INSERT INTO t_group_member(group_seq, member_name, member_cellphone, reg_datetime, update_datetime) ";
		                $strSql .= "VALUES (".$group_seq.", '".$member_name."', '".$member_cellphone."', now(), now())";

			            $objConn->query($strSql);
				        echo "<script language='javascript'>\n";
					    echo "  alert('등록되었습니다.');\n";
	                    echo "  opener.window.location.reload();\n";
						echo "	self.close();\n";
						echo "</script>";
					}
					else if ($mode == "mod")
					{
						$strSql  = "UPDATE t_group_member ";
						$strSql .= "SET member_name = '$member_name', member_cellphone = '$member_cellphone', update_datetime = now() ";
						$strSql .= "WHERE group_seq = '$group_seq' AND member_cellphone = '$member_old_cellphone' ";

						$objConn->query($strSql);
						echo "<script language='javascript'>\n";
						echo "  alert('수정되었습니다.');\n";
						echo "  opener.window.location.reload();\n";
						echo "  self.close();\n";
						echo "</script>";
					}
                }
				else if ($cnt > 0 && $mode == "mod" && $member_cellphone == $member_old_cellphone)
				{
					$strSql  = "UPDATE t_group_member ";
					$strSql .= "SET member_name = '$member_name', member_cellphone = '$member_cellphone', update_datetime = now() ";
					$strSql .= "WHERE group_seq = '$group_seq' AND member_cellphone = '$member_old_cellphone' ";

					$objConn->query($strSql);
					echo "<script language='javascript'>\n";
					echo "  alert('수정되었습니다.');\n";
					echo "  opener.window.location.reload();\n";
					echo "  self.close();\n";
					echo "</script>";
				}
				else if ($cnt == 1 && $mode == "del")
				{
					$strSql  = "DELETE FROM t_group_member ";
                    $strSql .= "WHERE group_seq = ".$group_seq." AND member_cellphone = '".$member_old_cellphone."'";
					$objConn->query($strSql);
					echo "<script language='javascript'>\n";
					echo "  alert('삭제되었습니다.');\n";
					echo "  opener.window.location.reload();\n";
					echo "  self.close();\n";
					echo "</script>";
				}					
				else
                {
                    echo "<script language='javascript'>\n";
                    echo "  alert('이미 등록되었습니다.');\n";
                    echo "  history.back(-1);\n";
                    echo "</script>";
                }
            }

			$objConn->close();
        }
    }else
    {
        echo "<script language='javascript'>\n";
        echo "  alert('잘못된 접근입니다..');\n";
        echo "  history.back(-1);\n";
        echo "</script>";
        die;
    }
?>
