<?php
	if($_SERVER['REQUEST_METHOD'] == "POST"){
		$twitchUsername = safeString($_POST["twitchUsername"]);
		
	    $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/kraken/users/".$twitchUsername); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        $jsonreturn = json_decode(curl_exec($ch), true);
        if(isset($jsonreturn["error"]))
        {
            print('
    	        <div class="alert alert-danger alert-dismissable">
                    <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>Gebruiker bestaat niet.
                </div>
    	    ');
        }
        else
        {
            if($stmt = $mysqli -> prepare("INSERT INTO Twitch(uid,username) VALUES(?,?) ON DUPLICATE KEY UPDATE username=VALUES(username)"))
            {
        	$stmt -> bind_param("is",$userid,$twitchUsername);
        	$stmt -> execute();
        	$stmt -> close();
        	reDirect("twitch.php?i=overzicht");	
            }
        }
	}
?>

<div class="col-lg-12">
    <div class="ibox float-e-margins">
        <div class="ibox-title">
            <h5>Configureer Twitch account</h5>
            <div class="ibox-tools">
                <a class="collapse-link">
                    <i class="fa fa-chevron-up"></i>
                </a>
            </div>
        </div>
        <div class="ibox-content">
            <form method="POST" class="form-horizontal">
                <div class="form-group"><label class="col-sm-2 control-label">Naam</label>
                    <div class="col-sm-10"><input name="twitchUsername" type="text" class="form-control"></div>
                </div>
		        <div class="form-group">
                        <div class="col-sm-4 col-sm-offset-2">
                        <button class="btn btn-primary" type="submit">Naam opslaan</button>
                        <a href="twitch.php?i=overzicht" class="btn btn-primary" role="button">Annuleren</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>