<?
session_start();
include($_SERVER['DOCUMENT_ROOT'].'/config.php');
include($_SERVER['DOCUMENT_ROOT'].'/funkcion.php');

switch ($_REQUEST['action']) {
case 'sample1':
$id_kat = valideint($_POST["id"]);
$res = mysql_query("SELECT `name`,`text` FROM `nano_kat` WHERE `id`='$id_kat'");
$row = mysql_fetch_array($res);
$name = $row["name"];
$text = $row["text"];
echo "
<div class='panel panel-primary'>
<div class='panel-heading'><h3 class='panel-title'>Редактировать</h3></div>
<div class='panel-body'>
<form class='form-horizontal'>
<div class='form-group'>
<div class='col-md-12'>                                            
<div class='input-group'>
<span class='input-group-addon' data-toggle='tooltip' title='Введите новое название категории.'><span class='fa fa-database'></span></span>
<input type='text' class='form-control' value='$name' id='name_kat'>
</div>
</div>
</div>
<div class='form-group'>
<div class='col-md-12'>
<div class='input-group'>
<span class='input-group-addon' data-toggle='tooltip' title='Описание категории.'><span class='fa fa-database'></span></span>
<input type='text' class='form-control' value='$text' id='text_kat'>
</div>
</div>
</div>
</form>
</div>
<div class='panel-footer'>
<div class='form-group'>                                        
<div class='col-md-12'><button class='btn btn-danger btn-block' href='javascript:void(0);' onclick='edit_kat_$id_kat();'>Редактировать</button></div>
</div>
</div>
</div>

<script language='javascript' type='text/javascript'>
$(document).ready(function(){
	$(\"#imgLoad\").hide();
});

function edit_kat_$id_kat() {
{
var name_kat = ($('#name_kat').val());
$.ajax({
type: 'POST', 
url: \"/admin/ajax/kat.php\", 
data: { 'func' : 'edit_kat', 'name_kat' : name_kat, 'id_kat' : $id_kat},
beforeSend: function(){ progress = true;},
success: function(data) {
if(data == 1) { QBinfo('Поздравляем:','Название категории успешно изменено.',4000,'bottom-right');
setTimeout('location.replace(\"/admin/add_kat\")', 2000);
}
progress = false;
}
});
}
}
</script>
";

sleep(1);
break;
}

$id_to = mysql_result(mysql_query("SELECT `id` FROM `nano_kat` ORDER BY `id` DESC"),0,0);
$id_kat = $id_to + '1';

// Загрузка фотографии
$uploaddir = ($_SERVER['DOCUMENT_ROOT'].'/upload/kat/'); 
$file = $uploaddir . basename($id_kat.translit_p($_FILES['uploadfile']['name'])); 
$name = basename($id_kat.translit_p($_FILES['uploadfile']['name']));

if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
mysql_query("INSERT INTO `nano_tmp` (`name_p`) VALUES('$name')");
echo "1"; 
}

// Добавление категории
if($_POST['func'] == "add_kat") {
$name_kat = check_text($_POST["name_kat"]);
$text_kat = check_text($_POST["text_kat"]);
$img = mysql_result(mysql_query("SELECT `name_p` FROM `nano_tmp`"),0,0);
mysql_query("INSERT INTO `nano_kat` (`name`,`text`,`img`) VALUES ('$name_kat','$text_kat','$img')") or die(mysql_error());
echo "1";
}

// Редактирование категории
if($_POST['func'] == "edit_kat") {
$id_kat = valideint($_POST["id_kat"]);
$name_kat = check_text($_POST["name_kat"]);
$text_kat = check_text($_POST["text_kat"]);

mysql_query("UPDATE `nano_kat` SET `name`='$name_kat', `text`='$text_kat' WHERE `id`='$id_kat'");
echo "1";
}

// Удаление категории
if($_POST['func'] == "del_kat") {
$id = valideint($_POST["id"]);

mysql_query("DELETE FROM `nano_kat` WHERE `id`='$id'");
echo "1";
}
?>