<?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');
?>
<head>
  <style type="text/css">
    textarea{
      width: 400px;
      height: 110px;
      border-radius: var(--bs-border-radius);
    }
  </style>
</head>
<?php


    if(isset($_GET['id'])){
        $hid=$_GET['id'];
    }
    
    if(isset($_POST["btnServiceAdd"]))
    {
        $servicename=$_POST['txtServiceName'];
        $description=$_POST['txtDescription'];
        
        $filename = $_FILES["fileImg"]["name"]; 
        $tempname = $_FILES["fileImg"]["tmp_name"]; 
        
        $filename1 = $_FILES["fileImg1"]["name"]; 
        $tempname1 = $_FILES["fileImg1"]["tmp_name"]; 

        if($filename==""){
             $folder=$_POST['imagepath'];
        }
        else {
            $folder ="uploads/services/".$filename;
        }
        if($filename1==""){
             $folder1=$_POST['imagepath1'];
        }
        else {
            $folder1 ="uploads/serviceimage/".$filename1;
        }
        $oldfile=$_POST['imagepath'];
        $newfile ="uploads/services/".$filename;
        
        $oldfile1=$_POST['imagepath1'];
        $newfile1 ="uploads/serviceimage/".$filename1;

        $insert1 = mysqli_query($con,"UPDATE `tbl_services` SET `service_name`='$servicename',`service_description`='$description',`service_icon`='$folder', `service_image`='$folder1' WHERE  service_id=$hid");

         if($insert1) {
                if($filename!=""){
                    $fold=move_uploaded_file($tempname, $folder);
                    $fold1=move_uploaded_file($tempname1, $folder1);
                     if($oldfile!=$newfile){
                        unlink($oldfile);
                    }
                     if($oldfile1!=$newfile1){
                        unlink($oldfile1);
                    }
                }
                if($filename1!=""){
                  
                    $fold1=move_uploaded_file($tempname1, $folder1);
                     
                     if($oldfile1!=$newfile1){
                        unlink($oldfile1);
                    }
                }
                echo "<script>alert('Service Updated');</script>";

            }
            else{
                echo "<script>alert('Updation Failed!');</script>";
            }    

    }
    ?>

  <main id="main" class="main">
    <div class="pagetitle">   
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="index.html">Home</a></li>
          <li class="breadcrumb-item active">Update Service</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">Update Service</h5></div>
              <div class="col-lg-5"><h5 class="card-title"><a href="view_services.php" class="btn btn-secondary">View All Services</a></h5></div>
              </div>           
                <?php
                        $selres = mysqli_query($con,"SELECT * FROM tbl_services WHERE service_id=$hid");
                         while($rowres = mysqli_fetch_array($selres)) {                                
                ?>

                <!-- 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">Service Name</label>
                  <div class="col-sm-5">
                    <input type="text" id="txtServiceName" name="txtServiceName" value="<?php echo $rowres['service_name']; ?>" class="form-control" required="">
                  </div>
                </div>    
                 <div class="row mb-3">
                  <label for="inputText" class="col-sm-3 col-form-label">Description</label>
                  <div class="col-sm-5">
                    <textarea  id="txtDescription" name="txtDescription" required=""><?php echo $rowres['service_description']; ?></textarea>
                  </div>
                </div>   
                <div class="row mb-3">
                  <label for="inputText" class="col-sm-3 col-form-label">Service Icon</label>
                  <div class="col-sm-5">
                    <input type="text" id="image_u" name="imagepath" class="form-control" value="<?php echo $rowres['service_icon']; ?>" hidden>
                    <img  height="100px" id="image_u1" src="<?php echo $rowres['service_icon']; ?>"><br><br>
                    <input type="file" name="fileImg" id="fileImg" height="100"  value="">    
                  </div>
                </div>   
                 <div class="row mb-3">
                  <label for="inputText" class="col-sm-3 col-form-label">Service Image</label>
                  <div class="col-sm-5">
                    <input type="text" id="image_uu" name="imagepath1" class="form-control" value="<?php echo $rowres['service_image']; ?>" hidden>
                    <img  height="100px" id="image_uu1" src="<?php echo $rowres['service_image']; ?>"><br><br>
                    <input type="file" name="fileImg1" id="fileImg1" height="100"  value="">    
                  </div>
                </div>  
                <div class="row mb-3">
                  <label class="col-sm-3 col-form-label"></label>
                  <div class="col-sm-5 ">
                     <input type="submit" class="btn btn-primary btn-center" name="btnServiceAdd" id="btnServiceAdd" value="Update">
                    
                  </div>
                </div>

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

              
                 <?php
                }
                ?>
            </div>
          </div>

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

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