<?php
include("checklogin.php");
include("../includes/inc.php");

$metatitle ="Add | Edit Testimonials";
include("header.php");
?>

<section class="content-header mt-2">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-sm-6">
        <h6>Add | Edit Testimonials</h6>
      </div>
      <div class="col-sm-6 text-right">
         <ol class="breadcrumb float-sm-right">
          <li class="breadcrumb-item"><a href="<?php echo ADMIN_SITE_URL;?>"> <i class="fa fa-dashboard"></i> Dashboard</a></li>
          <li class="breadcrumb-item"><a href="<?php echo ADMIN_SITE_URL ?>/manage_testimonials.php?status=Y">Manage Testimonials</a></li>
          <li class="breadcrumb-item active">Add | Edit Testimonials</li>
        </ol>
      </div>
    </div>
  </div>
</section>

<section class="content-header mt-4">
  <div class="container-fluid"> 
    <?php
    if (isset($_POST['pID']) && $_POST['pID']=='addeditForm') {
      $_REQUEST['slno'] = $_POST['slno'];

      if (!empty($_POST['slno'])) {
        // --- Update existing ---
        db_query("UPDATE site_testimonials SET 
          reviewer_email='".addslashes($_POST['reviewer_email'])."', 
          
          reviewer_name  = '".db_real_escape(trim($_POST['reviewer_name']))."', 
          review_text = '".db_real_escape(trim($_POST['review_text']))."', 
          display_status='".$_POST['display_status']."', 
          rating='".$_POST['rating']."' 
          WHERE slno='".$_POST['slno']."' ");

        // Handle image upload on edit
        if (!empty($_FILES['menuImage1']['name'])) {
            $ext = strtolower(pathinfo($_FILES['menuImage1']['name'], PATHINFO_EXTENSION));
            $allowed = ['jpg', 'jpeg', 'png'];
            if (in_array($ext, $allowed)) {
                $image_name = $_POST['slno'] . "." . $ext;
                $upload_dir = BASEDIR . "/testimonial/";
                if (!is_dir($upload_dir)) {
                    mkdir($upload_dir, 0777, true);
                }
                $image_upload_path = $upload_dir . $image_name;
                if (move_uploaded_file($_FILES['menuImage1']['tmp_name'], $image_upload_path)) {
                    db_query("UPDATE site_testimonials SET pre_img='" . $image_name . "' WHERE slno='" . $_POST['slno'] . "' ");
                }
            }
        }

        ?>
        <script type="text/javascript">
        window.location.href = "<?php echo ADMIN_SITE_URL;?>/manage_testimonials.php?status=<?php echo $_POST['display_status'];?>&success=yes";  
        </script>
        <?php
        exit;            
      }
      else {
        // --- Insert new ---
        db_query("INSERT IGNORE INTO site_testimonials SET 
          reviewer_email='".addslashes($_POST['reviewer_email'])."', 
          
          reviewer_name  = '".db_real_escape(trim($_POST['reviewer_name']))."', 
          review_text = '".db_real_escape(trim($_POST['review_text']))."', 
          display_status='".$_POST['display_status']."', 
          rating='".$_POST['rating']."', 
          recv_date_time='".date("Y-m-d h:i:s")."' ");

        $insert_id = db_insert_id(); // Get newly inserted ID

        // Handle image upload on add
        if (!empty($_FILES['menuImage1']['name']) && $insert_id > 0) {
            $ext = strtolower(pathinfo($_FILES['menuImage1']['name'], PATHINFO_EXTENSION));
            $allowed = ['jpg', 'jpeg', 'png'];
            if (in_array($ext, $allowed)) {
                $image_name = $insert_id . "." . $ext;
                $upload_dir = BASEDIR . "/testimonial/";
                if (!is_dir($upload_dir)) {
                    mkdir($upload_dir, 0777, true);
                }
                $image_upload_path = $upload_dir . $image_name;
                if (move_uploaded_file($_FILES['menuImage1']['tmp_name'], $image_upload_path)) {
                    db_query("UPDATE site_testimonials SET pre_img='" . $image_name . "' WHERE slno='" . $insert_id . "' ");
                }
            }
        }

        ?>
        <script type="text/javascript">
        window.location.href = "<?php echo ADMIN_SITE_URL;?>/manage_testimonials.php?status=<?php echo $_POST['display_status'];?>&success=yes";  
        </script>
        <?php
        exit;
      }
    }

    if(!empty($_REQUEST['slno'])) {
      $stestimonials_qry = db_query("SELECT * FROM site_testimonials WHERE slno ='".$_REQUEST['slno']."'");
      if(db_num_rows($stestimonials_qry)>0) {
        $testiArr = db_fetch_assoc($stestimonials_qry);
        
        $_POST['reviewer_name']  = $testiArr['reviewer_name'];                
                      
        $_POST['reviewer_email'] = $testiArr['reviewer_email']; 
        $_POST['review_text']    = $testiArr['review_text'];  
        $_POST['display_status'] = $testiArr['display_status'];
        $_POST['rating']         = $testiArr['rating'];        
      }  
    }    
    ?>

    <form name="searchfrm" action="<?php echo $_SERVER['PHP_SELF']?>" method="post" data-parsley-validate autocomplete="off" enctype="multipart/form-data">
      <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12">
          <div class="x_panel">          
            <div class="x_content">
            <?php            
            if(!empty($errorMsg)) {  
              ?>
              <div class="text-danger text-center mt-3"><h3><?php echo $errorMsg;?></h3></div><br>
              <?php
            }
            ?>  

            <div class="form-group row"> 
              <label for="reviewer_name" class="col-sm-3 form-control-label">* Reviewer Name</label>
              <div class="col-sm-6">
              <input class="form-control" id="reviewer_name" required name="reviewer_name" type="text" value="<?php echo isset($_POST['reviewer_name']) ? $_POST['reviewer_name'] : '' ?>">
              </div>
            </div>
            <!-- <div class="form-group row"> 
              <label for="service_name" class="col-sm-3 form-control-label">Service Name</label>
              <div class="col-sm-6">
              <input class="form-control" id="service_name" required name="service_name" type="text" value="<?php echo isset($_POST['service_name']) ? $_POST['service_name'] : '' ?>">
              </div>
            </div> -->

            <div class="form-group row"> 
              <label for="reviewer_email" class="col-sm-3 form-control-label"> Email Address</label>
              <div class="col-sm-6">
              <input class="form-control" id="reviewer_email" name="reviewer_email" type="text" value="<?php echo isset($_POST['reviewer_email']) ? $_POST['reviewer_email'] : '' ?>">
              </div>
            </div>

            <div class="row mb-3">
              <label class="col-sm-3 form-control-label">&nbsp;Reviewer Image</label>
              <div class="col-sm-5">
                <input class="form-control" type="file" id="menuImage1" name="menuImage1"> 
                <small class="text-danger">Spec: (W:120px & H:120px) and JPEG,JPG,PNG only</small>
              </div>
              <?php if (!empty($testiArr['pre_img'])) { ?>
                <div class="col-sm-2">
                  <a href="<?php echo SITE_URL; ?>/testimonial/<?php echo $testiArr['pre_img'] ?>" target="_blank"><small>View Image</small></a>
                </div>
              <?php } ?>
            </div>

            <div class="form-group row">
              <label for="review_text" class="col-sm-3 form-control-label">Testimonials</label>
              <div class="col-sm-8">
              <textarea class="form-control" name="review_text" style="height:100px;"><?php echo isset($_POST['review_text']) ? $_POST['review_text'] : '' ?></textarea>  
              </div>
            </div>  

            <div class="form-group row">
              <label for="rating" class="col-sm-3 form-control-label">* Rating</label>
              <div class="col-sm-3"> 
                <select name="rating" class="form-control">
                  <option value="">-- Select One--</option>
                  <?php
                  for($i=1;$i<=5;$i++) {
                    $selected = (isset($_POST['rating']) && $_POST['rating']==$i) ? "selected" : "";
                    echo "<option value='$i' $selected>$i</option>";
                  }
                  ?>      
                </select>
              </div>     
            </div>

            <div class="form-group row">
              <label for="display_status" class="col-sm-3 form-control-label">* Display Status</label>
              <div class="col-sm-3"> 
                <select name="display_status" class="form-control" id="display_status">
                  <option value="">-- Select One--</option>
                  <?php
                  foreach ($status_arr as $key => $value) {
                    $selected = (isset($_POST['display_status']) && $_POST['display_status']==$key) ? "selected" : "";
                    echo "<option value='$key' $selected>$value</option>";
                  }
                  ?>      
                </select>
              </div>     
            </div> 

            <div class="form-group row">
              <label class="col-sm-3 form-control-label">&nbsp;</label>
              <div class="col-sm-5"> 
              <input type="hidden" name="pID" value="addeditForm">
              <input type="hidden" name="slno" value="<?php echo isset($_REQUEST['slno']) ? $_REQUEST['slno'] : '' ?>">
              <button type="submit" class="btn-info bg-info text-white"><i class="fa fa-save"></i> SUBMIT</button>
              </div>
            </div>

          </div>
        </div>
      </div>
    </div>
</form>
</div>
</section>

<?php
include("footer.php");
?>
</body>
</html>
