<?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_user = $_SESSION['id_user'];
$id_map = (int)$_GET['id_map'];

$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();

$map = get_map($id_map,$id_user);
$address_format = json_decode($map['address_format'],true);

$query = "SELECT m.id,m.lat,m.lon,m.name,m.street,m.city,m.postal_code,m.country,m.county,m.state,m.active,m.visible_map,m.visible_search,m.to_validate,m.centered,m.featured,m.`order`,m.icon,m.color_hex,m.icon_color_hex,GROUP_CONCAT(DISTINCT IF(ccc.name IS NOT NULL,CONCAT(ccc.name, ' - ', cc.name, ' - ', c.name),IF(cc.name IS NOT NULL,CONCAT(cc.name, ' - ', c.name),c.name))) as category,i.image as icon_image,CONCAT(m.city,' ',m.street,' ',m.postal_code,' ',m.country) as full_address,COUNT(im.image) as count_images FROM sml_markers as m 
LEFT JOIN sml_markers_categories_assoc as ca ON ca.id_marker=m.id
LEFT JOIN sml_categories as c ON ca.id_category=c.id
LEFT JOIN sml_categories as cc ON c.id_category_parent=cc.id
LEFT JOIN sml_categories as ccc ON cc.id_category_parent=ccc.id
LEFT JOIN sml_icons as i ON i.id=m.id_icon_library
JOIN sml_images as im ON im.id_marker=m.id
WHERE m.id_map=$id_map AND im.to_validate=1
GROUP BY m.id,m.order 
ORDER BY m.id,m.order DESC";
$table = "( $query ) t";

$primaryKey = 'id';

$columns = array(
    array(
        'db' => 'id',
        'dt' => 'DT_RowId',
        'formatter' => function($d,$row) {
            return $d;
        }
    ),
    array( 'db' => 'name',  'dt' =>0, 'formatter' => function($d,$row) {
        $icon = $row['icon'];
        $color_hex = $row['color_hex'];
        $icon_color_hex = $row['icon_color_hex'];
        $icon_image = $row['icon_image'];
        if(empty($icon_image)) {
            $icon = explode("|",$icon)[0];
            $icon_h = "<div style='display:inline-block;vertical-align:middle;background-color:$color_hex;border-radius:50%;width:20px;height:20px;line-height:16px;text-align:center'><i style='font-size:8px;color:$icon_color_hex;' class='$icon'></i></div>";
        } else {
            $icon_h = "<img style='height:15px;vertical-align:middle' src='../viewer/icons/$icon_image' />";
        }
        return $icon_h."&nbsp;&nbsp;".$d;
    }),
    array( 'db' => 'category',  'dt' =>1 ),
    array( 'db' => 'full_address',  'dt' =>2, 'formatter' => function($d,$row) {
        global $address_format;
        if(!is_numeric($row['lat']) || !is_numeric($row['lon'])) {
            $icon = '<i style="color:darkred"; class="fas fa-map-pin"></i>';
        } else {
            $icon = '<i style="color:darkgreen"; class="fas fa-map-pin"></i>';
        }
        $address = format_address_string($row, $address_format);
        return $icon." ".$address;
    }),
    array( 'db' => 'count_images',  'dt' =>3 ),
);

$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 )
);