<?php

include getenv("DOCUMENT_ROOT")."/include/session_include.php";

//$func->checkAdmin("index.php");

/*
for($i=0; $i<sizeof($_POST); $i++) {
	list($key, $value) = each($_POST);
	$$key = $value;

	if(is_array($value))
	{
		$count = 10;
		for($i = 0; $i < $count; $i ++) {
			if ($value[$i]) echo "ARRAY[$key][$value[$i]]<br>";
		}
	}
	else 	echo "[$key][$value]<br>";
	//print_r($_POST);
}

//exit;
*/

$mode = $mmode;

if($mode == "update" && $m_uid == "") {
			echo 0;
			exit();
}

$columns = array();
$values = array();

if($mode == "create") {
			$columns[] = "m_userid";
			$columns[] = "m_signupdate";
}

$columns[] = "m_pwd";
$columns[] = "m_firstname";
$columns[] = "m_lastname";
//$columns[] = "m_initial";
$columns[] = "m_cell";

if ($actionPage == "ADMINUSERINFO") {}

$columns[] = "m_status";
$columns[] = "m_level";
$columns[] = "m_comment";


if ($m_level == 9) {
	$columns[] = "m_initial";
	$columns[] = "m_currentdriver";
	$columns[] = "m_defaultdriver";
	$columns[] = "m_currentdriverinitial";
	$columns[] = "m_defaultdriverinitial";	
	$columns[] = "m_regionuid";
	$columns[] = "m_region";
}

////////////	
// data
////////////	
if($mode == "create") {
			$values[] = trim($m_userid);
			$values[] = date("YmdHis");
}

$pwdSize = 50;
$feed = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for ($i=0; $i < $pwdSize; $i++)
	$rand_str .= substr($feed, rand(0, strlen($feed)-1), 1); 

$CRYPT_WORD = md5($rand_str);

$hash = crypt($m_pwd,$CRYPT_WORD);
//echo"[$rand_str]<br>[$CRYPT_WORD]<br>[$hash]";exit;

$values[] = $hash;
$values[] = str_replace("\\", "", trim($m_firstname));
$values[] = str_replace("\\", "", trim($m_lastname));
//$values[] = str_replace("\\", "", trim($m_initial));
$values[] = str_replace("-", "", trim($m_cell));

// password 입력없으면 password update 안함
/*
if ($user_password != "" && $user_password_chk != "") {
			if ($_SERVER["REMOTE_ADDR"] != "127.0.0.1") $user_passwordSTR = password_hash($user_password, PASSWORD_DEFAULT);
			else $user_passwordSTR = $user_password;
			
			$values[] = $user_passwordSTR;
}
*/

if ($actionPage == "ADMINUSERINFO") {}

$values[] = $m_status;
$values[] = $m_level;		// Admin : 1, Manager : 3, Staff : 5, Accounting : 6, Sales : 7, Driver : 9
$values[] = str_replace("\\", "", trim($m_comment));

if ($m_level == 9) {
	$values[] = $m_initial;
	$values[] = $m_currentdriver;
	$values[] = $m_defaultdriver;

	$qry_cd = "SELECT * FROM tbl_driver WHERE dr_uid = '$m_currentdriver'";
	$rt_cd = $jdb->fQuery($qry_cd, "query error");
	
	$values[] = $rt_cd['dr_initial'];	
	
	$qry_dd = "SELECT * FROM tbl_driver WHERE dr_uid = '$m_defaultdriver'";
	$rt_dd = $jdb->fQuery($qry_dd, "query error");
	
	$values[] = $rt_dd['dr_initial'];	
	
	$values[] = $m_regionuid;
	
	$qry_rg = "SELECT * FROM tbl_region WHERE rg_uid = '$m_regionuid'";
	$rt_rg = $jdb->fQuery($qry_rg, "query error");

	$values[] = $rt_rg['rg_region'];
}

//for ($i=0; $i < count($columns); $i++)
//echo "[$columns[$i]][$values[$i]]<br>";
//echo "[UID=$uid][ID=$userid][MAXUID=$maxuid]";
//exit;	

if($mode == "create") {
			$jdb->iQuery("tbl_member", $columns, $values);
			echo 1;
			exit();
}

else if($mode == "update") {
			$jdb->uQuery("tbl_member", $columns, $values, " where m_uid = '$m_uid' ");
			echo 1;
			exit();
			
}

exit();

?>