<?php
include("CheckSession.php");
?>
<!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" />
<meta name="description" content="馬戲團" />
<title>馬戲團</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>

<script language="JavaScript" type="text/JavaScript"> 
<!-- 
function MM_openBrWindow(theURL,winName,win_width,win_height) { 
  var PosX = ((screen.width-win_width)/2); 
  var PosY = ((screen.height-win_height)/2);
  features = "status=no,toolbar=no,scrollbars=yes,resizable=yes,width="+win_width+",Height="+win_height+",top="+PosY+",left="+PosX;; 
  var urlStr = theURL;
  var newwin = window.open(urlStr,winName,features); 
}
//--> 
</script>
<?php

require_once("class/DBConfig.php");
require_once("class/DBOperation.php");

//设定每一页显示的记录数
$pagesize=20;

//取得记录总数，计算总页数用
$db = new DBOperation();
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
$querySql = "";
if($_SESSION['rightLevel'] == "0")
{
	$querySql = "select count(*) from FileData";
}
elseif($_SESSION['rightLevel'] == "1")
{
	$querySql = "select count(*) from FileData,AdminUser where AFlag = '1' and AGroupId = '".$_SESSION['groupId']."' And CCreateAcc = AId";
}
elseif($_SESSION['rightLevel'] == "2")
{
	$querySql = "select count(*) from FileData where CCreateAcc = '".$_SESSION['userId']."'";
}


$db->query($querySql);
$row = $db->fetch_row();
$db->close_db();
$numrows = $row[0];

//?算???
$pages=intval($numrows/$pagesize);

if($numrows%$pagesize)
{
	$pages++;
}
$page = $_GET["page"];
//判????置与否，如??定??首?
if(!isset($page))
{
  $page=1;
}
//判??到??
//if(isset($ys))
//{
//  if($ys>$pages)
//  {
//  	$page=$pages;
//  }
//  else
//  {     
//  	$page=$ys;
//  }
//}

//?算??偏移量     
$offset=$pagesize*($page-1);
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
if($_SESSION['rightLevel'] == "0")
{
	$querySql = "select CId,CMemo,CNewFileName,COrgFileName from FileData order by CCreateDateTime desc limit $offset,$pagesize";
}
elseif($_SESSION['rightLevel'] == "1")
{
	$querySql = "select CId,CMemo,CNewFileName,COrgFileName from FileData,AdminUser where AFlag = '1' and AGroupId = '".$_SESSION['groupId']."' And CCreateAcc = AId order by CCreateDateTime desc limit $offset,$pagesize";
}
elseif($_SESSION['rightLevel'] == "2")
{
	$querySql = "select CId,CMemo,CNewFileName,COrgFileName from FileData where CCreateAcc = '".$_SESSION['userId']."' order by CCreateDateTime desc limit $offset,$pagesize";
}


$db->query($querySql);

?>
<SCRIPT language="JavaScript">
function checkDelete(id,newFileName)
{
	if(confirm("確定要刪除嗎????"))
	{
     location.href = "FileDataDeleteDeal.php?id=" + id + "&CNewFileName=" + newFileName;
	}
}
function SendDel()
{	
  var cboxes = document.getElementsByName('chkId[]');
    var len = cboxes.length;
    var isDel = false;
    for (var i=0; i<len; i++) {
        if(cboxes[i].checked)
        {
          isDel = true;
        	if(confirm("確定要刪除嗎????"))
					{
				  	form1.action = "MultiFileDel.php";
				  	form1.submit();
			  	}
	      	break;
        }
    }
    if(!isDel)
    {
    	alert("請勾選要刪除的資料!!!");
    }
}

function check_all(obj,cName) 
{ 
    var checkboxs = document.getElementsByName(cName); 
    for(var i=0;i<checkboxs.length;i++){checkboxs[i].checked = obj.checked;} 
} 
</SCRIPT>
<body>
<form name="form1" method="post" action="FileDataInput.php?actionOp=Insert">
<table border="0" cellspacing="1" cellspadding="1" width="95%">
<!--
<tr>
<td align="center" colspan="2">
<span class="title"><b>檔案資料管理</b></span>
</td>
</tr>
-->
<tr>
<td align="center" colspan="2">
&nbsp;
</td>
</tr>
<?php
if(str_indexof($_SESSION['rights'],"C1I") >= 0)
{
?>
<tr>
<td align="left" colspan="2">
<span class="t12">新增</span>
<input type="text" name="CFileCount" value="1" id="CFileCount" size="2" maxlength="2" style="font-size: 12.0pt;font-family: 微軟正黑體;"/>
<span class="t12">筆檔案資料</span>
<input type="submit" name="btnSub" id="btnSub" value="新增" class="t12" />
&nbsp;
<input type="button" name="btnMulti" id="btnMulti" value="多選檔案上傳" class="t12" 
 onclick="MM_openBrWindow('MultiFileUploadIndex.php','MultiUploadWin',700,400)")" />
<input type="button" name="btnMultiDel" id="btnMultiDel" value="多筆檔案刪除" class="t12" 
 onclick="javascript:SendDel();" /> 
