<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
ob_start();
session_start();
if((($_SERVER['SERVER_ADDR']==$_SESSION['demo_server_ip']) && ($_SERVER['REMOTE_ADDR']!=$_SESSION['demo_developer_ip']) && ($_SESSION['id_user']==$_SESSION['demo_user_id'])) || ($_SESSION['sml_si']!=session_id())) {
    //DEMO CHECK
    die();
}
$id_map = $_SESSION['id_map_sel'];
require_once("../functions.php");
require_once("../../db/connection.php");
$id_category = (int)$_POST['id'];
$name = strip_tags($_POST['name']);
$id_category_parent = (int)$_POST['id_category_parent'];
$default_selected = (int)$_POST['default_selected'];
$featured = (int)$_POST['featured'];
$add_from_map = (int)$_POST['add_from_map'];
if($id_category_parent==0) $id_category_parent=NULL;
$color_hex_c = strip_tags($_POST['color_hex_c']);
$icon_color_hex_c = strip_tags($_POST['icon_color_hex_c']);
$icon_c = strip_tags($_POST['icon_c']);
$id_icon_library_c = (int)$_POST['id_icon_library_c'];
$array_lang = json_decode($_POST['array_lang'],true);
$status = false;
if($id_category==0) {
    $query = "INSERT INTO sml_categories(id_map,name,id_category_parent,default_selected,add_from_map,color_hex,icon_color_hex,icon,id_icon_library,featured) VALUES(?,?,?,?,?,?,?,?,?,?);";
    if($smt = $mysqli->prepare($query)) {
        $smt->bind_param('isiiisssii',$id_map,$name,$id_category_parent,$default_selected,$add_from_map,$color_hex_c,$icon_color_hex_c,$icon_c,$id_icon_library_c,$featured);
        $result = $smt->execute();
        if($result) {
            $id_category = $mysqli->insert_id;
            $status = true;
        }
    }
} else {
    $markers_size = $_POST['markers_size'];
    $markers_color_hex = $_POST['markers_color_hex'];
    $markers_icon_color_hex = $_POST['markers_icon_color_hex'];
    if(empty($markers_icon_color_hex)) $markers_icon_color_hex="#ffffff";
    $markers_color_hex_hover = $_POST['markers_color_hex_hover'];
    $markers_icon_color_hex_hover = $_POST['markers_icon_color_hex_hover'];
    $markers_id_icon_library = $_POST['markers_id_icon_library'];
    $markers_icon = strip_tags($_POST['markers_icon']);
    $default_am_fields = $_POST['default_am_fields'];
    if($default_am_fields=='{}') $default_am_fields=null;
    $query = "UPDATE sml_categories SET name=?,id_category_parent=?,default_selected=?,add_from_map=?,markers_size=?,markers_color_hex=?,markers_icon_color_hex=?,markers_id_icon_library=?,markers_icon=?,markers_color_hex_hover=?,markers_icon_color_hex_hover=?,color_hex=?,icon_color_hex=?,icon=?,id_icon_library=?,default_am_fields=?,featured=? WHERE id=?;";
    if($smt = $mysqli->prepare($query)) {
        $smt->bind_param('siiidssissssssisii',$name,$id_category_parent,$default_selected,$add_from_map,$markers_size,$markers_color_hex,$markers_icon_color_hex,$markers_id_icon_library,$markers_icon,$markers_color_hex_hover,$markers_icon_color_hex_hover,$color_hex_c,$icon_color_hex_c,$icon_c,$id_icon_library_c,$default_am_fields,$featured,$id_category);
        $result = $smt->execute();
        if($result) {
            $status = true;
        }
    }
}
if($status) {
    save_input_langs($array_lang,'sml_categories_lang','id_category',$id_category);
    ob_end_clean();
    echo json_encode(array("status"=>"ok"));
} else {
    ob_end_clean();
    echo json_encode(array("status"=>"error"));
}