<?php
session_start();
include "header.html";
require_once "../MessageBox.php";
include "../datasource.inc.php";
include "../Function.php";
$Message = new MessageBox;
if ( empty($_SESSION["usernumber"]) and $_SESSION["userjs"] != "1") {
    $Message->setMessage("对不起，您没有权限进入后台管理!","logout.php");
    $Message->showMessage();
}
$page    = $_REQUEST['page'];
if ( $_REQUEST['action'] == "tj" ) {
	$file_name    = $_FILES['picpath']['name'];
    mt_srand((double)microtime()*1000000);
    $shpid = uniqid(mt_rand());
    $shpid = substr($shpid,0,6);
	$file_name1 = $shpid.$file_name;
	$picpath 	= $image_path.$file_name1;
	if ($file_name) {
		copy($_FILES['picpath']['tmp_name'],$picpath);
		$shpidimgname   = $shpid.".".substr($file_name,-3);
		@rename("$image_path/$file_name1","$image_path/$shpidimgname");
	}
    $query = "insert into pic_img values ('','$_REQUEST[fl1]','$shpidimgname')";
	mysql_query($query);
    $Message->setMessage("添加成功!","cs_pic.php?page=add&id=$_REQUEST[id]");
    $Message->showMessage();
}
if ($_REQUEST['action'] == 'update') {
	$file_name    = $_FILES['picpath']['name'];
	$file_name2    = $_FILES['picpath2']['name'];
    mt_srand((double)microtime()*1000000);
    $shpid = uniqid(mt_rand());
    $shpid = substr($shpid,0,6);
	$file_name1 = $shpid.$file_name;
	$file_name3 = $shpid."_2".$file_name2;
	$picpath 	= $image_path.$file_name1;
	$picpath2 	= $image_path.$file_name3;
	if ($file_name) {
		copy($_FILES['picpath']['tmp_name'],$picpath);
		$shpidimgname   = $shpid.".".substr($file_name,-3);
		@rename("$image_path/$file_name1","$image_path/$shpidimgname");
	}
	if ($file_name2) {
		copy($_FILES['picpath2']['tmp_name'],$picpath2);
		$shpidimgname2   = $shpid."_2".".".substr($file_name2,-3);
		@rename("$image_path/$file_name3","$image_path/$shpidimgname2");
	}
	if ($file_name && $file_name2){
		$sql  = "update pic_img set fl1='$_REQUEST[fl1]',fl2='$_REQUEST[fl2]',name='$_REQUEST[name]',style='$_REQUEST[style]',medium='$_REQUEST[medium]',number='$_REQUEST[number]',picpath='$shpidimgname',picpath2='$shpidimgname2',commet='$commet',body='$body',description='$description',keywords='$keywords' where id='$_REQUEST[id]'";
	} elseif ($file_name) {
		$sql  = "update pic_img set fl1='$_REQUEST[fl1]',fl2='$_REQUEST[fl2]',name='$_REQUEST[name]',style='$_REQUEST[style]',medium='$_REQUEST[medium]',number='$_REQUEST[number]',picpath='$shpidimgname',commet='$commet',body='$body',description='$description',keywords='$keywords' where id='$_REQUEST[id]'";
	} elseif ($file_name2){
		$sql  = "update pic_img set fl1='$_REQUEST[fl1]',fl2='$_REQUEST[fl2]',name='$_REQUEST[name]',style='$_REQUEST[style]',medium='$_REQUEST[medium]',number='$_REQUEST[number]',picpath2='$shpidimgname2',commet='$commet',body='$body',description='$description',keywords='$keywords' where id='$_REQUEST[id]'";
	} else {
		$sql  = "update pic_img set fl1='$_REQUEST[fl1]',fl2='$_REQUEST[fl2]',name='$_REQUEST[name]',style='$_REQUEST[style]',medium='$_REQUEST[medium]',number='$_REQUEST[number]',commet='$commet',body='$body',description='$description',keywords='$keywords' where id='$_REQUEST[id]'";
	}
    $result  = mysql_query($sql);
   $Message->setMessage("编辑成功!","cs_pic.php?id=$_REQUEST[id]");
    $Message->showMessage();
}
?>
<table width="700" border="0" cellspacing="0" cellpadding="0">
        <tr>
         <TD vAlign=top><FONT color=#565656><IMG height=14
      src="../images/closedfold.gif" width=14>&nbsp;&nbsp;您当前的位置<BR><IMG
      height=15 src="../images/bar.gif" width=15><IMG height=14
      src="../images/openfold.gif" width=14>&nbsp;&nbsp;图片信息管理</FONT></TD>
        </tr>
