<?php
 session_start();
    if(!$_SESSION['id']){
        header('location:index.php');
    }
?>
<?php 
  require_once '../system/conn.php';
  if(isset($_POST["submit"]))
  {
    $updated_date=date("Y-m-d");
    $name= $_POST['name'];
    $branch= $_POST['branch'];
    $id= $_GET['id'];

    $query = "UPDATE `site` SET name='$name' , branch='$branch' , updated_date='$updated_date' where id='$id' ";
    //print_r($query);exit;
    $statement = $conn->prepare($query)->execute();
    

    if($statement){
       echo "<script>alert('Site name updated successfully.')</script>";
       echo " <script>window.location.assign('site-list.php')</script>";
    }else{
      echo "Not updated";
    }
  } 
?>
<!doctype html>
<html lang="en" class="semi-dark">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
	<link href="assets/css/pace.min.css" rel="stylesheet" />
	<script src="assets/js/pace.min.js"></script>

    <link href="assets/plugins/perfect-scrollbar/css/perfect-scrollbar.css" rel="stylesheet" />
    <link href="assets/plugins/simplebar/css/simplebar.css" rel="stylesheet" />
    <link href="assets/plugins/metismenu/css/metisMenu.min.css" rel="stylesheet" />

    <link href="assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/bootstrap-extended.css" rel="stylesheet">
    <link href="assets/css/style.css" rel="stylesheet">
    <link href="assets/css/icons.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap" rel="stylesheet">

    <link href="assets/css/dark-theme.css" rel="stylesheet" />
    <link href="assets/css/semi-dark.css" rel="stylesheet" />
    <link href="assets/css/header-colors.css" rel="stylesheet" />
    <title>Edit Site</title>
  </head>
  <body>
    

  <!--start wrapper-->
  <div class="wrapper">
    <?php include'system/header.php'?>

    <div class="page-content-wrapper">
      <div class="page-content">

        <div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
          <div class="breadcrumb-title pe-3">Edit Site</div>
          <div class="ps-3">
            <nav aria-label="breadcrumb">
              <ol class="breadcrumb mb-0 p-0 align-items-center">
                <li class="breadcrumb-item"><a href="javascript:;">Site Management</a></li>
                <li class="breadcrumb-item active" aria-current="page">Edit Site</li>
              </ol>
            </nav>
          </div>
          <div class="ms-auto"></div>
        </div>

        <div class="row">
          <div class="col-xl-12 mx-auto">
            <h6 class="mb-0 text-uppercase">Edit Site</h6>
            <hr/>
            <div class="card">
              <div class="card-body">
                <div class="p-4 border rounded">
                  <?php
                    $sth = $conn->prepare("SELECT * FROM `site` where id='".$_GET['id']."' ");
                    $sth->execute();
                    $row = $sth->fetch(PDO::FETCH_ASSOC); 
                  ?> 
                  <form class="row g-3 needs-validation" novalidate method="post" enctype="multipart/form-data" action="">
                    <div class="col-md-6">
                      <label for="validationCustom01" class="form-label">Enter Site Name</label>
                      <input type="text" class="form-control" id="validationCustom01" placeholder="Enter Site Name" name="name" value="<?php echo $row['name']; ?>" required>
                      <div class="valid-feedback">Enter Site Name!</div>
                    </div>
                    <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">Branch Name</label>
                          <select class="form-control" id="validationCustom01" name="branch" placeholder="Branch" required>
                              <option value="<?php echo $row['branch']; ?>">
                                <?php
                                    $sth1 = $conn->prepare("SELECT * FROM `branch` where id='".$row['branch']."' ");
                                    $sth1->execute();
                                    $row1 = $sth1->fetch(PDO::FETCH_ASSOC); 
                                ?> 
                                <?php echo $row1['name']; ?>
                              </option>
                              <?php
                                $stmt = $conn->prepare("SELECT * FROM `branch` ");
                                $stmt->execute();
                                $sql1 = $stmt->fetchAll();
                                foreach($sql1 as $row) 
                                { ?>
                                  <option value="<?php echo $row['id'];?>"><?php echo $row['name'];?></option>  
                              <?php } ?>
                          </select>
                          <div class="valid-feedback">Enter Branch!</div>
                    </div>
                    <div class="col-12">
                      <button class="btn btn-primary" type="submit" name="submit" value="submit" >Submit form</button>
                    </div>
                  </form>
                </div>
              </div>
            </div>
          </div>
        </div>

      </div>
    </div>
<?php include'system/footer.php'?>