<?php
// Check if both cookies are set
if (!isset($_COOKIE['access_token']) || !isset($_COOKIE['user_id'])) {
    // Remove whichever cookies exist
    if (isset($_COOKIE['access_token'])) {
        setcookie('access_token', '', time() - 3600, '/');
    }
    if (isset($_COOKIE['user_id'])) {
        setcookie('user_id', '', time() - 3600, '/');
    }

    // Redirect to login
    header("Location: login.php");
    exit;
}

$current_page = basename($_SERVER['PHP_SELF']);
$access_token = $_COOKIE['access_token'];
$userId = $_COOKIE['user_id'];

include 'config.php';

$sql = "SELECT * FROM users WHERE userid='$userId'";
$result = $conn->query($sql);

$upworklinked = false;

if ($result->num_rows == 1) {
    $mainrow = $result->fetch_assoc();
    $sql = "SELECT * FROM upwork WHERE user_id='$userId'";
    $result = $conn->query($sql);
    if ($result->num_rows == 1) {
        $upworklinked = true;
        $showprofile = true;
        $upworkrow = $result->fetch_assoc();
        $nametoshow = $upworkrow['name'];
        $title = $upworkrow['title'];
        $pp = $upworkrow['image'];
    } else {
        $upworklinked = false;
    }
         // Check freelancer autobid
         if ($mainrow['upwork'] == 0) {
            $upworkplan = false;
        } else {
            $upworkplan = true;
        }
    // If no plan and not already on billing page → redirect
    if ($mainrow['plan'] == 0) {
        echo "<script>alert('You dont have a valid plan');window.location.href='https://app.autobpo.ai/billing.php';</script>";
        exit();
    }
    if ($upworkplan == false) {
        echo "<script>alert('You dont have access to AutoBPO 2.0');window.location.href='https://app.autobpo.ai/upwork.php';</script>";
        exit();
    }

    if ($upworklinked == false) {
        header("Location: link-upwork.php");
        exit();
    }
}

$current_page = basename($_SERVER['REQUEST_URI']);



