<?php
	require_once('_approot.php');
	
	$openck=new cktb();
	if($openck->checktable("ls_problem","CREATE TABLE  ls_problem (ID INT( 4 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , cate INT( 4 ) NOT NULL ,
 problemTitle VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL , problemText MEDIUMTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
 problemDate DATE NULL , Num INT( 6 ) NOT NULL DEFAULT  '10', enable INT( 1 ) NOT NULL) ENGINE = INNODB;"))	$ls_msg = $ls_msg .$openck->ls_msg;

	$selectcate=$_GET['selectcate'];
	$sort_field = $_GET['sort_field'];
	
	if ($_GET['ls_msg']=="")
	{
		$ls_msg="常见问题";
		$ls_msg2="常见问题操作";
	}	
	else
	{
		$ls_msg=$_GET['ls_msg'];	
	}
		$ls=$_GET['ls'];
		$id=$_GET['id'];
	switch ($ls)
	{
	 	case "unenable":
			$unenable_sql="update ls_problem set enable=0 where id=".$id." ";
			//echo $unenable_sql;
			$unenable_query=$opendb->query($unenable_sql);
			$ls_msg2="不显示操作成功！";
			break;
		case "enable":
			$enable_sql="update ls_problem set enable=1 where id=".$id." ";
			//echo $enable_sql;
			$enable_query=$opendb->query($enable_sql);
			$ls_msg2="显示操作成功！";
			break;
		case "del":
			$del_sql="delete from ls_problem where ID=".$id."";
	   		//echo $del_sql;
	   		$del_query=$opendb->query($del_sql);
			writelog($_SERVER['PHP_SELF'],"delete problem: ".$problemTitle,"ID: ".$id);

	   	$ls_msg2="删除操作成功！";
			break;
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<link href="css/admin.css" rel="stylesheet" type="text/css">
		<script language="JavaScript" src="js/ls.js"></script>
		<title>无标题文档</title>
	</head>

 <body>
	<table width="96%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#6396d6">
			<tr>
				<td height="18" background="images/admin_bg.gif"><div align="center"><font color="#ffffff"><strong><?=$ls_msg?>管理</strong></font></div>
				</td>
			</tr>
			<tr>
				<td height="213" valign="top" bgcolor="#dee7ff"><br>
					<form name="del" method="Post" action="delall.php" onSubmit="return ConfirmDel();">
					<table border="0" width="90%" align="center">
						<tr>
							<td width="63%" height="28" >提示：<font color="#FF0000"><?php echo $ls_msg2; ?></font></td>
				<td width="9%" align="right" ><div align="left"><span style="padding-left:20px;">
							 
								
								<?php 			$problem_sql="SELECT * FROM ls_functioncates  where functionName='常见问题' order by Num";//count the error by searchtype
			$problem_query=$opendb->query($problem_sql);
			?>

							</span></div></td>
							<td width="28%" align="right" ><img src="images/shao.gif">&nbsp;<a href="addproblem.php?selectcate=<?=$selectcate?>"><u>添加</u></a></td>
						</tr>
						<tr>
						  <td height="26" colspan="3" >
                          <?=$ls_msg?>分类： 
                          <?php                           while($problem_rows2=mysqli_fetch_assoc($problem_query))
		  					{
								echo "<a href='?selectcate=".$problem_rows2['ID']."'>".$problem_rows2['cateName']."</a> | ";
							}
				 			
						  ?>
                          <a href='?selectcate='> 全部 </a>
                          </td>
					  </tr>
					</table>
					<br>
					
					<?php
					// 获取当前页数
													if( isset($_GET['page']) ){
													   $page = intval( $_GET['page'] );
													}
													else{
													   $page = 1;
													}
													// 每页数量
													$PageSize = $NewPageNum;
													
													// 获取总数据量
													
													$sort='';
													if($problemSort=="ID") {
														$sort = "ID";
													}else if($problemSort=="Num"){
														$sort = "Num";
													}else{
														$sort = "ID";
													}
													if($sort_field != NULL){
														if($sort_field == 'Num'){
															$sort = " ".$sort_field." asc";
														}else{
															$sort = " ".$sort_field." desc";
														}
													}
		/********************************************************************************/
														if($selectcate<>''&&$selectcate<>'全部'){	$sql="select ls_problem.ID, ls_problem.cate, ls_problem.enable, ls_problem.problemTitle, ls_problem.problemDate, ls_functioncates.cateName, ls_problem.Num from ls_problem,ls_functioncates where ls_problem.cate='".$selectcate."' and ls_functioncates.ID = ls_problem.cate order by ls_problem.$sort ";}
														else
														{	$sql="select ls_problem.ID, ls_problem.cate, ls_problem.enable, ls_problem.problemTitle, ls_problem.problemDate, ls_functioncates.cateName, ls_problem.Num from ls_problem,ls_functioncates where ls_functioncates.ID = ls_problem.cate order by ls_problem.$sort ";	}
														$result=$opendb->query($sql);
													
													/********************************************************************************/
													// 获取总数据量
													
													$row = mysqli_num_rows($result);
													$amount = $row;
													$problem2_rows=mysqli_fetch_assoc($result);
													
													// 记算总共有多少页
													if( $amount ){
													   if( $amount < $PageSize ){ $page_count = 1; }			   //如果总数据量小于$PageSize，那么只有一页
													   if( $amount % $PageSize ){								  //取总数据量除以每页数的余数
														   $page_count = (int)($amount / $PageSize) + 1;		   //如果有余数，则页数等于总数据量除以每页数的结果取整再加一
													   }else{
														   $page_count = $amount / $PageSize;					  //如果没有余数，则页数等于总数据量除以每页数的结果
													   }
													}
													else{
													   $page_count = 0;
													}
													
													// 翻页链接
													$page_string = '';
														
													if( $page == 1 ){
													   $page_string .= '首页 | 上一页 |';
													}
													else{
													   $page_string .= '<a href=?selectcate='.$selectcate.'&page=1><u><font color=\"#0000FF\">首页</font></u></a> | <a href=?selectcate='.$selectcate.'&page='.($page-1).'><u><font color=\"#0000FF\">上一页</font></u></a> | ';
													}
													if( ($page == $page_count) || ($page_count == 0) ){
													   $page_string .= ' 下一页 | 尾页';
													}
													else{
													   $page_string .= ' <a href=?selectcate='.$selectcate.'&page='.($page+1).'><u><font color=\"#0000FF\">下一页</font></u></a> | <a href=?selectcate='.$selectcate.'&page='.$page_count.'><u><font color=\"#0000FF\">尾页</font></u></a>';
													}
														 $page_string .= " &nbsp;&nbsp;页次：<font color='#FF0000'><b>".$page."</b></font>/<b>".$page_count."</b>页";
														 $page_string .= " &nbsp;&nbsp;<b>".$PageSize."</b>个每页";
														 
													$ls_msg2= $ls_msg."管理 >> ".($problem2_rows['cateName']==''?'全部':$problem2_rows['cateName']);
					
					?>
					
				
				<table border="0" width="90%" align="center">
						<tr>
							<td><?php echo $ls_msg2; ?></td>
							<td align="right">共 <b><?php echo $row; ?></b>条</td>
				  </tr>
					 </table>
					 <table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
						<tr bgcolor="#B9DAFF">
							<td align="center" height="25" width="30"></td>
							<td align="center" height="25" width="30">编号</td>
							<td align="center" width="266"><?=$ls_msg?>标题</td>
							<td align="center" width="113"><a href='?selectcate=<?=$selectcate?>&sort_field=problemDate'>日期</a></td>
		          <td align="center" width="144"><?=$ls_msg?>类别</td>
							<td align="center" width="36"><a href='?selectcate=<?=$selectcate?>&sort_field=Num'>序号</a></td>
							<td align="center" colspan="2">操作</td>
							<td align="center" width="36">允许</td>
						</tr>
						<?php
													$problem_sql=$sql." limit ". ($page-1)*$PageSize .",".$PageSize." ";	
													$problem_query=$opendb->query($problem_sql);
						
													$i=1;
													while($problem_rows=mysqli_fetch_assoc($problem_query))
										 			 {
													echo "<tr bgcolor='#f4f4f4'";
												echo "onMouseOver=";
													echo " 	this.style.backgroundColor='#e5e5e5' ";
													echo "onMouseOut=";
													echo " 	this.style.backgroundColor='#f4f4f4' ";
													echo ">";
													echo "	<td align='center'>";
													echo "		<input name='select_id".$i."' type='checkbox' onclick=\"unselectall()\" id=\"select_id\" value='".$problem_rows['ID']."'>";
													echo "	</td>";
												  echo "	<td style='padding-left:10px;'>";
												  echo 		$i;
												  echo "	</td>";
												  echo "	<td style='padding-left:10px;'>";
												  echo 		$problem_rows['problemTitle'];
												  echo "	</td>";
												  echo "	<td style='padding-left:10px;'>";
												  echo 		$problem_rows['problemDate'];
												  echo "	</td>";
												  echo "	<td style='padding-left:10px;'>";
												  echo 		$problem_rows['cateName'];
												  echo "	</td>";
												  echo "	<td style='padding-left:10px;'>";
												  echo 		$problem_rows['Num'];
												  echo "	</td>";
												  echo "	<td align='center'   width='36'> ";
													echo "		<a href='modiproblem.php?selectcate=".$selectcate."&page=$page&id=".$problem_rows['ID']."' title='编辑'><img src='images/b_edit.png' border='0'></a>";
													echo "  </td>";
													echo "	<td align='center'  width='36'> ";
													echo "		<a href='?selectcate=".$selectcate."&page=$page&id=".$problem_rows['ID']."&ls=del' onclick='return ConfirmDelSubCate();' title='删除'><img src='images/b_drop.png' border='0'></a>";
													echo "  </td>";
													echo "	<td align='center'>";
											  
													  if($problem_rows['enable']==1)
														{
															echo "<a href='?selectcate=".$selectcate."&page=$page&id=".$problem_rows['ID']."&ls=unenable' title='显示'><img src='images/enable.gif' border='0'></a>";
														}
													  if($problem_rows['enable']==0)
													  	{
															echo "<a href='?selectcate=".$selectcate."&page=$page&id=".$problem_rows['ID']."&ls=enable' title='不显示'><img src='images/unenable.gif' border='0'></a>";
														}
																				
													echo "	</td>";
												  echo "</tr>";
												  
												  $i++;
												 }
												 	echo "<tr bgcolor=\"#B9DAFF\">";
												  echo "<td colspan='9' align='center' height='25'>";
												  echo $page_string;
												  echo "</td>";
												  echo "</tr>";
						
					?>
					</table>
					<br>
					<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" >
					<tr >
							<td width="82%" style="padding-left:6px;">
							<input name="chkAll" type="checkbox" id="chkAll" onclick=CheckAll(this.form) value="checkbox"> 本页显示的所有新闻
							<input name="submit" type='submit' value='删除的新闻'>
							<input name="Action" type="hidden" id="Action" value="problemDel">
							
							
								</td>
							<td width="18%" align="right" style="padding-left:6px;"><span style="border-bottom:1px solid #FFF;"><img src="images/shao.gif" alt="" />&nbsp;<a href="addproblem.php?selectcate=<?=$selectcate?>"><u>添加</u></a></span></td>
						</tr>
					</table>
					<br />
                  </form>
				
					
				</td>
			</tr>
	</table>
 </body>
</html>