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

<?php
if(isset($_POST['edit_uni'])){
  $req_field = array('stage-name');
  validate_fields($req_field);
  $uni_name = remove_junk($db->escape($_POST['stage-name']));
  $sta_order = remove_junk($db->escape($_POST['stage-order']));
  
  if(empty($errors)){
        $sql = "UPDATE stagedetails SET StageName='{$uni_name}', StageOrder={$sta_order}";
       $sql .= " WHERE StageId={$unit['StageId']}";
     $result = $db->query($sql);
     if($result && $db->affected_rows() === 1) {
       $session->msg("s", "Successfully updated Stage");
       redirect('Stage.php',false);
     } else {
       $session->msg("d", "Sorry! Failed to Update");
       redirect('Stage.php',false);
     }
  } else {
    $session->msg("d", $errors);
    redirect('Stage.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($unit['StageName']));?></span>
        </strong>
       </div>
       <div class="panel-body">
         <form method="post" action="edit_stage.php?StageId=<?php echo (int)$unit['StageId'];?>">
           <div class="form-group">
               <input type="text" class="form-control" name="stage-name" value="<?php echo remove_junk(ucfirst($unit['StageName']));?>">
               <input type="number" class="form-control" name="stage-order" value="<?php echo remove_junk(ucfirst($unit['StageOrder']));?>">
            </div>
           <button type="submit" name="edit_uni" class="btn btn-primary">Update Stage</button>
       </form>
       </div>
     </div>
   </div>
</div>



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