<?php
	require_once ($_SERVER['DOCUMENT_ROOT']."/inc/pgsql.inc");
	
	$group_seq 			= isset($_GET['group']) ? $_GET['group'] 	: $_POST['group'];
	$member_cellphone 	= isset($_GET['cell']) 	? $_GET['cell'] 	: $_POST['member_cellphone'];
	
	if($member_cellphone != "" && $group_seq != "")
	{
		$objConn = new DB_Sql;
		$enCoding = "set client_encoding = 'uhc'";
		$objConn->query($enCoding);
		 
		if(getenv("REQUEST_METHOD") == "GET")
		{
			$strSql  = "SELECT member_name, member_cellphone ";
			$strSql .= "FROM t_group_member ";
			$strSql .= "WHERE group_seq = ".$group_seq." AND member_cellphone = '".$member_cellphone."'";
			
			$objConn->query($strSql);
			
			if($objConn->nf() == 1)
			{
				$objConn->next_record();
				$member_name = $objConn->f("member_name");
				$member_cellphone = $objConn->f("member_cellphone");
			}else
			{
				echo "<script language='javascript'>\n";
				echo "	alert('잘못된 접근입니다..');\n";
				echo "	history.back(-1);\n";
				echo "</script>";			
				die;
			}
?>	
<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.actiontype.value = 1;
			frm.submit();
		}
		
		function delForm()
		{
			frm = document.info;	
			if(confirm('정말 삭제 하겠습니까?'))
			{
				frm.actiontype.value = 0;
				frm.member_cellphone.value = '<?= $member_cellphone ?>';
				frm.submit();
			}
		}
	</script>		
</head>
<body>			
<form name='info' method='POST' action='./mod_member.html'>
	<table>
		<tr>
			<td>&nbsp;이름</td>
			<td><input name='member_name' maxlength='20' value='<?= $member_name ?>'></td>			
		</tr>
		<tr>
			<td>&nbsp;핸드폰</td>
			<td><input name='member_cellphone' maxlength='15' value='<?= $member_cellphone ?>'></td>			
		</tr>
		<tr>
			<td colspan='2'>
				<input type='hidden' name='group' value='<?= $group_seq ?>'>				
				<input type='hidden' name='actiontype'>
				<input type='hidden' name='org_member_cellphone' value='<?= $member_cellphone ?>'>
				<div align='right'><a href='javascript:history.back(-1)'>뒤로</a>&nbsp;<a href='javascript:delForm()'>삭제</a>&nbsp;<a href='javascript:chkForm();'>수정</a></div>
				</td>
		</tr>
	</table>		
</form>			
</body>
</html>
<?php			
		}else if(getenv("REQUEST_METHOD") == "POST")
		{
			$action_type 		  = trim($_POST['actiontype']);
			$group_seq 	 		  = trim($_POST['group']);
			$member_name 		  = trim($_POST['member_name']);
			$member_cellphone 	  = trim(str_replace("-", "", $_POST['member_cellphone']));
			$org_member_cellphone = trim(str_replace("-", "", $_POST['org_member_cellphone']));
			
			$strSql  = "SELECT COUNT(group_seq) AS cnt ";
			$strSql .= "FROM t_group_member ";
			$strSql .= "WHERE group_seq = ".$group_seq." AND member_cellphone = '".$org_member_cellphone."'";
			
			$objConn->query($strSql);
			if($objConn->nf() != 0)
			{
				$objConn->next_record();
				$cnt = $objConn->f("cnt");	
				
				if($cnt == 1)
				{		
					if($action_type == 0)		//삭제
					{
						$strSql  = "DELETE FROM t_group_member ";
						$strSql .= "WHERE group_seq = ".$group_seq." AND member_cellphone = '".$org_member_cellphone."'";	
						
					}else if($action_type == 1)	//수정
					{
						$strSql  = "UPDATE t_group_member ";
						$strSql .= "SET member_name ='".$member_name."', ";
						$strSql .= "    member_cellphone ='".$member_cellphone."', ";
						$strSql .= "    update_datetime = now() ";
						$strSql .= "WHERE group_seq = ".$group_seq." AND member_cellphone = '".$org_member_cellphone."'";
					}					
					
					$objConn->query($strSql);
					echo "<script language='javascript'>\n";
					if($action_type == 0)
					{
						echo "	alert('삭제되었습니다.');\n";
					}else if($action_type == 1)
					{
						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>";			
		die;
	}
?>