<?php
date_default_timezone_set('Asia/Kolkata'); 
//echo $date = date("Y-m-d", time());
include ('../config.php');
@session_start();
@$user_id1=$_SESSION['user_id1'];
@$idvfied1=$_SESSION['idverified1'];
@$namevfied1=$_SESSION['namevfied1'];
if(!isset($_SESSION['user_id1'])){
	header("Location: index.php");
}

$timeout = 60 * 60 * 24 * 365;
$s_name = session_name();
if(isset( $_COOKIE[ $s_name ] )) {
  setcookie( $s_name, $_COOKIE[ $s_name ], time() + $timeout, '/' );
}

$sel1=mysqli_query($conn,"select * from hr_dump where empid='".@$user_id1."'");
$fetch1=mysqli_fetch_array($sel1);
?>

<!doctype html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, viewport-fit=cover" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>AmbitionBox Reviews</title>
	
	<?php	include ('header.php');	?>
	
	<div id="appCapsule">
        <div class="section wallet-card-section pt-1 mb-4">
            <div class="wallet-card">
				
				<?php	include ('include/wallet-balance.php');	?>
				
				<div class="row text-center">
					<div class="wallet-footer"><h6 class="text-center">AmbitionBox Reviews</h6></div>

<?php
if (isset($_POST['SubReview'])) {
    $empid = $_POST['empid'];
    $Employee_Full_Name = $_POST['Employee_Full_Name'];
    $Team_Name = $_POST['Team_Name'];
    $Location_of_Work = $_POST['Location_of_Work'];
    $Platform = $_POST['Platform'];
    $Status = $_POST['Status'];
    $Source = $_POST['Source'];

    // Check if a "Clicked" or "Notyet" entry already exists
    $checkExisting = mysqli_query($conn, "SELECT id FROM review_logs WHERE empid='$empid' AND platform='AmbitionBox' AND status IN ('Clicked', 'Notyet')");
    
    if (mysqli_num_rows($checkExisting) == 0) {
        // Insert only if no existing pending entry
        $sql = "INSERT INTO review_logs (empid, Employee_Full_Name, Team_Name, Location_of_Work, Platform, Status, Source) 
                VALUES ('$empid', '$Employee_Full_Name', '$Team_Name', '$Location_of_Work', '$Platform', '$Status', '$Source')";

        if (mysqli_query($conn, $sql)) {
            echo "<script>
                window.location.href = window.location.href;
            </script>";
            exit;
        } else {
            echo "Error: " . mysqli_error($conn);
        }
    } else {
        // Already Clicked or Notyet - refresh page
        echo "<script>window.location.href = window.location.href;</script>";
        exit;
    }
}

// Handle radio button submission (status update)
if (isset($_POST['UpdateReviewStatus'])) {
    $empid = $_POST['empid'];
    $response = $_POST['response'];
    $confirm_time = date("Y-m-d H:i:s");

    if ($response === 'yes') {
        // Update to Submitted and set confirm_time
        $update = "UPDATE review_logs 
                   SET status = 'Submitted', confirm_time = '$confirm_time' 
                   WHERE empid = '$empid' AND platform = 'AmbitionBox' AND status IN ('Clicked', 'Notyet') 
                   ORDER BY id DESC LIMIT 1";
    } else {
        // Update to Notyet only
        $update = "UPDATE review_logs 
                   SET status = 'Notyet' 
                   WHERE empid = '$empid' AND platform = 'AmbitionBox' AND status IN ('Clicked', 'Notyet') 
                   ORDER BY id DESC LIMIT 1";
    }

    if (mysqli_query($conn, $update)) {
        //echo "<script>window.location.href = window.location.href;</script>";
		echo "<script>window.location.href='dashboard.php';</script>";
        exit;
    } else {
        echo "Error: " . mysqli_error($conn);
    }
}
?>

<!-- Display Form -->
<form action="" method="post" enctype="multipart/form-data">
    <input type="hidden" name="empid" value="<?php echo $fetch1['empid']; ?>">
    <input type="hidden" name="Employee_Full_Name" value="<?php echo $fetch1['Employee_Full_Name']; ?>">
    <input type="hidden" name="Team_Name" value="<?php echo $fetch1['Team_Name']; ?>">
    <input type="hidden" name="Location_of_Work" value="<?php echo $fetch1['Location_of_Work']; ?>">
    <input type="hidden" name="Platform" value="AmbitionBox">
    <input type="hidden" name="Status" value="Clicked">
    <input type="hidden" name="Source" value="IC-AMS">
    <div class="col-sm-4 mt-1">
        <?php
        $empid = $fetch1['empid'];
        // Already submitted?
        $checkSubmitted = mysqli_query($conn, "SELECT id FROM review_logs WHERE empid='$empid' AND platform='AmbitionBox' AND status='Submitted'");
        if (mysqli_num_rows($checkSubmitted) > 0) {
            echo "✅ You have already submitted a review on AmbitionBox.";
        } else {
            // Pending (Clicked or Notyet)?
            $checkPending = mysqli_query($conn, "SELECT id FROM review_logs WHERE empid='$empid' AND platform='AmbitionBox' AND status IN ('Clicked', 'Notyet')");
            if (mysqli_num_rows($checkPending) > 0) {
                // Show status update options
                echo '<form method="post" action="">
                        <input type="hidden" name="empid" value="' . $empid . '">
                        <p>Have you submitted the AmbitionBox review?</p>
                        <label><input type="radio" name="response" value="yes" required> ✅ Yes, I submitted</label><br>
                        <label><input type="radio" name="response" value="no"> ❌ Not yet</label><br>
                        <button type="submit" name="UpdateReviewStatus" class="btn btn-primary btn-sm mt-1">Update Status</button>
                      </form>';
				echo '<br><br><a class="btn btn-success btn-sm" href="https://www.ambitionbox.com/reviews/investors-clinic-reviews?page=8&sort_by=latest" target="_blank">Write AmbitionBox Review</a>';
            } else {
                // Show "Write AmbitionBox Review" button
                //echo '<button class="btn btn-success btn-sm" type="submit" name="SubReview">Write AmbitionBox Review</button>';
				echo '<button class="btn btn-success btn-sm" type="button" id="ambitionboxReviewBtn">Write AmbitionBox Review</button>';
            }
        }
        ?>
    </div>
</form>

<script>
document.getElementById("ambitionboxReviewBtn")?.addEventListener("click", function() {
    // Open the review page immediately in a new tab
    window.open('https://www.ambitionbox.com/reviews/investors-clinic-reviews?page=8&sort_by=latest', '_blank');

    // Submit the form after opening
    const form = this.closest("form");
    if (form) {
        // Create a hidden input to indicate the button pressed
        const input = document.createElement("input");
        input.type = "hidden";
        input.name = "SubReview";
        input.value = "1";
        form.appendChild(input);
        form.submit();
    }
});
</script>

				</div>
				
            </div>
        </div>
		
	<?php	include ('include/copyright.php');	?>
	</div>
	
	<?php	include ('footer.php');	?>