<?php   include_once('config/db.php');   include_once('config/check.php');  


// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);

//if (filter_has_var(INPUT_POST, "edit")) 
//try{
    $id = $_POST['edit']; 
    if($id == '')
          {   header("location:item_rms");   }

else  {              
          $stmt = $conn->prepare("SELECT * FROM pitem  WHERE stockcoa=?");
          $stmt->execute([$id]); $details = $stmt->fetch();                
      }

//} catch(Exception $e) { echo 'Message: ' .$e->getMessage();} 

if (filter_has_var(INPUT_POST, "submit")) 
try{
    
    $cid =                  htmlspecialchars ( $_POST['cid']);
    $stockcoa =                  htmlspecialchars ( $_POST['stockcoa']);
    $name =                  htmlspecialchars ( $_POST['name']);
    $code =                  htmlspecialchars ( $_POST['code']);
    $uom =                        htmlspecialchars ( $_POST['uom']);
    $ctype =                       htmlspecialchars ( $_POST['ctype']);
    $category =                   htmlspecialchars ( $_POST['category']);
    $level =                    htmlspecialchars ( $_POST['reorderlevel']);
    //$cost =                       htmlspecialchars ( $_POST['cost']);
    $whouse =                  htmlspecialchars ( $_POST['warehouse']);
    $status =                     htmlspecialchars ( $_POST['status']);
    $desc =           htmlspecialchars ( $_POST['description']);

  $sql = "UPDATE  pitem  SET name=:name, code=:code,uom=:uom,category=:category, description=:desc ,reorderlevel=:level,warehouse=:whouse,status=:status  WHERE id=:id" ;
  $conn->prepare($sql)->execute([ 'name' => $name,'code' => $code,'uom' => $uom,'category'=>$category,'desc' => $desc ,'level'=> $level,'whouse'=> $whouse,'status'=>$status, 'id'=>$cid ]);

  $sql = "UPDATE  coa SET subgroup=:subgroup,name=:name, code=:code,uom=:uom, description=:desc ,reorderlevel=:level,warehouse=:whouse,active=:active  WHERE id=:stockid " ;
  $conn->prepare($sql)->execute([ 'subgroup'=>$category,'name' => $name,'code' => $code, 'uom' => $uom,'desc' => $desc ,'level'=> $level,'whouse'=> $whouse,'active'=>$status, 'stockid'=>$stockcoa ]);


    
    
  header("location:item_rms.php");
} catch(Exception $e) { echo 'Message: ' .$e->getMessage();}

?>

<!DOCTYPE html>
<html>
<head>
<?php include_once('inc/link.php');   ?>  
</head>
<body <?php include_once('inc/nav.php');  ?>>
<div class="wrapper">

  <!-- Main Header -->
    <header class="main-header">

    <!-- Logo -->
       <?php include_once('inc/brand.php');   ?>

    <!-- Header Navbar -->
    <?php include_once('inc/navbar.php');   ?>

  </header>
 
  <!-- Left side column. contains the logo and sidebar -->
    <aside class="main-sidebar">

    <!-- sidebar: style can be found in sidebar.less -->
     <?php include_once('inc/menu.php');   ?>
    <!-- /.sidebar -->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
        <section class="content-header">
          <h1>   <i class="fa fa-cubes"></i> UPDATE ITEM    </h1>
            <ol class="breadcrumb">
            <!-- <li><a href="#"><i class="fa fa-dashboard"></i> </a></li>
            <li><a href="#"> </a></li>
            <li class="active"> </li> -->
            <li><a href="invitemsl.php" class="btn btn-default btn-md"> <i class="fa fa-cubes"></i> View All Listed Items  </a></li>
            </ol>
        </section>


