<?php if(!defined('IN_GATEWEI')) {	
	exit('access denied');
	}
require_once GATEWEI_ROOT.'./engine/attachment.func.php';
require_once GATEWEI_ROOT.'./engine/post.func.php';
$pagerecords=5;
$curpage=isset($page)?$page:1;
$startrecord=($curpage-1)*$pagerecords;
$totalrecord = $db->result($db->query("select count(*) from g_products where top = 0"),0);
$q = $db->query("select * from g_products where top =0 order by ord desc limit $startrecord , $pagerecords");
$qurl="siteadmin.php?iframe=productmanagement";
$topq = $db->query("select * from g_products where top =1 order by ord desc limit 0 , 5");
$ctopq = $db->query("select count(*) from g_products where top =1");

	if($ipost=='query'){
		if($action=='tag'){
			$totalrecord = $db->result($db->query("select count(*) from g_products where tag=$qtxt"),0);
			$q = $db->query("select * from g_products where tag=$qtxt order by ord desc limit $startrecord , $pagerecords");
			$qurl="siteadmin.php?iframe=productmanagement&ipost=query&action=tag&qtxt=".$qtxt;
		}elseif($action=='cid'){
			$textarray=explode(',',$qtxt);
			if($textarray[0]==0 && $textarray[1]==0){
				gadminmessage("请选择产品分类");
			}
			$totalrecord = $db->result($db->query("select count(*) from g_products where pid =$textarray[0] and categoryid = $textarray[1]"),0);
			$q = $db->query("select * from g_products where pid =$textarray[0] and categoryid = $textarray[1] order by ord desc limit $startrecord , $pagerecords");
			$qurl="siteadmin.php?iframe=productmanagement&ipost=query&action=cid&qtxt=".$qtxt;
		}elseif($action=="producttitle"){
			if(empty($qtxt)){
				gadminmessage("查询关键字不能为空");
			}
			$totalrecord = $db->result($db->query("select count(*) from g_products where producttitle like '%$qtxt%'"),0);
		    $q = $db->query("select * from g_products where producttitle like '%$qtxt%' order by ord desc limit $startrecord , $pagerecords");
			$qurl="siteadmin.php?iframe=productmanagement&ipost=query&action=producttitle&qtxt=".$qtxt;
		}elseif($action=="timeslice"){
			$date=explode(';',$qtxt);
			list($sy,$sm,$sd)=explode(',',$date[0]);
			list($ey,$em,$ed)=explode(',',$date[1]);
			$stime=mktime(0,0,0,$sm,$sd,$sy);
			$etime=mktime(23,59,59,$em,$ed,$ey);
			$totalrecord = $db->result($db->query("select count(*) from g_products where subtime > $stime and subtime < $etime"),0);
		    $q = $db->query("select * from g_products where subtime > $stime and subtime < $etime order by ord desc limit $startrecord , $pagerecords");
			$qurl="siteadmin.php?iframe=productmanagement&ipost=query&action=timeslice&qtxt=".$qtxt;
		}
	}

	$multipage=multi($totalrecord, $pagerecords, $curpage, $qurl);


if($ipost=='setproduct'){
	if(!$pervalue{16}){
		showmsg("该用户组无此权限");
	}
	if($action=='unsettop'){
		productsetting($action,$top_product_check);
	}elseif($action=='delete'){
		$product_check = isset($product_check)?$product_check:array();
		$top_product_check = isset($top_product_check)?$top_product_check:array();
		$merge_check=array_merge($product_check,$top_product_check);
		productsetting($action,$merge_check);
	}else{
		productsetting($action,$product_check);
	}
}

