<?php require_once('../Connections/BE2.php'); ?>
<?php
 
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
	
  if(!empty($_FILES['file1']['name'])){
	 $_POST['sgact2']=$_FILES['file1']['name'];
	 $dd='/www/bus104/signupdoc/'.$_POST['sgact2'] ;
	 $tempFile = $_FILES['file1']['tmp_name'];
	 move_uploaded_file($tempFile, $dd );
	 }
  
  $_POST['sgdate']=$_POST['y1'].sprintf('%02d',$_POST['m1']).sprintf('%02d',$_POST['d1']);	
  $_POST['sgdates']=$_POST['y2'].sprintf('%02d',$_POST['m2']).sprintf('%02d',$_POST['d2']);	
  $_POST['sgdatee']=$_POST['y3'].sprintf('%02d',$_POST['m3']).sprintf('%02d',$_POST['d3']);	
  $maxman=($_POST['sgman2']>15)?15:$_POST['sgman2'];	
  
  
  $updateSQL = sprintf("UPDATE signup1 SET sgname=%s, sgdate=%s, sgdates=%s, sgdatee=%s, sgaddr=%s, sgact1=%s, sgact2=%s, sgman=%s,sgman2=%s, sgunit=%s WHERE sgid=%s",
                       GetSQLValueString($_POST['sgname'], "text"),
                       GetSQLValueString($_POST['sgdate'], "date"),
                       GetSQLValueString($_POST['sgdates'], "date"),
                       GetSQLValueString($_POST['sgdatee'], "date"),
                       GetSQLValueString($_POST['sgaddr'], "text"),
                       GetSQLValueString($_POST['doc'], "text"),
                       GetSQLValueString($_POST['sgact2'], "text"),
                       GetSQLValueString($_POST['sgman'], "int"),
					   GetSQLValueString($maxman, "int"), 
                       GetSQLValueString($_POST['sgunit'], "text"),
                       GetSQLValueString($_POST['sgid'], "int"));

  mysql_select_db($database_BE2, $BE2);
  $Result1 = mysql_query($updateSQL, $BE2) or die(mysql_error());

  $updateGoTo = $_POST['url'];
  header(sprintf("Location: %s", $updateGoTo));
}

$colname_Rt1 = "-1";
if (isset($_GET['id'])) {
  $colname_Rt1 = $_GET['id'];
}
mysql_select_db($database_BE2, $BE2);
$query_Rt1 = sprintf("SELECT * FROM signup1 WHERE sgid = '%s'", GetSQLValueString($colname_Rt1, "int"));
$Rt1 = mysql_query($query_Rt1, $BE2) or die(mysql_error());
$row_Rt1 = mysql_fetch_assoc($Rt1);
$totalRows_Rt1 = mysql_num_rows($Rt1);

