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

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

$regionResources = simulateRegion($_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>
    <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="setPopupVisible()"> Make a trade offer </button></p>
<br>
<br>
<p class="bigText">Current offers : </p>
<table class="tab">
<?

$req = $bdd->prepare('SELECT * FROM cTradeOffers');
$req->execute(array());

$count = 0;
$popup = "";
while ($data = $req->fetch()) {
    $path = getPath($_SESSION['regionId'], $data['region']);
    
    if ($path["possible"]) {
        $_SESSION['tmpPath'][$count] = $path;
        ?>
        <tr>
            <td>
             <? echo $data['region']; ?>
            </td>
            <td>
             <? echo $resources[$data['resource']]['longName']; ?>
            </td>
            <td>
             <? echo $data['amount']; ?> ton(s)
            </td>
            <td>
             <? echo $path['time']*2; ?> h for the round trip
            </td>
            <td>
            <button class="niceButton2 blueButton" onclick="takeOffer(<? echo $data['id']; ?>)">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="popup"><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";
    popupMakeOffer.visibility = "visible";
    
}

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

function preparePopupPath(i) {
    popupVisible = document.getElementById("popupMakeOffer");
    setPopupVisible();
}

function postOffer() {
    var resource = document.getElementById("resourceSelect").selectedIndex + 1;
    var amount = document.getElementById("inputAmount").value;
    window.location.replace('postOffer.php?resource=' + resource + '&amount=' + amount);
}
</script>
<?php include("bottom.php"); ?>