if($ipost=="update"){
		upproduct2db($producttitle,$productintro,$g_editor_doc,$tag,$productid);
	}

	if($ipost=="modify"){
		if($action=='delete'){
			echo POSITION;
			modifydelattach($mid,$fid,2);
		}
		$q = $db->query("select * from g_product_content b , g_products a left join g_attachments c On a.productid = c.bid and  c.typeid=2 and c.islogo=1 where a.productid=b.productid and a.productid= $mid limit 1");
		$aq = $db->query("select * from g_attachments where bid = $mid and islogo=0 and typeid=2");
		$modifyinfo=$db->fetch_array($q);
		?>


<script language=javascript src="./engine/js/calendar.js"></script>
<form enctype="multipart/form-data" method="post" action="siteadmin.php?iframe=productmanagement&ipost=update">
<table>
<caption>产品信息修改</caption>
   <tr>
      <td width="20%"><span>产品名称<input type="hidden" name="productid" value="<?php echo $modifyinfo['productid']?>"></span></td>
      <td width="80%"><input type="text" id="producttitle" name="producttitle" class="formtext" value="<?php echo $modifyinfo['producttitle']?>">&nbsp;
	  <select name="tag" class="smallselect">
	  <?php 
	  foreach($productidentification as $key => $val){
		  $tagselect=$modifyinfo['tag']==$key ? 'selected':'';
		  echo '<option value="'.$key.'" style="color:'.$val[0].'" '.$tagselect.'>'.$val[1].'</option >';
	  }
	  ?>
      </select></td>
   </tr>
    <tr>
	  <td>自定义发布时间</td>
	  <td> <input type="checkbox" name="iscustom" id="iscustom" value="1"></td>
	  </tr>
	  <tbody id="custom_tbody">
		  <tr>
		 <td>设置发布时间</td>
		<td><span id="calendar_subtime"></span></td>
		</tr>
	  </tbody>
     	  <tr>
	 <td>产品标识图</td>
      <td>
	  <?php if($modifyinfo['islogo']){?>
	  <span id="modifyattach<?php echo $modifyinfo['fid']?>"></span>
	  <span id="attach<?php echo $modifyinfo['fid']?>"><a href="javascript:popwindows('<?php echo $modifyinfo['storename']?>',1,0)"><?php echo $modifyinfo['name']?></a> <a href="###" onclick="modifyattach(<?php echo $modifyinfo['fid']?>)">[修改]</a> <a href="###" onclick="deleteattach('product',<?php echo $mid?>,<?php echo $modifyinfo['fid']?>,false)">[删除]</a></span>	    
	  <?php }else{?>
	  <input type="file" id="logoimg" name="logoimg[1]" class="postmsgattach">
	  <?php }?>
	  </td>
	  </tr>
  <tr>
    <td><span>所属分类</span></td>
    <td >
	  <select id="cid" name="cid" class="formselect">
		<option value="0,0" selected >产品根分类</option >
<?php
$q=$db->query("select * from g_productcategory where pid = 0");
while($category=$db->fetch_array($q)){ 
	$categoryselect=($modifyinfo['pid'] == $category['pid'] && $modifyinfo['categoryid'] == $category['pcid'])?'selected':'';
	?>
<option value="<?php echo $category['pid'].','.$category['pcid']?>"  style="background-color:#b1c3d9" <?php echo $categoryselect?>><?php echo $category['categorytitle']?></option >
<?php 
$pid=$category['pcid'];
$subq=$db->query("select * from g_productcategory where pid = $pid");
while($subcategory=$db->fetch_array($subq)){
	$subcategoryselect=($modifyinfo['pid'] == $subcategory['pid'] && $modifyinfo['categoryid'] == $subcategory['pcid'])?'selected':'';
				echo '<option value="'.$subcategory['pid'].','.$subcategory['pcid'].'" '.$subcategoryselect.'>'.$subcategory['categorytitle'].'</option>';
			}
		}
?>
      </select>
	</td>
  </tr>

  <tr>
          <td valign="top">
	       <span>产品简介</span>
	      </td>
		    <td >
	       <textarea name="productintro" class="formtextarea"><?php echo $modifyinfo['productintro']?></textarea>
	      </td>
   </tr>
    		<tr>
          <td colspan="2">
	       <span>产品内容</span>
	      </td>
   </tr>
<tr>
          <td colspan="2" valign="top">
<?php
require_once("./admin/g_admin_editor.func.php");
geditor($modifyinfo['content']);
?>
</td>
   </tr>  	
   </table>
<?php
if($db->fetch_array($aq)){
?>
   	  <table>
	  <tr>
          <td colspan="4">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
			<tr>
				<th width="55%">产品附件</th>
				<th width="12%">附件大小</th>
				<th width="25%">附件类型</th>
				<th width="8%">下载次数</th>
			</tr>
   <?php

	while($modproductattach=$db->fetch_array($aq)){
	$fid=$modproductattach['fid'];
	?>
	<tr>
		  <td>
		  <div id="attach<?php echo $fid?>">
				[<a href="###" onclick="deleteattach('product',<?php echo $productid?>,<?php echo $fid?>,false)">删除</a>] 
				[<a href="###" onclick="modifyattach('<?php echo $fid?>')">修改</a>] 
				[<a href="###" onclick="insertAttachtext(<?php echo $fid?>);return false;">插入</a>]
				<?php echo $modproductattach['name']?>
				(<?php echo $modproductattach['fid']?>)
				</div>
				<span id="modifyattach<?php echo $fid?>"></span>
			</td>
			<td><?php echo sizecount($modproductattach['size'])?></td>
			<td><?php echo $modproductattach['type']?></td>
			<td><?php echo $modproductattach['downtimes']?></td>
			</td>
   </tr>
<?php
		}
	}
?>
     	<tr>
          <td colspan="4">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
   <tr>
    <td  colspan="4"> <div id="submitdiv"><input name="g_product_submit" type="submit" value="提&nbsp;&nbsp;交" ></div></td>
    </tr>
</table>

</form>
<SCRIPT LANGUAGE="JavaScript">
loadcalendar("_subtime");
loadcustomtime();
</SCRIPT>
<?php
	gexit();
	}
