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

<?php
if(isset($_POST['edit_pla'])){
  $req_field = array('plan-name');
  validate_fields($req_field);
  $pla_name = remove_junk($db->escape($_POST['plan-name']));
  $start_date = remove_junk($db->escape($_POST['start-date']));
  $end_date = remove_junk($db->escape($_POST['end-date']));


  if (empty($errors)) {
    $sql = "UPDATE plandetails SET PlanName='{$pla_name}', StartDate='{$start_date}', EndDate='{$end_date}'";
    $sql .= " WHERE PlanId='{$plan['PlanId']}'";
     $result = $db->query($sql);
     if($result && $db->affected_rows() === 1) {
       $session->msg("s", "Successfully updated Plan");
       redirect('plan.php',false);
     } else {
       $session->msg("d", "Sorry! Failed to Update");
       redirect('plan.php',false);
     }
  } else {
    $session->msg("d", $errors);
    redirect('plan.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($plan['PlanName']));?></span>
        </strong>
       </div>
       <div class="panel-body">
       <form method="post" action="edit_plan.php?PlanId=<?php echo (int)$plan['PlanId'];?>">
          <div class="form-group">
            <input type="text" class="form-control" name="plan-name" value="<?php echo remove_junk(ucfirst($plan['PlanName']));?>" placeholder="Plan Name">
          </div>
          <div class="form-group">
            <label for="start-date">Start Date (YYYY-MM-DD)</label>
            <input type="date" class="form-control" name="start-date" id="start-date" value="<?php echo $plan['StartDate'];?>" >
          </div>
          <div class="form-group">
            <label for="end-date">End Date (YYYY-MM-DD)</label>
            <input type="date" class="form-control" name="end-date" id="end-date" value="<?php echo $plan['EndDate'];?>" >
          </div>
          <button type="submit" name="edit_pla" class="btn btn-primary">Update Plan</button>
        </form>
       </div>
     </div>
   </div>
</div>



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