<?php
include("CheckSession.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="馬戲團" />
<title>馬戲團</title>
</head>
<?php
require_once("class/DBConfig.php");
require_once("class/DBOperation.php");
require_once("class/MacOperation.php");

$action = $_POST["actionOp"];
$id = $_POST["id"];
$CUser = $_POST["AId"];
$AName = $_POST["AName"];
$CTel = $_POST['CTel'];
$CClass = $_POST['radClass'];

$CNickname = $_POST['CNickname'];
$CMiss = nl2br($_POST['CMiss']);

$CSource = "";
$chkSourceL = $_POST['chkSourceL'];
if($chkSourceL != null && $chkSourceL != "")
{
	if($CSource == "")
	{
		$CSource = "L";
	}
	else
	{
		$CSource = $CSource.",L";
	}
}
$chkSourceW = $_POST['chkSourceW'];
if($chkSourceW != null && $chkSourceW != "")
{
	if($CSource == "")
	{
		$CSource = "W";
	}
	else
	{
		$CSource = $CSource.",W";
	}
}
$chkSourceT = $_POST['chkSourceT'];
if($chkSourceT != null && $chkSourceT != "")
{
	if($CSource == "")
	{
		$CSource = "T";
	}
	else
	{
		$CSource = $CSource.",T";
	}
}
$chkSourceG = $_POST['chkSourceG'];
if($chkSourceG != null && $chkSourceG != "")
{
	if($CSource == "")
	{
		$CSource = "G";
	}
	else
	{
		$CSource = $CSource.",G";
	}
}

$chkSourceS = $_POST['chkSourceS'];
if($chkSourceS != null && $chkSourceS != "")
{
	if($CSource == "")
	{
		$CSource = "S";
	}
	else
	{
		$CSource = $CSource.",S";
	}
}

$chkSourceO = $_POST['chkSourceO'];
if($chkSourceO != null && $chkSourceO != "")
{
	if($CSource == "")
	{
		$CSource = "O";
	}
	else
	{
		$CSource = $CSource.",O";
	}
}

$CArea = "";
$chkTaipei = $_POST['chkTaipei'];
if($chkTaipei != null && $chkTaipei != "")
{
	if($CArea == "")
	{
		$CArea = "N:".$chkTaipei;
	}
	else
	{
		$CArea = $CArea.","."N:".$chkTaipei;
	}
}
$chkTaoyuan = $_POST['chkTaoyuan'];
if($chkTaoyuan != null && $chkTaoyuan != "")
{
	if($CArea == "")
	{
		$CArea = "N:".$chkTaoyuan;
	}
	else
	{
		$CArea = $CArea.","."N:".$chkTaoyuan;
	}
}
$chkZhongli = $_POST['chkZhongli'];
if($chkZhongli != null && $chkZhongli != "")
{
	if($CArea == "")
	{
		$CArea = "N:".$chkZhongli;
	}
	else
	{
		$CArea = $CArea.","."N:".$chkZhongli;
	}
}
$chkHsinchu = $_POST['chkHsinchu'];
if($chkHsinchu != null && $chkHsinchu != "")
{
	if($CArea == "")
	{
		$CArea = "M:".$chkHsinchu;
	}
	else
	{
		$CArea = $CArea.","."M:".$chkHsinchu;
	}
}
$chkTaichung = $_POST['chkTaichung'];
if($chkTaichung != null && $chkTaichung != "")
{
	if($CArea == "")
	{
		$CArea = "M:".$chkTaichung;
	}
	else
	{
		$CArea = $CArea.","."M:".$chkTaichung;
	}
}
$chkZhanghua = $_POST['chkZhanghua'];
if($chkZhanghua != null && $chkZhanghua != "")
{
	if($CArea == "")
	{
		$CArea = "M:".$chkZhanghua;
	}
	else
	{
		$CArea = $CArea.","."M:".$chkZhanghua;
	}
}
$chkTainan = $_POST['chkTainan'];
if($chkTainan != null && $chkTainan != "")
{
	if($CArea == "")
	{
		$CArea = "S:".$chkTainan;
	}
	else
	{
		$CArea = $CArea.","."S:".$chkTainan;
	}
}
$chkKaohsiung = $_POST['chkKaohsiung'];
if($chkKaohsiung != null && $chkKaohsiung != "")
{
	if($CArea == "")
	{
		$CArea = "S:".$chkKaohsiung;
	}
	else
	{
		$CArea = $CArea.","."S:".$chkKaohsiung;
	}
}
$chkHualien = $_POST['chkHualien'];
if($chkHualien != null && $chkHualien != "")
{
	if($CArea == "")
	{
		$CArea = "E:".$chkHualien;
	}
	else
	{
		$CArea = $CArea.","."E:".$chkHualien;
	}
}
$chkToufen = $_POST['chkToufen'];
if($chkToufen != null && $chkToufen != "")
{
	if($CArea == "")
	{
		$CArea = "M:".$chkToufen;
	}
	else
	{
		$CArea = $CArea.","."M:".$chkToufen;
	}
}

$CMemo = "";
$CGreenTel = "";
$CBlackTel = "";
$CRedTel = "";
for($x=1;$x<=52;$x++)
{
  $tempStr = "";
  if($x<=9)
  	$tempStr = "0".$x;
  else
  	$tempStr = (string)$x;
	$chk01 = $_POST['chk'.$tempStr];
	if($chk01 != null && $chk01 != "")
	{
		if($CMemo == "")
		{
			$CMemo = $chk01;
		}
		else
		{
			$CMemo = $CMemo.",".$chk01;
		}
	}
	
	if($CGreenTel == "" && $CRedTel == "" && $CBlackTel == "")
	{
		if(($chk01 != null) && (($chk01 == "21")||((int)$chk01 >= 43 && (int)$chk01 <= 47)) && ($chk01 != "45"))
		{
			$CGreenTel = $CTel;
		}
		elseif(($chk01 != null && $chk01 == "01") || 
		   ($chk01 != null && $chk01 == "02") ||
		   ($chk01 != null && $chk01 == "03") ||
		   ($chk01 != null && $chk01 == "04") ||
		   ($chk01 != null && $chk01 == "05") ||
		   ($chk01 != null && $chk01 == "06"))
		{
			$CRedTel = $CTel;
		}
		elseif(((int)$chk01 >= 7 && (int)$chk01 <= 20) ||
		       ((int)$chk01 >= 22 && (int)$chk01 <= 42)|| ($chk01 == "45") || ($chk01 == "48") || ($chk01 == "49") || ($chk01 == "50") || ($chk01 == "51") || ($chk01 == "52"))
		{
			$CBlackTel = $CTel;
		}
	}
}

if($CGreenTel == "" && $CRedTel == "" && $CBlackTel == "")
{
	$CBlackTel = $CTel;
}
$db = new DBOperation();

$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
				
if($action == "Insert")
{
  $tempCustomerId = $_POST['tempCustomerId'];
	$insertSql = "INSERT INTO Customer (CNickname, CArea,CSource,CGreenTel,CBlackTel,"
	              ."CRedTel,CMemo,CCreateDateTime,CUpdateDateTime,CCreateAcc,CRealCreator,CDataSource,CClass,ConTimes)"
	              ."VALUES ('".$db->mysql_real_escape_string($CNickname)."'"
	              .",'".$db->mysql_real_escape_string($CArea)."'"
	              .",'".$db->mysql_real_escape_string($CSource)."'"
	              .",'".$db->mysql_real_escape_string($CGreenTel)."'"
	              .",'".$db->mysql_real_escape_string($CBlackTel)."'"
	              .",'".$db->mysql_real_escape_string($CRedTel)."'"
	              .",'".$db->mysql_real_escape_string($CMemo)."'"	              
	              .",'".Date('Y-m-d H:i:s')."'"
	              .",'".Date('Y-m-d H:i:s')."'"
	              .",'".$db->mysql_real_escape_string($CUser)."'"
	              .",'".$_SESSION['userId']."'"
	              .",'0'"
	              .",'".$db->mysql_real_escape_string($CClass)."'"
	              .",'0')";
	$db->query($insertSql);
	//echo $insertSql;
	$customerId = $db->get_insert_id();
	
	$updateMissSql = "UPDATE CMissContent set CCId = '".$customerId."'"
	                 ." where CCId = '".$tempCustomerId."'";
  
	$db->query($updateMissSql);
	
  $db->close_db();	
	echo '<script type="text/javascript">';
  echo 'alert("新增成功!!!!");';
  echo 'location.href="CustomerIndex.php?page=1"';
  echo '</script>';
}
else
{
	$updateSql = "UPDATE Customer set CNickname = '".$db->mysql_real_escape_string($CNickname)."'"
	             .",CArea = '".$db->mysql_real_escape_string($CArea)."'"
               .",CSource = '".$db->mysql_real_escape_string($CSource)."'"
               .",CMemo = '".$db->mysql_real_escape_string($CMemo)."'"
               .",CGreenTel = '".$db->mysql_real_escape_string($CGreenTel)."'"  
               .",CBlackTel = '".$db->mysql_real_escape_string($CBlackTel)."'"                       
               .",CRedTel = '".$db->mysql_real_escape_string($CRedTel)."'"
               .",CClass = '".$db->mysql_real_escape_string($CClass)."'"                         
	             .",CUpdateDateTime = '".Date('Y-m-d H:i:s')."'";
	if($_SESSION['username'] == "admin")
	{
	 		$updateSql = $updateSql.",CCreateAcc = '".$db->mysql_real_escape_string($CUser)."'";
	}                        
	$updateSql = $updateSql." where CId = ".$id;
	$db->query($updateSql);
  $db->close_db();
  
  $url = "CustomerIndex.php?page=1&AId=".$CUser;
    
	echo '<script type="text/javascript">';
  echo 'alert("更新成功!!!!");';
   echo 'location.href= "'.$url.'";';
  echo '</script>';
}
?>
</html>
