<?php
	/*
	* 使用方法 MinusVtime($a,$b)
	* 機能: 時間差を計算する
	* 入力: 0:00~23:59という形式の時刻データを2つ
	* 出力: 1なら1分 100ならば1時間という形式で出す
	*/
	function MinusVtime($a,$b){
		return date("Hi", strtotime($a) - strtotime($b) - 60*60*9);
	}

	/*
	* 使用方法 Timeadd($a,$b)
	* 機能: 時間を加算する
	* 入力: 0:00~23:59という形式の時刻データ($a) 加算する数値($b)
	* 出力: $aに$bを足して0:00の形式で時刻を出す
	*/
	function Timeadd($a,$b){
		$a=str_replace(":","",$a);
		for($i=0;$i<$b;$i++){
			$a++;
			if($a%100>=60){
				$a+=40;
			}
			if($a==2400){
				$a=0;
			}
		}
		if(floor($a/1000)!=0){
			$a = (floor($a/1000).floor(floor($a-floor($a/1000)*1000)/100).':'.floor(($a-floor($a/100)*100)/10).floor(($a-floor($a/100)*100)%10));
		}else{
			$a = (floor(floor($a-floor($a/1000)*1000)/100).':'.floor(($a-floor($a/100)*100)/10).floor(($a-floor($a/100)*100)%10));
		}
		return $a;
	}

	//0101という形式の日付データ
	$today = date("md");

	$r_id = $_GET['r_id'];
	$r_id2="";
	if($r_id<10){
		$r_id2=0;
	}

	//各ルートの運行状態(0:運行終了,1:運行中,2:運行準備,3:点検など,4:運休)
	$status = rtrim(file_get_contents("./maintenance/data/location/status".$r_id2.$r_id.".txt", FILE_IGNORE_NEW_LINES));

	//現在の便番号
	$now_bin = rtrim(file_get_contents("./maintenance/data/location/bin".$r_id2.$r_id.".txt", FILE_IGNORE_NEW_LINES));

	if($r_id==9){
		//現在のバス停通過番号
		$bus_flag = rtrim(file_get_contents("./maintenance/data/location/bus_flag".$r_id2.$r_id.".txt", FILE_IGNORE_NEW_LINES));
	}

	//現在のバス停通過番号
	$now_pass = rtrim(file_get_contents("./maintenance/data/location/pass_bus".$r_id2.$r_id.".txt", FILE_IGNORE_NEW_LINES));

	if($r_id==9 && $status==2 && $now_bin==4){
		$now_pass=28;
	}else if($r_id==9 && $status==2 && $now_pass!=0 && $now_bin!=4){
		$now_pass=0;
	}else if($status==2 && $now_pass!=0){
		$now_pass=0;
	}
	if($now_pass==0){
		$check_bus=1;
	}else{
		$check_bus=0;
	}

	if($_GET['debug']==1){
		/*---------送信されたデータを出力-------*/
		print('<br>'.' 現在の運行状況:'.$status.'<br>');
		print('<br>'.' 現在の便番号:'.$now_bin.'<br>');
		if($r_id==9){
			print('<br>'.' 現在の方向:'.$bus_flag.'<br>');
		}
		print('<br>'.' 現在のバス停通過番号:'.$now_pass.'<br><br>');
		/*------------------------------------------*/
	}

	/*------------走行ルートの抽出------------*/
	$cr = array(" 着"," 発");
	$con=@mysql_connect("localhost","event","L523GpA");
	if(!$con){
		unset($con,$data,$r_id,$lat,$lng,$time,$day);
		die("DB接続エラー");
	}
	$select_db=@mysql_select_db("eventdb");
	if(!$select_db){
		unset($con,$select_db,$r_id,$lat,$lng,$time,$day);
		die ("DB選択エラー");
	}
	@mysql_query("set names utf8");
	//$sql="select busstop_id,bus_name from nbus_master where r_id=".$r_id." and busstop_id = ".$now_pass."
	//	  union 
	//	  select busstop_id,bus_name from nbus_master inner join nbus_view using(r_id,busstop_id) where r_id=".$r_id." and view_bin=".$now_bin." and busstop_id > ".$now_pass."  and view_fg=1 group by bus_name order by busstop_id asc";

	$sql="select busstop_id,bus_name from nbus_master inner join nbus_view using(r_id,busstop_id) where r_id=".$r_id." and view_bin=".$now_bin." and view_fg=1 order by busstop_id asc";
	$res = mysql_query($sql);
	for($i = 1;$row = mysql_fetch_array($res);$i++){
		$stop_id[$i]=intval($row['busstop_id']);
		$stop_name[$i]=str_replace($cr,"",$row['bus_name']);
		if($_GET['debug']==2){
			echo $stop_id[$i]."<br>";
			echo $stop_name[$i]."<br>";
		}
	}
	$array_key=array_search($now_pass,$stop_id);
	mysql_free_result($res);
	$close=@mysql_close($con);
	if(!$close){
		unset($con,$select_db,$res,$sql,$row,$close,$r_id,$lat,$lng,$time,$day);
		die("DB切断エラー");
	}
	unset($con,$select_db,$res,$sql,$row,$close);
	/*------------------------------------------*/

	/*------------通過ログのチェック------------*/
	$con=@mysql_connect("localhost","event","L523GpA");
	if(!$con){
		unset($con,$data,$r_id,$lat,$lng,$time,$day);
		die("DB接続エラー");
	}
	$select_db=@mysql_select_db("eventdb");
	if(!$select_db){
		unset($con,$select_db,$r_id,$lat,$lng,$time,$day);
		die ("DB選択エラー");
	}
	@mysql_query("set names utf8");
	$sql="select log_lat,log_lng,log_time from nbus_log where r_id=".$r_id." and busstop_id = ".$now_pass." and log_day=".$today;
	$res = mysql_query($sql);
	$log_data[0] = @mysql_fetch_array($res);//ログデータ
	$delay_fg=1;
	if($log_data[0]['log_time']!=null){
		if($log_data[0]['log_time'][0]!=0){
			$log_time=$log_data[0]['log_time'][0].$log_data[0]['log_time'][1].":".$log_data[0]['log_time'][2].$log_data[0]['log_time'][3];
		}else{
			$log_time=$log_data[0]['log_time'][1].":".$log_data[0]['log_time'][2].$log_data[0]['log_time'][3];
		}
		if($_GET['debug']==1){
			echo "通過時刻:".$log_time."<br>";
		}
	}else{
		$delay_fg=0;
	}
	mysql_free_result($res);
	$close=@mysql_close($con);
	if(!$close){
		unset($con,$select_db,$res,$sql,$row,$close,$r_id,$lat,$lng,$time,$day);
		die("DB切断エラー");
	}
	unset($con,$select_db,$res,$sql,$row,$close);
	/*------------------------------------------*/

	/*------------時刻データの取出し------------*/
	$con=@mysql_connect("localhost","event","L523GpA");
	if(!$con){
		unset($con,$data,$r_id,$lat,$lng,$time,$day);
		die("DB接続エラー");
	}
	$select_db=@mysql_select_db("eventdb");
	if(!$select_db){
		unset($con,$select_db,$r_id,$lat,$lng,$time,$day);
		die ("DB選択エラー");
	}
	@mysql_query("set names utf8");
	if($now_pass>0){
		$sql="select time from ntime_tbl where r_id=".$r_id." and busstop_id in (".$stop_id[$array_key].",".$stop_id[($array_key+1)].") and busservice_id=".$now_bin." order by busstop_id asc";
	}else{
		$sql="select time from ntime_tbl where r_id=".$r_id." and busstop_id in (".$stop_id[($array_key+1)].") and busservice_id=".$now_bin." order by busstop_id asc";
	}
	$res = mysql_query($sql);
	for($i = 0;$row = mysql_fetch_array($res);$i++){
		$stop_time[$i]=$row['time'];//0:現在の通過バス停,1:次のバス停
		if($r_id==9 && $now_bin==4){
			$data_array=explode(",", $stop_time[$i]);
			$stop_time[$i]=$data_array[$bus_flag];
		}
		if($_GET['debug']==1){
			echo "時刻表の時刻:".$stop_time[$i]."<br>";
		}
	}
	mysql_free_result($res);
	$close=@mysql_close($con);
	if(!$close){
		unset($con,$select_db,$res,$sql,$row,$close,$r_id,$lat,$lng,$time,$day);
		die("DB切断エラー");
	}
	unset($con,$select_db,$res,$sql,$row,$close);
	/*------------------------------------------*/
	if($log_time==null){
		$log_time=$stop_time[0];
	}
	if($delay_fg==1){
		if($log_time>$stop_time[0]){
			if($_GET['debug']==1){
				echo "遅延<br>";
			}
			$delay_time=intval(MinusVtime($log_time,$stop_time[0]));//遅延時間
		}else{
			if($_GET['debug']==1){
				echo "早発<br>";
			}
			$delay_time=(intval(MinusVtime($stop_time[0],$log_time))*-1);//早発時間
		}
		$delay_time=$delay_time-(40*(($delay_time-($delay_time%100))/100));
	}else{
		$delay_time=0;
	}
	if($status==1){
		if($now_pass>0){
			//print("「".$array_key.":".$stop_name[$array_key]."→".($array_key+1).":".$stop_name[($array_key+1)]."」 間を走行中,(".Timeadd($stop_time[1],$delay_time)." 到着予定)");
			print("「".$stop_name[$array_key]."→".$stop_name[($array_key+1)]."」 間を走行中,(".Timeadd($stop_time[1],$delay_time)." 到着予定)");
		}else{
			//print("「".($array_key+1).":".$stop_name[($array_key+1)]."」 へ向かっています。,(".$stop_time[0]." 到着予定)");
			print("「".$stop_name[($array_key+1)]."」 へ向かっています。,(".$stop_time[0]." 到着予定)");
		}
	}else if($status==2){
		print("現在、運行準備中です。次の便は、,".$stop_name[($array_key+1)]."を".$stop_time[0]."に出発予定です。");
	}
	unset($stop_time,$r_id,$now_pass,$now_bin,$stop_id,$stop_name,$today,$status,$log_data,$check_bus,$route_data)
?>