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

$id_map = (int)$_GET['id_map'];

$query = "SELECT c.id,c.order,c.default_selected,c.featured,c.add_from_map,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,c.icon,c.icon_color_hex,c.color_hex,i.image as icon_image,
                (SELECT COUNT(sc.id) FROM sml_categories sc WHERE sc.id_category_parent=c.id) as child_count
                FROM sml_categories as c
                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=c.id_icon_library
                WHERE c.id_map=$id_map 
                GROUP BY c.id,c.order,c.default_selected,c.add_from_map,c.name,c.icon,c.icon_color_hex,c.color_hex,i.image,cc.name
                ORDER BY category,c.order ASC";
$table = "( $query ) t";
$primaryKey = 'id';

$columns = array(
    array(
        'db' => 'id',
        'dt' => 'DT_RowId',
        'formatter' => function( $d, $row ) {
            return $d;
        }
    ),
    array( 'db' => 'id',  'dt' =>0 ),
    array( 'db' => 'category',  'dt' =>1, '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' => 'default_selected',  'dt' =>2, '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' =>3, 'formatter' => function($d,$row) {
        if(isset($row['child_count']) && $row['child_count'] > 0) {
            return "--";
        }
        if($d) {
            return "<i class='fa fa-check'></i>";
        } else {
            return "<i class='fa fa-times'></i>";
        }
    }),
    array( 'db' => 'add_from_map',  'dt' =>4, 'formatter' => function($d,$row) {
        if($d) {
            return "<i class='fa fa-check'></i>";
        } else {
            return "<i class='fa fa-times'></i>";
        }
    }),
    array( 'db' => 'order',  'dt' =>5, 'formatter' => function($d,$row) {
        $html = "<i onclick='change_order_c(".$row['id'].",\"up\");' class='order_icon fa fa-caret-up'></i>&nbsp;&nbsp;$d&nbsp;&nbsp;<i onclick='change_order_c(".$row['id'].",\"down\");' class='order_icon fa fa-caret-down'></i>";
        return "$html";
    }),
    array( 'db' => 'child_count', 'dt' => 'child_count' )
);

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