<?php require_once('../Connections/BE2.php'); ?>
<?php
if (!isset($_SESSION)){ session_start(); }
if($_SESSION['login_companyck']<>"Y"){ return -1 ;}
   
   if( isset($_POST["MM_update"]) && ($_POST["MM_update"] == "fp1")){
  
   	if($_POST['wid']==0){
	  $updateSQL = sprintf("INSERT INTO ecardext (wtitle,wlink,wimg,wimgext,windex,wflag,wkid,wkid2,wuid)VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)",                       
	                   GetSQLValueString($_POST['wtitle'], "text"),
					   GetSQLValueString($_POST['wlink'], "text"),
					   GetSQLValueString($_POST['wimg'], "text"), 
					   GetSQLValueString($_POST['wimgext'], "text"),
					   GetSQLValueString($_POST['windex'], "int"),
					   GetSQLValueString($_POST['wflag'], "int"),
					   GetSQLValueString($_POST['wkid'], "int"),
					   GetSQLValueString('P', "text"),   // P= product, I = user define image
                       GetSQLValueString($_SESSION['login_coid'], "int")); //$_SESSION['login_memid']
		} else{ 
		  $updateSQL = sprintf("UPDATE ecardext SET wtitle=%s,wlink=%s,wimg=%s,wimgext=%s,wflag=%s,windex=%s  WHERE wkid=2 AND wid=%s AND wuid=%s",
                       GetSQLValueString($_POST['wtitle'], "text"),
					   GetSQLValueString($_POST['wlink'], "text"),
					   GetSQLValueString($_POST['wimg'], "text"),
					   GetSQLValueString($_POST['wimgext'], "text"),
					   GetSQLValueString($_POST['wflag'], "int"),
					   GetSQLValueString($_POST['windex'], "int"),
					   GetSQLValueString($_POST['wid'], "int"),
                       GetSQLValueString($_SESSION['login_coid'], "int")); //$_SESSION['login_memid']
		
		}
   mysql_select_db($database_BE2, $BE2);
   $Result1 = mysql_query($updateSQL, $BE2) or die(mysql_error());
   $msg='更新完成....' ;
   $updateGoTo = "netinshop2.html";
 
   echo  "<script>alert('".$msg."');location.href='".$updateGoTo."';</script>";	
	  
   
	  
   } 
  


 

 //update `member` set company_title=mem_name WHERE mem_auto>10 and mem_auto<26


$canedit=true;
$ed='-1';
if(isset($_GET["ed"])&& $_GET["ed"]<>''){
	$ed=$_GET["ed"];
	}

$id= (isset($_GET["id"]))?$_GET["id"]:-1;

// if($id>45){ $id=45;}	
// 

mysql_select_db($database_BE2, $BE2);
$query_Rt3 = sprintf("SELECT * FROM ecardext WHERE wkid=2 AND wuid= '%s' AND wid='%s' " ,$_SESSION['login_coid'],$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);
 
mysql_select_db($database_BE2, $BE2);
$query_Rt2 = sprintf("SELECT * FROM product_data WHERE company_num= '%s' and p_check='上架' AND p_new=1 order by mem_auto desc " ,$_SESSION['login_coid']);
//echo $query_Rt2 ;
$Rt2 = mysql_query($query_Rt2, $BE2) or die(mysql_error());
$row_Rt2 = mysql_fetch_assoc($Rt2);
$totalRows_Rt2 = mysql_num_rows($Rt2);
 
// echo $query_Rt3 ."<br>";
// echo $query_Rt2 ."<br>";
?>
<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">
                                  <select name="pname" id="pname" class="form-control placeholder col-xs-12 col-sm-4 mb-4" onchange="Selectdata()">
                <option value="0" >無</option>
		<? 
		 
		$imagepath='';		
		do { 
		  if($row_Rt2['mem_auto']==$row_Rt3['wlink']){
			 $imagepath = "/admin/upload2_image/".sprintf("%07d",$row_Rt2['company_num']).'/'.$row_Rt2['meta_img'].'.'.$row_Rt2['p_tag']; 
			}
		?>
        <option value="<?= $row_Rt2['mem_auto']; ?>" <?= ($row_Rt2['mem_auto']==$row_Rt3['wlink'])?'selected="selected"':""; ?>><?= mb_substr($row_Rt2['p_title'],0,40); ?></option>
        <?php } while ($row_Rt2 = mysql_fetch_assoc($Rt2));   ?>  
        
        </select> 
                                   <span class="help-block"> </span>
			                      </div>
                                </div>  
                                <br><br><br>
				                <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="wkid" id="wkid" value="2" /> 
 
                                     <input type="hidden" name="wuid" id="wuid" value="<?= $_SESSION['login_coid'] ?>" />
                                     <input type="hidden" name="wkid2" id="wkid2" value="P" />
				                        <span class="help-block"> </span>
		                            
			                      </div>
				                </div>
                                 <br><br>
                                 
                                <div class="form-group">
				                    <label class="col-md-3 control-label" for="windex"> 請填排列順序: </label>
			                      <div class="col-md-9">
                                     <input type="text" name="windex" id="windex" value="<?= isset($row_Rt3['windex'])?$row_Rt3['windex']:"0"; ?>" placeholder="請填排列順序"/>
				                     <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="hidden" id="wlink" value="<?= $row_Rt3['wlink'] ; ?>" maxlength="200" size="40" readonly="readonly" />
                                      <input name="wlinktmp" type="text" id="wlinktmp" placeholder="請填寫要連結的網址" value="<?= ($row_Rt3['wlink']=='')?'':'/categoryDetail-'.$row_Rt3['wlink'].'.html' ; ?>" maxlength="200" size="40" readonly="readonly" />
                                      	
				                       <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">
				                     
				                    <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="wuid" name="wuid" value="<?= $row_Rt3['wuid'] ; ?>" />

						      
						  </form>
						</div>
			        </div>
			    </div>
                <div class="col-sm-6">
                 <? //if($imagepath<>''){?>
                  <img name="simg" id="simg" src="<?= $imagepath;  ?>" width="350" height="350"  /> 
                 <? // }?> 
                </div>
		     </div>
		<!-- end: Content -->
	 
        <? } ?>
        <button onclick="window.location.href='netinshop2.html';" >回上頁</button>
	   <!-- end: Content -->
 <script type="text/javascript">
 function Selectdata() {
	var imgpath = '<?= "/admin/upload2_image/".sprintf("%07d",$_SESSION['login_coid']).'/' ;?>' ; 
	var pname= $("#pname").val();  // $(city).find(':selected')[0].id;
    $.ajax({
        type: 'POST',
        url: 'SelectproductApi.php',
        data: {'mem_auto': pname},
        success: function (data) {
		 jsondata = JSON.parse(data);
		 $('#wtitle').val($("#pname").find(':selected').text());
		 $('#wlink').val(jsondata.id);
		 $('#wlinktmp').val('/categoryDetail-'+jsondata.id+'.html');
		 
		 if(pname=='0'){
			$('#wlinktmp').val(''); 
			$('#wtitle').val('');
			  }
		
		 
		 $('#wimg').val(jsondata.img);
		 $('#wimgext').val(jsondata.imgext);
		 
		 
		 var imgname=imgpath+jsondata.img+'.'+jsondata.imgext;	
		 $('#simg').attr('src',imgname);	
 
        }
    });	
 
}
 </script>       
       <?php
mysql_free_result($Rt3);
mysql_free_result($Rt2);
?>
