<?php require_once('../Connections/BE2.php'); ?>
<?php

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  
  if($_POST['raid']=='9999'){
	 $webid=$_POST['webid'];  
	  $insertSQL = "INSERT INTO c_ap2 (webid, raid, radoc, ratshow, ratxt, ramark, raindex,radef,raco) VALUES ($webid,1,'首頁動畫','N','N','0','100','N',-1),
	  ( $webid,9,'基本資料','Y','Y','0','200','Y',-2),($webid,'9','關於我們','Y','Y','0','300','Y',-2),
	  ( $webid,9,'服務項目','Y','Y','0','400','Y',-3),($webid,14,'精選影片','N','N','0','500','N',-3),($webid,10,'精選照片','Y','N','0','600','N',-2);";
 
      mysql_select_db($database_BE2, $BE2);
      mysql_query($insertSQL, $BE2) or die(mysql_error());  
  
      mysql_select_db($database_BE2, $BE2);
      $query_Rtc = sprintf("select adid from c_ap2 WHERE webid='%s' order by adid asc limit 6",$webid);
      $Rtc = mysql_query($query_Rtc, $BE2) or die(mysql_error());
      $row_Rtc = mysql_fetch_assoc($Rtc);	 
	  $i=0;
	  $arrxid=array();
	  do { 
	   $arrxid[$i]= $row_Rtc['adid'];
	   $i++;
	  } while ($row_Rtc = mysql_fetch_assoc($Rtc));
	  mysql_free_result($Rtc);
	  
	  $picpath= "/shoppic/"; 
	  
	  $path = $wwwpath.$picpath.$webid; 
      if(!is_dir($path)){mkdir($path);}
	  
      $date1=date('Y-m-d');
      $date2=date('Y-m-d',time()+86400*360);
	  //動畫
      for($i=0;$i<3;$i++){
         $img=sprintf("%06d",$webid).sprintf("%04d",rand(1,9999)).sprintf("%04d",rand(1,9999)).sprintf("%04d",rand(1,9999));
         $imgext=''; 
	     $imgtxt=''; 
         $ind=($i+1)*100;
         $updateSQL = sprintf("insert into c_adpic (flag,doc,link,ind,bid,img,imgext,imgtxt,xid,linktag,sdate,edate) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
          "N","hello","",$ind,$webid,$img,$imgext,$imgtxt,$arrxid[0],"_self",$date1,$date2);
        mysql_select_db($database_BE2, $BE2);
        $Result1 = mysql_query($updateSQL, $BE2) or die(mysql_error());  
      }	  
	  
	  //'基本資料 關於我們 服務項目'
      for($i=0;$i<3;$i++){
         $img=sprintf("%06d",$webid).sprintf("%04d",rand(1,9999)).sprintf("%04d",rand(1,9999)).sprintf("%04d",rand(1,9999));
         $imgext=''; 
	     $imgtxt=''; 
         $ind=($i+1)*100;
         $updateSQL = sprintf("insert into c_adpic (flag,doc,link,ind,bid,img,imgext,imgtxt,xid,linktag,sdate,edate) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
          "Y","hello","",$ind,$webid,$img,$imgext,$imgtxt,$arrxid[1+$i],"_self",$date1,$date2);
        mysql_select_db($database_BE2, $BE2);
        $Result1 = mysql_query($updateSQL, $BE2) or die(mysql_error());  
        }
     //'精選影片'
	  for($i=0;$i<2;$i++){
         $img=sprintf("%06d",$webid).sprintf("%04d",rand(1,9999)).sprintf("%04d",rand(1,9999)).sprintf("%04d",rand(1,9999));
         $imgext=''; 
	     $imgtxt=''; 
         $ind=($i+1)*100;
         $updateSQL = sprintf("insert into c_adpic (flag,doc,link,ind,bid,img,imgext,imgtxt,xid,linktag,sdate,edate) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
          "N","hello","",$ind,$webid,$img,$imgext,"HELLO".$i,$arrxid[4],"_self",$date1,$date2);
        mysql_select_db($database_BE2, $BE2);
        $Result1 = mysql_query($updateSQL, $BE2) or die(mysql_error());  
      } 
	  //'精選照片' 
	  for($i=0;$i<12;$i++){
         $img=sprintf("%06d",$webid).sprintf("%04d",rand(1,9999)).sprintf("%04d",rand(1,9999)).sprintf("%04d",rand(1,9999));
         $imgext=''; 
	     $imgtxt=''; 
         $ind=($i+1)*100;
         $updateSQL = sprintf("insert into c_adpic (flag,doc,link,ind,bid,img,imgext,imgtxt,xid,linktag,sdate,edate) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
          "N","hello","",$ind,$webid,$img,$imgext,"HELLO".$i,$arrxid[5],"_self",$date1,$date2);
        mysql_select_db($database_BE2, $BE2);
        $Result1 = mysql_query($updateSQL, $BE2) or die(mysql_error());  
      }    
	                 
	  
	  } else{
 	
	
     $sn2=explode(",",$_POST['sn']);
     $st2=explode(",",$_POST['st']);
     $x= array_search($_POST['raid'],$sn2) ;
     $myst=$st2[$x]; 	
     $insertSQL = sprintf("INSERT INTO c_ap2 (webid, raid, radoc, ratshow, ratxt, ramark, raindex) VALUES (%s,%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['webid'], "int"),
                       GetSQLValueString($_POST['raid'], "int"),
                       GetSQLValueString($_POST['radoc'], "text"),
					   GetSQLValueString($_POST['ratshow'], "text"),
					   GetSQLValueString($myst, "text"),
					   GetSQLValueString($_POST['ramark'], "int"),
                       GetSQLValueString($_POST['raindex'], "int"));

     mysql_select_db($database_BE2, $BE2);
     $Result1 = mysql_query($insertSQL, $BE2) or die(mysql_error());
 
    // if html make html data
     if($_POST['raid']==9 ){
       mysql_select_db($database_BE2, $BE2);
       $query_Rt2 = sprintf("select adid from c_ap2 WHERE webid='%s' order by adid desc limit 1",$_POST['webid']);
       $Rt2 = mysql_query($query_Rt2, $BE2) or die(mysql_error());
       $row_Rt2 = mysql_fetch_assoc($Rt2);
       $bid=$row_Rt2['adid']; 	 
	 
       $insertSQL = sprintf("insert into c_adpic (xid,flag,ind,imgtxt,linktag,sdate,edate) VALUES('%s','%s','%s','%s','%s','%s','%s')",$bid,"Y",100,"HELLO","_self",date('Y-m-d'),date('Y-m-d',time()+86400*360));
       mysql_select_db($database_BE2, $BE2);
       $Result1 = mysql_query($insertSQL, $BE2) or die(mysql_error());  
       mysql_free_result($Rt2);
	  }
 
	
   } //end 9999
   
   
  $insertGoTo = $_POST['url'];
  header(sprintf("Location: %s", $insertGoTo));
}

 mysql_select_db($database_BE2, $BE2);
 $query_Rtf = "SELECT * FROM ap1 WHERE ra4='Y' ORDER BY raid ASC";
 $Rtf = mysql_query($query_Rtf, $BE2) or die(mysql_error());
 $row_Rtf = mysql_fetch_assoc($Rtf);
 $totalRows_Rtf = mysql_num_rows($Rtf);
 
 mysql_select_db($database_BE2, $BE2);
 $query_Rtc = sprintf("SELECT * FROM c_ap2 WHERE webid='%s' AND radef='Y'",$_GET['webid']);
 $Rtc = mysql_query($query_Rtc, $BE2) or die(mysql_error());
 $row_Rtc = mysql_fetch_assoc($Rtc);
 $totalRows_Rtc = mysql_num_rows($Rtc); 
 $newuser=false;
 if($totalRows_Rtc==0){ $newuser=true; }
 
