<?php
		$r_id = $_GET['r_id'];
		$mord = $_GET['mord'];
		$l_id = $_GET['l_id'];
		$pointNo = $_GET['p_id'];
		
		//DB接続
		$con = @mysql_connect("localhost","event","L523GpA");
		if(!$con){
			unset($con);
			die("1");
		}
		$db_select = @mysql_select_db("eventdb");
		if(!$db_select){
			unset($con,$db_select);
			die("2");
		}
		mysql_query("set names utf8");

		//バス停データ取得
		$res = @mysql_query("select r_lat,r_lng,p_id from route_bus where l_id = ".$l_id." and r_id = ".$r_id." order by p_id desc");
		if(!$res){
			unset($con,$db_select,$res);
			die("3");
		}
		for($i = 0;$row = mysql_fetch_array($res);$i++){
			$point[$i] = $row['p_id'];
			$ido[$i] = $row['r_lat'];
			$kid[$i] = $row['r_lng'];
		}
		$max = $i;
		//バックアップデータ取得
		$res2 = @mysql_query("select r_lat,r_lng,p_id from route_bus where l_id = ".$l_id." and r_id = 99 order by p_id asc");
		if(!$res2){
			unset($con,$db_select,$res,$res2);
			die("4");
		}
		for($i = 0;$row = mysql_fetch_array($res2);$i++){
			$point2[$i] = $row['p_id'];
			$ido2[$i] = $row['r_lat'];
			$kid2[$i] = $row['r_lng'];
		}
		mysql_free_result($res);
		mysql_free_result($res2);
		$close = @mysql_close($con);
		if(!$close){
			unset($con,$db_select,$res,$res2,$row,$ido,$kid,$point,$ido2,$kid2,$point2);
		}

		//DB接続
		$con = @mysql_connect("localhost","event","L523GpA");
		if(!$con){
			unset($con);
			die("5");
		}
		$db_select = @mysql_select_db("eventdb");
		if(!$db_select){
			unset($con,$db_select);
			die("6");
		}
		mysql_query("set names utf8");
		//追加時の戻る
		if($mord == 1){
			@mysql_query("delete from route_bus where r_id = ".$r_id." and l_id = ".$l_id." and p_id = ".$point2[0]);
		//削除時の戻る
		}else if($mord==2){
			//トランザクションをはじめる準備
			$Query = "set autocommit = 0";
			mysql_query($Query);

			//トランザクション開始
			$Query = "begin";
			mysql_query($Query);
			if($pointNo<=$max){
				$data3 = @mysql_query("update route_bus set r_lat= '".$ido2[0]."',r_lng = '".$kid2[0]."' where r_id = ".$r_id." and l_id = ".$l_id." and p_id = ".$pointNo);
				for($i=$pointNo;$i<$max;$i++){
					$cnt=$i+1;
					$data1 = @mysql_query("update route_bus set r_lat= '".$ido[$i]."',r_lng = '".$kid[$i]."' where r_id = ".$r_id." and l_id = ".$l_id." and p_id = ".$cnt);
				}
				$cnt3=$max+1;
				$data2 = mysql_query("insert into route_bus (r_id,l_id,p_id,r_lat,r_lng) values(".$r_id.",".$l_id.",'".$cnt3."','".$ido[0]."','".$kid[0]."')");
			}else{
				$cnt3=$max+1;
				$data2 = mysql_query("insert into route_bus (r_id,l_id,p_id,r_lat,r_lng) values(".$r_id.",".$l_id.",'".$cnt3."','".$ido2[0]."','".$kid2[0]."')");
			}
			if($data1 === true && $data2 === true && $data3 === true){
			//コミット
				$Query = "commit";
				mysql_query($Query);
			}else{
			//ロールバック
				$Query = "rollback";
				mysql_query($Query);
			}

			unset($max,$i,$data1,$data2,$data3);

		//書き換え時の戻る
		}else if($mord==3){
			
			//トランザクションをはじめる準備
			$Query = "set autocommit = 0";
			mysql_query($Query);

			//トランザクション開始
			$Query = "begin";
			mysql_query($Query);

			$data2 = mysql_query("update route_bus set r_lat = '".$ido2[0]."' where r_id = ".$r_id." and l_id = ".$l_id." and p_id = ".$pointNo);
			$data3 = mysql_query("update route_bus set r_lng = '".$kid2[0]."' where r_id = ".$r_id." and l_id = ".$l_id." and p_id = ".$pointNo);

			if($data2 === true && $data3 === true){
			//コミット
				$Query = "commit";
				mysql_query($Query);
			}else{
			//ロールバック
				$Query = "rollback";
				mysql_query($Query);
			}

			unset($data2,$data3);
		//挿入時の戻る
		}else if($mord==4){
			
			//トランザクションをはじめる準備
			$Query = "set autocommit = 0";
			mysql_query($Query);

			//トランザクション開始
			$Query = "begin";
			mysql_query($Query);

			if($pointNo<=$max){
				$data1 = @mysql_query("delete from route_bus where r_id = ".$r_id." and l_id = ".$l_id." and p_id = ".$pointNo);
				for($i=$max;$i>$pointNo+1;$i--){
					$cnt2=$i-1;
					$data2 = @mysql_query("update route_bus set r_lat= '".$ido[$max-$i]."',r_lng = '".$kid[$max-$i]."' where r_id = ".$r_id." and l_id = ".$l_id." and p_id = ".$point[$max-$cnt2]);
				}
				$data3 = mysql_query("insert into route_bus (r_id,l_id,p_id,r_lat,r_lng) values(".$r_id.",".$l_id.",'".$pointNo."','".$ido[$max-$pointNo-1]."','".$kid[$max-$pointNo-1]."')");
				$data4 = @mysql_query("delete from route_bus where r_id = ".$r_id." and l_id = ".$l_id." and p_id = ".$max);
			}
			
			if($data1 === true && $data2 === true && $data3 === true && $data4 === true){
			//コミット
				$Query = "commit";
				mysql_query($Query);
			}else{
			//ロールバック
				$Query = "rollback";
				mysql_query($Query);
			}
			unset($max,$cnt3,$i,$data1,$data2,$data3,$data4);
		}
		//mysql_query("delete from route_bus where r_id = 99");
		$close = @mysql_close($con);
		if(!$close){
			unset($con,$db_select,$ido,$kid,$point,$ido2,$kid2,$point2);
			die("7");
		}
		unset($con,$db_select,$ido,$kid,$point,$bus2,$ido2,$kid2,$point2);
		die("正常終了");
?>