<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
session_start();
if($_SESSION['sml_si']!=session_id()) {
    die();
}
require(__DIR__.'/ssp.class.php');
require(__DIR__.'/../../config/config.inc.php');
require(__DIR__.'/../functions.php');

$id_map = (int)$_GET['id_map'];
$id_marker = (int)$_GET['id_marker'];
if($id_marker!=0) {
    $where = "AND m.id=$id_marker";
} else {
    $where = "";
}

$settings = get_settings();
$user_info = get_user_info($_SESSION['id_user']);
if(!isset($_SESSION['lang'])) {
    if(!empty($user_info['language'])) {
        $language = $user_info['language'];
    } else {
        $language = $settings['language'];
    }
} else {
    $language = $_SESSION['lang'];
}
$default_language = $settings['language'];
set_language($language,$settings['language_domain']);
session_write_close();

$query = "SELECT m.name as marker,r.*,IF(r.email <> '',CONCAT(r.name,' - <a href=\'mailto:',r.email,'\'>',r.email,'</a>'),r.name) as user FROM sml_reviews as r
JOIN sml_markers as m ON r.id_marker=m.id
WHERE m.id_map=$id_map $where
ORDER BY r.create_date DESC";
$table = "( $query ) t";
$primaryKey = 'id';

$delete_label = _('delete');
$reply_label = _('reply');

$columns = array(
    array(
        'db' => 'id',
        'dt' => 'DT_RowId',
        'formatter' => function( $d, $row ) {
            return $d;
        }
    ),
    array( 'db' => 'create_date',  'dt' =>0, 'formatter' => function($d,$row) {
        global $language;
        $create_date = formatTime("dd MMM y",$language,strtotime($d));
        return '<span style="display:none">'.strtotime($d).'</span>'.$create_date;
    }),
    array( 'db' => 'marker',  'dt' =>1, 'formatter' => function($d,$row) {
        $id_marker = $row['id_marker'];
        $id = $row['id'];
        return "<span>$d</span>&nbsp;&nbsp;<a target='_blank' href='index.php?p=edit_marker&id=$id_marker'><i class='fas fa-external-link'></i></a>";
    }),
    array( 'db' => 'user',  'dt' =>2 ),
    array( 'db' => 'rating',  'dt' =>3 ),
    array( 'db' => 'comment',  'dt' =>4, 'formatter' => function($d,$row) {
        $id = $row['id'];
        return "<span id='comment_$id'>$d</span>";
    }),
    array( 'db' => 'reply',  'dt' =>5, 'formatter' => function($d,$row) {
        $id = $row['id'];
        return "<span id='reply_$id'>$d</span>";
    }),
    array( 'db' => 'id',  'dt' =>6, 'formatter' => function($d,$row) {
        global $delete_label,$reply_label;
        return "<button onclick='delete_review($d);' style='line-height:1;' class='btn btn-sm btn-danger'>$delete_label</button>&nbsp;&nbsp;&nbsp;
                <button onclick='open_reply_review($d);' style='line-height:1;' class='btn btn-sm btn-primary'>$reply_label</button>";
    })
);

$sql_details = array(
    'user' => DATABASE_USERNAME,
    'pass' => DATABASE_PASSWORD,
    'db' => DATABASE_NAME,
    'host' => DATABASE_HOST);

echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);