<?php
# Name: PHP版程氏音乐CMS管理系统 v3.0
# Author: 程氏<web@chshcms.com> [QQ:848769359]
# Homepage:http://www.chshcms.cn/
include_once("../../lib/cs_label.php");
include_once("../loginstate.php");
SystemPer('2_2');
$ac=CS_Request("ac");
?>
<!doctype html>
<html>
<head>
<meta charset="gbk">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>chshdjcms management</title>
<link href="../res/css/admin_style.css" rel="stylesheet" />
<link href="../../public/css/webox.css" rel="stylesheet" />
<script type="text/javascript" src="../res/js/ajax.js"></script>
<script language="javascript" src="../res/js/jquery.js"></script>
<script type="text/javascript" src="../../public/js/webox.js"></script>
<script language="javascript">
function del(_id){ 
  if (confirm("确定删除吗？")){
    window.location='?ac=del&id='+_id;
    return true;
  }else{
     return false;
  }		
}

function Dhid(_id){ 
      var cur=document.getElementById('dh_'+_id);
      var cur2=document.getElementById('ddh_'+_id);
        if(cur.style.display=='none'){
              cur.style.display='';
              cur2.className='away_icon';
        }else{
              cur.style.display='none';
              cur2.className='start_icon';
        }	
}
</script>
</head>
<body>
<div class="wrap">
<!-- start -->
<div class="nav">
	<ul class="cc">
			<li class="current"><a href="?">歌曲分类</a></li>
		        <li><a href="javascript:Popsboxif('添加分类',450,470,'class_add.php');">添加分类</a></li>
	</ul>
</div>
<form method="post" class="J_ajaxForm" action="?ac=edit" data-role="list">
<div class="table_list">
	<table width="100%" id="J_table_list" style="table-layout:fixed;">
		<colgroup>
			<col width="30">
			<col width="380">
			<col width="160">
			<col width="180">
			<col>
		</colgroup>
		<thead>
			<tr>
				<td></td>
				<td>[顺序] 分类名称</td>
				<td>英文别名</td>
				<td>模版文件</td>
				<td>数据总数</td>
				<td class="tac">启用</td>
				<td>操作</td>
			</tr>
		</thead>
<?php
	global $db;
        $sql="select * from ".Getdbname('class')." where cs_sid=1 and CS_FID=0 order by CS_XID asc";
	$rs=$db->query($sql);
	$num=$db->num_rows($rs);
 	for($i=0;$i<$num;$i++){ 
		$row=$db->fetch_array($rs);
                $ernum= $db -> num_rows($db -> query("select * from ".Getdbname('class')." where cs_fid=".$row['CS_ID'].""));
                $dancenum= $db -> num_rows($db -> query("select * from ".Getdbname('dance')." where cs_cid=".$row['CS_ID'].""));
                $CS_Template=$row['CS_Template'];
                if(empty($CS_Template)) $CS_Template='dance-list.html';
?>
		<tbody>
		<tr>
			<td><?php if($ernum>0){echo '<span id="ddh_'.$row['CS_ID'].'" onClick="Dhid('.$row['CS_ID'].');" class="J_start_icon away_icon"></span>';}else{echo '<span class="zero_icon"></span>';}?></td>
			<td>
                                <input name="CS_ID[]" type="hidden" value="<?php echo $row['CS_ID']?>" />
				<input name="CS_XID<?php echo $row['CS_ID']?>" type="text" class="input length_0 mr10" value="<?php echo $row['CS_XID']?>">
				<input name="CS_Name<?php echo $row['CS_ID']?>" type="text" class="input length_3 mr5" value="<?php echo $row['CS_Name']?>">
			</td>
			<td><input name="CS_AliasName<?php echo $row['CS_ID']?>" type="text" class="input length_2" value="<?php echo $row['CS_AliasName']?>"></td>
			<td><input name="CS_Template<?php echo $row['CS_ID']?>" type="text" class="input length_2" value="<?php echo $CS_Template?>"></td>
			<td><a href="dance.php?cid=<?php echo $row['CS_ID']?>">共<?php echo $dancenum?>条</a></td>
			<td class="tac"><input name="CS_YID<?php echo $row['CS_ID']?>" type="checkbox" value="ok" <?php if($row['CS_YID']==0) echo 'checked';?>></td>
			<td>
				<a href="javascript:Popsboxif('分类编辑',450,470,'class_add.php?id=<?php echo $row['CS_ID']?>');" class="mr10 J_dialog" title="分类编辑">[编辑]</a>
				<a style="cursor:pointer;"  onclick="del(<?php echo $row['CS_ID']?>);" class="mr10 J_ajax_del">[删除]</a>
			</td>
		</tr>
		</tbody>
         <?php
		if($ernum>0) echo '<tbody id="dh_'.$row['CS_ID'].'" style="display:">';
		//处理此分类下的子分类
		$sql2="select * from ".Getdbname('class')." where CS_FID=".$row['CS_ID']."";
		$class2=$db->query($sql2);
		if($class2){
			while($row2=$db->fetch_array($class2)){
                        $dancenum2= $db -> num_rows($db -> query("select * from ".Getdbname('dance')." where cs_cid=".$row2['CS_ID'].""));
                        $CS_Template2=$row2['CS_Template'];
                        if(empty($CS_Template2)) $CS_Template2='dance-list.html';
         ?>
			<tr>
				<td>&nbsp;</td>
				<td><span class="plus_icon  plus_end_icon mr10"></span>
                                    <input name="CS_ID[]" type="hidden" value="<?php echo $row2['CS_ID']?>" />
				    <input name="CS_XID<?php echo $row2['CS_ID']?>" type="text" class="input length_0 mr10" value="<?php echo $row2['CS_XID']?>">
				    <input name="CS_Name<?php echo $row2['CS_ID']?>" type="text" class="input length_3 mr5" value="<?php echo $row2['CS_Name']?>">
			        </td>
				<td>
					<input name="CS_AliasName<?php echo $row2['CS_ID']?>" type="text" class="input length_2" value="<?php echo $row2['CS_AliasName']?>">
				</td>
			        <td><input name="CS_Template2<?php echo $row2['CS_ID']?>" type="text" class="input length_2" value="<?php echo $CS_Template2?>"></td>
				<td><a href="dance.php?cid=<?php echo $row2['CS_ID']?>">共<?php echo $dancenum2?>条</a></td>
				<td class="tac"><input name="CS_YID<?php echo $row2['CS_ID']?>" type="checkbox" value="ok" <?php if($row2['CS_YID']==0) echo 'checked';?>></td>
				<td>
				        <a href="javascript:Popsboxif('分类编辑',450,470,'class_add.php?id=<?php echo $row2['CS_ID']?>');" class="mr10 J_dialog" title="分类编辑">[编辑]</a>
				        <a style="cursor:pointer;"  onclick="del(<?php echo $row2['CS_ID']?>);" class="mr10 J_ajax_del">[删除]</a>
				</td>
			</tr>
<?php
			}
		}
                if($ernum>0) echo '</tbody>';
 	} 
