<?php include("top.php"); ?>
<?

$buildings = getBuildingsList();
$resources = getResourcesList();
$terrains = getTerrainsList();

$regionResources = simulate($_SESSION['regionId']);
?>
<div id="bgpopup" onClick="hidePopup()">
</div>
<br>
<br>
<p class="bigText"> Current Region : <? echo $_SESSION['regionId']; ?> </p>
<br>
<br>
<p class="bigText">Current transports : </p>
<table class="tab">
<th>
    <td>
        Start region
    </td>
    <td>
        End region
    </td>
    <td>
        Resource
    </td>
    <td>
        Arrival
    </td>
    <td>
        Way
    </td>
    <td>
    </td>
    <td>
    </td>
</th>
<?

$req = $bdd->prepare('SELECT * FROM cTransports WHERE cTransports.regionStart = ? OR cTransports.regionEnd = ? ');
$req->execute(array($_SESSION['capital'], $_SESSION['capital']));

while ($data = $req->fetch()) {
    ?>
    <tr>
        <td>
        </td>
        <td>
         <? echo $data['regionStart']; ?>
        </td>
        <td>
         <? echo $data['regionEnd']; ?>
        </td>
        <td>
         <? echo $data['amount'] . " tons of " . $resources[$data['resource']]['longName']; ?>
        </td>
        <td>
         <? echo timeToString($data['timeArrived'] - time(), "<br>");; ?>
        </td>
        <td>
         <? if ($data['way'] == 0) echo 'Outward'; else echo 'Return'; ?>
        </td>
        <td>
        <button class="niceButton2 blueButton" onclick="cancelTransport(<? echo $data['id']; ?>)">Cancel</button>
        </td>
        <td>
        </td>
    </tr>
    <?
}
?>

<script>
//------------------------------------------------------------------------
//PopUp
//------------------------------------------------------------------------

var bgPopup = document.getElementById("bgpopup");
var popupMakeOffer = document.getElementById("popupMakeOffer");
var postOfferButton = document.getElementById("postOfferButton");

var popupVisible = 0;

function hidePopup() {
    bgPopup.style.visibility = "hidden";
    popupVisible.style.visibility = "hidden";
    postOfferButton.style.transitionDuration = "0s";
}

function setPopupVisible() {
    bgPopup.style.visibility = "visible";
    popupVisible.style.visibility = "visible";
}

function preparePopupMakeOffer() {
    popupVisible = popupMakeOffer;
    postOfferButton.style.transitionDuration = "0.4s";
    setPopupVisible();
}

function preparePopupPath(i) {
    popupVisible = document.getElementById("popup" + i);
    popupVisible.style.left = window.innerWidth/2 - 600/2 + "px";
    setPopupVisible();
}

function postOffer() {
    var resource = document.getElementById("resourceSelect").selectedIndex + 1;
    var amount = document.getElementById("inputAmount").value;
    var cost = document.getElementById("inputCost").value;
    window.location.replace('postOffer.php?resource=' + resource + '&amount=' + amount + '&cost=' + cost);
}

function takeOffer(id, index) {
    window.location.replace('takeOffer.php?id=' + id + "&index=" + index);
}
</script>
<?php include("bottom.php"); ?>