<?php
  $page_title = 'All Stages';
  require_once('includes/load.php');
  // Checkin What level user has permission to view this page
  page_require_level(1);
  
  $all_units = find_all('stagedetails')
?>
<?php
 if(isset($_POST['add_uni'])){
   $req_field = array('stage-name','stage-order');
   validate_fields($req_field);
   $sta_name = remove_junk($db->escape($_POST['stage-name']));
   $sta_order = remove_junk($db->escape($_POST['stage-order']));
   $userid = current_user();
     $p_userid = $userid['id'];
   if(empty($errors)){
      $sql  = "INSERT INTO stagedetails (StageName,StageOrder,id)";
      $sql .= " VALUES ('{$sta_name}',$sta_order,{$p_userid})";
      if($db->query($sql)){
        $session->msg("s", "Successfully Added New Stage");
        redirect('Stage.php',false);
      } else {
        $session->msg("d", "Sorry Failed to insert.");
        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>
   <div class="row">
    <div class="col-md-5">
      <div class="panel panel-default">
        <div class="panel-heading">
          <strong>
            <span class="glyphicon glyphicon-th"></span>
            <span>Add New Stage</span>
         </strong>
        </div>
        <div class="panel-body">
          <form method="post" action="Stage.php">
            <div class="form-group">
                <input type="text" class="form-control" name="stage-name" placeholder="Stage Name">
            </div>
            <div class="form-group">
                <input type="number" class="form-control" name="stage-order" placeholder="Stage Order">
            </div>
            <button type="submit" name="add_uni" class="btn btn-primary">Add Stage</button>
        </form>
        </div>
      </div>
    </div>
    <div class="col-md-7">
    <div class="panel panel-default">
      <div class="panel-heading">
        <strong>
          <span class="glyphicon glyphicon-th"></span>
          <span>All Stages</span>
       </strong>
      </div>
        <div class="panel-body">
          <table id="Unittable" class="table table-bordered table-striped table-hover">
            <thead>
                <tr>
                    <th class="text-center" style="width: 50px;">#</th>
                    <th>Stages</th>
                    <th>Stages Order</th>
                    <th class="text-center" style="width: 100px;">Actions</th>
                </tr>
            </thead>
            <tbody>
              <?php foreach ($all_units as $uni):?>
                <tr>
                    <td class="text-center"><?php echo count_id();?></td>
                    <td><?php echo remove_junk(ucfirst($uni['StageName'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($uni['StageOrder'])); ?></td>
                    <td class="text-center">
                      <div class="btn-group">
                        <a href="edit_stage.php?StageId=<?php echo (int)$uni['StageId'];?>"  class="btn btn-xs btn-warning" data-toggle="tooltip" title="Edit">
                          <span class="glyphicon glyphicon-edit"></span>
                        </a>
                        <a href="delete_stage.php?StageId=<?php echo (int)$uni['StageId'];?>"  class="btn btn-xs btn-danger" data-toggle="tooltip" title="Remove" onclick="return confirm('Are you sure you want to delete this item?');">
                          <span class="glyphicon glyphicon-trash"></span>
                        </a>
                      </div>
                    </td>

                </tr>
              <?php endforeach; ?>
            </tbody>
          </table>
       </div>
    </div>
    </div>
   </div>
  </div>
  <?php include_once('layouts/footer.php'); ?>