?>
<form action="" method="post" name="form1" id="form1">
  <table width="380" align="center">
    <tr valign="baseline">
      <th width="89" align="right" nowrap="nowrap"><span class="font2">版型模塊:</span></th>
      <td width="224"><select name="raid" id="raid">
       <? 
	    if($newuser){?><option value="9999" >建立基本模版 動畫+基本資料組合</option> <? }else{
		?>
	   <?php 
	   $st='';
	   $sn='';	
       do {  
           $st .= $row_Rtf['ra7'].",";
           $sn .= $row_Rtf['raid'].","; 
         ?>
        <option value="<?php echo $row_Rtf['raid']?>" ><?php echo $row_Rtf['ra1'].")".$row_Rtf['ra2']?></option>
        <?php } while ($row_Rtf = mysql_fetch_assoc($Rtf)); 
	      }
		?>
        
      </select></td>
    </tr>
    <tr> </tr>
    <tr valign="baseline">
      <th nowrap="nowrap" align="right"><span class="font2">模塊說明:</span></th>
      <td><input type="text" name="radoc" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <th nowrap="nowrap" align="right"><span class="font2">模塊標題顯示:</span></th>
      <td><select name="ratshow" id="ratshow">
        <option value="Y">前台顯示</option>
        <option value="N">前台不顯示</option>
      </select></td>
    </tr>
    <tr valign="baseline">
      <th height="23" align="right" nowrap="nowrap"><span class="font2">物件標題顯示</span></th>
      <td><select name="ramark" id="ramark">
        <option value="0" selected="selected">不顯示</option>
        <option value="1">上標</option>
        <option value="2">下標</option>
        <option value="3">圖形內標</option>
      </select></td>
    </tr>
    <tr valign="baseline">
      <th nowrap="nowrap" align="right"><span class="font2">排列序:</span></th>
      <td><input type="text" name="raindex" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><input name="sn" type="hidden" id="sn" value="<? echo $sn;?>" />
      <input name="st" type="hidden" id="st" value="<? echo $st;?>" /></td>
      <td><input type="submit" value="插入記錄" /></td>
    </tr>
  </table>
  <input type="hidden" name="webid" value="<?= $_GET['webid'] ?>" />
  <input type="hidden" name="MM_insert" value="form1" />
  <input name="url" type="hidden" id="url" value="<? echo $_SERVER['HTTP_REFERER'];?>" />
</form>

<?php
mysql_free_result($Rtf);
mysql_free_result($Rtc);
?>