</table>
</center>
<script language = "JavaScript">
var onecount;
onecount=0;
subcat = new Array();
<?
$sql = "select * from zht_profl2 order by id desc";
$result = mysql_query( $sql);
$count = 0;
while($res = mysql_fetch_row($result)){
?>
subcat[<?=$count?>] = new Array("<?=$res[0]?>","<?=$res[1]?>","<?=$res[2]?>");
<?
$count++;
}
echo "onecount=$count;";
?>
//联动函数
function changelocation(locationid)
    {
    document.myform.fl2.length = 0; 
    var locationid=locationid;
    var i;
    for (i=0;i < onecount; i++)
        {
            if (subcat[i][2] == locationid)
            { 
       //var newOption1=new Option(subcat[i][1], subcat[i][0]);
                //document.all.ctype.add(newOption1);
                document.myform.fl2.options[document.myform.fl2.length] = new Option(subcat[i][1], subcat[i][0]);
            }        
        }
        
    }    
</script>
<script language=javascript>
function check_input()
{
	if (document.form.name.value=='') {
		alert("请输入图片名称!");
		return false;
	}
 return true;
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
</script>
<?php
switch ($page){
    case "add":
?>
<center>
<p align="center">&nbsp;<b><font size="3">请输入图片信息</font></b></p>
<form action="cs_pic.php?page=add" enctype="multipart/form-data"  method="post" name="myform" onsubmit="return check_input()">
<table width="550"  border=0 cellpadding=0 cellspacing=1 bgcolor=#6795B4>
 <tr height=24>
    <td width="20%" bgColor=#EBF1F5><center>
      <b>图片ID</b>
    </center></td>
    <td width="80%" bgcolor=ffffff>
	<select name="fl1" size="1">
	<option value="<?php echo $_REQUEST[id]?>"><?php echo $_REQUEST[id]?></option>
    </select> 
    </td>
  </tr>
 
  
  <tr height=24>
    <td width="20%" bgColor=#EBF1F5><center><b>图片</b></center></td>
    <td width="80%" bgColor=#ffffff><input type=file size="30" name="picpath"></td>
  </tr>

</table><br>
<input type="hidden" value="tj" name="action">
<input type="hidden" name="id" value="<?php echo $_REQUEST[id]?>">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="submit" value="提 交" >&nbsp;&nbsp;<input type="reset"  value="取 消">
</form>
<?php
		break;
	case "delete":
		$sql   = "delete from pic_img where id='$_REQUEST[id]'";
		mysql_query($sql);
		$Message->setMessage("删除成功!","javascript:history.go(-1)");
		$Message->showMessage();
		break;
    case "update":
		$sql     = "select * from pic_img where id='$_REQUEST[id]';";
		$result  = mysql_query($sql);
		$row     = mysql_fetch_array($result);
		$body    = $row['body'];
		$commet  = str_replace("<BR>", "\n", $row['commet']);
		$description  = str_replace("<BR>", "\n", $row['description']);
		$keywords  = str_replace("<BR>", "\n", $row['keywords']);
?>
<center>
<p align="center">&nbsp;<b><font size="3">请修改图片资料</font></b></p>
<form action="cs_pic.php" enctype="multipart/form-data" method=post name=form onsubmit="return check_input()">
<table width="550"  border=0 cellpadding=0 cellspacing=1 bgcolor=#6795B4>
<tr height=24>
    <td width="20%" bgColor=#EBF1F5><center><b>图片ID</b></center></td>
    <td width="80%" bgcolor=ffffff>
	<select name="fl1" size="1">
	<option value="<?php echo $row['fl1']?>"><?php echo $row['fl1']?></option>
    </select> 

    </td>
  </tr><!--  
  <tr height=24>
    <td width="20%" bgColor=#EBF1F5><center><b>图片小分类</b></center></td>
    <td width="80%" bgcolor=ffffff>
    <select name="fl2">
    <?php
	$sql3    = "select * from zht_profl2 where id='$row[fl2]'";
	$result3 = mysql_query($sql3);
	$row3    = mysql_fetch_array($result3);
	echo "<option selected value=\"$row[fl2]\">$row3[fl2]</option>";
	$sql4    = "select * from zht_profl2 order by id desc";
    $result4 = mysql_query($sql4);
	while ($row4 = mysql_fetch_array($result4) ) {
       echo "<option value=\"$row4[id]\">$row4[fl2]</option>";
    }
	?>
	</select>
    </td>
  </tr>
  <tr height=24>
    <td width="20%" bgColor=#EBF1F5><center><b>图片名称</b></center></td>
    <td width="80%" bgColor=#ffffff><input type=text size="40" name="name" value="<? echo $row['name']?>"></td>
  </tr><tr height=24>
    <td width="20%" bgColor=#EBF1F5><center><b>排序</b></center></td>
    <td width="80%" bgColor=#ffffff><input type=text size="5" name="style" value="<? echo $row['style']?>"></td>
  </tr>
  <tr height=24>
    <td width="20%" bgColor=#EBF1F5><center><b>推荐</b></center></td>
    <td width="80%" bgColor=#ffffff>
	<?php
	if ($row['medium']=="是") {
		echo "<input type=\"radio\" name=\"medium\" value=\"是\" checked=\"checked\" />
      是 <input name=\"medium\" type=\"radio\" value=\"否\" />
      否";
	} else {
		echo "<input type=\"radio\" name=\"medium\" value=\"是\" />
      是 <input name=\"medium\" type=\"radio\" value=\"否\" checked=\"checked\" />
      否";
	}
	?>
	</td>
  </tr>-->
  <!--
  <tr height=24>
    <td width="20%" bgColor=#EBF1F5><center><b>型号</b></center></td>
    <td width="80%" bgColor=#ffffff><input type=text size="20" name="number" value="<? echo $row['number']?>"></td>
  </tr>-->
  <tr height=24>
    <td width="20%" bgColor=#EBF1F5><center><b>图片</b>
    </center></td>
    <td width="80%" bgColor=#ffffff><input type=file size="30" name="picpath"></td>
  </tr>
  <!-- <tr height=24>
    <td width="20%" bgColor=#EBF1F5><center><b>图片图片(大)</b></center></td>
    <td width="80%" bgColor=#ffffff><input type=file size="30" name="picpath2"></td>
  </tr>
 
  <tr height=24>
    <td width="20%" bgColor=#EBF1F5><center><b>网页描述</b></center></td>
    <td width="80%" bgColor=#ffffff><textarea name="description" rows="3" cols=60><? echo $description?></textarea></td>
  </tr>
  <tr height=24>
    <td width="20%" bgColor=#EBF1F5><center><b>关键词</b></center></td>
    <td width="80%" bgColor=#ffffff><textarea name="keywords" rows="3" cols=60><? echo $keywords?></textarea></td>
  </tr>
  <tr height=24>
    <td width="20%" bgColor=#EBF1F5><center><b>摘要</b></center></td>
    <td width="80%" bgColor=#ffffff><textarea name="commet" rows="4" cols=60><? echo $commet?></textarea></td>
  </tr>
  <tr height=24>
    <td width="20%" bgColor=#EBF1F5 colspan=2>&nbsp;&nbsp;&nbsp;&nbsp;<b>特性</b></td>
  </tr>
  <tr height=24>
    <td width="100%" bgColor=#EBF1F5 colspan=2><textarea name="body" style="display:none"><? echo $row['body']?></textarea><iframe id="body___Frame" src="../fckeditor/editor/fckeditor.html?InstanceName=body&amp;Toolbar=Basic" width="760px" height="560px" frameborder="0" scrolling="no"></iframe></td>
  </tr>-->
</table><br>
<?php echo "<input type=hidden name=id value=$row[id]>";?>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="hidden" value="update" name="action">
<input type="submit" value="编 辑" >&nbsp;&nbsp;<input type="reset"  value="取 消">
</form>
<?php
    	break;
	default:
	$sql    = "select * from pic_img where title_id='$_REQUEST[id]' order by id desc;";
    $result = mysql_query($sql);
	$num    = mysql_num_rows($result);		
    if ( $num>0 ){
    	$last   = intval(($num-1)/30)+1;
    	if ( $page < 1 ) $page=1;
    	if ( $page > $last ) $page=$last;
    	mysql_data_seek($result,($page-1)*30);
		echo "<table border=0 cellpadding=0 cellspacing=1 bgcolor=#6795B4 width=620 align=center>";
    	echo "<tr>";
    	echo "<td width=100 height=18 bgColor=#EBF1F5><div align=center><b>分类ID</b></div></td>";    	
    	echo "<td width=80 height=18 bgColor=#EBF1F5><div align=center><b>图片</b></div></td>";		
    	echo "<td width=40 height=18 bgColor=#EBF1F5><div align=center><b>编辑</b></div></td>";		
    	echo "<td width=40 height=18 bgColor=#EBF1F5><div align=center><b>删除</b></div></td>";		 		 	 		 
    	echo "</tr>"; 		 	
		for ( $i=0;$i<30;$i++ ) {
        	$row     = mysql_fetch_array($result);
	     	$id      = $row['id'];
		 	if ( $id ) {
				echo "<tr bgColor=#ffffff>";
				echo "<td width=100 height=18 bgColor=#ffffff><div align=center>$row[title_id]</div></td>";
				echo "<td width=170 height=18 bgColor=#ffffff><div align=center><a href=../upload-images/$row[filepath]><img src=../upload-images/$row[filepath] width=80 height=100></a></td>";
				echo "<td width=40 height=18 bgColor=#ffffff><div align=center><a title=点击编辑内容 href=cs_pic.php?page=update&id=$row[id]><img src=../images/edit.gif border=0></a></div></td>";
				echo "<td width=40 height=18 bgColor=#ffffff><div align=center><a title=点击删除本条记录 href=cs_pic.php?page=delete&id=$row[id] onclick=\"return confirm('确定要删除吗？')\"><img src=../images/clearicon.gif border=0></a></div></td>";
				echo "</tr>";
			}
		}
		echo "</table><br>";

		echo "<table width=500 border=0 cellspacing=0 cellpadding=0 height=24 align=center>";
		echo "<tbody>";
		echo "<tr><td colspan=4><center>";
		echo "总共找到<font color=red>$num</font>条记录,第<font color=red>$page</font>页,共<font color=red>$last</font>页。";
		echo "</center></td></tr><tr>";
		if ( $page <> 1 ) {
			echo "<td align=center>&nbsp;&nbsp;&nbsp;<a href=cs_pic.php?page=1&title=$_REQUEST[title]&fl1=$_REQUEST[fl1]&fl2=$_REQUEST[fl2]>第一页</a></td>";
			echo "<td align=center><a href=cs_pic.php?page=".($page-1)."&title=$_REQUEST[title]&fl1=$_REQUEST[fl1]&fl2=$_REQUEST[fl2]>上一页</a></td>";
		}
		if ( $page <> $last ) {
			echo "<td align=center><a href=cs_pic.php?page=".($page+1)."&title=$_REQUEST[title]&fl1=$_REQUEST[fl1]&fl2=$_REQUEST[fl2]>下一页</a></td>";
			echo "<td align=center><a href=cs_pic.php?page=$last&title=$_REQUEST[title]&fl1=$_REQUEST[fl1]&fl2=$_REQUEST[fl2]>最后一页</a></td>";
		}
		echo "</tbody>";
		echo "</tr>";
		echo "</table><br>";
		echo "</center>";
	} else {
		echo "<center>对不起，您还没有添加图片,<a href=cs_pic.php?page=add>我要添加</a>!</center>";
	}
	break;
}
?>