<?php

$url = 'http://@concordia_host@:@concordia_port@';
$data = array("operation" => "concordiaSearch","tmId" => intval($_GET["tmId"]),"pattern" => $_GET["pattern"]);

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => json_encode($data),
    ),
);
$context  = stream_context_create($options);
$response = file_get_contents($url, false, $context);

$data = json_decode($response);
?>

<html>
    <head>
        <script src="js/jquery-1.11.3.min.js"></script>
        <script src="js/cat.js"></script>
		<link rel="stylesheet" href="css/iatagger.css" />
        <meta charset="UTF-8">
    </head>
    <body>
        <div id="header">
        </div>
        <div id="content">
            <div id="result">
                <pre><?php print_r($data);?></pre>
                <div id="result-score">Concordia score: <b><?= round($data->result->bestOverlayScore*100) ?>%</b></div>
            <?php

            $inputSentence = $_GET["pattern"];
            $markedSentence = "";
            $fragments = array();
            $lastInsertedEnd = 0;
            for($i=0; $i<2;$i++) {
                $fragment = data->result->bestOverlay[i];
                //previous unmarked fragment
                $markedSentence += substr($inputSentence,$lastInsertedEnd, $fragment->matchedPatternStart - $lastInsertedEnd);

                //the marked fragment
                $markedSentence += '<span onclick="displayDetails(this, '+i+')" class="matchedFragment">'+substr($inputSentence,$fragment->matchedPatternStart, $fragment->matchedPatternEnd - $fragment->matchedPatternStart)+'</span>';

                $lastInsertedEnd = $fragment->matchedPatternEnd;

                //fragments += renderFragment(fragment, i);

            }
            //remaining unmarked fragment
            $markedSentence += substr($inputSentence, $lastInsertedEnd);

            ?>
                <div id="result-sentence" onMouseUp="phraseSearch(this)"><?=$markedSentence ?></div>
                <br/><br/><br/>
            </div>
        </div>
    </body>
</html>