<?php
  $page_title = 'Edit article';
  require_once('includes/load.php');
  // Checkin What level user has permission to view this page
  page_require_level(1);
?>
<?php
  //Display all articles.
  $article = find_by_id_new('articledetails',(int)$_GET['ArticleId'],'ArticleId');
  if(!$article){
    $session->msg("d","Missing article id.");
    redirect('article.php');
  }
?>

<?php
if(isset($_POST['edit_art'])){
  $req_field = array('article-name');
  validate_fields($req_field);
  $art_name = remove_junk($db->escape($_POST['article-name']));
  
  if(empty($errors)){
        $sql = "UPDATE articledetails SET ArticleName='{$art_name}'";
       $sql .= " WHERE ArticleId='{$article['ArticleId']}'";
     $result = $db->query($sql);
     if($result && $db->affected_rows() === 1) {
       $session->msg("s", "Successfully updated Article");
       redirect('article.php',false);
     } else {
       $session->msg("d", "Sorry! Failed to Update");
       redirect('article.php',false);
     }
  } else {
    $session->msg("d", $errors);
    redirect('article.php',false);
  }
}
?>
<?php include_once('layouts/header.php'); ?>

<div class="row">
   <div class="col-md-12">
     <?php echo display_msg($msg); ?>
   </div>
   <div class="col-md-5">
     <div class="panel panel-default">
       <div class="panel-heading">
         <strong>
           <span class="glyphicon glyphicon-th"></span>
           <span>Editing <?php echo remove_junk(ucfirst($article['ArticleName']));?></span>
        </strong>
       </div>
       <div class="panel-body">
         <form method="post" action="edit_article.php?ArticleId=<?php echo (int)$article['ArticleId'];?>">
           <div class="form-group">
               <input type="text" class="form-control" name="article-name" value="<?php echo remove_junk(ucfirst($article['ArticleName']));?>">
           </div>
           <button type="submit" name="edit_art" class="btn btn-primary">Update article</button>
       </form>
       </div>
     </div>
   </div>
</div>



<?php include_once('layouts/footer.php'); ?>