?>
<script language=javascript src="./engine/js/calendar.js"></script>
<table>
<caption>产品信息管理</caption>

	<form id="productquery" method="post">
		<input type="hidden" id="qtxt" name="qtxt">
	</form>

<tr>
      <td width="20%"><span>产品标题关键字</span></td>
      <td width="80%"><input type="text" id="producttitle" name="producttitle" class="formtext">  <input type="button" value="查询" onclick="pquery('producttitle')"></td>   </tr>
<tr>
      <td width="20%"><span>信息标示</span></td>
      <td width="80%">
	  <select id="tag" name="tag" class="formselect">
	  <?php 
	  foreach($productidentification as $key => $val){
		  echo '<option value="'.$key.'" style="color:'.$val[0].'">'.$val[1].'</option >';
	  }
	  ?>
      </select>    <input type="button" value="查询" onclick="pquery('tag')">
	  </td>   
	  </tr>
 <tr>
    <td><span>所属分类</span></td>
    <td >
	  <select id="cid" name="cid" class="formselect">
		<option value="0,0" selected >产品根分类</option >
<?php
$qc=$db->query("select * from g_productcategory where pid = 0");
while($category=$db->fetch_array($qc)){ ?>
<option value="<?php echo $category['pid'].','.$category['pcid']?>"  style="background-color:#b1c3d9"><?php echo $category['categorytitle']?></option >
<?php 
	$pid=$category['pcid'];
	//$subqc 子类分类查询值
	$subqc=$db->query("select * from g_productcategory where pid = $pid");
	while($subcategory=$db->fetch_array($subqc)){
				echo '<option value="'.$subcategory['pid'].','.$subcategory['pcid'].'">'.$subcategory['categorytitle'].'</option>';
			}
		}
?>
      </select>
	    <input type="button" value="查询" onclick="pquery('cid')">
	</td>
  </tr>
      <tr>
    <td><span>产品发布时间段</span></td>
    <td >
	<div id="calendar_start" class="calendar"></div>
	<div class="calendarspace">至</div>
	<div id="calendar_end" class="calendar"></div>
	<div class="calendarquery"><input type="button" value="查询" onclick="pquery('timeslice')"></div>
	</td>
  </tr>
    	   <tr>
          <td colspan="5">
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
</table>



