<?php
require_once(dirname(__FILE__)."/config.php");
$db = new DedeSql(false);
if($action == 'add')
{
	$name = trim($name);
	if($name == ''){
		ShowMsg('分类名称不能为空','asktype.php');
		exit();
	}
	$sql = "insert into #@__asktype (name, reid) values ('$name', $reid);";

	$db->SetQuery($sql);
	if($db->ExecuteNoneQuery())
	{
		ShowMsg('添加分类成功，将返回分类管理页面','asktype.php');
		exit();
	}else
	{
		ShowMsg('添加分类失败，将返回分类管理页面','asktype.php');
		exit();
	}

}elseif($action == 'edit')
{
	if($step != 2){
		$sectorscache = '<option value="0">无(作为一级分类)</option>';
		$sql = "select * from #@__asktype where id=$id";
		$db->SetQuery($sql);
		$sector = $db->GetOne();
		$sql = "select * from #@__asktype where reid=0 and id!=$id order by disorder asc, id asc";
		$db->SetQuery($sql);
		$db->Execute();
		while($topsector = $db->GetArray())
		{
			$check = '';
			if($sector['reid'] != 0 && $topsector['id'] == $sector['reid'])
			{
				$check = 'selected';
			}
			$sectorscache .= '<option value="'.$topsector['id'].'" '. $check.'>'.$topsector['name'].'</option>';
		}
		include(dirname(__FILE__)."/templets/asktype.htm");

	}else{
		$sql = "update #@__asktype set name='$name', reid=$reid, disorder=$disorder where id=$id";
		$db->SetQuery($sql);
		if($db->ExecuteNoneQuery())
		{
			ShowMsg('编辑分类成功，将返回分类管理页面','asktype.php');
		}else
		{
			ShowMsg('编辑分类成功，将返回分类管理页面','asktype.php');
		}
	}

}elseif($action == 'update')
{
	//print_r($names);exit;
	$errinfo = '';
	foreach($disorders as $key => $disorder)
	{
		$sql = "update #@__asktype set disorder=$disorder, name='$names[$key]' where id=$key";
		$db->SetQuery($sql);
		if(!$db->ExecuteNoneQuery())
		{
			$errinfo .= $sql."\n";
		}
	}
	if(trim($errinfo)  != '' )
	{
		ShowMsg($errinfo,'asktype.php');
	}else
	{
		ShowMsg('更新分类成功，将返回分类管理页面','asktype.php');
	}

}elseif($action == 'delete')
{
	if($step != 2)
	{
		include(dirname(__FILE__)."/templets/asktype.htm");
	}else
	{
		$id = intval($id);
		if($id < 1)
		{
			ShowMsg('分类编号不正确，将返回分类管理页面','asktype.php');
		}else
		{
			$sql = "delete from #@__asktype where id=$id or reid=$id";
			$db->SetQuery($sql);
			if($db->ExecuteNoneQuery())
			{
				ShowMsg('删除分类成功，将返回分类管理页面', 'asktype.php');
			}else
			{
				ShowMsg('删除分类失败，将返回分类管理页面 ','asktype.php');
			}
		}
	}

}else{
	$sectors = $topsectors = $subsectors = array();
	$sectorscache = '';

	$sql = "select * from #@__asktype order by disorder asc, id asc";
	$db->SetQuery($sql);
	$db->Execute();
	while($row = $db->GetArray())
	{
		//print_r($row);
		if($row['reid'] == 0)
		{
			$topsectors[] = $row;
		}else
		{
			$subsectors[] = $row;
		}
	}
	foreach($topsectors as $topsector)
	{
		$sectors[] = $topsector;
		$sectorscache .= '<option value="'.$topsector['id'].'">|- '.$topsector['name'].'</option>';
		foreach($subsectors as $subsector)
		{
			if($subsector['reid'] == $topsector['id'])
			{
				$sectors[] = $subsector;
			}
		}
	}
	include(dirname(__FILE__)."/templets/asktype.htm");
}












?>