<?php
//producttxt.states = 0/1/2 正常/删除/已销售
define('IN_OUN', true);
include_once( "includes/command.php");

if($Aconf['priveMessage'] != '')
{
   echo showMessage($Aconf['priveMessage']);
   exit;
}
$Astates = array(0=>'正常',1=>'已删除',2=>'已售出');
/*------------------------------------------------------ */
//-- 批量删除产品记录
/*------------------------------------------------------ */

if ($_REQUEST['action'] == 'del')
{
    if (isset($_POST['checkboxes']))
    {
		$_GET['prid'] = $_GET['prid'] +0;
        $count = 0;		
        foreach ($_POST['checkboxes'] AS $key => $id)
        {
			$id = $id +0;
          $db_table = $pre."producttxt";
          $sql = "UPDATE ".$db_table." SET states='1' ".
                 " WHERE prid=$id AND domain_id='$_SESSION[$un_domain_id]'";
          $oPub->query($sql);

		  $db_table = $pre."product";
          $sql = "UPDATE ".$db_table." SET states='1' ".
                 " WHERE prid=$id AND domain_id='$_SESSION[$un_domain_id]'";
           $oPub->query($sql);
        }
        $strMessage =  "批量删除成功!";
		$tmpID = implode(",",$_POST['checkboxes']);
   }
   else if(isset($_GET['prid']))
   {
        $id = $_GET['prid'];
		$db_table = $pre."producttxt";
        $sql = "UPDATE ".$db_table." SET states='1' ".
               " WHERE prid=$id AND domain_id='$_SESSION[$un_domain_id]'";
        $oPub->query($sql);
		$db_table = $pre."product";
        $sql = "UPDATE ".$db_table." SET states='1' ".
                 " WHERE prid=$id AND domain_id='$_SESSION[$un_domain_id]'";
        $oPub->query($sql);
		$tmpID = $id;

		$strMessage =  "删除成功!";
   }
   else
   {
      $strMessage =  "没有选择需要删除的信息!";
	  $tmpID = 0;
   }

   $db_table = $pre.'account_log';
   $change_desc = real_ip().' |  '.date("m月d日 h:i").' |  domain_id:'.$_SESSION[$un_domain_id];
   $change_desc .= ' | '.$_SESSION[$un_auser_name].' 产品删除:'.$tmpID;
   $Afields=array('user_id'=>$_SESSION[$un_auser_id],'type'=>'prodDel','change_desc'=>$change_desc,'domain_id'=>$_SESSION[$un_domain_id]);
   $oPub->install($db_table,$Afields);
}

/* 查询条件 */
$db_table = $pre."producttxt";
$filter['sort_by']    = empty($_REQUEST['sort_by'])    ? 'dateadd' : trim($_REQUEST['sort_by']);
$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
if($_REQUEST["pcid"]) {
	$_REQUEST[pcid] = $_REQUEST[pcid] + 0;
	$where = "pcid='$_REQUEST[pcid]' and  states<>1 AND  domain_id = '".$_SESSION[$un_domain_id]."'";
} else {
     $where = "states<>1 AND domain_id = '".$_SESSION[$un_domain_id]."'";
}


$db_table = $pre."producttxt";
$sql = "SELECT COUNT(*) as count FROM ".$db_table." AS a WHERE 1 AND ". $where;
$row = $oPub->getRow($sql);
$filter['record_count'] = $row[count];
unset($row);
$page = new ShowPage;
$page->PageSize = 30;
$page->Total = $filter['record_count'];
$pagenew = $page->PageNum();
$page->LinkAry = array('pcid'=>$_REQUEST[pcid],'sort_by'=>$filter['sort_by']); 
$strOffSet = $page->OffSet();

$sql = "SELECT * FROM ".$db_table.
       " WHERE  $where ".
       " ORDER BY top DESC,".$filter['sort_by']." ".$filter['sort_order'].
       " LIMIT ". $strOffSet;
$row = $oPub->select($sql);
if($row ) { 
	$db_table = $pre."productcat";
    foreach ($row AS $key=>$val) {
        $row[$key]['dateadd']  = ($val['dateadd'])?date("m月d日 h:i", $val['dateadd']):'';
        $sql = "SELECT name FROM ".$db_table." WHERE pcid=$val[pcid]";
		$row2 = $oPub->getRow($sql);
		$row[$key]['acname'] = $row2[name];
    }

}
$StrtypeAll = '';
$n = 0;

