<?php
if(!$_SESSION['admin_id']){
	pe_goto("index.php");
}
$menumark = "category_list";
switch ($act) {
	//#####################@ 增加分类 @#####################//
	case 'add':
		pe_lead('hook/category.hook.php');
		$category_treelist = category_treelist();
		//套餐列表
		$package_list = $db->pe_selectall('package');

		$seo = pe_seo('增加分类', '', '', 'admin');
		$action = "admin.php?mod=category&act=addsql";
		include(pe_tpl('category_add.html'));
	break;
	//#####################@ 增加分类sql @#####################//
	case 'addsql':
		if ($db->pe_insert('category', $_p_info)) {
			//更新缓存
			cache::write('category', $db->index('category_id')->pe_selectall('category', array('order by'=>'category_order asc'), '*'));
			cache::write('category_arr', $db->index('category_pid|category_id')->pe_selectall('category', array('order by'=>'category_order asc'), '*'));
			pe_success('分类增加成功!', "admin.php?mod=category&act=list");
		}
		else {
			pe_error('分类增加失败!');
		}
	break;
	//#####################@ 修改分类 @#####################//
	case 'edit':
		$info = $db->pe_select('category', array('category_id'=>$_g_id));
		pe_lead('hook/category.hook.php');
		$category_treelist = category_treelist();
		//不允许移动到的分类id数组
		$category = new category();
		$category_noid = $category->getcid_arr($category_treelist, $info['category_id']);
		$category_noid[] = $info['category_id'];

		//套餐列表
		$package_list = $db->pe_selectall('package');

		$seo = pe_seo('修改分类', '', '', 'admin');
		$action = "admin.php?mod=category&act=editsql&id={$_g_id}";
		include(pe_tpl('category_add.html'));
	break;
	//#####################@ 修改分类sql @#####################//
	case 'editsql':
		if(empty($_p_info['vip']))$_p_info['vip']=0;
		if ($db->pe_update('category', array('category_id'=>$_g_id), $_p_info)) {
			//更新缓存
			cache::write('category', $db->index('category_id')->pe_selectall('category', array('order by'=>'category_order asc'), '*'));
			cache::write('category_arr', $db->index('category_pid|category_id')->pe_selectall('category', array('order by'=>'category_order asc'), '*'));
			pe_success('分类修改成功!', "admin.php?mod=category&act=list");
		}
		else {
			pe_error('分类修改失败!');
		}
	break;
	//#####################@ 分类排序sql @#####################//
	case 'ordersql':
		foreach ($_p_category_order as $k=>$v) {
			$result = $db->pe_update('category', array('category_id'=>$k), array('category_order'=>$v));
		}
		if ($result) {
			//更新缓存
			cache::write('category', $db->index('category_id')->pe_selectall('category', array('order by'=>'category_order asc'), '*'));
			cache::write('category_arr', $db->index('category_pid|category_id')->pe_selectall('category', array('order by'=>'category_order asc'), '*'));
			pe_success('分类排序成功!');
		}
		else {
			pe_error('分类排序失败!');
		}
	break;
	//#####################@ 分类删除 @#####################//
	case 'delsql':
		if ($db->pe_delete('category', array('category_id'=>is_array($_p_category_id) ? $_p_category_id : $_g_id))) {
			//更新缓存
			cache::write('category', $db->index('category_id')->pe_selectall('category', array('order by'=>'category_order asc'), '`category_id`,`category_pid`,`category_name`,`category_order`'));
			cache::write('category_arr', $db->index('category_pid|category_id')->pe_selectall('category', array('order by'=>'category_order asc'), '`category_id`,`category_pid`,`category_name`,`category_order`'));
			pe_success('分类删除成功!');
		}
		else {
			pe_error('分类删除失败...');
		}
	break;
	//#####################@ 分类列表 @#####################//
	default :
		//套餐列表
		$cache_package = cache::get('package');

		pe_lead('hook/category.hook.php');
		$info_list = category_treelist();
		$seo = pe_seo('课程分类', '', '', 'admin');
		include(pe_tpl('category_list.html'));
	break;
}
pe_result();
?>