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

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

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

            header("location:coas.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 style="color:green; font-size:20px ;font-weight:bold; " > <i class="fa fa-file-o" ></i> UPDATE Chart of accounts </p>
         
         <ol class="breadcrumb">
          
          <li> <a href="coas" > <button class="btn  btn-sm btn-default " > <i class="fa fa-bars "></i> VIEW COA GROUPS  </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-2">
                               <label>  Date  </label>
                               <input type="text" class="form-control datepickere " name="date"  value="<?php  echo $details["date"]?>"  autocomplete="off">
                               <input type="hidden" class="form-control  " name="cid"  value="<?php  echo $details["id"]?>"  autocomplete="off">
                           </div>  

                        
                           <div class="col-sm-3">
                               <label>  Select Coa Group   </label>  
                               <select class="form-control  js-example-basic-single  "  required="" name="cgroup" 
                                                        style="width:100%" >
                                                            <?php  $stmt = $conn->prepare("SELECT id,name FROM coa_group"); $stmt->execute();
                                                            foreach($stmt as $row)
                                                                {
                                                                    if( $row['id'] == $details["cgroup"] )
                                                                            { ?>
                                                                                <option selected value="<?php echo $details["cgroup"]  ?>" > 
                                                                                        <?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>  Select Subgroup   </label>   
                               <select class="form-control  js-example-basic-single  "  required="" name="subgroup"     style="width:100%" >
                                        <?php  $stmt = $conn->prepare("SELECT id,name FROM coa_group_sub"); $stmt->execute();
                                        foreach($stmt as $row)
                                            {
                                                if( $row['id'] == $details["subgroup"] )
                                                        { ?>
                                                            <option selected value="<?php echo $details["subgroup"]  ?>" > 
                                                                    <?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-4">
                               <label>  Chart of account  name    </label>
                               <input type="text" class="form-control " name="name"   value="<?php  echo $details["name"]?>"   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">  <i class="fa fa-plus"></i>  Update Coa </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>