<?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:category");   }

        else  {              
                $stmt = $conn->prepare("SELECT * FROM itemcategory  WHERE 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 ($_POST['name']);
            $code =               htmlspecialchars ($_POST['code']);
            $description =        htmlspecialchars ($_POST['description']);
             
            $sql = "UPDATE itemcategory SET type=:type,name=:name,code=:code,description=:description   WHERE id=:id " ;
            $stmt = $conn->prepare($sql)->execute(['type'=>$type,'name'=>$name,'code'=>$code,'description'=>$description,'id'=>$cid  ]);

            header("location:categories.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/smenuedit.php');   ?>
     
    <!-- /.sidebar -->
  </aside>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
     <section class="content-header" >
         <p class="listheaderedit" > <i class="fa fa-pencil " ></i> Edit Category  </p>
         
         <ol class="breadcrumb">
          
          <li> <a href="categories" > <button class="btn  btn-sm btn-default " > <i class="fa fa-bars "></i> ALL Categories  </button>    </a></li>
                   
        </ol>
      </section>

    <section class="content container-fluid" style="padding-top:0px;">           
        <div class="box box-default box-xs">
            <div class="box-body">
                <div class="row">
                    <form class="form-horizontal"  action="" method="post" >
                        <div class=" ">
                           
                            <div class="col-sm-3">
                               <label>  Category Type  </label>   
                               <select class="form-control  js-example-basic-single  "  required="" name="type"     style="width:100%" >
                                        <?php  $stmt = $conn->prepare("SELECT id,name FROM invgroup "); $stmt->execute();
                                        foreach($stmt as $row)
                                            {
                                                if( $row['id'] == $details["type"] )
                                                        { ?>
                                                            <option selected value="<?php echo $details["type"]  ?>" > 
                                                                    <?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-6">
                               <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  " name="code"  value="<?php  echo $details["code"]?>"  autocomplete="off">                             
                           </div>
                           <div class="col-sm-12">
                               <label>  Description </label>  
                               <input type="text" class="form-control  " name="description"  value="<?php  echo $details["description"]?>"  autocomplete="off">                             
                           </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-default col-md-3 subbtn ">  <i class="fa fa-plus"></i>  Update Branch </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>