<?php

if (!isset($_SESSION)) //判斷是否已經開啟SESSION
{
	header("Cache-control:private");
session_start();}//開起SESSION}
$mId=$_SESSION['mId'];
include("../function.php");
check_login();
putenv("NLS_LANG=TRADITIONAL CHINESE_TAIWAN.AL32UTF8");
if($mId!=''){
	 $chk_usr=chk_user($mId);
 if($chk_usr!='no'){

require_once("../db.php");





	if(isset($_POST['markerAddress']))
		$aAddress=$_POST['markerAddress'];
	










$aId=$_POST["aId"];
$mapid=$_POST["mapid"];
$aName=$_POST['aName'];
//$host=$_POST['host'];

//rBz
/*
$date=$_POST['date'];
$pre_date=explode(" - ",$date);
$start_date=$pre_date[0];
$end_date=$pre_date[1];
*/
$start_date=$_POST['start_date'];
$end_date=$_POST['end_date'];
//$aPlace=$_POST['aPlace'];
//$aAddress=$_POST['aAddress'];
$content=$_POST['content'];

//$fees=$_POST['fees'];
$aType=$_POST['aType'];
//$website=$_POST['website'];
//$aPhone=$_POST['aPhone'];

if(empty($aName)||empty($aAddress)||empty($content)||empty($aType))
{
	echo "<script>";
	echo "alert(\"fail!!\");";
	echo "history.go(-1)";
	echo "</script>";
}else{
if(isset($_POST['delete']))		//是否有刪除原本上傳的檔案
		{
			$delete=$_POST['delete'] ;
			
				$separate = explode("&",$delete);
				$delete_id = $separate[0];
				$delete_file = $separate[1];
				unlink($delete_file);
				$res = oci_parse($conn,"delete from PICTURE where PID = '$delete_id'") or die('Insert error');
                 oci_execute($res);


				
			
		}

//WǦhɮ
/*
	$i = 0;
	while($_FILES['picture']['name'][$i])
	{
	*/
	
		$error=$_FILES['picture']['error'] ;
			if($error== UPLOAD_ERR_OK){
				
		$file = $_FILES['picture']['name'];
		$tmpname = $_FILES['picture']['tmp_name'];
		
		
		$seq=rand(0,10000).time();
		$subname = explode(".",$file);	
      //$pName = "picture/".$aId."_".$pId.".".$subname[1];
		//$filename = "tmp/".$news_id."_".$doc_id.".".$subname[1];
		//$mod_doc = "update AMS_TR05 set FILEPATH = '$filename' where DOC_SN = '$doc_id'";
		//$result_mod_doc = odbc_exec($conn,$mod_doc);
		
			if($subname[count($subname)-1]=='gif'||$subname[count($subname)-1]=='png'||$subname[count($subname)-1]=='jpg'||$subname[count($subname)-1]=='JPG'||$subname[count($subname)-1]=='PNG'||$subname[count($subname)-1]=='GIF'){
		$subname[1] = strtolower($subname[1]);
		
	
		$pName = "picture/".$seq.".".$subname[1];
		
        //$sql_doc = "insert into news_doc (id,filename) values ('$news_id','$filename')";
		$sql_doc = "insert into PICTURE (AID,PID,PPATH,PNAME) values ('$aId',PIC_ID.NEXTVAL,,'$pName','$tmpname')";
		$inpic = oci_parse($conn,$sql_doc) or die('Insert error');
        oci_execute($inpic);
		
		}else{
		echo "檔名錯誤";
		
		}
	
	
	
		if (copy($tmpname,$pName))
		{
			unlink($tmpname);
		}
		
		
		//$i++;
	}else if ($error != UPLOAD_ERR_NO_FILE){
			echo "<script>";
	echo "alert('". $_FILES['picture']['name']."上傳失敗');";
	echo "history.go(-1)";
	echo "</script>";
		}
	
	
	 if(empty($_POST['lng']) ||empty($_POST['lat']) ){
	 
	 $content=nl2br(htmlspecialchars($content));
	$aName=nl2br(htmlspecialchars($aName));
//$host=nl2br(htmlspecialchars($host));
$start_date=nl2br(htmlspecialchars($start_date));
$end_date=nl2br(htmlspecialchars($end_date));
//$aPlace=nl2br(htmlspecialchars($aPlace));
//$fees=nl2br(htmlspecialchars($fees));
$aType=nl2br(htmlspecialchars($aType));
//$website=nl2br(htmlspecialchars($website));
//$aPhone=nl2br(htmlspecialchars($aPhone));
$aAddress=nl2br(htmlspecialchars($aAddress));

$upd = oci_parse($conn,"UPDATE ACTIVITY SET ANAME='$aName',ATIME='$start_date', end_date='$end_date',ADDRESS='$aAddress',ACONTENT='$content',TID='$aType' where AID='$aId'") or die('Insert error');
                 oci_execute($upd);


	echo "
		<html>
        	<head>
        	<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
        	</head>
        	<body>
        	<script language=javascript>
        	alert('活動修改完成！');
        	location.href='activity_show_detail.php?aId=$aId';
        	</script>
        	</body>
        	</html>
		";

  
  }else{
  if(isset($_POST['lng']))
		$lng=$_POST['lng'];
	if(isset($_POST['lat']))
		$lat=$_POST['lat'];
		$content=nl2br(htmlspecialchars($content));
$aName=nl2br(htmlspecialchars($aName));
//$host=nl2br(htmlspecialchars($host));
$start_date=nl2br(htmlspecialchars($start_date));
$end_date=nl2br(htmlspecialchars($end_date));
//$aPlace=nl2br(htmlspecialchars($aPlace));
//$fees=nl2br(htmlspecialchars($fees));
$aType=nl2br(htmlspecialchars($aType));
//$website=nl2br(htmlspecialchars($website));
//$aPhone=nl2br(htmlspecialchars($aPhone));
$aAddress=nl2br(htmlspecialchars($aAddress));

$updmap = oci_parse($conn,"UPDATE MAP SET LON='$lng',LAT='$lat' where MAPID='$mapid'") or die('Insert error');
                 oci_execute($updmap);

$upd = oci_parse($conn,"UPDATE ACTIVITY SET ANAME='$aName',ATIME='$start_date', end_date='$end_date',ADDRESS='$aAddress',ACONTENT='$content',TID='$aType' where AID='$aId'") or die('Insert error');
                 oci_execute($upd);

	
	echo "
		<html>
        	<head>
        	<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
        	</head>
        	<body>
        	<script language=javascript>
        	alert('活動修改完成！');
        	location.href='activity_show_detail.php?aId=$aId';
        	</script>
        	</body>
        	</html>
		";
	
	}
}



}else{echo "非本人喔~";}
}else {echo "請先登入";}

?>

	