?><form action="" method="POST" enctype="multipart/form-data" name="form1" id="form1">
  <table width="500" border="1" align="center">
    <tr valign="baseline">
      <td colspan="2" align="center" nowrap="nowrap">更新 活動內文</td>
    </tr>
    <tr valign="baseline">
      <td width="110" align="right" nowrap="nowrap">活動名稱:</td>
      <td width="340"><input type="text" name="sgname" value="<?php echo $row_Rt1['sgname']; ?>" size="32" />
      <input type="hidden" name="sgid" value="<?php echo $row_Rt1['sgid']; ?>" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">活動日期:</td>
      <td><input type="hidden" name="sgdate" value="<?php echo $row_Rt1['sgdate']; ?>" />
       <select id="y1" name="y1">
			<? $year=date('Y',time()) ; ?>
			<? 
			   $yy=substr($row_Rt1['sgdate'],0,4)-0;
			   $mm=substr($row_Rt1['sgdate'],5,2)-0;
			   $dd=substr($row_Rt1['sgdate'],8,2)-0; //2014-01-01
			
			for($ax=$year-2;$ax<$year+2;$ax++){ ?>
                <option value="<? echo $ax;?>" <? if ($ax==$yy){ echo 'selected="selected"' ;}?>><? echo $ax;?></option>
            <? }?>   
            </select>
            年
              <select id="m1" name="m1">
			 <? for($ax=1;$ax<13;$ax++){ ?>
                 <option value="<? echo $ax;?>" <? if ($ax==$mm){ echo 'selected="selected"' ;}?>><? echo $ax;?></option>
			 <? }?>	 
                </select>
              月
                <select id="d1" name="d1">
			 <? for($ax=1;$ax<32;$ax++){ ?>
                 <option value="<? echo $ax;?>" <? if ($ax==$dd){ echo 'selected="selected"' ;}?>><? echo $ax;?></option>
			 <? }?>	 
                </select>
                日</td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">報名起始日期:</td>
      <td><input type="hidden" name="sgdates" value="<?php echo $row_Rt1['sgdates']; ?>"/>
          <select id="y2" name="y2">
			<? $year=date('Y',time()) ; 
			   $yy=substr($row_Rt1['sgdates'],0,4)-0;
			   $mm=substr($row_Rt1['sgdates'],5,2)-0;
			   $dd=substr($row_Rt1['sgdates'],8,2)-0; //2014-01-01
			    
			?>
			<? for($ax=$year-2;$ax<$year+2;$ax++){ ?>
                <option value="<? echo $ax;?>" <? if ($ax==$yy){ echo 'selected="selected"' ;}?> ><? echo $ax;?></option>
            <? }?>   
            </select>
            年
              <select id="m2" name="m2">
			 <? for($ax=1;$ax<13;$ax++){ ?>
                 <option value="<? echo $ax;?>"<? if ($ax==$mm){ echo 'selected="selected"' ;}?> ><? echo $ax;?></option>
			 <? }?>	 
                </select>
              月
                <select id="d2" name="d2">
			 <? for($ax=1;$ax<32;$ax++){ ?>
                 <option value="<? echo $ax;?>" <? if ($ax==$dd){ echo 'selected="selected"' ;}?>><? echo $ax;?></option>
			 <? }?>	 
                </select>
                日</td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">報名截止日期:</td>
      <td><input type="hidden" name="sgdatee" value="<?php echo $row_Rt1['sgdatee']; ?>" />
         <select id="y3" name="y3">
			<? $year=date('Y',time()) ; ?>
			<? 
			   $yy=substr($row_Rt1['sgdatee'],0,4)-0;
			   $mm=substr($row_Rt1['sgdatee'],5,2)-0;
			   $dd=substr($row_Rt1['sgdatee'],8,2)-0; //2014-01-01
			
			for($ax=$year-2;$ax<$year+2;$ax++){ ?>
                <option value="<? echo $ax;?>" <? if ($ax==$yy){ echo 'selected="selected"' ;}?>><? echo $ax;?></option>
            <? }?>   
            </select>
            年
              <select id="m3" name="m3">
			 <? for($ax=1;$ax<13;$ax++){ ?>
                 <option value="<? echo $ax;?>" <? if ($ax==$mm){ echo 'selected="selected"' ;}?> ><? echo $ax;?></option>
			 <? }?>	 
                </select>
              月
                <select id="d3" name="d3">
			 <? for($ax=1;$ax<32;$ax++){ ?>
                 <option value="<? echo $ax;?>" <? if ($ax==$dd){ echo 'selected="selected"' ;}?> ><? echo $ax;?></option>
			 <? }?>	 
                </select>
                日</td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">活動地點:</td>
      <td><input type="text" name="sgaddr" value="<?php echo $row_Rt1['sgaddr']; ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">活動辦法:</td>
      <td><label for="fileField"></label>
      <input type="file" name="file1" id="file1" />
      <input name="sgact2" type="hidden" id="sgact2" value="<?php echo $row_Rt1['sgact2']; ?>" />
      <input name="doc" type="hidden" id="doc" value="<?php echo $row_Rt1['sgact1']; ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">報名人數:</td>
      <td><input name="sgman" type="text" value="<?php echo $row_Rt1['sgman']; ?>" size="6" maxlength="5" />
        0 表示不設限</td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">攜伴人數:</td>
      <td><input name="sgman2" id="sgman2" type="text" value="<?php echo $row_Rt1['sgman2']; ?>" size="6" maxlength="5" />
      0 表示無攜伴 /最多攜伴15人</td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">活動單位:</td>
      <td><input type="text" name="sgunit" value="<?php echo $row_Rt1['sgunit']; ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><input name="url" type="hidden" id="url" value="<? echo $_SERVER['HTTP_REFERER']?>" /></td>
      <td><input type="submit" value="存檔" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_update" value="form1" />
 
</form>
 
<?php
mysql_free_result($Rt1);
?>