<!-- Main content -->

  <section class="content container-fluid"> 
        <div class="box box-default box-xs">
            <div class="box-body">
                <div class="row">
                    <form class="form-horizontal add_form "  action="" method="post"  >
                        <div class="">    
                                       
                            <div class="col-sm-2"> 
                                <label > Date added </label>  
                                <input type="hidden"   autocomplete="off" class="form-control " name="cid"  value="<?php  echo $details["id"]?>"  required=""   > 
                                <input type="hidden"   autocomplete="off" class="form-control " name="stockcoa"  value="<?php  echo $details["stockcoa"]?>"  required=""   > 
                                <input type="text"   autocomplete="off" class="form-control datepickere" name="date"  value="<?php  echo $details["date"]?>"  required=""   > 
                            </div>                                             
                            <div class="col-sm-4"> 
                                <label > Name  </label>  
                                    <input type="text"  name="name"  autocomplete="off" class="form-control"  value="<?php  echo $details["name"]?>"  required=""   > 
                            </div>                                                  
                            <div class="col-md-2">
                                <label > Code  </label>                          
                                <input type="text" class="form-control" name="code" value="<?php  echo $details["code"]?>"  required="" autocomplete="off" >
                            </div>    
                            <div class="col-md-4">
                                <label > Description  </label>                          
                                <input type="text" class="form-control" name="description"  value="<?php  echo $details["description"]?>" required="" autocomplete="off" >
                            </div>  
                            <div class="col-sm-3"> 
                                <label > Category </label>  
                                <select class="form-control  js-example-basic-single  "  required="" name="category" 
                                                  style="width:100%" >
                                             <?php  $stmt = $conn->prepare("SELECT id,name FROM coa_group_sub where cgroup=17 "); $stmt->execute();
                                               foreach($stmt as $row)
                                                   {
                                                      if( $row['id'] == $details["category"] )
                                                             { ?>
                                                                   <option selected value="<?php echo $details["category"]  ?>" > 
                                                                          <?php echo $row['name'] ?>  <option>
                                                                   <?php  }    
                                                       else  { ?>
                                                                    
                                                                    <option  value="<?php echo $row['id']; ?>" > 
                                                                              <?php echo $row['name'] ?>  <option>
                                                                    <?php } 
                                                    } 
                                              ?>
                                        </select>                                                                          
                            </div>    
                            <div class="col-sm-3"> 
                                <label > Item Uom </label>  
                                <select class="form-control  js-example-basic-single  "  required="" name="uom" 
                                                  style="width:100%" >
                                             <?php  $stmt = $conn->prepare("SELECT id,name FROM aduom"); $stmt->execute();
                                               foreach($stmt as $row)
                                                   {
                                                      if( $row['id'] == $details["uom"] )
                                                             { ?>
                                                                   <option selected value="<?php echo $details["uom"]  ?>" > 
                                                                          <?php echo $row['name'] ?>  <option>
                                                                   <?php  }    
                                                       else  { ?>
                                                                    
                                                                    <option  value="<?php echo $row['id']; ?>" > 
                                                                              <?php echo $row['name'] ?>  <option>
                                                                    <?php } 
                                                    } 
                                              ?>
                                        </select>                                                                          
                            </div>                                           
                        
                          
                            <div class="col-md-2">
                                <label >Item Reorder level    </label>                          
                                <input type="text" class="form-control" name="reorderlevel" value="<?php  echo $details["reorderlevel"]?>"   required="" autocomplete="off" >
                            </div>  
                            
                               
                            <div class="col-sm-2"> 
                                <label > Warehouse stored  </label>  
                                        <select class="form-control  js-example-basic-single  "  required="" name="warehouse" 
                                                  style="width:100%" >
                                             <?php  $stmt = $conn->prepare("SELECT id,name FROM invwarehouse"); $stmt->execute();
                                               foreach($stmt as $row)
                                                   {
                                                      if( $row['id'] == $details["warehouse"] )
                                                             { ?>
                                                                   <option selected value="<?php echo $details["warehouse"]  ?>" > 
                                                                          <?php echo $row['name'] ?>  <option>
                                                                   <?php  }    
                                                       else  { ?>
                                                                    
                                                                    <option  value="<?php echo $row['id']; ?>" > 
                                                                              <?php echo $row['name'] ?>  <option>
                                                                    <?php } 
                                                    } 
                                              ?>
                                        </select>

                                                                           
                            </div> 
                                                              
                            <div class="col-sm-2"> 
                                <label > Item Status   </label>  
                                <select class="form-control  js-example-basic-single  "  required="" name="status" 
                                                  style="width:100%" >
                                             <?php  $stmt = $conn->prepare("SELECT id,name FROM pitemactive "); $stmt->execute();
                                               foreach($stmt as $row)
                                                   {
                                                      if( $row['id'] == $details["status"] )
                                                             { ?>
                                                                   <option selected value="<?php echo $details["status"]  ?>" > 
                                                                          <?php echo $row['name'] ?>  <option>
                                                                   <?php  }    
                                                       else  { ?>
                                                                    
                                                                    <option  value="<?php echo $row['id']; ?>" > 
                                                                              <?php echo $row['name'] ?>  <option>
                                                                    <?php } 
                                                    } 
                                              ?>
                                        </select>                                        
                            </div>  
                           
                           
                                                               
                                                                                                          
                        </div>                           
                      
                       
                        
                        
                        <!-- submit button -->
                        <div class="form-group">                              
                            <div class="col-sm-offset-4 col-sm-10" style="padding:0.5rem;">
                                    <button type="submit"  name="submit" class="btn btn-warning col-md-3">  <i class="fa fa-cubes"></i>  Update Items   </button> </div>
                        </div> 
                    </form>               
                </div>
            </div>
        </div>
                                
    </section>

</div>
  <!-- /.content-wrapper -->
  
  <!-- Main Footer -->
  <footer class="main-footer">
    <?php  include_once('inc/footer.php'); ?>
  </footer>
  
</div>
                         
<!-- ./wrapper -->

<!-- REQUIRED JS SCRIPTS -->
<?php include_once('inc/linkjs.php') ?>

</body>
</html>