<?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'];

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

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

$query = "SELECT m.id,m.update_date,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,m.schedule,AVG(r.rating) as avg_rating 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
LEFT JOIN sml_reviews as r ON m.id=r.id_marker
WHERE m.id_map=$id_map
GROUP BY m.id,m.order 
ORDER BY m.id,m.order DESC";

if (is_ssl()) { $protocol = 'https'; } else { $protocol = 'http'; }
$link = $protocol ."://". $_SERVER['SERVER_NAME'] . str_replace("backend/ajax/get_markers.php","viewer/index.php?code=",$_SERVER['SCRIPT_NAME']);
$map = get_map($id_map,$_SESSION['id_user']);
$link_marker = $link.$map['code']."&m=";

$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' => 'active',  'dt' =>3, 'formatter' => function($d,$row) {
        $schedule = $row['schedule'];
        $schedule_icon = "";
        if(!empty($schedule)) {
            $schedule_icon = "&nbsp;&nbsp;<i class='fa fa-clock'></i>";
        }
        if($d) {
            return "<i class='fa fa-check'></i> $schedule_icon";
        } else {
            return "<i class='fa fa-times'></i> $schedule_icon";
        }
    }),
    array( 'db' => 'visible_map',  'dt' =>4, 'formatter' => function($d,$row) {
        $visible_map = $d;
        $visible_search = $row['visible_search'];
        if($visible_map) {
            $visible_icon = "<i style='color:darkgreen;' class='far fa-map'></i>";
        } else {
            $visible_icon = "<i style='color:darkred;' class='far fa-map'></i>";
        }
        if($visible_search) {
            $visible_icon .= "&nbsp;&nbsp;<i style='color:darkgreen;' class='fa fa-search'></i>";
        } else {
            $visible_icon .= "&nbsp;&nbsp;<i style='color:darkred;' class='fa fa-search'></i>";
        }
        return $visible_icon;
    }),
    array( 'db' => 'to_validate',  'dt' =>5, 'formatter' => function($d,$row) {
        if($d) {
            return "<i class='fa fa-times'></i>";
        } else {
            return "<i class='fa fa-check'></i>";
        }
    }),
    array( 'db' => 'centered',  'dt' =>6, 'formatter' => function($d,$row) {
        if($d) {
            return "<i class='fa fa-check'></i>";
        } else {
            return "<i class='fa fa-times'></i>";
        }
    }),
    array( 'db' => 'featured',  'dt' =>7, 'formatter' => function($d,$row) {
        if($d) {
            return "<i class='fa fa-check'></i>";
        } else {
            return "<i class='fa fa-times'></i>";
        }
    }),
    array( 'db' => 'update_date',  'dt' =>8, 'formatter' => function($d,$row) {
        global $language;
        return formatTime("dd MMM y", $language, strtotime($d));
    }),
    array( 'db' => 'order',  'dt' =>9, 'formatter' => function($d,$row) {
        $html = "<i onclick='change_order(".$row['id'].",\"up\");' class='order_icon fa fa-caret-up'></i>&nbsp;&nbsp;$d&nbsp;&nbsp;<i onclick='change_order(".$row['id'].",\"down\");' class='order_icon fa fa-caret-down'></i>";
        return "$html";
    }),
    array( 'db' => 'avg_rating',  'dt' =>10, 'formatter' => function($d,$row) {
        global $link_marker;
        $id = $row['id'];
        $avg_rating = number_format($d,1);
        $html_link = "<a style='line-height:1;' target='_blank' class='btn btn-sm btn-dark btn_link_marker' href='$link_marker$id'><i class=\"fas fa-external-link\"></i></a>";
        $html_link .= "<a style='line-height:1;' href='index.php?p=reviews&id_marker=$id' class='btn btn-sm btn-warning btn_review_marker ".(($avg_rating==0) ? 'disabled' : '')."'><i class=\"fas fa-star\"></i> $avg_rating</a>";
        return "$html_link <button onclick='modal_clone_marker($id);' style='line-height:1;' class='btn btn-sm btn-secondary btn_duplicate_marker'><i class=\"fas fa-clone\"></i></button> <button onclick='modal_delete_marker($id);' style='line-height:1;' class='btn btn-sm btn-danger btn_delete_marker'><i class=\"fas fa-trash\"></i></button><input style='display:none;' id='marker_$id' class='check_bulk_marker' type='checkbox' />";
    })
);

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