<?php
    $conf_basedir	= "../../";	//相对主目录的位置
    $conf_where		= "admin";	//选择调用相应模块
    $directory		= basename(dirname(__FILE__));	//定义当前所在的目录，引入模块公用函数
    include_once($conf_basedir."base.inc.php");
    checkAuth('32');	//判断登录和权限


	if ($actiontype) doaction($actiontype,$actionid);
    $css="<link href='http://esfadmin.house365.com/script/css/autocomplete.css' rel='stylesheet' type='text/css' />";
    $js = "<script type='text/javascript' src='http://esfadmin.house365.com/script/js/showpic.js'></script>
    <script type='text/javascript' src='http://esfadmin.house365.com/script/js/public.js'></script>
   <script type='text/javascript' src='http://esfadmin.house365.com/script/js/jquery_ui_autocomplete/ui/jquery.ui.core.js'></script>
   <script type='text/javascript' src='http://esfadmin.house365.com/script/js/jquery_ui_autocomplete/ui/jquery.ui.position.js'></script>
   <script type='text/javascript' src='http://esfadmin.house365.com/script/js/jquery_ui_autocomplete/ui/jquery.ui.widget.js'></script>
   <script type='text/javascript' src='http://esfadmin.house365.com/script/js/jquery_ui_autocomplete/ui/jquery.ui.autocomplete.js'></script>";
    $tbl='block';

    if($action == 'xiala')
{
	$line = trim(urldecode($line));
	$line = iconv('UTF-8','GBK',$line);

	$type = intval($type);
	
	$select_sql = $cond_type = $cond_line = "";
	if($type > 0){
		$cond_type = " type = ".$type." AND ";
	}
	

		$cond_line = " line LIKE '".$line."%'";
		$select_sql = "distinct(line)";
	


	$str = "SELECT $select_sql FROM traffic WHERE $cond_type $cond_line ORDER BY bid ASC LIMIT 10";

    $data = $db->GetAll($str);

    if (empty($data)) {
        $ss.="<span onselect=\"this.text.value ='$blockname';$('bi_blockid').value=$id\">请重新输入关键字！未找到相关的线路！</span>";
        echo $ss;
        exit;
    }
	else
	{
		foreach($data as $val) {
			$line = $val[line];
			$ss .=  "<span onselect=\"this.text.value ='$line';getStation('$line');\">".$line."</span>";
		}
		echo $ss;
	}
    exit;
}
else if($action == 'getstation')
{
	$line = trim(urldecode($line));
	$line = iconv('UTF-8','GBK',$line);

	$sql = "SELECT bid,type,tofor,station FROM traffic WHERE line = '".$line."' ORDER BY tofor,sid ASC ";
	$station_arr = $dbback->GetAll($sql);
	
	$station = array();
	$up_num = 0;
	$down_num = 0;
	$single_num =0;
	$sub_num =0;
	for( $i = 0;$i < count($station_arr);$i++){
		$tofor	= $station_arr[$i]['tofor'];
		$bid	= $station_arr[$i]['bid'];
		$type	= $station_arr[$i]['type'];
		if( $type == 1){
			if($tofor==1){
				$station[$tofor][$up_num]['bid'] = $station_arr[$i]['bid'];
				$station[$tofor][$up_num]['station'] = $station_arr[$i]['station'];
				$up_num ++;
			}elseif($tofor==2){
				$station[$tofor][$down_num]['bid'] = $station_arr[$i]['bid'];
				$station[$tofor][$down_num]['station'] = $station_arr[$i]['station'];
				$down_num++;
			}elseif($tofor==3){
				$station[$tofor][$single_num]['bid'] = $station_arr[$i]['bid'];
				$station[$tofor][$single_num]['station'] = $station_arr[$i]['station'];
				$single_num++;
			}
		}else{
			$station[4][$sub_num]['bid'] = $station_arr[$i]['bid'];
			$station[4][$sub_num]['station'] = $station_arr[$i]['station'];
			$sub_num ++;
		}
	}
	$ss = '';
	if($station)
	{
		foreach($station as $key=>$arr)
		{
			//$ss .= $key == 1 ? "上行线路" : ($key == 2 ? "下行线路" : "");
			if($key == 1)
			{
				$ss.="<font style='color:red;'>上行线路</font>";
				foreach($arr as $val)
				{
				$ss .= "<input type='checkbox' style='width:18px;' name='station[]' value='".$val['bid']."'>".$val['station']."&nbsp;";
				}
                $ss.="<br/>";
				
			}

			if($key == 2)
			{
				$ss.="<font style='color:red;'>下行线路</font>";
				$array=array_reverse($arr); 
				foreach($array as $val)
				{   
					$ss .= "<input type='checkbox' style='width:18px;' name='station[]' value='".$val['bid']."'>".$val['station']."&nbsp;";
				}
			    $ss.="<br/>";
			}

			if($key == 3)
			{
				$ss.="<font style='color:red;'>单行或循环线路</font>";
				$array=array_reverse($arr); 
				foreach($array as $val)
				{   
					$ss .= "<input type='checkbox' style='width:18px;' name='station[]' value='".$val['bid']."'>".$val['station']."&nbsp;";
				}
				$ss.="<br/>";
			}

			if($key == 4)
			{
				$ss.="<font style='color:red;'>轨道交通线路</font>";
				$array=array_reverse($arr); 
				foreach($array as $val)
				{   
					$ss .= "<input type='checkbox' style='width:18px;' name='station[]' value='".$val['bid']."'>".$val['station']."&nbsp;";
				}
				$ss.="<br/>";
			}
		}
	}
	else
	{
		$ss = '该线路还没有添加站点！';
	}
	
	echo $ss;
	exit;
}
else if($action == 'getstation2')
{
	$line = trim(urldecode($line));
	$line = iconv('UTF-8','GBK',$line);
	$type = intval($type);

	$sql = "SELECT bid,line,type,tofor,station FROM traffic WHERE station = '".$line."' and type = $type ORDER BY bid Asc,tofor ASC ";
	$station_arr = $dbback->GetAll($sql);

	$station = array();
	$up_num = 0;
	$down_num = 0;
	$single_num =0;
	$sub_num =0;
	for( $i = 0;$i < count($station_arr);$i++){
		$tofor	= $station_arr[$i]['tofor'];
		$bid	= $station_arr[$i]['bid'];
		$type	= $station_arr[$i]['type'];
		$line	= $station_arr[$i]['line'];
		if( $type == 1){
			$station[$line][$sub_num]['bid']		= $station_arr[$i]['bid'];
			$station[$line][$sub_num]['station']	= $station_arr[$i]['station'];
			$station[$line][$sub_num]['line']		= $station_arr[$i]['line'];
			$station[$line][$sub_num]['tofor']		= $station_arr[$i]['tofor'];
			$sub_num ++;
		}else{
			$station[4][$sub_num]['bid']		= $station_arr[$i]['bid'];
			$station[4][$sub_num]['station']	= $station_arr[$i]['station'];
			$station[4][$sub_num]['line']		= $station_arr[$i]['line'];
			$sub_num ++;
		}
	}
	$ss = "";
	if($station)
	{
		foreach($station as $key=>$arr)
		{
			$ss.="<div style='float:left;width:100%;padding:3px 0;border-bottom:1px dashed #CCC;' onmouseover='this.style.background=\"#DDD\";' onmouseout='this.style.background=\"#FFF\";'><div style='width:120px;float:left;'>&nbsp;&nbsp;".$key."</div>";
			$array=array_reverse($arr); 
			foreach($array as $val)
			{   
				$tofor = $val['tofor'] == 1 ? "【上】" : ($val['tofor'] == 2 ? "【下】" : "");
				$ss .= "<div style='width:160px;float:left;'><input type='checkbox' style='width:18px;' name='station[]' value='".$val['bid']."' />".$val['station'].$tofor."</div>";
			}
			$ss.="</div>";
		}

		$ss .= "<div style='width:100%;float:left;text-align:right;padding:3px 0;'><input type='checkbox' id='chkall' onclick='checkallbyname(\"station[]\")' />&nbsp;选择全部</div>";
	}
	else
	{
		$ss = '该线路还没有添加站点！';
	}
	echo $ss;
	exit;
}
else if($action == 'submitstation')
{
	$type = intval($type);
	$blockid = intval($blockid);

	if(is_array($station) && !empty($station))
	{
		$sarr = $dbback->GetAll("select t.bid from traffic_block tb left join traffic t on tb.bid = t.bid where tb.type = '".$type."' and tb.blockid = '".$blockid."'");
		
		$block_station_arr = array();
		if($sarr)
		{
			foreach($sarr as $val)
			{
				$block_station_arr[] = $val['bid'];
			}
		}

		foreach($station as $val)
		{
			if(!in_array($val,$block_station_arr))
			{
				$db->Execute("insert into traffic_block (type,bid,blockid) values ('".$type."','".$val."','".$blockid."')");
			}
		}

		echo "<script>alert('站点设置成功！');window.opener.location.reload();window.location.href='block_traffic_modify.php?blockid=".$blockid."&type=".$type."'</script>";
	}
	else
	{
		echo "<script>alert('站点未选择！');window.location.href='block_traffic_modify.php?blockid=".$blockid."&type=".$type."'</script>";
	}
	exit;
}
else if($action == 'delstation')
{
	$id = intval($id);

	$db->Execute("delete from traffic_block where id = '".$id ."'");
	echo "<script>alert('站点删除成功！');window.opener.location.reload();window.close();</script>";
}
else
{
	$title = "小区交通信息修改";
	$css = "<link href='http://esfadmin.house365.com/script/autocomplete/styles/autocomplete.css' rel='stylesheet' type='text/css' />";
	$js = "<script type='text/javascript' src='http://esfadmin.house365.com/script/autocomplete/scripts/autocomplete.js'></script>";
	$empty = array("" => "*请选择*");
}
    

    //引用模版文件
    include_once(Template("header"));
    include_once(Template("block/block_traffic_modify"));
    include_once(Template("footer"));
?>