<?php require('system/login.php');  ?>
<?php require_once('../Connections/BE2.php'); ?>
<?php
 
$picpath= "/products/"; 
$write='';
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
    $img=sprintf("%04d",$_POST["bid"]).sprintf("%04d",rand(1,9999)).sprintf("%04d",rand(1,9999));
    $imgext='';
	$tfile="fileToUpload1";

	if($_POST['webpicname']<>''){
	    $ext= explode('.',$_POST['webpicname']) ;
	    $imgext=strtolower($ext[count($ext)-1]); //取最後一筆
	    $pic=$wwwpath.$picpath.$img.".".$imgext;
	    copy("../webpic/".$_POST['webpicname'],$pic);
		} else if($_FILES[$tfile]["name"]<>''){
		   $ext= explode('.',$_FILES[$tfile]['name']) ;
	       $imgext=strtolower($ext[count($ext)-1]); //取最後一筆
		   $pic=$wwwpath.$picpath.$img.".".$imgext;
		   move_uploaded_file($_FILES[$tfile]["tmp_name"],$pic);
			}
	 
  $updateSQL = sprintf("insert into adpic (flag,doc,link,ind,bid,img,imgext,imgtxt,xid,linktag,sdate,edate,bman,bmm1,bmm2) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
          "Y",$_POST['doc'],$_POST['link'],$_POST['ind'],$_POST['bid'],$img,$imgext,$_POST['imgtxt'],$_POST['xid'],$_POST['linktag'],$_POST['sdate'],$_POST['edate'],$_POST['bussman'],$_POST['bmm1'],$_POST['bmm2']);
  mysql_select_db($database_BE2, $BE2);
  $Result1 = mysql_query($updateSQL, $BE2) or die(mysql_error());  
  $write='資料已更新...';
  
  
 $insertGoTo = $_POST['url'];
 // header(sprintf("Location: %s", $insertGoTo)); 
 echo  "<script>alert('".$write."');location.href='".$_POST['url']."';</script>";
  }
 

$bid=-1;
if(isset($_GET['id'])){
	$bid=$_GET['id'];
	}  
$webid=-1; //webid
if(isset($_GET['webid'])){
	$webid=$_GET['webid'];
	} 
mysql_select_db($database_BE2, $BE2);
$query_Rt2 = sprintf("select a.*,b.*,c.* from ap1 as a, ap2 as b,adpage as c WHERE a.raid=b.raid AND b.webid=c.pid AND b.adid='%s'",$bid);
$Rt2 = mysql_query($query_Rt2, $BE2) or die(mysql_error());
$row_Rt2 = mysql_fetch_assoc($Rt2);
$totalRows_Rt2 = mysql_num_rows($Rt2);

mysql_select_db($database_BE2, $BE2);
$query_Rtm = "SELECT * FROM buser WHERE flag = 'Y' ORDER BY id ASC";
$Rtm = mysql_query($query_Rtm, $BE2) or die(mysql_error());
$row_Rtm = mysql_fetch_assoc($Rtm);
$totalRows_Rtm = mysql_num_rows($Rtm);

 // if($myadmin){ echo $query_Rt2; } 
?>

<form id="form1" name="form1" method="POST" enctype="multipart/form-data" action="">
<table width="51%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <th nowrap="nowrap" class="font2" >&nbsp;</th>
    <th colspan="4" nowrap="nowrap" class="font2" ><? echo $row_Rt2['pdoc']." ".$row_Rt2['radoc'];?></th>
    </tr>
  <tr>
    <th width="5%" nowrap="nowrap" class="font2" >&nbsp;</th>
    <th width="25%" nowrap="nowrap" class="font2" >圖片說明</th>
    <th width="40%" nowrap="nowrap" class="font2" ><input name="xid" type="hidden" id="xid" value="<? echo $row_Rt2['adid'];?>" />
      連結</th>
    <th width="8%" nowrap="nowrap" class="font2" >連結模式</th>
    <th width="22%" nowrap="nowrap" class="font2" >排序</th>
    </tr>
 
    <tr >
      <td>&nbsp;</td>
      <td><input name="doc" type="text" id="doc" value="" />
        <input name="bid" type="hidden" id="bid" value="<? echo $row_Rt2['raid'];?>" /></td>
      <td><input name="link" type="text" id="link" value="" size="40" /></td>
      <td><select name="linktag" id="linktag">
        <option value="_blank">開新視窗</option>
        <option value="_self" selected="selected" >無</option>
      </select></td>
      <td><input name="ind" type="text" id="ind" value="" size="8" maxlength="5" /></td>
    </tr>
 
    <tr>
      <td height="28">&nbsp;</td>
      <th align="right">啟用日期</th>
      <td><label for="sdate"></label>
      <input name="sdate" type="text" id="sdate" value="<? echo date("Y-m-d");?>" /></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td height="28">&nbsp;</td>
      <th align="right">截止日期</th>
      <td><input name="edate" type="text" id="edate" value="<? echo date("Y-m-d",time()+(86400*365));?>" />
      <input name="url" type="hidden" id="url" value="<? echo $_SERVER['HTTP_REFERER']; ?>" /></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <th align="right"><span class="font2">其它說明</span></th>
      <td><textarea name="imgtxt" cols="40" rows="4" id="imgtxt"></textarea></td>
      <td>&nbsp;</td>
      <td><img id="webpicid" src=""/></td>
    </tr>
    <tr>
      <td height="26">&nbsp;</td>
      <th align="right" nowrap="nowrap">業務</th>
      <td><select name="bussman" id="bussman">
        <?php do { ?>
        <option value="<?php echo $row_Rtm['id']?>"><?php echo $row_Rtm['aname']?></option>
        <?php
       } while ($row_Rtm = mysql_fetch_assoc($Rtm));
      ?>
      </select></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td height="28">&nbsp;</td>
      <th align="right">商品金額</th>
      <td><input name="bmm1" type="text" id="bmm1" value="" size="12" maxlength="6" /></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td height="35">&nbsp;</td>
      <th align="right">業務獎金</th>
      <td><input name="bmm2" type="text" id="bmm2" value="" size="12" maxlength="6" /></td>
      <td><input type="button" name="selwebpic" value="瀏覽主機端" onClick="show_webpic();"/></td>
      <td><input name="webpicname" type="hidden" id="webpicname" value="" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td colspan="4"><input name="fileToUpload1" type="file" id="fileToUpload1" />
        (廣告 <? echo $row_Rt2['ra1'];?> 區)<? echo $row_Rt2['ra2'];?> </td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td colspan="2"><? if($row_Rt2['raid']==14){?>YOUTUBE影片 請直接輸入紅色連結字串即可<br> 
      例 https://www.youtube.com/watch?v=<font color="#FF0000">kTAv4HCCnw0</font><? }?></td>
    <td><input type="submit" name="Submit" value="送出" /></td>
    <td>&nbsp;</td>
    </tr>
</table> 

<input name="MM_insert" type="hidden" id="MM_insert" value="form1">
<input name="MM_bannerfile" type="hidden" id="MM_bannerfile" value="bannerfile" />
</form>


<?php
mysql_free_result($Rt2);
mysql_free_result($Rtm);
?>
