<!doctype html>
<html lang="en">
<head>
<title>Tim's Test PHP page</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<style>
    table.main, td.main {
        border: 1px solid red;
        border-collapse: collapse;
        padding:5px;
    }
    table.meaning, td.meaning {
        border: none;
        border-collapse: collapse;
    }
   
    p.debug {
        display: none;
    }

</style>
<script>

    function enter_meaning(word_id, meaning_language) {
        alert("word_id:=" + word_id + ", meaning_language:=" + meaning_language)
    }
    
    // AJAX request to remove meaning
    function remove_meaning() {
        id = document.getElementById("i_id").value
        //alert("word entered is: " + word)
        const xhttp = new XMLHttpRequest();
        xhttp.onload = function() {
            //
            alert("response: " + this.responseText)
            result = JSON.parse(this.responseText)
            //document.getElementById("message").innerHTML = "From word: " + result.from_word + "<br/>To word: " + result.to_word + "<br/>Part of speech: " + result.part;
        }
        request = "remove_meaning.php?meaning_id=" + id 
        xhttp.open("GET", request, true);
        xhttp.send();
       
    }
    
    // AJAX request to remove meaning
    function check_json() {
        json_obj = JSON.parse('{"request_type":"add_meaning","parm_from_word_id":"276","parm_to_word":"large","parm_to_word_lang":"1","parm_part_of_speech_id":"5","db_status":"Connected to: Localhost via UNIX socket"Word object created: largestoring new word...word stored on Db!,"to_word":{"type":"Word","id":"288","word":"large","language_id":"1","date_added":"2024-02-03 09:58:53","date_updated":"","is_deleted":"0","is_on_db":"1","status":"New word added!"},"word_meaning":{"type":"Word_Meaning","id":"153","from_word_id":"276","to_word_id":"288","part_of_speech_id":"5","date_added":"2024-02-03 09:58:53","date_updated":"","is_deleted":"0","is_on_db":"1","status":"New word meaning added!"}}')
        alert("json_obj" + json_obj)
    }
    
</script>
</head>
<body>

<h1>Tim's Test PHP</h1>
<h2>Word Check</h2>

    <br/>From Language: <select id="slt_from_language">
        <option value="0">--- Select ---</option>
        <option value="1">English</option>
        <option value="2">Greek</option>
        <option value="3">French</option>
    </select>
    <br/>To Language: <select id="slt_to_language">
        <option value="0">--- Select ---</option>
        <option value="1">English</option>
        <option value="2">Greek</option>
        <option value="3">French</option>
    </select>
    Enter Meaning Id: <input type="text" id="i_id"/>
    <br/><button onclick="remove_meaning()">Remove Meaning</button>
<br/><button onclick="check_json()">Check JSON</button>
<div id="message"></div>

<?php
/*
if ($_GET["debug"]) {
    $debug = TRUE;
} else {
    $debug = FALSE;
}
include "classes/DatabaseConnection.php";
include "classes/Word.php";

$db_conn = DatabaseConnection::getConnection();
if ($debug) {
    echo "<br/>Connection status: " . $db_conn->status;;
}
// default to English
if ($_GET["from_language"]) {
    $from_language = $_GET["from_language"];
} else {
    $from_language = 1;
}
$query = "SELECT fw.word as from_word, tw.word as to_word FROM words fw left join word_meanings wm on fw.id = wm.from_word_id left join words tw on wm.to_word_id = tw.id where fw.language = " . $from_language . " order by fw.word";

if ($debug) {
   echo "<br/>SQL created: " . $query;
}

$result = $db_conn->connection->query($query);
if ($debug) {
    echo "<br/>SQL executed?...";
    echo "<br/>Number of rows in result: " . $result->num_rows;
}
echo "<table>\n";
foreach ($result as $row) {
    echo "<tr><td>" . $row['from_word'] . "</td><td>" . ($row['to_word'] ?$row['to_word']:"none") . "</td></tr>\n";
}
echo "</table>\n";
*/
?>

</body>
</html>