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

//if (filter_has_var(INPUT_POST, "edit")) 
//try{
         $id = $_POST['edit']; 
              if($id == '')
                    {   header("location:products"); }
        else  {              
                $stmt = $conn->prepare("SELECT a.*  FROM item a   WHERE a.id=?");
                $stmt->execute([$id]); $details = $stmt->fetch();                
                }
   //} catch(Exception $e) { echo 'Message: ' .$e->getMessage();} 

  if (filter_has_var(INPUT_POST, "submit")) 
     try{
            
            $date =            htmlspecialchars (strtoupper($_POST['date']));
            $date =            date("Y-m-d", strtotime($date));
            $cid =               htmlspecialchars (strtoupper($_POST['cid']));
       
           // $type =               htmlspecialchars ($_POST['type']);
            $name =                htmlspecialchars ( strtoupper($_POST['name']));
            //$code =                htmlspecialchars ($_POST['code']);
            $desc =         htmlspecialchars ( strtoupper($_POST['description']));
            $coagroup =            htmlspecialchars ($_POST['coagroup']);
            $category =            htmlspecialchars ($_POST['category']);
            $subcategory =         htmlspecialchars ($_POST['subcategory']);
            $uom =                 htmlspecialchars ($_POST['uom']);
            $rlevel =            htmlspecialchars ($_POST['reorderlevel']);
            $branch =              htmlspecialchars ($_POST['branch']);
            $brand =              htmlspecialchars ($_POST['brand']);
             
            $sql = "UPDATE item SET name=:name,description=:desc,uom=:uom,coagroup=:coagroup,category=:category,subcategory=:subcategory,reorderlevel=:rlevel   WHERE id=:id " ;
            $stmt = $conn->prepare($sql)->execute(['name'=>$name,'desc'=>$desc,'uom'=>$uom,'coagroup'=>$coagroup,'category'=>$category,'subcategory'=>$subcategory,'rlevel'=>$rlevel,'id'=>$cid  ]);

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

?>

<!DOCTYPE html>
<html>
<head>
<?php include_once('../inc/slink.php');   ?>

</head>
<body <?php include_once('../inc/snav.php'); ?>
<div class="wrapper">

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

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


    <!-- Header Navbar -->
       <?php include_once('../inc/snavbar.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/smenu.php');   ?>
     
    <!-- /.sidebar -->
  </aside>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
     <section class="content-header" >
         <p class="listheaderedit" style="color:orange;" > <i class="fa fa-pencil " ></i> EDIT PRODUCTS  </p>
         <ol class="breadcrumb">          
          <li> <a href="products" > <button class="btn  btn-sm btn-warning " > <i class="fa fa-arrow-left "></i> GO BACK TO PRODUCTS  </button>    </a></li>  
        </ol>
      </section>

    <section class="content container-fluid" style="padding-top:0px;">           
        <div class="box box-warning box-xs">
            <div class="box-body">
                <div class="row">
                    <form class="form-horizontal"  action="" method="post" >
                        <div class="">
                            <div class="col-sm-3">
                               <label>  Date   </label>  
                                  <input type="text" class="form-control datepicker " name="date"    autocomplete="off">                             
                            </div>
                            <div class="col-sm-3">
                               <label>  Name  </label>  
                               <input type="hidden" class="form-control  " name="cid"  value="<?php  echo $details["id"]?>"  autocomplete="off">
                               <input type="text" class="form-control  " name="name"  value="<?php  echo $details["name"]?>"  autocomplete="off">                             
                            </div>
                            <div class="col-sm-3">
                               <label>  Code  </label>  
                                   <input type="text" class="form-control" readonly="true" name="code"  value="<?php  echo $details["code"]?>"  autocomplete="off">                             
                            </div>
                            <div class="col-sm-3">
                               <label>  Description  </label>  
                                   <input type="text" class="form-control"  name="description"  value="<?php  echo $details["description"]?>"  autocomplete="off">                             
                            </div>
                            <div class="col-sm-3">
                               <label>  COA group </label>   
                               <select class="form-control  js-example-basic-single  "  required="" name="coagroup"     style="width:100%" >
                                        <?php  $stmt = $conn->prepare("SELECT * FROM coaitem where type=2 "); $stmt->execute();
                                        foreach($stmt as $row)
                                            {
                                                if( $row['id'] == $details["coagroup"] )
                                                        { ?>
                                                            <option selected value="<?php echo $details["coagroup"]  ?>" > 
                                                                    <?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>  Category </label>   
                               <select class="form-control  js-example-basic-single  "  required="" name="category"     style="width:100%" >
                                        <?php  $stmt = $conn->prepare("SELECT * FROM itemcategory  "); $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>  Sub Category </label>   
                               <select class="form-control  js-example-basic-single  "  required="" name="subcategory"     style="width:100%" >
                                        <?php  $stmt = $conn->prepare("SELECT * FROM itemcategorysub  "); $stmt->execute();
                                        foreach($stmt as $row)
                                            {
                                                if( $row['id'] == $details["subcategory"] )
                                                        { ?>
                                                            <option selected value="<?php echo $details["subcategory"]  ?>" > 
                                                                    <?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>  UOM  </label>   
                               <select class="form-control  js-example-basic-single  "  required="" name="uom"     style="width:100%" >
                                        <?php  $stmt = $conn->prepare("SELECT * 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-sm-3">
                               <label>  Reorder Level  </label>  
                               <input type="text" class="form-control " name="reorderlevel"  value="<?php echo $details["reorderlevel"]; ?>"  autocomplete="off">                             
                            </div>
                            <div class="col-sm-3">
                               <label>  Available in  </label>   
                               <select class="form-control  js-example-basic-single  "  required="" name="branch"     style="width:100%" >
                                         
                                        <?php
                                           if (  $details["branch"]==0)
                                                { ?>
                                                  <option selected value="0" > All branches  <option> <?php    
                                                }
                                            else {
                                                        $stmt = $conn->prepare("SELECT * FROM admbranch  "); $stmt->execute();
                                                        foreach($stmt as $row)
                                                            {
                                                                if( $row['id'] == $details["branch"] )
                                                                        { ?>
                                                                            <option selected value="<?php echo $details["branch"]  ?>" > 
                                                                                    <?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>  Brand  </label>   
                               <select class="form-control  js-example-basic-single  "  required="" name="brand"     style="width:100%" >
                                        <?php  $stmt = $conn->prepare("SELECT * FROM itembrand  "); $stmt->execute();
                                        foreach($stmt as $row)
                                            {
                                                if( $row['id'] == $details["brand"] )
                                                        { ?>
                                                            <option selected value="<?php echo $details["brand"]  ?>" > 
                                                                    <?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>  Status  </label>   
                               <select class="form-control  js-example-basic-single  "  required="" name="status"     style="width:100%" >
                                        <?php  $stmt = $conn->prepare("SELECT * FROM itemstatus  "); $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>                         

                        <div class="col-md-12">
                          <div class="form-group">                              
                           <div class="col-sm-offset-4 col-sm-10" style="padding-top:0.4rem;">
                                   <button type="submit"  name="submit" class="btn btn-warning col-md-3 subbtn ">  <i class="fa fa-plus"></i>  Update Price </button> </div>
                           </div> 
                        </div>    

                        
                    </form>
                </div>
            </div>
        </div>
                                
    </section>
</div>



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



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