if($row)
foreach ($row AS $key=>$val)
{
	   $tmpstr = ($n % 2 == 0)?"even":"odd";
	   $n ++ ;
       $StrtypeAll .= '<TR class='.$tmpstr.'>';
	   $StrtypeAll .= '<TD align=left>';
	   $StrtypeAll .= '<input type="checkbox" name="checkboxes['.$val["prid"].']" value="'.$val["prid"].'" />';
	   $StrtypeAll .= '</TD>';
       $StrtypeAll .= '<TD align=left>';
	   $StrtypeAll .= '<span id="name_'.$val["prid"].'">';
	   $StrtypeAll .= '<INPUT TYPE="text" value="'.$val["name"].'" size="36" onDblClick=pro_list_edit(\'name\',\''.$val["prid"].'\',this.value) />';
	   $StrtypeAll .= '</span>';
	   $StrtypeAll .= '</TD><TD align=left>';
	   $StrtypeAll .= '<span id="price_'.$val["prid"].'">';
	   $StrtypeAll .= '<INPUT TYPE="text" value="'.$val['shop_price'].'" size="10" onDblClick=pro_list_edit(\'price\',\''.$val["prid"].'\',this.value) />';
	   $StrtypeAll .= '</span>';
	   $StrtypeAll .= '</TD><TD align=left>'.$val["acname"].'</TD>';
      
       $tmpstr = ($val["top"])?'是':'<font color="CCCCCC">否</font>';
	   $StrtypeAll .= '<TD align=left id="top_'.$val["prid"].'"><span style="cursor:pointer" onmousedown="return pro_list_edit(\'top\',\''.$val["prid"].'\','.$val["top"].')">'.$tmpstr.'</span></TD>';

	   if($val["colors"]=='#00FF00')
	   {
		    $tmpstr = '<font color="#00FF00">绿色</font>';
	   }else if($val["colors"]=='#FF0000')
	   {
		   $tmpstr = '<font color="#FF0000">红色</font>';
	   }else if($val["colors"]=='#0000FF')
	   {
		   $tmpstr = '<font color="#0000FF">蓝色</font>';
	   }else
	   {
		    $tmpstr = '默认';
	   }

	   $StrtypeAll .= '<TD align=left>'.$tmpstr.'</TD>';
	   $StrtypeAll .= '<TD align=left>'.$Astates[$val["states"]].'</TD>';
	   $StrtypeAll .= '<TD align=left>'.$val["dateadd"].'</TD>';
       $StrtypeAll .= '<TD align=center>';
	   $StrtypeAll .= '<a href="../product.php?id='.$val["prid"].'&action=read&page='.$pagenew.'" target="_blank"><IMG SRC="images/icon_view.gif" WIDTH="16" HEIGHT="16" BORDER="0" ALT="阅读"></a> _ ';
	   $StrtypeAll .= '<a href="productsend.php?prid='.$val["prid"].'&action=edit&page='.$pagenew.'" target="main"><IMG SRC="images/b_edit.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="编辑"></a> _ ';
	   $StrtypeAll .= '<a href="'.$_SERVER["PHP_SELF"].'?prid='.$val["prid"].'&action=del&page='.$pagenew.'" onclick="return(confirm(\'确定删除?\'))"><IMG SRC="images/b_drop.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="删除"></a>';

	   if($val["comms"] > 0 )
	   {
	       $StrtypeAll .= ' _ <a href="product_comms.php?prid='.$val["arid"].'" target="_blank"><IMG SRC="images/zoo.gif" WIDTH="16" HEIGHT="16" BORDER="0" ALT="评论管理"></a>';
	   }
       $StrtypeAll .= '</TD></TR>';    
}



/* 找到所有的分类到select start*/
$db_table = $pre."productcat"; 
$sql = "SELECT * FROM ".$db_table." where fid = 0 AND domain_id=".$_SESSION[$un_domain_id]." ORDER BY pcid ASC";
$AnormAll = $oPub->select($sql);

$Stropt = '<SELECT NAME="pcid" onchange="chkSearch(this.options[this.options.selectedIndex].value)">';
$tmp = ($filter['pcid'] == 0)?'SELECTED':'';
$Stropt .= '<OPTION VALUE="0" '.$tmp.'>所有分类</OPTION>';
$n = 0;
while( @list( $key, $value ) = @each( $AnormAll) ) {
	   $n ++;
       $selected = ($_GET['pcid'] == $value["pcid"])? 'SELECTED':'';
       $Stropt .= '<OPTION VALUE="'.$value["pcid"].'" '.$selected.' >'.$n.'、'.$value["name"].'</OPTION>';
	   /* 查找儿子 */
       if($value["next_node"] != ''){          
           $Stropt .= get_next_node($value["next_node"],$_GET['pcid'] );
	   }	   
}
$Stropt .= '</SELECT>';
/* 找到所有的分类到select end*/
?>
<?php
   include_once( "header.php");
	if ($strMessage != '')
	{
		 echo  '<SCRIPT language="javascript"> alert( "'.$strMessage.'");</script>';
	}
?>
	
