<?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/FileConfig.php");
require_once("class/FileOperation.php");

$AName = $_POST["AName"];
$AId = $_POST["AId"];

$db = new DBOperation();

$fo = new FileOperation();

$tempFileName = "myfile";
	  
if($_FILES[$tempFileName]['name'] != null && $_FILES[$tempFileName]['name'] != "")
{
  $orgfilName = $_FILES[$tempFileName]['name'];
  $baseFileName = $fo->get_basename(rtrim($orgfilName, '/'));
	$useuploadfile = $_FILECONFIG['FileInDir'].$baseFileName;	
	
	//檔案上傳成功
	if(!move_uploaded_file($_FILES[$tempFileName]['tmp_name'], iconv("utf-8", "big5", $useuploadfile)))
	{
	  $url = "CustomerFileIn.php?AId=".$AId;	
		echo '<script type="text/javascript">';
		echo 'alert("檔案上傳失敗!!!!");';
		echo 'location.href= "'.$url.'";';
		echo '</script>';
		exit();
	}
	
	//處理匯入檔案
  $fp=fopen(iconv("utf-8", "big5", $useuploadfile),"r");
  $count = 1;
  while(!feof($fp))
  {
  	$mycsv=$fo->_fgetcsv($fp);
  	
  	if($count == 1)
  	{
  		$count = $count + 1;
  		continue;
  	}
    $count = $count + 1;
  	$tempArea = "";
  	$tempNickName = $mycsv[5];  	
  	$tempAreaName1 = $mycsv[0];
  	$tempAreaName2 = $mycsv[1];
  	
  	if($tempAreaName1 != null && $tempAreaName1 != "" && $tempAreaName1 == "台北")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "N:Taipei";
  		}
  		else
  		{
  			$tempArea = $tempArea.",N:Taipei";
  		}
  	}
  	elseif($tempAreaName1 != null && $tempAreaName1 != "" && $tempAreaName1 == "桃園")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "N:Taoyuan";
  		}
  		else
  		{
  			$tempArea = $tempArea.",N:Taoyuan";
  		}
  	}
  	elseif($tempAreaName1 != null && $tempAreaName1 != "" && $tempAreaName1 == "中壢")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "N:Zhongli";
  		}
  		else
  		{
  			$tempArea = $tempArea.",N:Zhongli";
  		}
  	}
  	elseif($tempAreaName1 != null && $tempAreaName1 != "" && $tempAreaName1 == "新竹")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "M:Hsinchu";
  		}
  		else
  		{
  			$tempArea = $tempArea.",M:Hsinchu";
  		}
  	}
  	elseif($tempAreaName1 != null && $tempAreaName1 != "" && $tempAreaName1 == "台中")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "M:Taichung";
  		}
  		else
  		{
  			$tempArea = $tempArea.",M:Taichung";
  		}
  	}
  	elseif($tempAreaName1 != null && $tempAreaName1 != "" && $tempAreaName1 == "彰投")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "M:Zhanghua";
  		}
  		else
  		{
  			$tempArea = $tempArea.",M:Zhanghua";
  		}
  	}
  	elseif($tempAreaName1 != null && $tempAreaName1 != "" && $tempAreaName1 == "台南")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "S:Tainan";
  		}
  		else
  		{
  			$tempArea = $tempArea.",S:Tainan";
  		}
  	}  	
  	elseif($tempAreaName1 != null && $tempAreaName1 != "" && $tempAreaName1 == "高雄")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "S:Kaohsiung";
  		}
  		else
  		{
  			$tempArea = $tempArea.",S:Kaohsiung";
  		}
  	}  	
  	elseif($tempAreaName1 != null && $tempAreaName1 != "" && $tempAreaName1 == "花蓮")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "E:Hualien";
  		}
  		else
  		{
  			$tempArea = $tempArea.",E:Hualien";
  		}
  	}
  	
  	if($tempAreaName2 != null && $tempAreaName2 != "" && $tempAreaName2 == "台北")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "N:Taipei";
  		}
  		else
  		{
  			$tempArea = $tempArea.",N:Taipei";
  		}
  	}
  	elseif($tempAreaName2 != null && $tempAreaName2 != "" && $tempAreaName2 == "桃園")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "N:Taoyuan";
  		}
  		else
  		{
  			$tempArea = $tempArea.",N:Taoyuan";
  		}
  	}
  	elseif($tempAreaName2 != null && $tempAreaName2 != "" && $tempAreaName2 == "中壢")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "N:Zhongli";
  		}
  		else
  		{
  			$tempArea = $tempArea.",N:Zhongli";
  		}
  	}
  	elseif($tempAreaName2 != null && $tempAreaName2 != "" && $tempAreaName2 == "新竹")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "M:Hsinchu";
  		}
  		else
  		{
  			$tempArea = $tempArea.",M:Hsinchu";
  		}
  	}
  	elseif($tempAreaName2 != null && $tempAreaName2 != "" && $tempAreaName2 == "台中")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "M:Taichung";
  		}
  		else
  		{
  			$tempArea = $tempArea.",M:Taichung";
  		}
  	}
  	elseif($tempAreaName2 != null && $tempAreaName2 != "" && $tempAreaName2 == "彰投")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "M:Zhanghua";
  		}
  		else
  		{
  			$tempArea = $tempArea.",M:Zhanghua";
  		}
  	}
  	elseif($tempAreaName2 != null && $tempAreaName2 != "" && $tempAreaName2 == "台南")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "S:Tainan";
  		}
  		else
  		{
  			$tempArea = $tempArea.",S:Tainan";
  		}
  	}  	
  	elseif($tempAreaName2 != null && $tempAreaName2 != "" && $tempAreaName2 == "高雄")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "S:Kaohsiung";
  		}
  		else
  		{
  			$tempArea = $tempArea.",S:Kaohsiung";
  		}
  	}
  	elseif($tempAreaName2 != null && $tempAreaName2 != "" && $tempAreaName2 == "花蓮")
  	{
  		if($tempArea == "")
  		{
  			$tempArea = "E:Hualien";
  		}
  		else
  		{
  			$tempArea = $tempArea.",E:Hualien";
  		}
  	}
  	$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
  	
  	$insertSql = "INSERT INTO Customer (CNickname,CArea,CSource,CGreenTel,CBlackTel,"
	              ."CRedTel,CMemo,CCreateDateTime,CUpdateDateTime,CCreateAcc,CRealCreator,CDataSource)"
	              ."VALUES ('".$db->mysql_real_escape_string($tempNickName)."'"
	              .",'".$db->mysql_real_escape_string($tempArea)."'"
	              .",'".$db->mysql_real_escape_string($mycsv[7])."'"
	              .",'".$db->mysql_real_escape_string($mycsv[2])."'"
	              .",'".$db->mysql_real_escape_string($mycsv[3])."'"
	              .",'".$db->mysql_real_escape_string($mycsv[4])."'"
	              .",''"	              
	              .",'".Date('Y-m-d H:i:s')."'"
	              .",'".Date('Y-m-d H:i:s')."'"
	              .",'".$db->mysql_real_escape_string($AId)."'"
	              .",'".$_SESSION['userId']."'"
	              .",'1,".$orgfilName."')";	 
		$db->query($insertSql);
		//$db->close_db();
		
		//$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
		
		$onlyId = $db->get_insert_id();
		for($x=8;$x<=37;$x++){
		  if($mycsv[$x] != null && $mycsv[$x] != "")
		  {
		    $tempMissName = $mycsv[$x];
				$insertSql = "INSERT INTO CMissContent (CCId,CMiss,CPayDate,CCreateDateTime,CUpdateDateTime,CCreateAcc)"
					              ."VALUES ('".$db->mysql_real_escape_string($onlyId)."'"
					              .",'".$db->mysql_real_escape_string($tempMissName)."'"
					              .",''"			                         
					              .",'".Date('Y-m-d H:i:s')."'"
					              .",'".Date('Y-m-d H:i:s')."'"
					              .",'".$_SESSION['userId']."')";
				$db->query($insertSql);
			}
		}
		$db->close_db();
  }  
  fclose($fp);
  $url = "CustomerIndex.php?page=1&AId=".$AId;	
	echo '<script type="text/javascript">';
  echo 'alert("匯入成功!!!!");';
	echo 'window.opener.location.href = "'.$url.'";';
	echo 'window.close();';
  echo '</script>';
}
  
?>
</html>
