<?php

	include $_SERVER["DOCUMENT_ROOT"].'/common/lib/comm.php';

	$table_name = "member_master";

	$PageNo = trim(avoid_crack($_REQUEST["PageNo"]));

	$mode = trim(avoid_crack($_POST["mode"]));
	$cd_user = trim(avoid_crack($_POST["cd_user"]));
	$cd_userid = trim(avoid_crack($_POST["cd_userid"]));
	$nm_name = trim(avoid_crack($_POST["nm_name"]));
	$nm_pass = trim(avoid_crack($_POST["nm_pass"]));
	$ds_type = trim(avoid_crack($_POST["ds_type"]));
	$ds_level = trim(avoid_crack($_POST["ds_level"]));
	$nm_company = trim(avoid_crack($_POST["nm_company"]));
//	$nm_birth = trim(avoid_crack($_POST["nm_birth"]));
//	$nm_tel = trim(avoid_crack($_POST["nm_tel"]));
//	$nm_tel_01 = trim(avoid_crack($_POST["nm_tel_01"]));
//	$nm_tel_02 = trim(avoid_crack($_POST["nm_tel_02"]));
//	$nm_tel_03 = trim(avoid_crack($_POST["nm_tel_03"]));
	$nm_hp = trim(avoid_crack($_POST["nm_hp"]));
	$nm_hp_01 = trim(avoid_crack($_POST["nm_hp_01"]));
	$nm_hp_02 = trim(avoid_crack($_POST["nm_hp_02"]));
	$nm_hp_03 = trim(avoid_crack($_POST["nm_hp_03"]));
	$nm_email = trim(avoid_crack($_POST["nm_email"]));
	$ds_company_bp = trim(avoid_crack($_POST["ds_company_bp"]));
	$ds_branch = trim(avoid_crack($_POST["ds_branch"]));
	$ds_area = trim(avoid_crack($_POST["ds_area"]));
	$ds_area_sub = trim(avoid_crack($_POST["ds_area_sub"]));
	$ds_agree1 = trim(avoid_crack($_POST["ds_agree1"]));
	$ds_agree2 = trim(avoid_crack($_POST["ds_agree2"]));
	$ds_agree3 = trim(avoid_crack($_POST["ds_agree3"]));
	$ds_agree4 = trim(avoid_crack($_POST["ds_agree4"]));
/*
	$nm_zip = trim(avoid_crack($_POST["nm_zip"]));
	$nm_addr = trim(avoid_crack($_POST["nm_addr"]));
	$nm_addr_sub = trim(avoid_crack($_POST["nm_addr_sub"]));
	$ds_job = trim(avoid_crack($_POST["ds_job"]));
	$nm_etc_01 =trim(avoid_crack($_POST["nm_etc_01"]));
	$nm_etc_02 = trim(avoid_crack($_POST["nm_etc_02"]));
	$nm_etc_03 = trim(avoid_crack($_POST["nm_etc_03"]));
*/
	$ds_status = trim(avoid_crack($_POST["ds_status"]));
	$ds_delind = trim(avoid_crack($_POST["ds_delind"]));

	if(!empty($_POST["arr_cd_user"])) $arr_cd_user = trim(avoid_crack(implode(",",$_POST["arr_cd_user"])));
	$cd_procid = $_SESSION['admin_cd_userid'];

