<?php 
require "../../Run.php";
require "../../include/page.inc.php";
require "../../include/getmkurl.fun.php";

function sum_download($wapid,$gro){
    if($gro==0){
		$sql = "SELECT count(*) FROM download WHERE siteid='{$wapid}' AND gro='{$gro}'";  
	}else{
		$sql = "SELECT count(*) FROM download WHERE siteid='{$wapid}' AND gro='{$gro}'";  
	} 
	$result = mysql_query($sql); 
	list($nums) = mysql_fetch_row($result);
	return $nums;
}
function sum_download2($wapid){
	$sql = "SELECT count(*) FROM download WHERE siteid='{$wapid}'";   
	$result = mysql_query($sql); 
	list($nums) = mysql_fetch_row($result);
	return $nums;
}

function get_page($wapid,$gro){
$page=$_GET['page'];
	if($page>=0){
	$page = 1;
	}elseif($page>sum_download($wapid,$gro)){
	$page = sum_download($wapid,$gro);
	}
	return $page;
}

function start($wapid,$gro,$listnums){
	$start=(get_page($wapid,$gro) - 1)*$listnums;
    return $start;
}
function get_list($wapid,$gro,$listnums){
	$start=start($wapid,$gro,$listnums);
	if($gro==0){
	$sql="SELECT fileid,siteid,gro,title,file_path,file_ver,file_time,file_end,file_content,file_v,file_down,file_pic,file_auter,file_lan,file_pt FROM download WHERE siteid='{$wapid}' ORDER BY fileid DESC LIMIT {$start},{$listnums}";
	}else{
	$sql="SELECT fileid,siteid,gro,title,file_path,file_ver,file_time,file_end,file_content,file_v,file_down,file_pic,file_auter,file_lan,file_pt FROM download WHERE siteid='{$wapid}' AND gro='{$_GET['cid']}' ORDER BY fileid DESC LIMIT {$start},{$listnums}";
	}
		$result = mysql_query($sql);      
			if($result && mysql_num_rows($result) > 0){
			$i = 0;
			while(list($fileid,$siteid,$gro,$title,$file_path,$file_ver,$file_time,$file_end,$file_content,$file_v,$file_down,$file_pic,$file_auter,$file_lan,$file_pt) = mysql_fetch_row($result)){
			 $div = $num++%2 == 0?'line1':'line2';
			$list.='<div class="'.$div.'"><a herf="content.php?cid='.$gro.'&id='.$fileid.'&">'.$title.'</a>[审核.<a href="?action=del&cid='.$_GET['cid'].'&id='.$fileid.'&sid='.$_GET['sid'].'">删除</a>.编辑]</div>';
			$i++; 
		}
	}else{
		$list='<div cid="tip">暂时还木有任何记录</div>';
	}
	return $list;
}
//输出组合HTML
    
	$class_name = class_name($_GET['cid'],'cn',$wapid);
	$gro = class_name($_GET['cid'],'gro',$wapid);
	$listnums=class_get_info($_GET['cid'],"listnums",$wapid);
if($listnums<=0||$listnums==NULL){
  $listnums=10;
  }
    echo UBB($wap_top.class_get_info($_GET['cid'],"top_two",$wapid),$wapid);
	if($admin==1||$admin==2){
	if($_GET['cid'] == NULL){
	echo head('选择对应栏目');
		/*栏目分类列表*/
		echo '<div class="title">选择栏目|<a href="?cid=0&">所有记录>></a></div>';
		$sql="SELECT cid,cn,xuhao FROM class WHERE url='".get_mkid('download/list.php')."' AND siteid ='{$wapid}' ORDER BY cid DESC";
		$result = mysql_query($sql);
		$dnum=0;
		while(list($cid,$cn,$number) = mysql_fetch_row($result)){
		$div=$dnum++%2==0?'line1':'line2';
		echo '<div class="'.$div.'">';
		echo "[".$cid."]";
		echo "<a href=\"list.php?cid=".$cid."&page=1&id=".$articleid."&sid=".$sid_info."\">".$cn."</a>"; 
		/*echo "<a href=\"index.php?action=article&type=Modclass&cid=".$cid."&page=".$_GET['page']."&sid=".$sid_info."\">
		修改</a>"; 
		echo "<a href=\"index.php?action=article&type=delclass&cid=".$cid."&page=".$_GET['page']."&sid=".$sid_info."\">
		删除</a>*/
		echo "</div>"; 
	  }
	echo "<div class=\"mylink\"><a href=\"../../admin/wap_admin.php?\">返回上级</a><a href=\"../../index.php?\">网站首页</a></div>";
	}else{
	
		$sql="SELECT fileid,siteid,gro,title,file_path FROM download WHERE siteid='{$wapid}' AND gro='{$_GET['del']}' AND fileid='{$_GET['id']}'";
		$result = mysql_query($sql);      
			list($dfileid,$dsiteid,$dgro,$dtitle,$dfile_path) = mysql_fetch_row($result);
		if($_GET['action']=='del'){
			echo head('删除记录');
			if($_GET['del']!=NULL&&$_GET['del']!=''){
				$sql = "DELETE FROM download WHERE fileid='{$_GET['del']}' AND siteid='{$wapid}'";
				$del = mysql_query($sql);
				if($del && mysql_affected_rows()>0){
					echo '<div class="tip">成功删除</div>';
					if(!unlink("../".$dfile_path)){
					echo '<div class="tip">ERROR:文件删除失败，可能文件已经不存在！</div>';
					}else{
					echo '<div class="tip">提示：本资源所属的文件也已经一并删除！</div>';
						}
					}
			
			}else{
			echo '<div class="tip">是否确认删除此条记录（ID:'.$_GET['id'].'）?<br><a href="?action=del&cid='.$_GET['cid'].'&del='.$_GET['id'].'&sid='.$_GET['sid'].'">确认删除此条记录</a></div>';
				}
			
		}else{
			if($_GET['cid']==0){
			echo head('全部记录');
			echo get_list($wapid,$_GET['cid'],$listnums);
			
			}else{
			echo head($class_name).'<body>';
	echo '<div class="subtitle">文件管理</div>';		
	echo get_list($wapid,$_GET['cid'],$listnums);
	$url = 'list.php?cid='.$_GET['cid'].'&';
	echo '<div class="showpage">'.page(sum_download($wapid,$_GET['cid']),$listnums,$url,NULL,'view').'</div>';
		}
	echo UBB(class_get_info($_GET['cid'],"bottom_two",$wapid),$wapid);
	echo '<div class="mylink"><a href="../../index.php?cid='.$gro.'&">返回上级</a><a href="../../index.php?">网站首页</a>
	</div>';}
		}
	}else{
		echo '<div class="tip">权限不足，操作被拦截</div>';
	}
	echo footer(1);
?>