<?php
	require_once( "../inc/session.php" );
    require_once ($_SERVER['DOCUMENT_ROOT']."/inc/pgsql.inc");
	$mode = $_GET["mode"];
	$group_seq = $_GET["group"];

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

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

	$delete_btn = "";
	$which_word = "추가";
	if($group_seq != "" && $mode == "mod")	
	{
		$objConn = new DB_Sql;

		$strSql  = " SELECT group_id, group_name ";
		$strSql .= " FROM t_group ";
		$strSql .= " WHERE group_seq = '$group_seq' ";
		
		$objConn->query($strSql);
		if($objConn->nf() != 0)
        {
	        $objConn->next_record();
			$group_id = $objConn->f("group_id");
			$group_name = $objConn->f("group_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(getenv("REQUEST_METHOD") != "POST")
    {
?>

<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.group_id.value == '')
            {
                alert('그룹아이디를 입력하세요!');
				frm.group_id.focus();
                return;
            }
            if(frm.group_name.value == '')
            {
                alert('그룹명을 입력하세요!');
				frm.group_name.focus();
                return;
            }
            if(frm.group_password.value == '' || frm.group_password_confirm.value == '')
            {
                alert('비밀번호를 입력하세요!');
				frm.group_password.focus();
                return;
            }
            if(frm.group_password.value != frm.group_password_confirm.value)
            {
                alert('비밀번호를 확인 하십시오!');
				frm.group_password_confirm.focus();
                return;
            }
            frm.submit();
        }
	function delForm()
	{
		frm = document.info;
		if(confirm('정말 삭제 하겠습니까?'))
		{
			frm.mode.value = "del";
			frm.submit();
		}
	}
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="info" method="post" action="sms_group_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>그룹<?=$which_word?></td>
        </tr>
      </table>
    </td>
  </tr>
  <tr> 
    <td> 
      <table border='0' width='300' cellpadding="3" cellspacing="1" bgcolor="e1e1e1">
        <tr> 
          <td width='100' align='center' bgcolor="#F6F5F2" class="txt"><font color="666666">그룹ID</font></td>
          <td bgcolor="#FFFFFF" align='center' class="name_link" width="204"> 
            <input type="text" name="group_id" value="<?=$group_id?>">
			<input type="hidden" name="group_old_id" value="<?=$group_id?>">
          </td>
        </tr>
        <tr> 
          <td align='center' bgcolor="#F6F5F2" class="txt"><font color="666666">그룹명</font></td>
          <td bgcolor="#FFFFFF" align='center' class="name_link" width="204"> 
            <input type="text" name="group_name" value="<?=$group_name?>">
          </td>
        </tr>
        <tr>
          <td align='center' bgcolor="#F6F5F2" class="txt">패스워드</td>
          <td bgcolor="#FFFFFF" align='center' class="name_link" width="204">
            <input type="password" name="group_password">
          </td>
        </tr>
        <tr>
          <td align='center' bgcolor="#F6F5F2" class="txt">패스워드확인</td>
          <td bgcolor="#FFFFFF" align='center' class="name_link" width="204"> 
            <input type="password" name="group_password_confirm">
			<input type="hidden" name="mode" value="<?=$mode?>">
			<input type="hidden" name="group_seq" 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")
    {
		$mode = trim($_POST['mode']);
		$gseq = trim($_POST['group_seq']);
        $group_id = trim($_POST['group_id']);
		$group_old_id = trim($_POST['group_old_id']);
        $group_name = trim($_POST['group_name']);
        $group_password = trim($_POST['group_password']);
        $group_password_confirm = trim($_POST['group_password_confirm']);

        $objConn = new DB_Sql;
        $enCoding = "set client_encoding = 'uhc'";
        $objConn->query($enCoding);

		$strSql  = "SELECT COUNT(group_seq) AS cnt ";
		$strSql .= "FROM t_group ";
		$strSql .= "WHERE group_id = '".$group_id."' AND deleted_yn = 'N' ";

		$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(group_id, group_password, group_name, deleted_yn, ";
					$strSql .= "                    reg_datetime, update_datetime) ";
					$strSql .= "VALUES ('".$group_id."', '".$group_password."', '".$group_name."', 'N', now(), now())";

					$objConn->query($strSql);
					echo "<script language='javascript'>\n";
					echo "  alert('등록되었습니다.');\n";
					echo "	opener.window.location.reload();\n";
					echo "	self.close();";
					echo "</script>";
				}
				else if ($mode == "mod")
				{
					$strSql  = "UPDATE t_group ";
					$strSql .= "SET group_id = '$group_id', group_name = '$group_name', group_password = '$group_password' ";
					$strSql .= "WHERE group_seq = '$gseq' ";

					$objConn->query($strSql);
					echo "<script language='javascript'>\n";
					echo "  alert('수정되었습니다.');\n";
					echo "  opener.window.location.reload();\n";
					echo "  self.close();";
					echo "</script>";
				}
			}
			else if ($mode == "mod" && $cnt == 1 && $group_id == $group_old_id)
			{
				$strSql  = "UPDATE t_group ";
				$strSql .= "SET group_id = '$group_id', group_name = '$group_name', group_password = '$group_password' ";
				$strSql .= "WHERE group_seq = '$gseq' ";

				$objConn->query($strSql);
				echo "<script language='javascript'>\n";
				echo "  alert('수정되었습니다.');\n";
				echo "  opener.window.location.reload();\n";
				echo "  self.close();";
				echo "</script>";

			}
			else if ($mode == "del" && $cnt == 1)
			{
				$strSql  = "UPDATE t_group SET deleted_yn = 'Y' ";
				$strSql .= "WHERE group_seq = '$gseq' ";
				$objConn->query($strSql);

				$strSql  = "DELETE FROM t_group_member ";
				$strSql .= "WHERE group_seq = '$gseq' ";
				$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>";
				die;
			}
		}else
		{
			echo "<script language='javascript'>\n";
			echo "  alert('잘못된 접근입니다.');\n";
			echo "  history.back(-1);\n";
			echo "</script>";
			die;			
		}
	$objConn->close();
	}
	die;
?>
