<?php
class cls_tab_sys_area extends cls_base_table
{
	private $tab_area;
	private $tab_areaattribute;
	private $obj_db;
	//构造函数
	function __construct()
	{
		global $obj_db;
		parent::__construct();
		$this->obj_db=$obj_db;
		$this->tab_area=DB_PRE."sys_area";
	}
	function on_save($arr_fields=array())
	{
		$arr_return=array("err"=>"");
		(!isset($arr_fields["area_id"]))? $arr_fields["area_id"]=0:$arr_fields["area_id"]=intval($arr_fields["area_id"]);
		$lng_id=$arr_fields["area_id"];
		unset($arr_fields["area_id"]);//释放id
		if($lng_id<1){
			$arr_return=self::_insert($arr_fields);
		}else{
			$str_where="area_id='".$lng_id."'";
			$arr_return=self::_update($arr_fields,$str_where);
			$arr_return["id"]=$lng_id;
		}

		return $arr_return;
	}
	/*添加
	arr_fields:关健字数组
	*/
	function _insert($arr_fields)
	{
		$arr_return=array("err"=>"","id"=>0);
		if(!isset($arr_fields["area_name"])||$arr_fields["area_name"]==""){
			$arr_return['err']="请输入地区名！";
			return $arr_return;
		}
		$obj_result = $this->obj_db->query("SELECT * FROM ".DB_PRE."sys_area where area_name='".$arr_fields["area_name"]."' and area_pid='".$arr_fields["area_pid"]."'");
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$arr_return['err']="输入地区名已存在！";
			return $arr_return;
		}
		$arr_return=parent::on_insert($this->tab_area,$arr_fields);
		return $arr_return;
	}
	function _update($arr_fields,$msg_where)
	{
		$arr_return=array("err"=>"");
		$arr_return=parent::on_update($this->tab_area,$arr_fields,$msg_where);
		return $arr_return;
	}
	function on_del($arr_id)
	{
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要删除的记录！";
			return $arr_return;
		}
		//当删除分类时删除下级分类
		$arr_id_next=array();
		$str_sql="select area_id from ".$this->tab_area." where area_pid in (".$str_id.")";
		$obj_result = $this->obj_db->query($str_sql);
		while($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$arr_id_next[]=$obj_rs["area_id"];
		}
		if(count($arr_id_next)>0)
		{
			$arr_return=self::on_del($arr_id_next);
		}
		$arr_return=parent::on_del($this->tab_area,"area_id in(".$str_id.")");
		return $arr_return;
	}
	function get_select_area($msg_name,$msg_area="")
	{
		
		$str_sql="select * from ".$this->tab_area." where area_pid=0";
		$obj_result = $this->obj_db->query($str_sql);
		$str_area="";
		while($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$str_area.="<option value='".$obj_rs["area_id"]."'>".$obj_rs["area_name"]."</option>";
		}
		if($msg_area==""){
			$str_button="";
			$str_input="style='display:none'";
			$str_select="";
		}else{
			$str_select="style='display:none'";
			$str_input="";
			$str_button="<a href=\"javascript:on_area_edit('id_area_".$msg_name."');\">编辑</a>";
		}
		$str_area="<span id='id_area_".$msg_name."'><input type='hidden' name='".$msg_name."' id='".$msg_name."' value='".$msg_area."'><span ".$str_input.">".$msg_area."</span><select onchange=\"get_area_select_1('id_area_".$msg_name."',this.value,1);\" ".$str_select."><option value=''>--请选择--</option>".$str_area;
		$str_area.="</select> ".$str_button."</span>";
		return $str_area;
	}
	function get_next_area($msg_id)
	{		
		$str_sql="select * from ".$this->tab_area." where area_pid='".$msg_id."'";
		$obj_result = $this->obj_db->query($str_sql);
		$str_area="";
		$arr_area=array();
		$lng_lay=-1;
		while($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			if($lng_lay<1){
				$str_pids=$obj_rs["area_pids"];
				$arr_pids=explode(",",$str_pids);
				$lng_lay=count($arr_pids);
			}
			$arr_area[]=$lng_lay.",".$obj_rs["area_id"]."=".$obj_rs["area_name"];
		}
		$str_area=implode("|",$arr_area);
		return $str_area;
	}
}
?>
