<?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('articlestagedetails',(int)$_GET['StageId'],'Id');
  if(!$unit){
    $session->msg("d","Missing article stage id.");
    redirect('articlestage.php');
  }
?>


<?php
if(isset($_POST['edit_uni'])){
  $req_field = array('sapcode','clicking','folding','cuttex','printingout','printingin','stitchingout','stitchingin');
    validate_fields($req_field);
    $StageId = remove_junk($db->escape($_POST['StageId']));
    $sapcode = remove_junk($db->escape($_POST['sapcode']));
    $clicking = remove_junk($db->escape($_POST['clicking']));
    $folding = remove_junk($db->escape($_POST['folding']));
    $cuttex = remove_junk($db->escape($_POST['cuttex']));
    $printingout = remove_junk($db->escape($_POST['printingout']));
    $printingin = remove_junk($db->escape($_POST['printingin']));
    $stitchingout = remove_junk($db->escape($_POST['stitchingout']));
    $stitchingin = remove_junk($db->escape($_POST['stitchingin']));
  
  if(empty($errors)){
        $sql = "UPDATE articlestagedetails SET SAPCode='{$sapcode}', Clicking={$clicking}, Folding={$folding}, Cuttex={$cuttex},";
        $sql .=" PrintingOUT={$printingout}, PrintingIN={$printingin}, StitchingOUT={$stitchingout}, StitchingIN={$stitchingin} ";
       $sql .= " WHERE Id={$unit['Id']}";
     $result = $db->query($sql);
     if($result && $db->affected_rows() === 1) {
       $session->msg("s", "Successfully updated Article Stage");
       redirect('articlestage.php',false);
     } else {
       $session->msg("d", "Sorry! Failed to Update");
       redirect('articlestage.php',false);
     }
  } else {
    $session->msg("d", $errors);
    redirect('articlestage.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['SAPCode']));?></span>
        </strong>
       </div>
       <div class="panel-body">
         <form method="post" action="edit_articlestage.php?StageId=<?php echo (int)$unit['Id'];?>">
           <div class="form-group">
               <input type="text" class="form-control" name="sapcode" value="<?php echo remove_junk(ucfirst($unit['SAPCode']));?>">
               <input type="number" class="form-control" name="clicking" value="<?php echo remove_junk(ucfirst($unit['Clicking']));?>">
               <input type="number" class="form-control" name="folding" value="<?php echo remove_junk(ucfirst($unit['Folding']));?>">
               <input type="number" class="form-control" name="cuttex" value="<?php echo remove_junk(ucfirst($unit['Cuttex']));?>">
               <input type="number" class="form-control" name="printingout" value="<?php echo remove_junk(ucfirst($unit['PrintingOUT']));?>">
               <input type="number" class="form-control" name="printingin" value="<?php echo remove_junk(ucfirst($unit['PrintingIN']));?>">
               <input type="number" class="form-control" name="stitchingout" value="<?php echo remove_junk(ucfirst($unit['StitchingOUT']));?>">
               <input type="number" class="form-control" name="stitchingin" value="<?php echo remove_junk(ucfirst($unit['StitchingIN']));?>">
            </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'); ?>
