<?php

if (!isset($_COOKIE['access_token']) OR !isset($_COOKIE['user_id'])) {
    header("Location: login.php");
    exit;
}

$access_token = $_COOKIE['access_token'];
$userId = $_COOKIE['user_id'];
include 'config.php';

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

if ($result->num_rows == 1) {
    $mainrow = $result->fetch_assoc();
    $username = $mainrow['username'];
}


$msg="";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $sql = "UPDATE upwork SET access_token = '$new_access_token' WHERE userid = '$userId'";
    if (mysqli_query($conn, $sql)) {
        setcookie('access_token', $new_access_token, time() + (86400 * 30), "/");
        header('Location: index.php');
        exit();
    } else {
        echo "Error updating record: " . mysqli_error($conn);
        exit();
    }
}
?>

<!doctype html>
<html lang="en" data-layout="vertical" data-topbar="light" data-sidebar="dark" data-sidebar-size="lg"
    data-sidebar-image="none" data-bs-theme="dark" data-body-image="img-1" data-preloader="disable">

<head>

    <meta charset="utf-8" />
    <title>Link Upwork | AutoBPO 2.0</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- App favicon -->
    <link rel="shortcut icon" href="https://autobpo.ai/wp-content/uploads/2024/09/cropped-Untitled-design-80-32x32.png">


    <!-- Layout config Js -->
    <script src="assets/js/layout.js"></script>
    <!-- Bootstrap Css -->
    <link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <!-- Icons Css -->
    <link href="assets/css/icons.min.css" rel="stylesheet" type="text/css" />
    <!-- App Css-->
    <link href="assets/css/app.min.css" rel="stylesheet" type="text/css" />
    <!-- custom Css-->
    <link href="assets/css/custom.min.css" rel="stylesheet" type="text/css" />
<link
    href="https://cdn.jsdelivr.net/npm/remixicon@4.5.0/fonts/remixicon.css"
    rel="stylesheet"
/>
</head>

<body>

    <div class="auth-page-wrapper pt-5">

        <!-- auth page content -->
        <div class="auth-page-content">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="text-center mt-sm-5 mb-4 text-white-50">
                            <div>
                                <a href="index.php" class="d-inline-block auth-logo">
                                    <img src="assets/images/logo-light.png" alt="" height="40">
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- end row -->

                <div class="row justify-content-center">
                    <div class="col-md-8 col-lg-6 col-xl-5">
                        <div class="card mt-4 card-bg-fill">

                            <div class="card-body p-4">
                                <div class="mb-4">
                                    <div class="avatar-lg mx-auto">
                                        <div class="avatar-title bg-light text-primary display-5 rounded-circle">
                                            <i class="ri-rotate-lock-line"></i>
                                        </div>
                                    </div>
                                </div>

                                <div class="p-2 mt-4">
                                    <div class="text-muted text-center mb-4 mx-lg-3">
                                        <h4>Link Upwork</h4>
                                        <p>Link your Upwork account to AutoBPO 2.0</p>
                                        <p>Login to upwork here <a target="_blank" href="https://www.upwork.com/ab/account-security/login">Click here</a>
                                        <p>Browse some projects here <a target="_blank" href="https://www.upwork.com/nx/search/jobs/?amount=0-99&contractor_tier=1&nbs=1&proposals=5-9,15-19&q=web%20developer&sort=recency&t=1">Click here</a>
                                        <p>Click the button below to fetch and update your Upwork authentication token.</p>
                                    </div>

                                    <div class="mt-4">
                                            <button id="linkUpworkBtn" class="btn btn-primary w-100" type="button">Link Upwork</button>
                                        </div>


                                </div>
                            </div>
                            <!-- end card body -->
                        </div>
                        <!-- end card -->

                        <div class="mt-4 text-center">
                            <p class="mb-0"><a href="logout.php"
                                    class="fw-semibold text-primary text-decoration-underline">Logout</a> </p>
                        </div>

                    </div>
                </div>
                <!-- end row -->
            </div>
            <!-- end container -->
        </div>
        <!-- end auth page content -->

        <!-- footer -->
        <footer class="footer">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="text-center">
                              <p class="mb-0 text-muted">&copy;
                                <script>
                                document.write(new Date().getFullYear())
                                </script> AutoBPO. Crafted with <i class="mdi mdi-heart text-danger"></i> by our Team
                            </p>
                             <div style="font-size: 0.01px" class="text-sm-end d-none d-sm-block">
                                Design & Develop by K3 Studio
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </footer>
        <!-- end Footer -->
    </div>
    <!-- end auth-page-wrapper -->

    <!-- JAVASCRIPT -->
    <script src="assets/libs/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="assets/libs/simplebar/simplebar.min.js"></script>
    <script src="assets/libs/node-waves/waves.min.js"></script>
    <script src="assets/libs/feather-icons/feather.min.js"></script>
    <script src="assets/js/pages/plugins/lord-icon-2.1.0.js"></script>
    <script src="assets/js/plugins.js"></script>

    <!-- particles js -->
    <script src="assets/libs/particles.js/particles.js"></script>
    <!-- particles app js -->
    <script src="assets/js/pages/particles.app.js"></script>
    <!-- two-step-verification js -->
    <script src="assets/js/pages/two-step-verification.init.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/chiragkpipal/autobpo_2_js/link-upwork.min.js"></script>
</body>

</html>