<?php
	require_once ($_SERVER['DOCUMENT_ROOT']."/inc/pgsql.inc");
	
	$group_seq = isset($_GET['group']) ? $_GET['group'] : $_POST['group'];
	
	if($group_seq != "")
	{
		$objConn = new DB_Sql;
		$enCoding = "set client_encoding = 'uhc'";
		$objConn->query($enCoding);
		
		if(getenv("REQUEST_METHOD") == "GET")
		{
?>	
<html>
<head>
	<title></title>
	<script language='javascript'>
		function chkForm()
		{
			frm = document.info;
			if(frm.member_name.value == '')
			{
				alert('이름을 입력하세요!');
				return;
			}
			if(frm.member_cellphone.value == "")
			{
				alert('핸드폰번호를 입력하세요!');
				return;
			}
			frm.submit();
		}
	</script>		
</head>
<body>			
<form name='info' method='POST' action='./add_member.html'>
	<table>
		<tr>
			<td>&nbsp;이름</td>
			<td><input name='member_name' maxlength='20'></td>
		</tr>
		<tr>
			<td>&nbsp;핸드폰</td>
			<td><input name='member_cellphone' maxlength='15'></td>
		</tr>
		<tr>
			<td colspan='2'>
				<input type='hidden' name='group' value='<?= $group_seq ?>'>
				<div align='right'><a href='javascript:history.back(-1)'>뒤로</a>&nbsp;<a href='javascript:chkForm();'>추가</a></div>
				</td>
		</tr>
	</table>		
</form>			
</body>
</html>
<?php			
		}else if(getenv("REQUEST_METHOD") == "POST")
		{
			$group_seq 	 		= trim($_POST['group']);
			$member_name 		= trim($_POST['member_name']);
			$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)
				{
					$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 "	location.href = './group_list.html';\n";
					echo "</script>";
				}else
				{
					echo "<script language='javascript'>\n";
					echo "	alert('이미 등록되었습니다.');\n";
					echo "	history.back(-1);\n";
					echo "</script>";
				}				
			}					
		}	
	}else
	{
		echo "<script language='javascript'>\n";
		echo "	alert('잘못된 접근입니다..');\n";
		echo "	history.back(-1);\n";
		echo "</script>";			
		die;		
	}
?>