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

        else  {              
                $stmt = $conn->prepare("SELECT * FROM admbranch  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']));
       
            $name =               htmlspecialchars ($_POST['name']);
            $code =               htmlspecialchars ($_POST['code']);
            $phone =              htmlspecialchars ($_POST['phone']);
            $email =              htmlspecialchars ($_POST['email']);
            $dcurrency =          htmlspecialchars ($_POST['currency']);

            $pin =                htmlspecialchars ($_POST['pin']);
            $vat =                htmlspecialchars ($_POST['vat']);
            $address =            htmlspecialchars ($_POST['address']);
            $location =            htmlspecialchars ($_POST['location']);
            $country =             htmlspecialchars ($_POST['country']);

                       
            $sql = "UPDATE admbranch SET date=:date,name=:name,code=:code,phone=:phone,dcurrency=:dcurrency,email=:email,pin=:pin,vat=:vat,address=:address,location=:location,country=:country   WHERE id=:id " ;
            $stmt = $conn->prepare($sql)->execute(['date'=>$date,'name'=>$name,'code'=>$code,'phone'=>$phone,
                                                   'dcurrency'=>$dcurrency,'email'=>$email,'pin'=>$pin,
                                                   'vat'=>$vat,'address'=>$address,'location'=>$location,
                                                   'country'=>$country,'id'=>$cid  ]);

            header("location:branches.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/smenuc.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 branch name  </p>
         
         <ol class="breadcrumb">
          
          <li> <a href="branches" > <button class="btn  btn-sm btn-default " > <i class="fa fa-bars "></i> ALL Branches  </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-4">
                               <label>  Name   </label>  
                               <input type="text" class="form-control  " name="name"  value="<?php  echo $details["name"]?>"  autocomplete="off">                             
                           </div>

                           <div class="col-sm-2">
                               <label>  Code  </label>  
                               <input type="text" class="form-control  " name="code"  value="<?php  echo $details["code"]?>"  autocomplete="off">                             
                           </div>

                           <div class="col-sm-4">
                               <label>  Phone  </label>  
                               <input type="text" class="form-control  " name="phone"  value="<?php  echo $details["phone"]?>"  autocomplete="off">                             
                           </div>
                           <div class="col-sm-2">
                               <label>  Currency  </label>   
                               <select class="form-control  js-example-basic-single  "  required="" name="currency"     style="width:100%" >
                                        <?php  $stmt = $conn->prepare("SELECT id,name FROM acccurrency "); $stmt->execute();
                                        foreach($stmt as $row)
                                            {
                                                if( $row['id'] == $details["dcurrency"] )
                                                        { ?>
                                                            <option selected value="<?php echo $details["dcurrency"]  ?>" > 
                                                                    <?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>  Email  </label>  
                               <input type="text" class="form-control  " name="email"  value="<?php  echo $details["email"]?>"  autocomplete="off">                             
                           </div>
                           <div class="col-sm-2">
                               <label>  Pin  </label>  
                               <input type="text" class="form-control  " name="pin"  value="<?php  echo $details["pin"]?>"  autocomplete="off">                             
                           </div>
                           <div class="col-sm-2">
                               <label>  Vat  </label>  
                               <input type="text" class="form-control  " name="vat"  value="<?php  echo $details["pin"]?>"  autocomplete="off">                             
                           </div>
                           <div class="col-sm-2">
                               <label>  Address  </label>  
                               <input type="text" class="form-control  " name="address"  value="<?php  echo $details["address"]?>"  autocomplete="off">                             
                           </div>
                           <div class="col-sm-2">
                               <label>  Location  </label>  
                               <input type="text" class="form-control  " name="location"  value="<?php  echo $details["location"]?>"  autocomplete="off">                             
                           </div>
                            <div class="col-sm-2">
                               <label>  Country </label>   
                               <select class="form-control  js-example-basic-single  "  required="" name="country"     style="width:100%" >
                                        <?php  $stmt = $conn->prepare("SELECT id,name FROM adcountry "); $stmt->execute();
                                        foreach($stmt as $row)
                                            {
                                                if( $row['id'] == $details["country"] )
                                                        { ?>
                                                            <option selected value="<?php echo $details["country"]  ?>" > 
                                                                    <?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-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>