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

$displayError = true;

if (isset($_GET['territory']) && isset($_GET['tech'])){
    //TODO : check everything
    
    $techId = intval($_GET['tech']);
    $territoryId = intval($_GET['territory']);
    
    $req = $bdd->prepare('SELECT cBuildings.type AS BTYPE FROM cResearch JOIN cBuildings ON cResearch.territory = cBuildings.territory WHERE cResearch.territory = ?');
    $req->execute(array($territoryId));
    if ($data = $req->fetch()) {
        $building = getBuildingsList()[$data['BTYPE']];
        $tech = getTechsList()[$techId];
        
        $req = $bdd->prepare('UPDATE cResearch SET tech = ?, progress = 0, timeBegun = ?, timeFinished = 0 WHERE cResearch.territory = ?');
        $req->execute(array($techId, time(), $territoryId));
        
        $displayError = false;
    }
}

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