<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

/*************************************************
 * eTravelSmart Bus Search Demo
 *************************************************/

$username=' ';
$password=' ';

$sourceCity='Hyderabad';
$destinationCity='Bangalore';
$doj=date('Y-m-d',strtotime('+1 day'));

$url="https://staging.etravelsmart.in/etsAPI/api/getAvailableBuses?"
.http_build_query([
"sourceCity"=>$sourceCity,
"destinationCity"=>$destinationCity,
"doj"=>$doj
]);

$ch=curl_init();

curl_setopt_array($ch,[
CURLOPT_URL=>$url,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_HTTPAUTH=>CURLAUTH_DIGEST,
CURLOPT_USERPWD=>"$username:$password",
CURLOPT_CONNECTTIMEOUT=>30,
CURLOPT_TIMEOUT=>60,
CURLOPT_SSL_VERIFYPEER=>true,
CURLOPT_SSL_VERIFYHOST=>2
]);

$response=curl_exec($ch);

if(curl_errno($ch)){
die("cURL Error : ".curl_error($ch));
}

$httpCode=curl_getinfo($ch,CURLINFO_HTTP_CODE);

curl_close($ch);

$data=json_decode($response,true);

?>
<!DOCTYPE html>
<html>
<head>

<meta charset="utf-8">

<title>Available Buses</title>

<style>

*{
box-sizing:border-box;
}

body{
margin:0;
background:#f2f2f2;
font-family:Arial,Helvetica,sans-serif;
}

.header{

background:#0b5ed7;
padding:20px;
color:#fff;
box-shadow:0 2px 10px rgba(0,0,0,.15);

}

.header h1{

margin:0;
font-size:28px;

}

.searchinfo{

margin-top:10px;
font-size:18px;

}

.container{

width:1200px;
margin:25px auto;

}

.summary{

background:#fff;
padding:15px;
border-radius:8px;
margin-bottom:20px;
box-shadow:0 2px 6px rgba(0,0,0,.1);

}

.card{

background:#fff;
border-radius:10px;
padding:20px;
margin-bottom:20px;
box-shadow:0 3px 10px rgba(0,0,0,.08);

}

.row{

display:flex;
justify-content:space-between;
align-items:center;

}

.operator{

font-size:24px;
font-weight:bold;
color:#0b5ed7;

}

.bustype{

margin-top:6px;
color:#666;

}

.time{

font-size:28px;
font-weight:bold;

}

.city{

font-size:13px;
color:#666;

}

.arrow{

font-size:30px;
color:#999;

}

.price{

font-size:34px;
font-weight:bold;
color:#28a745;

}

.seats{

margin-top:8px;
color:#d9534f;
font-weight:bold;

}

.viewbtn{

background:#0b5ed7;
color:#fff;
padding:12px 30px;
border-radius:6px;
text-decoration:none;
display:inline-block;
font-weight:bold;

}

.section{

margin-top:18px;

}

.section h3{

margin-bottom:10px;
color:#0b5ed7;

}

.point{

padding:6px 0;
border-bottom:1px solid #eee;

}

.canceltable{

width:100%;
border-collapse:collapse;

}

.canceltable td{

border:1px solid #ddd;
padding:8px;

}

details{

margin-top:10px;

}

.badge{

display:inline-block;
background:#eaf3ff;
padding:5px 10px;
border-radius:20px;
margin-right:8px;
font-size:13px;
color:#0b5ed7;

}

.success{

color:green;
font-weight:bold;

}

.error{

color:red;
font-weight:bold;

}

</style>

</head>

<body>

<div class="header">

<h1>🚌 eTravelSmart Bus Search</h1>

<div class="searchinfo">

<?=htmlspecialchars($sourceCity)?>
 →
<?=htmlspecialchars($destinationCity)?>

&nbsp;&nbsp;|&nbsp;&nbsp;

<?=htmlspecialchars($doj)?>

</div>

</div>

<div class="container">

<div class="summary">

<b>HTTP Status :</b>

<?php

echo ($httpCode==200)
?
"<span class='success'>200 OK</span>"
:
"<span class='error'>$httpCode</span>";

?>

</div>

<?php

if($httpCode!=200){

echo "<pre>";
echo htmlspecialchars($response);
echo "</pre>";
exit;

}

if(json_last_error()!=JSON_ERROR_NONE){

echo "<pre>";
echo htmlspecialchars($response);
echo "</pre>";
exit;

}

$list=[];

foreach($data as $value){

if(is_array($value) && isset($value[0]) && is_array($value[0])){

$list=$value;
break;

}

}

if(empty($list)){

echo "<pre>";
print_r($data);
echo "</pre>";
exit;

}

echo "<div class='summary'>";
echo "<b>Total Buses :</b> ".count($list);
echo "</div>";

foreach($list as $bus){

$fare=explode(",",$bus["fare"])[0];

?>

<div class="card">

<div class="row">

<div>

<div class="operator">

<?=htmlspecialchars($bus["operatorName"])?>

</div>

<div class="bustype">

<span class="badge">

<?=htmlspecialchars($bus["inventoryType"])?>

</span>

<span class="badge">

Route :
<?=htmlspecialchars($bus["routeScheduleId"])?>

</span>

</div>

</div>

<div align="center">

    <div class="time">
        <?=htmlspecialchars($bus["departureTime"])?>
    </div>

    <div class="arrow">
        ↓
    </div>

    <div class="time">
        <?=htmlspecialchars($bus["arrivalTime"])?>
    </div>

</div>

<div align="right">

    <div class="price">
        ₹<?=htmlspecialchars($fare)?>
    </div>

    <div class="seats">
        <?=htmlspecialchars($bus["availableSeats"])?> Seats Left
    </div>

</div>

</div>

<div class="section">

<h3>📍 Boarding Points</h3>

<?php

if(isset($bus["boardingPoints"]) && is_array($bus["boardingPoints"])){

foreach(array_slice($bus["boardingPoints"],0,5) as $bp){

?>

<div class="point">

<b><?=htmlspecialchars($bp["time"] ?? "")?></b>

&nbsp;&nbsp;

<?=htmlspecialchars($bp["pointName"] ?? $bp["location"] ?? "")?>

</div>

<?php

}

}

?>

</div>


<div class="section">

<h3>📍 Dropping Points</h3>

<?php

if(isset($bus["droppingPoints"]) && is_array($bus["droppingPoints"])){

foreach(array_slice($bus["droppingPoints"],0,5) as $dp){

?>

<div class="point">

<b><?=htmlspecialchars($dp["time"] ?? "")?></b>

&nbsp;&nbsp;

<?=htmlspecialchars($dp["pointName"] ?? $dp["location"] ?? "")?>

</div>

<?php

}

}

?>

</div>


<details>

<summary><b>Cancellation Policy</b></summary>

<br>

<table class="canceltable">

<tr>

<th>Hours Before Departure</th>

<th>Refund %</th>

</tr>

<?php

if(isset($bus["cancellationPolicy"]) && is_array($bus["cancellationPolicy"])){

foreach($bus["cancellationPolicy"] as $cp){

?>

<tr>

<td><?=htmlspecialchars($cp["cutoffTime"] ?? "")?></td>

<td><?=htmlspecialchars($cp["refundInPercentage"] ?? "")?>%</td>

</tr>

<?php

}

}

?>

</table>

</details>

<br>

<div style="text-align:right;">

<a href="#" class="viewbtn">

View Seats →

</a>

</div>

</div>

<?php

}

?>

</div>

</body>

</html>