<?php

if(isset($_POST['textnew']))
	{
		$q = $mysql->prepare("INSERT INTO db_news SET `Title` = ?, `Text` = ?, `DateAdd` = ?");
		$q->execute(array($_POST['title'], $_POST['textnew'], time()));
		Header("Location: /developer/index.php/?admpage=news");
		exit;
	}
	
    if(isset($_GET['dell']))
    {
        $dell = (int)$_GET['dell'];
        $mysql->query("DELETE FROM db_news WHERE Uid = '$dell'");
        Header("Location: /developer/index.php/?admpage=news");
            exit;
    }
	
    if(isset($_GET['add'])) { ?>

    <form class="form-horizontal" role="form" method="post" action=""><!-- Basic inputs -->
        <input type="hidden" name="uid" value="<?=$r['Uid']; ?>">
        <div class="panel panel-default">
            <div class="panel-heading"><h6 class="panel-title"><i class="icon-bubble4"></i> Редактирование пользователя <b><?=$r['Login']; ?></b></h6></div>
            <div class="panel-body">
                <div class="form-group"><label class="col-sm-2 control-label">Название новости: </label>
                    <div class="col-sm-10"><input type="text" name="title" value="" class="form-control"></div>
                </div>
                <div class="block-inner"><textarea class="editor" name="textnew" placeholder="Текст новости ..."></textarea></div>
                <div class="form-actions text-right">
                    <button type="submit" class="btn btn-primary">Сохранить</button>
                </div>
            </div>
        </div>
    </form>                                                     

    <?php return; } ?>

<?php

if(isset($_POST['textread']))
{
	$id = intval($_POST['uid']);
	$title = clean($_POST['title']);
	$text = $_POST['textread'];
	$q = $mysql->prepare("UPDATE db_news SET `Title` = ?, `Text` = ? WHERE Uid = ?");
	$q->execute(array($title, $text, $id));
	Header("Location: /developer/index.php/?admpage=news");
		exit;
}


if(isset($_GET['read']))
{
	$id = intval($_GET['read']);
	$q = $mysql->query("SELECT * FROM db_news WHERE Uid = '$id'");
	$r = $q->fetch();
?>

<form class="form-horizontal" role="form" method="post" action=""><!-- Basic inputs -->
	<input type="hidden" name="uid" value="<?=$r['Uid']; ?>">
            <div class="panel panel-default">
                <div class="panel-heading"><h6 class="panel-title"><i class="icon-bubble4"></i> Редактирование пользователя <b><?=$r['Login']; ?></b></h6></div>
                <div class="panel-body">
                    <input type="hidden" name="uid" value="<?=$r['Uid']; ?>">
                    <div class="form-group"><label class="col-sm-2 control-label">Название новости: </label>

                        <div class="col-sm-10"><input type="text" name="title" value="<?=$r['Title']; ?>" class="form-control"></div>
                    </div>
					
					 <div class="block-inner"><textarea class="editor" name="textread" placeholder="Текст новости ..."><?=$r['Text']; ?></textarea></div>
                    
					


                    
                   <div class="form-actions text-right">
                        <button type="submit" class="btn btn-primary">Сохранить</button>
                    </div>
                </div>
            </div>
           
           
            </form>
<?
return;
}
?>


<div class="tabbable page-tabs">
           
            <div class="tab-content"> <!-- First tab content -->
                <div class="tab-pane active fade in" id="inside"> <!-- Default datatable inside panel -->
                    <div class="panel panel-default">
                        <div class="panel-heading"><h6 class="panel-title"><i class="icon-table"></i> Список новостей --->>> <a href="/developer/index.php/?admpage=news&add">Добавить новость<a/></h6></div>
                        <div class="datatable">
                            <table class="table">
                                <thead>
                                <tr>
                                    <th>#ID</th>
                                    <th>Название</th>
                                   
                                  
                                   
                                    <th>Дата</th>
                                    <th>Действие</th>
                                    
                                </tr>
                                </thead>
                                <tbody>
								<?
								$q = $mysql->query("SELECT * FROM db_news ORDER BY Uid DESC");
								while($row = $q->fetch())
								{
									
								?>
                                <tr>
                                    <td><?=$row['Uid']; ?></td>
                                    <td><?=$row['Title']; ?></td>
                                 
									
                                    <td><?=date("d.m.Y", $row['DateAdd']); ?></td>
                                     <td><a href="/developer/index.php/?admpage=news&read=<?=$row['Uid']; ?>" class="btn btn-link">Редактировать</a> 
									|| <a href="/developer/index.php/?admpage=news&dell=<?=$row['Uid']; ?>" class="btn btn-link">Удалить</a></td>
                                   
                                </tr>
								<? } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
					
					
                    </div>
                    </div>
                    </div>