<TABLE width="100%" border=0>

  <TR class=bg1>
    <TD align=left colspan="9">
	   <span style="float: left">        
	   <?php echo $Stropt;?>
       </span>
	   <span style="float: right"><a href="productsend.php"> [添加新产品]</a> </span>
	</TD> 
  </TR>

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" name="listForm" target="_self">
  <TR class=bg5>
    <TD align=left>序号</TD>
    <TD align=left>标题[修改后双击鼠标,自动更新]</TD>
	<TD align=left>价格</TD>
	<TD align=left>分类</TD>
	<TD align=left>
	<a href="productlist.php?pcid=<?php echo $_REQUEST['pcid'].'&sort_by=top&page='.$pagenew.'"';?> target="main">
	置顶
	</a>
	</TD>
    <TD align=left>颜色</TD>
	<TD align=left>状态</TD>
	<TD align=left>时间</TD>
	<TD align=center>操作</TD>
  </TR>

 <?php echo $StrtypeAll;?>

  <TR class=bg5>
    <TD  align=right colspan="9">
	<span style="float: left">
	全选删除:<input onclick=selectAll() type="checkbox" name="check_all"/>
	<INPUT TYPE="submit" name="submit" value="确认删除" style="background-color: #FF9900">
	<INPUT TYPE="reset" name="reset" value="恢复" style="background-color: #CCFF99"> 
	<INPUT TYPE="hidden" name="action" value="del"> 
    </span>
	<span style="float: right">
	<?php echo $showpage = $page->ShowLink();?>
	</span>
	</TD>
  </TR>
  </form>
 </table>
<SCRIPT src="../js/ajax.js" type="text/javascript"></SCRIPT>
<script type="text/javascript" language="JavaScript">
function selectAll(){
	xx = listForm.check_all.checked
	for(var i=0;i<listForm.length;i++)
	{
		if(listForm.elements[i].type=="checkbox")
			listForm.elements[i].checked=xx;
	}
}

function chkSearch(obj)
{
	 
   location="<?php echo $_SERVER["PHP_SELF"];?>?pcid=" + obj;

}

  function pro_list_edit(edit,prid,edit_val)
  {
     obj = edit + "_" + prid;
     var strTemp = "ajax_pro_list_edit.php?op=" + edit + "&prid=" + prid + "&edit_val=" + escape(edit_val);
	 //alert(strTemp);
	 send_request(strTemp);
  }

</script>
<?php
/* OPTION 递归 */
function get_next_node($next_node,$fid,$str = '　')
{
   global $oPub,$pre;
   $db_table = $pre.'productcat';
   $Agrad = explode(',',$next_node);
   $Stropt = '';
   if(count($Agrad) > 0 )
	{
	   $str .= '　';
	   $tn = 0;
	   while( @list( $k, $v ) = @each( $Agrad ) ) {
           if ($v == 0 && $v =='')
		   {
              break;
		   }		   
		   $sql = "SELECT * FROM ".$db_table." where pcid = $v";
           $Anorm = $oPub->selectrow($sql);
		   if( $Anorm["name"] != ''){
			   $tn ++;
			   $selected = ($fid == $v)? 'SELECTED':'';
		      $Stropt .=  '<OPTION VALUE="'.$v.'" '.$selected.'>'.$str.$tn.'）'.$Anorm["name"].'</OPTION>';
              $Stropt .= get_next_node($Anorm["next_node"],$fid,$str);
		   }
		   
	   }
	}
	return $Stropt;
}
/* tbale 递归 */
function tab_next_node($next_node,$fid,$str = '　')
{
   global $oPub,$pre;
   $db_table = $pre.'productcat';
   $Agrad = explode(',',$next_node);
   $Strtab = '';
   if(count($Agrad) > 0 )
	{
	   $str .= '　';
	   $tn = 0;
	   while( @list( $k, $v ) = @each( $Agrad ) ) {
           if ($v == 0 && $v =='')
		   {
              break;
		   }		   
		    $sql = "SELECT * FROM ".$db_table." where pcid = $v";
            $Anorm = $oPub->selectrow($sql);
			if( $Anorm["name"] != ''){
			  $tn ++ ;
	          $tmpstr = ($n % 2 == 0)?"even":"odd";
              $Strtab  .= '<TR class='.$tmpstr.'>';

              $Strtab  .= '<TD align=left>'.$str.$tn.'）'.$Anorm["name"].'</TD>';
			  $Strtab  .= '<TD align=left>'.$Anorm["descs"].'</TD>';
			  $tmp = ($Anorm["ifshow"])?'是':'否';
			  $Strtab  .= '<TD align=left>'.$tmp.'</TD>';
	          $tmp = ($Anorm["ifnav"])?'是':'否';
	          $Strtab .= '<TD align=left>'.$tmp.'</TD>';
              $Strtab  .= '<TD align=left><a href="'.$_SERVER["PHP_SELF"].'?pcid='.$v.'&fid='.$fid.'&action=edit"><IMG SRC="images/b_edit.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="[编辑]"></a> ';
	          $Strtab  .= '<a href="'.$_SERVER["PHP_SELF"].'?pcid='.$v.'&fid='.$fid.'&action=del"><IMG SRC="images/b_drop.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="[删除]"></a></TD>';
              $Strtab  .= '</TR>';  
	          $n ++;
              $Strtab .= tab_next_node($Anorm["next_node"],$v,$str );
			}
	   }
	}
	return $Strtab;
}
?>	
<?php
include_once( "footer.php");
?>