<?php
    session_start();
	if ( empty($_SESSION['login']) ) {
        header( 'Location: login.php' );
        die;
	}

    include 'include/database.php';

    $connection = connect($database);

    $canAdmin = canAdministrate($connection, $_SESSION['login']);
    
    if($canAdmin) {
        $title = getPropertyDeviceCG($connection, $_GET['id']);
        $title = $title['device'] . '/' . $title['property'];
        $translations = getPropertyTranslationsCG($connection, $_GET['id']);
    } else {
        $title = '';
        $translations = array();
    }

	$connection = null;
    
    $table = '
        <table class="center" style="margin: 0 0 25px 0;">
            <tr>
                <th>Translation</th>
                <th>Original</th>
                <th>New</th>
            </tr>
    ';
    
    foreach ($translations as $translation) {
        $table .= "
            <tr>
                <td><button onclick=\"deleteTranslation('{$_GET['id']}', '{$translation['id']}')\">Delete</button></td>
                <td><input type=\"text\" id=\"TransName{$translation['id']}\" onblur=\"editTranslation({$translation['id']}, 'TransName{$translation['id']}', 'TransAltName{$translation['id']}', {$_GET['id']})\" value=\"{$translation['name']}\" /></td>
                <td><input type=\"text\" id=\"TransAltName{$translation['id']}\" onblur=\"editTranslation({$translation['id']}, 'TransName{$translation['id']}', 'TransAltName{$translation['id']}', {$_GET['id']})\" value=\"{$translation['translation']}\" /></td>
            </tr>
        ";
    }
    
    $table .= "
        </table>
    
        <button onclick=\"newTranslation('{$_GET['id']}')\" class=\"center block\">New Translation</button>
    ";
    
    $translations = array(
        "title" => $title,
        "table" => $table
    );
    
    header('Content-type: application/json');
    echo json_encode($translations);
?>