<?php require_once('../Connections/BE2.php'); ?>
<?php
 
if (!isset($_SESSION)){ session_start(); }
  
   
   if($_POST["MM_update"] == "fp1"){
	$upath_first= ($_POST['windex']=='0')?'user_':'wp_';
	 
	$idpath= sprintf("%07d",$_SESSION['login_memid']) ;
    $wwwpath='/www2/skybni';
    $picpath=  "/ecard/img/".$upath_first.$idpath.'_'.$_POST['windex'];  
	$spicpath= "/ecard/simg/".$upath_first.$idpath.'_'.$_POST['windex']; 
	   
	$wimg=$upath_first.$idpath.'_'.$_POST['windex'];   
    $tfile="fileToUpload1";
    $wimgext=$_POST['wimgext'];
    $img_ext_array=array("jpg","jpeg","bmp","png","gif");
	
    if($_FILES[$tfile]["name"]<>''){
		   $ext= explode('.',$_FILES[$tfile]['name']) ;
	       $imgext=strtolower($ext[count($ext)-1]); //取最後一筆
		   if(in_array($imgext,$img_ext_array)){
		    $spic=$wwwpath.$spicpath.".".$imgext;
		    $wimgext=$imgext;
		    move_uploaded_file($_FILES[$tfile]["tmp_name"],$spic);
			if($_POST['windex']>0){
			  $chk_file=true;
              while($chk_file) {
               if (file_exists($spic)){ $chk_file=false; }
              } 
			  $pic=$wwwpath.$picpath.".".$imgext;
			
			  $syscall_3a="/usr/local/bin/convert $spic -thumbnail \"300x300^\" $pic";	
		      $output1=`$syscall_3a`; 
			
		      $chk_file=true;
              while($chk_file) {
                if (file_exists($pic)){ $chk_file=false; }
              } 
		 
			  $repic ='/www2/skybni/ecard/reimg/'.$upath_first.$idpath.'_'.$_POST['windex'].".".$imgext; 
		      $syscall_3b="/usr/local/bin/convert $spic -resize 300x300 -size 300x300 xc:white +swap -gravity center -composite $repic";
              $output2=`$syscall_3b`; 	
			 } else{
				 // index 0
				 $pic=$wwwpath.$picpath.".".$imgext;
				 copy($spic, $pic); 
			 }
			
		   }
		   
    }  

   	if($_POST['wid']==0){
	  $updateSQL = sprintf("INSERT INTO ecardshop (wtitle,wlink,wimg,wimgext,windex,wflag,coid)VALUES (%s,%s,%s,%s,%s,%s,%s)",                       GetSQLValueString($_POST['wtitle'], "text"),
					   GetSQLValueString($_POST['wlink'], "text"),
					   GetSQLValueString($wimg, "text"), // GetSQLValueString($_POST['coid'], "int"),
					   GetSQLValueString($wimgext, "text"),
					   GetSQLValueString($_POST['windex'], "int"),
					   GetSQLValueString($_POST['wflag'], "int"),
                       GetSQLValueString($_SESSION['login_memid'], "int")); //$_SESSION['login_memid']
		} else{ 
		  $updateSQL = sprintf("UPDATE ecardshop SET wtitle=%s,wlink=%s,wimgext=%s,wflag=%s  WHERE wid=%s AND coid=%s",
                       GetSQLValueString($_POST['wtitle'], "text"),
					   GetSQLValueString($_POST['wlink'], "text"),
					   GetSQLValueString($wimgext, "text"),
					   GetSQLValueString($_POST['wflag'], "int"),
					   GetSQLValueString($_POST['wid'], "int"),
                       GetSQLValueString($_SESSION['login_memid'], "int")); //$_SESSION['login_memid']
		
		}
   mysql_select_db($database_BE2, $BE2);
   $Result1 = mysql_query($updateSQL, $BE2) or die(mysql_error());
   $msg='更新完成....' ;
   $updateGoTo = "ecardshop.html";
 
   echo  "<script>alert('".$msg."');location.href='".$updateGoTo."';</script>";	
	  
   } 
  


 

 //update `member` set company_title=mem_name WHERE mem_auto>10 and mem_auto<26
 
 
$id=-1;
$id= (isset($_GET["id"]))?$_GET["id"]:-1;
$canedit=false;


if ($id<>5){ $canedit=true;}
	
 mysql_select_db($database_BE2, $BE2);
 $query_Rt3 = sprintf("SELECT * FROM ecardshop WHERE coid= '%s' AND windex='%s' " ,$_SESSION['login_memid'],$id);
// $query_Rt3 = sprintf("SELECT * FROM webshop WHERE wid= '%s' ",$id); 
 $Rt3 = mysql_query($query_Rt3, $BE2) or die(mysql_error());
 $row_Rt3 = mysql_fetch_assoc($Rt3);
 $totalRows_Rt3 = mysql_num_rows($Rt3);
 
 
?>
<style>
     .box2{
         border-width:3px;
		 border-style:dashed;
		 border-color:#FFAC55;
		 padding:5px;
    } 
	
	img{
        max-width: 100%;
        max-height: 100%;
        display: block; /* remove extra space below image */
    }
    .box{
        width: 250px;        
        border: 5px solid black;
    }    
    .box.large{
        height: 300px;
    }
    .box.small{
        height: 100px;
    }
