<?php
class sitesControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	public function onDefault(){
		$this->smarty->display("sites/index.html");
	}
	public function oncityData(){
		
		$siteList=M('sites')->select(array(
			"where"=>"  is_open=1   ",
			"order"=>"siteid DESC"
		));
		$site=M('sites')->selectRow("siteid=".SITEID);
		$sdata=array(
			 
			"siteList"=>$siteList,
			"site"=>$site
		); 
		$this->goAll("success",0,$sdata);
	}
	
	public function onSchool(){
		
		 
		$this->smarty->display("sites/school.html");
		 
	}
	public function onSchoolData(){
		$options=M('options')->children(0,"sitearea",1);
		$provinceid=0;
		if($options){
			$provinceid=$options[0]["id"];
			foreach($options as $k=>$v){
				if($v['child']){
					foreach($v['child'] as $kk=>$vv){
						$vv['sites']=M('sites')->select(array(
							"where"=>"cityid=".$vv['id']." AND is_open=1   ",
							"order"=>"siteid DESC"
						));
						$v['child'][$kk]=$vv;
					}
				}
				$options[$k]=$v;
			}
		}
		 
		$site=M('sites')->selectRow("siteid=".SITEID);
		$sdata=array(
			"provinceid"=>$provinceid,
			"citys"=>$options,
			"site"=>$site
		);
		 $this->goAll("success",0,$sdata);
	}
	public function onSearch(){
		$keyword=get('keyword','h');
		if(empty($keyword)){
			$this->goAll("找不到",1);
		}
		$data=M("sites")->select(array(
			"where"=>" sitename like '%".$keyword."%' AND is_open=1",
			"limit"=>10
		));
		if($data){
			$this->goAll("success",0,$data);
		}else{
			$this->goAll("找不到",1);
		}
	}
	
	public function onNear(){
		$data=M("sites")->select(array("where"=>" is_open=1"));
		$lat=get('lat','h');
		$lng=get('lng','h');
		 
		if($data){
			foreach($data as $k=>$v){
				$distance[$k]=$v['distance']=distanceByLnglat($lng,$lat,$v['lng'],$v['lat']);
			}
			array_multisort ( $distance ,  SORT_ASC  ,  $data );
			$this->goAll("成功",0,$data[0]);
		}else{
			$this->goALl("error",1);
		}
	}
	
	public function onGet(){
		$siteid=max(1,get('siteid','i'));
		$data=M("sites")->selectRow("siteid=".$siteid);
		$this->goALl("success",0,$data);
	}
	public function onSetSite(){
		$siteid=get('siteid','i');
		setcookie("cksiteid",$siteid,time()+36000000,"/",DOMAIN);
	}
	
}