<table>
<form id="form_productlist" method="post">
<?php if($db->result($ctopq,0)){?>
 <tr>
    <th colspan="5">置顶产品<img src="images/siteadmin/g_admin_top.gif"></th>
  </tr>
<tr>
<tr>
     <td width="5%" ><input type="checkbox" name="top_product_check_all" id="top_product_check_all" onclick="checkall_onclick('top_product')"></td>
	 	<td  width="5%" >修改</td>
	    <td width="75%" >产品标题</td>
		<td width="15%" >发布时间</td>
  </tr>
  <?php
   $i=0;
	while($products=$db->fetch_array($topq)){	?>
    <tr>
    <td ><input type="checkbox" id="top_product_check_<?php echo $i?>" name="top_product_check[]" value="<?php echo $products['productid']?>"></td>
    <td ><a href="siteadmin.php?iframe=productmanagement&ipost=modify&mid=<?php echo $products['productid']?>"><img src="./images/siteadmin/g_admin_edit.gif"></a></td>
		<td >
	[<?php echo $productcategory[$products['categoryid']]['categorytitle']?>]
	[<font color="<?php echo $productidentification[$products['tag']][0]?>">
	<?php echo $productidentification[$products['tag']][1]?>
	</font>]
	<a href="viewproduct.php?productid=<?php echo $products['productid']?>" target="_blank" ><?php echo height_light($products['producttitle'],$products['hlight'],$productcategory[$products['categoryid']]['hlightway'])?>
	</a></td>
  </tr>
  <?php	
		$i++;
	  } ?>
<?php }?>

 <tr >
    <th colspan="4">产品列表</th>
  </tr>
<tr>
     <td width="5%" ><input type="checkbox" name="product_check_all" id="product_check_all" onclick="checkall_onclick('product')"></td>
	 	<td  width="5%" >修改</td>
	    <td width="75%" >产品标题</td>
		<td width="15%" >发布时间</td>
  </tr>
  <?php
   $i=0;
	while($products=$db->fetch_array($q)){	?>
    <tr>
    <td ><input type="checkbox" id="product_check_<?php echo $i?>" name="product_check[]" value="<?php echo $products['productid']?>"></td>
    <td >
	<a href="siteadmin.php?iframe=productmanagement&ipost=modify&mid=<?php echo $products['productid']?>"><img src="./images/siteadmin/g_admin_edit.gif"></a>
	</td>
	<td >
	<?php echo $productcategory[$products['categoryid']]['categorytitle']?> |
	<font color="<?php echo $productidentification[$products['tag']][0]?>">
	<?php echo $productidentification[$products['tag']][1]?>
	</font> |
	<a href="viewproduct.php?productid=<?php echo $products['productid']?>" target="_blank" >
	<?php echo height_light($products['producttitle'],$products['hlight'],$productcategory[$products['categoryid']]['hlightway'])?>
	</a>
	</td>
	<td >
	<?php echo gmdate($productcategory[$products['categoryid']]['dateformat'],$products['subtime']+3600*8) ?>
	</td>
  </tr>
  <?php	
		$i++;
	  } ?>
<tr >
    <td colspan="4"> 
	<?php	
				if($multipage){
				echo '<div class="pages_btns font_14">'.$multipage.'</div>';
				}
			?></td>
  </tr>

   <tr>
          <td colspan="4">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>


<div id="moveform" style="display:none;">
	<select id="move_select_cid" name="move_select_cid">
		<option value="0,0" selected >产品根分类</option >
         <?php echo $echoselectform?>
    </select> 
	<button onclick="psetting('siteadmin.php?iframe=productmanagement&','move',0,0)"> 开始转移</button> 

   </div>
	      </td>
   </tr>
    </form>
   <tr>
        <td colspan="4"> 
		<div id="toolbox">
	<button onclick="psetting('siteadmin.php?iframe=productmanagement&','settop',0,0)">产品置顶</button>
   <button onclick="psetting('siteadmin.php?iframe=productmanagement&','unsettop',0,0)"> 取消置顶</button>
	<button onclick="psetting('siteadmin.php?iframe=productmanagement&','priority',0,0)"> 产品优先</button>
	<button onclick="psetting('siteadmin.php?iframe=productmanagement&','unpriority',0,0)"> 取消优先</button>
	<button onclick="psetting('siteadmin.php?iframe=productmanagement&','sethlight',0,0)"> 高亮显示</button>
	<button onclick="psetting('siteadmin.php?iframe=productmanagement&','unsethlight',0,0)"> 取消高亮</button>
	<button onclick="psetting('siteadmin.php?iframe=productmanagement&','delete',0,0)"> 删除产品</button> 
	<button onclick='showmoveform()'> 转移产品</button> 
	</div>
	      </td>
   </tr>
</table>

<script language="javascript">

	loadcalendar("_start");
	loadcalendar("_end");

</script>
