<?php
session_start();
include("serverConnect.php");
include("functions.php");

$displayError = true;

if (isset($_GET['territory']) && isset($_GET['productionIndex'])){
    //TODO : check everything
    
    $productionIndex = intval($_GET['productionIndex']);
    $territoryId = intval($_GET['territory']);
    
    $req = $bdd->prepare('SELECT cBuildings.type AS BTYPE FROM cBuildings WHERE cBuildings.territory = ?');
    $req->execute(array($territoryId));
    if ($data = $req->fetch()) {
        $building = getBuildingsList()[$data['BTYPE']];
        
        $count = 0;
        foreach($building['production'] as $key => $pr) {
            
            if ($count == $productionIndex) {
                $req = $bdd->prepare('UPDATE cBuildings SET production = ? WHERE cBuildings.territory = ?');
                $req->execute(array(getResourceId($key), $territoryId));
            }
            
            $count++;
        }
        
        $displayError = false;
    }
}

if (!$displayError){
	header("Location:region.php");
	exit;
}
else {
    echo "FAIL";
}
?>