<?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['btnsServicedetailsAdd'])) {
    $service_id=$_POST['selService'];
    $uploadDirectory = "uploads/servicedetails/";

    // Ensure the uploads directory exists
    if (!is_dir($uploadDirectory)) {
        mkdir($uploadDirectory, 0777, true);
    }

    $totalFiles = count($_FILES['images']['name']);
    
    for ($i = 0; $i < $totalFiles; $i++) {
        $fileName = $_FILES['images']['name'][$i];
        $fileTmpName = $_FILES['images']['tmp_name'][$i];
        $fileSize = $_FILES['images']['size'][$i];
        $fileError = $_FILES['images']['error'][$i];
        $fileType = $_FILES['images']['type'][$i];

        // Allowed file types
        $allowedTypes = ['image/jpeg', 'image/png', 'image/jpg', 'image/webp'];
        if (in_array($fileType, $allowedTypes)) {
            if ($fileError === 0) {
                if ($fileSize < 5000000) { // 5MB limit
                    // $uniqueFileName = uniqid('', true) . '-' . basename($fileName);
                    // $fileDestination = $uploadDirectory . $uniqueFileName;
                    $fileDestination = $uploadDirectory . $fileName;

                    if (move_uploaded_file($fileTmpName, $fileDestination)) {
                        // Insert into database
                        $query = "INSERT INTO tbl_servicedetailsimage (service_id, servicedetails_image) VALUES ('$service_id' , '$fileDestination')";
                        if (mysqli_query($con, $query)) {
                            // echo "File $fileName uploaded and stored successfully!<br>";
                          $status="OK";
                            // echo "<script>alert('Service Details Images Added');</script>";
                        } else {
                            // echo "Database error: " . mysqli_error($con) . "<br>";
                            // echo "<script>alert('Failed');</script>";
                        }
                    } else {
                        echo "Error uploading $fileName.<br>";
                    }
                } else {
                    echo "File $fileName is too large.<br>";
                }
            } else {
                echo "Error uploading $fileName. Error code: $fileError<br>";
            }
        } else {
            echo "Invalid file type for $fileName. Only JPG, JPEG, and PNG are allowed.<br>";
        }
    }
     if ($status=='OK') {
                          
                            echo "<script>alert('Service Details Images Added');</script>";
                        } else {
                            // echo "Database error: " . mysqli_error($con) . "<br>";
                            echo "<script>alert('Failed');</script>";
                        }

}


    
    
    ?>

<head>
  <style type="text/css">
    textarea{
      width: 310px;
      height: 110px;
      border-radius: var(--bs-border-radius);
    }
  </style>
</head>
  <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 Service details</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 Service details</h5></div>
              <div class="col-lg-5"><h5 class="card-title title-right"><a href="view_servicedetailsimage.php" class="btn btn-secondary">View All Service Image details</a></h5></div>
              </div>
              <div class="row mb-3">

              </div>
              <!-- General Form Elements -->
                <form name="Formservicedetails" method="post" enctype="multipart/form-data" class="pt-20">
                  <div class="row mb-3">
                  <label for="inputText" class="col-sm-3 col-form-label">Service Name</label>
                  <div class="col-sm-5">
                    <select id="selService" name="selService"   class="form-control" required style="appearance: auto;">
                      <option value="" disabled="" selected="">--Select Service--</option>
                      <?php
                    $selservice = mysqli_query($con,"SELECT * FROM tbl_services ORDER BY service_name ASC");
                    $i=1;
                    while($srow = mysqli_fetch_array($selservice)) {
                    ?>
                      
                        <option value="<?php echo $srow['service_id']; ?>"><?php echo $srow['service_name']; ?></option>
                      
                      <?php 
                       $i++;
                     }
                     ?>
                    </select>
                  </div>
                </div>
               
                <div class="row mb-3">
                  <label for="inputText" class="col-sm-3 col-form-label">Service details Image Upload</label>
                  <div class="col-sm-5">
                    <input class="form-control" type="file"  name="images[]" id="images" 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="btnsServicedetailsAdd" id="btnsServicedetailsAdd" 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');
   ?>