</style>

<div class="main "> 
		 
			<div class="row">
				<div class="col-lg-12">
					<h3 class="page-header"><i class="fa fa-indent"></i>網店管理</h3>
					<ol class="breadcrumb">
						<li><i class="fa fa-home"></i><a href="index.html">首頁</a></li>
                        <li><i class="fa fa-list-alt"></i><a href="#">企業資料</a></li>
						<li><i class="fa fa-indent"></i>網店管理-廣告圖</li>				
					</ol>
				</div>
                
			</div>


           <?  if($canedit){ ?>
			<div class="row">	
              <div class="col-sm-6">
			        <div class="panel panel-default">
			            <div class="panel-heading">
			                <h2><i class="fa fa-indent red"></i><strong>網店資料管理-廣告圖</strong></h2>
			            </div>
			            <div class="panel-body">
    
						 <form action="" method="post" name="fop1" id="fop1" enctype="multipart/form-data">
				                <div class="form-group">
				                    <label class="col-md-3 control-label" for="wtitle"> 標題說明: </label>
			                      <div class="col-md-9">
                                    <input type="text" name="wtitle" id="wtitle"  placeholder="請填寫名片標題" value="<?= isset($row_Rt3['wtitle'])?$row_Rt3['wtitle']:"我的雲名片"; ?>" maxlength="200" size="40"/>
                                    
                                    <input type="hidden" name="wid" id="wid" value="<?= isset($row_Rt3['wid'])?$row_Rt3['wid']:0; ?>" />
                                    
                                    <input type="hidden" name="wimg" id="wimg" value="<?= isset($row_Rt3['wimg'])?$row_Rt3['wimg']:"" ?>" />
                                    
                                    <input type="hidden" name="wimgext" id="wimgext" value="<?= isset($row_Rt3['wimgext'])?$row_Rt3['wimgext']:""; ?>" />
                                     
                                     <input type="hidden" name="windex" id="windex" value="<?= $id ?>" />
                                     <input type="hidden" name="coid" id="coid" value="<?= $_SESSION['login_memid'] ?>" />
				                        <span class="help-block"> </span>
			                      </div>
				                </div>
                                
                                
                                
                                
                                <br><br>
				                <div class="form-group">
				                    <label class="col-md-3 control-label" for="wlink">連結:</label>
				                    <div class="col-md-9">
				                      <input name="wlink" type="text" id="wlink" placeholder="請填寫要連結的網址" value="<?= $row_Rt3['wlink'] ; ?>" maxlength="200" size="40"  />
				                       <span class="help-block"> </span>
			                        </div>
		                        </div>
                               <div class="form-group">
				                    <label class="col-md-3 control-label" for="wlink">啟用:</label>
				                    <div class="col-md-9">
                                      <? $ss= isset($row_Rt3['wflag'])?$row_Rt3['wflag']:"1"; ?>
				                      啟用<input name="wflag" type="radio" id="wflag" value="1" <?= ($ss==1)?'checked="checked"':'' ?> />
                                      不啟用<input type="radio" name="wflag" id="wflag" value="0" <?= ($ss==0)?'checked="checked"':'' ?>/>
				                       <span class="help-block"> </span>
		                         </div>
                           </div>
                                 
                               <div class="form-group">
				                    <label class="col-md-3 control-label" for="fileToUpload1">圖檔:</label>
				                    <div class="col-md-9">
				                      <input name="fileToUpload1" type="file" id="fileToUpload1" />
				                       <span class="help-block"> 圖檔 Size 350 x 350 dpi ,</span> 圖檔支援格式 &quot;jpg&quot;,&quot;jpeg&quot;,&quot;bmp&quot;,&quot;png&quot;,&quot;gif&quot; 300</div>
		                        </div>
                               
                             
				             <div class="form-group">
				                     
				                    <div class="col-md-9">
				                      
                                       <span class="help-block">    </span>
			                      </div>
		                       </div>
                              
                              <div class="form-group"> 
                                <div class="col-md-9">
				                    
				                    <span class="help-block"></span>
	                                
                                </div>
		                    </div>
                              
                              

				                <div class="form-group form-actions">
									<button type="submit" class="btn btn-primary">保存</button>
				                </div>
                    
                              <input type="hidden" name="MM_update" value="fp1" />
                              <input type="hidden" id="coid" name="coid" value="<?= $row_Rt3['coid'] ; ?>" />

						      
						  </form>
						</div>
			        </div>
			    </div>
                <div class="col-sm-6">
                 <? if(isset($row_Rt3['wimgext'])){?>
                  <?
				    if ($id=='0'){
						$img="user_".sprintf("%07d",$_SESSION['login_memid']).'.'.$row_Rt3['wimgext'];
						}else{
					    $img="wp_".sprintf("%07d",$_SESSION['login_memid']).'_'.$id.'.'.$row_Rt3['wimgext'];		
						} 
					
				   ?>
                  <img name="simg" id="simg" src="/ecard/img/<?= $img  ?>"  alt="" /> 
                 <?= $img ?> 
				 <? }?> 
                </div>
		     </div>
		<!-- end: Content -->
		<br><br><br>
        <? } ?>
        <button onclick="window.location.href='ecardshop.html';" >回上頁</button>
             
        
 
 
	   <!-- end: Content -->
       <?php
mysql_free_result($Rt3);

?>
