<?php
   include_once('connect.php');
    session_start(); 
    
    // Check if the user is logged in
    if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
         echo "<script type='text/javascript'>window.location.href = 'index.php';</script>"; // Redirect to login page
        exit();
    }
    
    
    include_once('head.php');
?>
<?php
    
   if (isset($_POST["btnGalleryAdd"])) {
    $countFiles = count($_FILES['fileImg']['name']);
    $successCount = 0;
    $failCount = 0;

    for ($i = 0; $i < $countFiles; $i++) {
        $filename = $_FILES["fileImg"]["name"][$i];
        $tempname = $_FILES["fileImg"]["tmp_name"][$i];
        $folder = "uploads/gallery/" . basename($filename);

        if (move_uploaded_file($tempname, $folder)) {
            $sql = mysqli_query($con, "INSERT INTO tbl_gallery (gallery_image) VALUES ('$folder')");
            if ($sql) {
                $successCount++;
            } else {
                $failCount++;
            }
        } else {
            $failCount++;
        }
    }

    mysqli_close($con);

    if ($successCount > 0) {
        echo "<script>alert('$successCount image(s) uploaded successfully! $failCount failed.');</script>";
    } else {
        echo "<script>alert('Image upload failed!');</script>";
    }
}
    ?>


  <main id="main" class="main">

    <div class="pagetitle">
      <!-- <h1>Dashboard</h1> -->
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="index.html">Home</a></li>
          <li class="breadcrumb-item active">Add Gallery</li>
        </ol>
      </nav>
    </div><!-- End Page Title -->
     <section class="section">
      <div class="row">
        <div class="col-lg-1">
        </div>
        <div class="col-lg-10">
          <div class="card">
            <div class="card-body">
              <div class="row mb-3 bord-bottom">
              <div class="col-lg-5"><h5 class="card-title">Add New Gallery</h5></div>
              <div class="col-lg-5"><h5 class="card-title title-right"><a href="view_gallery.php" class="btn btn-secondary">View All Gallery Images</a></h5></div>
              </div>
              <div class="row mb-3">

              </div>
              <!-- General Form Elements -->
                <form name="FormPromoter" method="post" enctype="multipart/form-data" class="pt-20">
                <!-- <div class="row mb-3">
                  <label for="inputText" class="col-sm-3 col-form-label">Gallery Type</label>
                  <div class="col-sm-5">
                    <input type="text" id="txtGalleryType" name="txtGalleryType" class="form-control" required="">
                  </div>
                </div> -->
                <div class="row mb-3">
                  <label for="inputText" class="col-sm-3 col-form-label">Gallery Image Upload</label>
                  <div class="col-sm-5">
                    <input class="form-control" type="file"  name="fileImg[]" id="fileImg" required="" multiple>
                  </div>
                </div>             
                <div class="row mb-3 pt-20">
                  <label class="col-sm-3 col-form-label"></label>
                  <div class="col-sm-5 "> 
                    <input type="submit" class="btn btn-primary btn-center" name="btnGalleryAdd" id="btnGalleryAdd" value="Submit">
                    
                  </div>
                </div>

              </form><!-- End General Form Elements -->

            </div>
          </div>

        </div>
         <div class="col-lg-1">
        </div>
      </div>
    </section>

  </main><!-- End #main -->
  <?php
        include_once('footer.php');
   ?>