//	$PageNo = trim(avoid_crack($_POST["PageNo"]));

	switch($mode) {
	//저장하기
	case "Add" :

		$RES = array('CODE'=>'9999', 'MSG'=>'');
		try {
			// if(strlen($cd_userid)<3 || strlen($cd_userid)>14) {
			// 	throw new Exception("아이디(3~14)의 자리수가 맞지 않습니다.", "9990");
			// }
			// if($cd_userid=="userid" || $cd_userid=="member" || $cd_userid=="admin"){
			// 	throw new Exception("중복된 아이디입니다. 관리자에게 문의하십시요.", "9990");
			// }
			// echo "select count(*) from member_master where cd_userid='$nm_email'";
			$check_id=@mysql_result(mysql_query("select count(*) from member_master where cd_userid='$nm_email'"),0,0);
			if($check_id>0) {
				throw new Exception("이미 사용중인 아이디입니다.다시 한번 확인하세요.", "9990");
			}

			// $sql = "insert into admin_info (cd_userid , nm_name ,nm_pass , ds_level , nm_info1, nm_info2, nm_info3, ds_company_bp, ds_branch, ds_open, dt_insert, nm_insert, dt_update, nm_update, ds_delind, auth1, auth2, auth3, auth4, auth5, auth6, auth7, auth8, auth9  ) values ('$cd_userid','$nm_name',password('$nm_pass'),'$ds_level','$nm_info1','$nm_info2','$nm_info3','$ds_company_bp','$ds_branch','$ds_open',now(),'$cd_userid',now(),'$cd_userid','N','$auth1','$auth2','$auth3','$auth4','$auth5','$auth6','$auth7','$auth8', '$auth9')";

			$sql  = "INSERT INTO $table_name SET
						cd_userid = '$nm_email'
						, nm_name = '$nm_name'
						, ds_type = '$ds_type'
						, ds_level = '$ds_level'
						, nm_hp = '$nm_hp'
						, nm_email = '$nm_email'
						, ds_company_bp = '$ds_company_bp'
						, ds_branch = '$ds_branch'
						, ds_area = '$ds_area'
						, ds_area_sub = '$ds_area_sub'
						, ds_agree1 = '$ds_agree1'
						, ds_agree2 = '$ds_agree2'
						, ds_agree3 = '$ds_agree3'
						, ds_agree4 = '$ds_agree4'
						, nm_pass = password('".$nm_pass."')
						, dt_insert = now()
						, nm_insert = '$cd_procid'
						, dt_update = now()
						, nm_update = '$cd_procid'
						, ds_delind = 'N'
			";
			//				"nm_company"=>$nm_company,
			//				"nm_birth"=>$nm_birth,
			//				"nm_hp_01"=>$nm_hp_01,
			//				"nm_hp_02"=>$nm_hp_02,
			//				"nm_hp_03"=>$nm_hp_03,
			//				"nm_zip"=>$nm_zip,
			//				"nm_addr"=>$nm_addr,
			//				"nm_addr_sub"=>$nm_addr_sub,
			//				"ds_status"=>$ds_status,
		// echo $sql;
		// exit;
			$result  = mysql_query($sql,$connect);

			if(!$result) {
				throw new Exception("회원 등록처리에 실패 하였습니다.잠시후 다시 시도해 주세요.", "9990");
			}

			$RES['CODE'] = "0000";
			$RES['MSG'] = "회원 등록이 완료되었습니다.";

		}catch(Exception $e){
			$RES['CODE'] = $e->getCode();
			$RES['MSG'] = $e->getMessage();
		}
		@mysql_close($connect);

		echo json_encode($RES);
		exit;

// 		$_fields = Array(
// 				"cd_userid"=>$cd_userid,
// 				"nm_name"=>$nm_name,
// 				"ds_type"=>$ds_type,
// 				"ds_level"=>$ds_level,
// //				"nm_company"=>$nm_company,
// //				"nm_birth"=>$nm_birth,
// 				"nm_hp"=>$nm_hp,
// //				"nm_hp_01"=>$nm_hp_01,
// //				"nm_hp_02"=>$nm_hp_02,
// //				"nm_hp_03"=>$nm_hp_03,
// 				"nm_email"=>$nm_email,
// 				"ds_company_bp"=>$ds_company_bp,
// 				"ds_branch"=>$ds_branch,
// 				"ds_area"=>$ds_area,
// 				"ds_area_sub"=>$ds_area_sub,
// 				"ds_agree1"=>$ds_agree1,
// 				"ds_agree2"=>$ds_agree2,
// 				"ds_agree3"=>$ds_agree3,
// 				"ds_agree4"=>$ds_agree4,
// //				"nm_zip"=>$nm_zip,
// //				"nm_addr"=>$nm_addr,
// //				"nm_addr_sub"=>$nm_addr_sub,
// //				"ds_status"=>$ds_status,
// 				"dt_insert" => now(),
// 				"nm_insert" => $cd_procid,
// 				"dt_update" => now(),
// 				"nm_update" => $cd_procid,
// 				"ds_delind" => "N"
// 			);
//
// 		$result  = $dbCon->insertDAO($_fields,$table_name);
//
// 		$sqlPwd="UPDATE member_master set nm_pass = password('".$nm_pass."')  where nm_email = '".$nm_email."'";
// 		$result  = $dbCon->query($sqlPwd);
//
// 		if ($result) {
// 			AlertRedirect("등록에 성공 하였습니다.","member.php");
// 		}else{
// 			AlertBack("등록 실패 하였습니다.");
// 		}

	break;

	//수정하기
	case "Update":

		$RES = array('CODE'=>'9999', 'MSG'=>'');
		try {
			// if(strlen($cd_userid)<3 || strlen($cd_userid)>14) {
			// 	throw new Exception("아이디(3~14)의 자리수가 맞지 않습니다.", "9990");
			// }
			// if($cd_userid=="userid" || $cd_userid=="member" || $cd_userid=="admin"){
			// 	throw new Exception("중복된 아이디입니다. 관리자에게 문의하십시요.", "9990");
			// }
			$add_pwd_sql = "";
			if($_POST["nm_pass"]){
				$add_pwd_sql = " , nm_pass = password('".$nm_pass."') ";
			}

			$sql="UPDATE $table_name set
						nm_name = '$nm_name'
						, nm_hp ='$nm_hp'
						, ds_company_bp ='$ds_company_bp'
						, ds_branch ='$ds_branch'
						, ds_area ='$ds_area'
						, ds_area_sub ='$ds_area_sub'
						, dt_update = now()
						, nm_update = '$cd_procid'
						$add_pwd_sql
					where cd_user = '$cd_user'";

					//				"ds_type"=>$ds_type,
					//				"ds_level"=>$ds_level,
					//				"nm_birth"=>$nm_birth,
					//				"nm_hp_01"=>$nm_hp_01,
					//				"nm_hp_02"=>$nm_hp_02,
					//				"nm_hp_03"=>$nm_hp_03,
					//				"nm_email"=>$nm_email,
					//				"ds_agree1"=>$ds_agree1,
					//				"ds_agree2"=>$ds_agree2,
					//				"ds_agree3"=>$ds_agree3,
					//				"ds_agree4"=>$ds_agree4,
					//				"nm_zip"=>$nm_zip,
					//				"nm_addr"=>$nm_addr,
					//				"nm_addr_sub"=>$nm_addr_sub,
					//				"ds_status"=>$ds_status,
					//				"dt_insert" => now(),
					//				"nm_insert" => $cd_procid,
// echo $sql;
// exit;
			$result  = mysql_query($sql,$connect);

			if(!$result) {
				throw new Exception("회원 정보 수정에 실패 하였습니다.", "9990");
			}


			$RES['CODE'] = "0000";
			$RES['MSG'] = "회원 수정이 완료되었습니다.";

		}catch(Exception $e){
			$RES['CODE'] = $e->getCode();
			$RES['MSG'] = $e->getMessage();
		}
		@mysql_close($connect);

		echo json_encode($RES);
		exit;

	break;
	//삭제하기
	case "Delete" :
		$RES = array('CODE'=>'9999', 'MSG'=>'');
		try {
			if(empty($cd_user)) {
				throw new Exception("회원 정보가 없습니다.", "9990");
			}
			$sql="UPDATE $table_name set
						ds_delind ='Y'
						, dt_update = now()
						, nm_update = '$cd_procid'
					where cd_user = '$cd_user'";
			// echo $sql;
			// exit;
			$result  = mysql_query($sql,$connect);

			if(!$result) {
				throw new Exception("회원 정보 삭제에 실패 하였습니다.", "9990");
			}


			$RES['CODE'] = "0000";
			$RES['MSG'] = "회원 삭제가 완료되었습니다.";

		}catch(Exception $e){
			$RES['CODE'] = $e->getCode();
			$RES['MSG'] = $e->getMessage();
		}
		@mysql_close($connect);

		echo json_encode($RES);
		exit;
		//
		// $_fields = Array(
		// 			"ds_delind"=>"Y",
		// 			"dt_update" => now(),
		// 			"nm_update" => $cd_procid
		// 		);
		//
		// $result  = $dbCon->updateDAO($_fields,$table_name,"cd_user='".$cd_user."'");
		//
		//
		// if ($result) {
		//
		// 	AlertRedirect("삭제 되었습니다.","list.php?PageNo=$PageNo");
		//
		//  }else{
		// 	AlertRedirect("삭제 실패하였습니다.","list.php?PageNo=$PageNo");
		//  }
	break;

	//삭제하기
	case "DeleteArr" :

		$RES = array('CODE'=>'9999', 'MSG'=>'');
		try {
			// if(strlen($cd_userid)<3 || strlen($cd_userid)>14) {
			// 	throw new Exception("아이디(3~14)의 자리수가 맞지 않습니다.", "9990");
			// }
			// if($cd_userid=="userid" || $cd_userid=="member" || $cd_userid=="admin"){
			// 	throw new Exception("중복된 아이디입니다. 관리자에게 문의하십시요.", "9990");
			// }
			$sql="UPDATE member_master set ds_delind='Y', dt_update = now(), nm_update = '".$cd_procid."'  where cd_user in (".$arr_cd_user.")";

			$result  = mysql_query($sql,$connect);

			if(!$result) {
				throw new Exception("탈퇴처리에 실패 하였습니다.", "9990");
			}


			$RES['CODE'] = "0000";
			$RES['MSG'] = "탈퇴처리가 완료되었습니다.";

		}catch(Exception $e){
			$RES['CODE'] = $e->getCode();
			$RES['MSG'] = $e->getMessage();
		}
		@mysql_close($connect);

		echo json_encode($RES);
		exit;

	break;

	//휴면 해제하기
	case "cancelDormant" :

		$RES = array('CODE'=>'9999', 'MSG'=>'');
		try {

			$sql="UPDATE member_master set ds_status='A0', dt_dormant = null where cd_user = '".$cd_user."' ";
			$result  = mysql_query($sql,$connect);

			if(!$result) {
				throw new Exception("휴면 해제 실패하였습니다.", "9990");
			}

			$RES['CODE'] = "0000";
			$RES['MSG'] = "휴면 해제 되었습니다.";

		}catch(Exception $e){
			$RES['CODE'] = $e->getCode();
			$RES['MSG'] = $e->getMessage();
		}
		@mysql_close($connect);

		echo json_encode($RES);
		exit;

	break;

	case "IdChk":

		$_whereis = "  nm_email = '". $nm_email . "'";

		$cnt = $dbCon->getOneDAO("count(*)",$table_name, $_whereis, 1);
		if($cnt==0) {
			echo("Y");
			exit;
		} else {
			echo("N");
			exit;
		}
	break;

	case "IdSearch":

		$_whereis = " ds_delind='N' And nm_name = '". $nm_name . "' AND nm_email = '". $nm_email . "'";

		$userid = $dbCon->getOneDAO("cd_userid",$table_name, $_whereis, 1);
		if($userid) {
			echo($userid);
			exit;
		} else {
			echo("");
			exit;
		}
	break;

	case "PwSearch":

		$_whereis = " ds_delind='N' And cd_userid = '".$cd_userid."' And nm_name = '". $nm_name . "' AND nm_email = '". $nm_email . "'";

		$userid = $dbCon->getOneDAO("cd_user",$table_name, $_whereis, 1);
		if($userid) {
			$new_passwd = generatePassword(8);

			$sqlPwd="UPDATE member_master set nm_pass = password('".$new_passwd."')  where cd_user = '".$userid."'";
			$result  = $dbCon->query($sqlPwd);

			echo($new_passwd);
			exit;
		} else {
			echo("");
			exit;
		}
	break;

	default :
		AlertBack("지정되지않은 요청입니다");
	break;
}
mysql_close($connect);
?>
