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

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

$regionResources = simulate($_SESSION['regionId']);
?>
<div id="bgpopup" onClick="hidePopup()">
</div>
<div id="popupMakeOffer" class="popup">
    <p class="bigText">Make a new offer</p>
    <p>Resource : <select id="resourceSelect" class='black'>
    <?

    foreach($resources as $key => $r) {
        if (is_int($key) && $key != 0) {
            ?>
            <option value="<? echo $key;?>"> <? echo $r['longName'];?> </option>
            <?
        }
    }
    ?>
    
    </select></p>
    <br>
    <p>Amount : <input id="inputAmount" class='black' type="number" name="amount" min="1"> </p>
    <br>
    <p>Cost : <input id="inputCost" class='black' type="number" name="cost" min="1"> </p>
    <br>
    <button id="postOfferButton" class="niceButton greenButton" onclick="postOffer()">Post offer</button>
    <p> </p>
    <p> </p>
</div>
<br>
<br>
<p class="bigText"> Current Region : <? echo $_SESSION['regionId']; ?> </p>
<br>
<br>
<p><button class="niceButton greenButton" onclick="preparePopupMakeOffer()"> Make a trade offer </button></p>
<br>
<br>
<p class="bigText">Current offers : </p>
<table class="tab">
<th>
    <td>
        Region
    </td>
    <td>
        Resource
    </td>
    <td>
        Amount per trip
    </td>
    <td>
        Time per round trip
    </td>
    <td>
        Cost per round trip
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
</th>
<?

$req = $bdd->prepare('SELECT cTradeOffers.* FROM cTradeOffers JOIN cRegions ON cRegions.id = cTradeOffers.region WHERE cRegions.owner != ?');
$req->execute(array($_SESSION['id']));

$count = 0;
$popup = "";
while ($data = $req->fetch()) {
    $landPath = getPath($_SESSION['regionId'], $data['region'], true);
    $seaPath = getPath($_SESSION['regionId'], $data['region'], false);
    
    if ($landPath['possible'] && (!$seaPath['possible'] || $landPath['time'] < $seaPath['time'])) {
        $path = $landPath;
    }
    else {
        $path = $seaPath;
    }
    
    if ($path["possible"]) {
        $_SESSION['tmpPath'][$count] = $path;
        ?>
        <tr>
            <td>
            </td>
            <td>
             <? echo $data['region']; ?>
            </td>
            <td>
             <? echo $resources[$data['resource']]['longName']; ?>
            </td>
            <td>
             <? echo $data['amount']; ?> ton(s)
            </td>
            <td>
             <? echo timeToString($path['time']*2, "<br>"); ?>
            </td>
            <td>
             <? echo $data['cost'] . " + " . $path["taxes"] . " €"; ?>
            </td>
            <td>
            <button class="niceButton2 blueButton" onclick="takeOffer(<? echo $data['id']; ?>, <? echo $count; ?>)">Take offer</button>
            </td>
            <td>
            <button class="niceButton2 blueButton" onclick="preparePopupPath(<? echo $count; ?>)">Display path</button>
            </td>
            <td>
            </td>
        </tr>
        <?
        $popup = $popup . '<div id="popup' . $count . '" class="popupImage"><img src="generatePathImage.php?i=' . $count . '" alt="Trade map"></div>';
        
        $count++;
    }
}

echo $popup;
?>

<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"); ?>