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

<?php
if(isset($_POST['edit_pla'])){
  $req_field = array('rawmaterial-name','rawmaterial-SAP');
  validate_fields($req_field);
  $pla_name = remove_junk($db->escape($_POST['rawmaterial-name']));
  $pla_SAP = remove_junk($db->escape($_POST['rawmaterial-SAP']));



  if (empty($errors)) {
    $sql = "UPDATE rawmaterialdetails SET RawMaterialName='{$pla_name}', RawMaterialSAP='{$pla_SAP}'";
    $sql .= " WHERE RawMaterialId='{$rawmaterial['RawMaterialId']}'";
     $result = $db->query($sql);
     if($result && $db->affected_rows() === 1) {
       $session->msg("s", "Successfully updated Ra Material");
       redirect('rawmaterial.php',false);
     } else {
       $session->msg("d", "Sorry! Failed to Update");
       redirect('rawmaterial.php',false);
     }
  } else {
    $session->msg("d", $errors);
    redirect('rawmaterial.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($rawmaterial['RawMaterialName']));?></span>
        </strong>
       </div>
       <div class="panel-body">
       <form method="post" action="edit_rawmaterial.php?RawMaterialId=<?php echo (int)$rawmaterial['RawMaterialId'];?>">
          <div class="form-group">
            <input type="text" class="form-control" name="rawmaterial-name" value="<?php echo remove_junk(ucfirst($rawmaterial['RawMaterialName']));?>" placeholder="Raw Material Name">
          </div>
          <div class="form-group">
            <input type="text" class="form-control" name="rawmaterial-SAP" value="<?php echo remove_junk(ucfirst($rawmaterial['RawMaterialSAP']));?>" placeholder="SAP Code">
          </div>
          <button type="submit" name="edit_pla" class="btn btn-primary">Update RawMaterial</button>
        </form>
       </div>
     </div>
   </div>
</div>



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