?>
	</table>
</div>
<div class="btn_wrap">
	<div class="btn_wrap_pd">
		<button class="btn btn_submit mr10 J_ajax_submit_btn" id="addlist" type="submit">提交</button>
                <div class="tips_error" id="Msg"></div>
	</div>
</div>	
</form>
<!-- end -->
</div>
<?php
   if($ac=='edit'){   //修改
        global $db;
   	$CS_ID=CS_RequestBox("CS_ID");
	$ID=explode(",",$CS_ID);
		for($i=0;$i<count($ID);$i++){
			$CS_Name=CS_Request("CS_Name".$ID[$i]);
			$CS_AliasName=CS_Request("CS_AliasName".$ID[$i]);
			$CS_Template=CS_Request("CS_Template".$ID[$i]);
			$CS_XID=CS_Request("CS_XID".$ID[$i]);
			$CS_YID=CS_Request("CS_YID".$ID[$i]);

                        if(empty($CS_XID)) $CS_XID=0;
                        if($CS_YID!='ok'){
                            $CS_YID=1;
                        }else{
                            $CS_YID=0;
                        }
                        if(!empty($CS_Name)&&!empty($CS_AliasName)&&!empty($CS_Template)){

				$sql="update ".Getdbname('class')." set CS_Name='".$CS_Name."',CS_AliasName='".$CS_AliasName."',CS_XID=".$CS_XID.",CS_Template='".$CS_Template."',CS_YID=".$CS_YID." where CS_ID=".$ID[$i]."";
				$db->query($sql);
                        }
		}

      echo '<script>document.getElementById("Msg").className="tips_success";$("#Msg").text("恭喜您,修改成功！").css("color","#026E12");</script>';
      echo "<script>setTimeout(\"window.location.href ='class.php';\",2000);</script>";
   }
   if($ac=='del'){   //删除
       $CS_ID=CS_Request("id");
		//判断有无子级
		$sql2="select count(CS_ID) as cc from ".Getdbname('class')." where CS_FID=$CS_ID";
		$row2=$db->getRow($sql2);
		if($row2){
			if($row2['cc']>0){
                           echo '<script>$("#Msg").text("抱歉,该分类下有子分类不能删除！").css("color","red");</script>';
                           exit();
			}
		}		
		$sqlstr="select count(CS_ID) as dcount from ".Getdbname('dance')." where CS_CID=$CS_ID";
		$row=$db->getRow($sqlstr);
		if($row){
			if($row['dcount']>0){
                           echo '<script>$("#Msg").text("抱歉,该分类下有数据不能删除！").css("color","red");</script>';
                           exit();
			}
		}
                $sql="delete from ".Getdbname('class')." where CS_ID=".$CS_ID;
		if($db->query($sql)){
                     echo '<script>document.getElementById("Msg").className="tips_success";$("#Msg").text("恭喜您,删除成功！").css("color","#026E12");</script>';
                     echo "<script>setTimeout(\"window.location.href ='class.php';\",2000);</script>";
                }else{
                     echo '<script>$("#Msg").text("抱歉,删除失败！").css("color","red");</script>';
                }
   }
?>
</body>
</html>