</td>
</tr>
<?php
}
?>
<tr>
<td align="left" colspan="2" width="100%">
<?php
if($myrow = $db->fetch_array())     
{     
  $i=0;     
  ?>       
  <table width="100%" border="1" cellspacing="2" cellpadding="2">     
  <tr>
  <td width="2%" bgcolor="#E1E9FB" align="center">
  <span class="t12">全選</span>
  <br>
  <input type="checkbox" name="chkAll" id="chkAll" value="All" onclick="check_all(this,'chkId[]')" >  
  </td>     
  <td width="10%" bgcolor="#E1E9FB" align="center"><span class="t12r">序號</span></td>     
  <td width="30%" bgcolor="#E1E9FB" align="center"><span class="t12r">檔名</span></td>
  <td width="30%" bgcolor="#E1E9FB" align="center"><span class="t12r">備註</span></td>
  <td width="10%" bgcolor="#E1E9FB" align="center"><span class="t12r">下載</span></td>
  <td width="10%" bgcolor="#E1E9FB" align="center"><span class="t12r">修改</span></td>  
  <td width="10%" bgcolor="#E1E9FB" align="center"><span class="t12r">刪除</span></td>
  </tr>     
  <?php
  $index = 0;
  do{  	
    $i++;
  ?>     
	  <tr>
	  <td bgcolor="#E6F2FF">
	  <input type="checkbox" name="chkId[]" id="chkId[]" value="<?php echo $myrow['CId'];?>">
	  </td>
	  <td bgcolor="#E6F2FF"><?php echo $myrow['CId'];?></td>     
	  <td bgcolor="#E6F2FF">
	  <span class="t12">
	  <?php echo $myrow['COrgFileName'];?></span></td>
	  <td bgcolor="#E6F2FF">
	  <span class="t12">
	  <?php echo $myrow['CMemo'];?></span></td>
	  <td bgcolor="#E6F2FF" align="center">
	   <?php
			if(str_indexof($_SESSION['rights'],"C1Q") >= 0)
			{
		?>
	  <span class="t12">
	  <a href="javascript:void(0);" onclick="MM_openBrWindow('FileDataDownload.php?id=<?php echo $myrow['CId'];?>','DownloadWin',100,100)")">
	  <b>下載</b></a></span>
	  <?php
			}
			else
				echo "&nbsp;";
		?>
	  </td>	 		 
	  <td bgcolor="#E6F2FF" align="center">
	   <?php
			if(str_indexof($_SESSION['rights'],"C1U") >= 0)
			{
		?>
	  <span class="t12">
	  <a href="FileDataInput.php?actionOp=Update&id=<?php echo $myrow['CId'];?>")">
	  <b>修改</b></a></span>
	  <?php
			}
			else
				echo "&nbsp;";
		?>
	  </td>	 		
	  <td bgcolor="#E6F2FF" align="center">
	  <?php
			if(str_indexof($_SESSION['rights'],"C1D") >= 0)
			{
		?>
	  <span class="t12">	  
	  <a href="javascript:void(0);" onclick="checkDelete('<?php echo $myrow['CId'];?>','<?php echo $myrow['CNewFileName'];?>');">
	  <b>刪除</b></a></span>
	   <?php
			}
			else
				echo "&nbsp;";
		?> 
	  </td>	  
	  </tr>     
  <?php     
  }while($myrow = $db->fetch_array());
  echo "</table>"   ;     
  }     
  //?示???     
  echo "<br><div align='left'>共有".$pages."頁 (".$page."/".$pages.")<br><br>";     
  //?示分??     
  for($i=1;$i<$page;$i++)     
  echo "&nbsp;<a href='FileDataIndex.php?page=".$i."'>第".$i."頁</a>&nbsp;";     
  echo "第".$page."頁";     
  for($i=$page+1;$i<=$pages;$i++)     
  echo "&nbsp;<a href='FileDataIndex.php?page=".$i."'>第".$i."頁</a>&nbsp;";
  //echo "<br><br>";     
  //?示?到??     
  //echo "<form action='FileDataIndex.php' method='post'>";     
  //?算首?、上一?、下一?、尾?的??值     
  $first=1;     
  $prev=$page-1;     
  $next=$page+1;     
  $last=$pages;     
  if($page>1)     
  {     
	  //echo "&nbsp;<a href='FileDataIndex.php?page=".$first."'>首頁</a>&nbsp;";     
	  //echo "&nbsp;<a href='FileDataIndex.php?page=".$prev."'>上一頁</a>&nbsp;";     
  }     
  if($page<$pages)     
  {     
	  //echo "&nbsp;<a href='fenye.php?page=".$next."'>下一頁</a>&nbsp;";     
	  //echo "&nbsp;<a href='fenye.php?page=".$last."'>尾頁</a>&nbsp;";     
  }     
  //echo "到&nbsp;<input type=text name='ys' size='2' value=".$page.">&nbsp;頁";     
  //echo "<input type=submit name='Submit' value='Go'>";     
  //echo "</form>";     
  echo "</div>"; 
?>
</td>
</tr>
</table>
</form>
</body>
</html>
