<?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"])) {


    // Database connection check (Ensure $con is properly connected)
    if (!$con) {
        die("Database connection failed: " . mysqli_connect_error());
    }

    $service_id = mysqli_real_escape_string($con, $_POST['selService']);
    $status = false; // Initialize status

    for ($i = 1; $i <= 3; $i++) {

        $servicedetails_title = mysqli_real_escape_string($con, $_POST['txtServicedetailsTitle' . $i]);
        $servicedetails_content = mysqli_real_escape_string($con, $_POST['txtServicedetailsContent' . $i]);

        if (!empty($servicedetails_title) && !empty($servicedetails_content)) {

            $sql = "INSERT INTO tbl_servicedetails (service_id, servicedetails_title, servicedetails_content)
                    VALUES ('$service_id', '$servicedetails_title', '$servicedetails_content')";
            
            if (mysqli_query($con, $sql)) {
                $status = true;
            } else {
                echo "<script>alert('Error: " . mysqli_error($con) . "');</script>";
            }
        }
    }

    // Final Success/Failure Message
    if ($status) {
        echo "<script>alert('Service Details Added Successfully');</script>";
    } else {
        echo "<script>alert('Insertion Failed. Please check your data!');</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-11">
          <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_servicedetails.php" class="btn btn-secondary">View All Service 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-1 col-form-label">Sl No.</label>
                  <label for="inputText" class="col-sm-6 col-form-label">Service details Title</label>
                  <div class="col-sm-5">
                   <label for="inputText" class="col-sm-8 col-form-label">Service details Content</label>
                  </div>
                </div>
                <hr>
                <?php
                  for ($i=1; $i <=3; $i++) { 
                   
                ?>
               
                <div class="row mb-3">
                  <label for="inputText" class="col-sm-1 col-form-label"><?php echo $i; ?></label>
                  <div class="col-sm-6">
                   <input type="text" id="txtServicedetailsTitle<?php echo $i; ?>" name="txtServicedetailsTitle<?php echo $i; ?>" class="form-control"  >
                 </div>
                  <div class="col-sm-5">
                   <textarea  id="txtServicedetailsContent<?php echo $i; ?>" name="txtServicedetailsContent<?php echo $i; ?>" class="textarea1" style="width: 300px;" ></textarea>
                  </div>
                </div>

                <?php 
                  }
                ?>
              
                
               <!--  <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="fileImg" id="fileImg" required="">
                  </div>
                </div>   -->           
                <hr>
                <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>
    </section>

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