<?php
session_start();
require_once('mysqli_connect.php');

// ---- Safety stuff: must be logged in + admin ----
if (!isset($_SESSION['user_id'])) {
  header("Location: login.php");
  exit();
}


$user_id = isset($_GET['user_id']) ? (int)$_GET['user_id'] : 0;


$q = "SELECT * from users 
    WHERE user_id = $user_id";
    
    
$r = mysqli_query($dbc, $q);

$row = mysqli_fetch_array($r, MYSQLI_ASSOC);


$first_name = $row['first_name'];
$last_name = $row['last_name'];
$photo = $row['photo'];
$email = $row['email'];


?>

<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Profile View • Sideline</title>
  <link rel="icon" type="image/x-icon" href="images/sideline_logo.ico">
   <style>
:root {
  --bg: #f6f7fb;
  --card: #fff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e5e7eb;
  --dark: #111827;
  --accent: #2563eb;
  --ok: #16a34a;
  --warn: #f59e0b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, Segoe UI, Arial, sans-serif;
  background: var(--bg);
  min-height: 100vh;
}

a { color: inherit; text-decoration: none; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  background: linear-gradient(to right, #251F47, #111827, #2B784A);
  color: #fff;
  border-bottom: 1px solid rgba(255,255,255,.08);
}

.brand { font-weight: 800; letter-spacing: .3px; }

.nav {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}

.nav a {
  background: #FFFFFF;
  color: #251F47;
  border: 1px solid rgba(255,255,255,.18);
  padding: 8px 10px;
  border-radius: 12px;
  cursor: pointer;
  opacity: .95;
}

.nav a.active { border-color: #fff; opacity: 1; }

.container {
  max-width: 1100px;
  margin: 22px auto;
  padding: 0 16px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 14px;
}

.col-12 { grid-column: span 12; }
.col-6 { grid-column: span 6; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 10px 30px rgba(2,6,23,.05);
}

h1, h2, h3 { margin: 0 0 10px; color: var(--text); }
p { margin: 8px 0; color: var(--muted); }

.titleRow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.stack { display: flex; flex-direction: column; gap: 10px; }

.btn {
  background: var(--dark);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 10px 12px;
  cursor: pointer;
  display: inline-block;
}

.btn.secondary {
  background: #251F47;
  color: #fff;
  border: 1px solid var(--line);
}

.btn.tertiary {
background: #ffffff;
  color: #251F47;
  border: 1px solid #251F47;
}


.split { display: flex; gap: 10px; flex-wrap: wrap; }


.notice {
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px dashed var(--line);
  background: #fafafa;
  color: #334155;
}

.success {
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid #bbf7d0;
  background: #ecfdf5;
  color: #166534;
  margin-top: 12px;
}

.bottomLayout {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 14px;
  grid-column: span 12;
  align-items: start;
}



.welcomeBox {
  background: linear-gradient(to right, #FFFFFF, #C5E8B7, #2B784A);
  color: #111827;
  padding: 18px 20px;
  border-radius: 14px;
  margin-bottom: 10px;
}
  
   input {
            width: 80%;
            padding: 6px;
            border: 2px solid var(--line);
            border-radius: 12px;
            margin-bottom: 10px;
        }

  </style>
  </head>
  
  
    <body>
          <header class="topbar">
  <!-- <div class="brand"><a href="coach_dashboard.php">Sideline <span style="opacity:.8;font-weight:600">Profile View</span></a></div>  -->
   <!-- Updated to dynamically enforce player vs coach returning -->
   <div class="brand">
  <a href="<?php 
    if (isset($_SESSION['user_id']) && ($_SESSION['role'] ?? '') === 'admin') {
            echo 'coach_dashboard.php';
    }
    elseif (isset($_SESSION['user_id']) && ($_SESSION['role'] ?? '') === 'player') {
            echo 'player_home.php';
    }
    else {
        echo 'index.php'; // fallback
    }
    
  ?>">
    Sideline <span style="opacity:.8;font-weight:600">Profile View</span>
  </a>
</div>
  <div class="nav">
    <a href="logout.php">Logout</a>
  </div>
</header>
        
        
        <main class="container">
<section class="grid">
        
       <div class="col-12">
<div class="welcomeBox" style="display:flex; align-items:center; gap:15px;">
    <h2>Personal Profile View</h2>
</div>
</div>


    <div class = "card col-6">
    
        <form action = "profile.php?user_id=<?= (int)$user_id?>" method = "POST">
            
            <h2>Current Information</h2>
            
            <p style= "display:inline">First Name: </p><?php echo$first_name."<br>";?>
            
            
            <p style= "display:inline">Last Name: </p><?php echo$last_name."<br>";?>
            
            
            <p style= "display:inline">email: </p><?php echo$email."<br>";?>
            
            
            <p style= "display:inline">Profile Picture: </p><?php echo$photo."<br>";?>
            </div>
            
            <div class = "card col-6">
                <h2>Edit Information</h2>
           <label>Update First Name to: </label><input type = "text" name = "updfirst_name" required value = "<?php echo$first_name;?>">
            <br>
            
            
            <label>Update Last Name to: </label><input type = "text" name = "updlast_name" required value = "<?php echo$last_name;?>">
            <br>
            
            
            <label>Update email to: </label><input type = "text" name = "updemail" required value = "<?php echo$email;?>">
            <br>
            
            
            <label>Update Profile Picture to (file name): </label><input type = "text" name = "updphoto" required value = "<?php echo$photo;?>">

            
            
            
            
             
            
            
            <div class = "split">
            <button class = "btn secondary" type= "submit">Update Info</button>
            <div class = "btn tertiary"><a href="player_home.php?user_id=<?= (int)$user_id?>">Cancel</a></div>
            </div>
        </form>
        
        <form action = "profile.php?user_id=<?= (int)$user_id?>" method = 'POST'>
        <!--<button class = "btn tertiary" type= "submit" name = "user_del" value = /*<?php $user_id?>*/> Delete Account</button>-->
        </form>
    
        
        
<?php


if (isset($_POST['updfirst_name']) && isset($_POST['updlast_name']) && isset($_POST['updemail']) && isset($_POST['updphoto'])){
$upd_fname = $_POST['updfirst_name'];
$upd_lname = $_POST['updlast_name'];
$upd_email = $_POST['updemail'];
$upd_pfp = $_POST['updphoto'];



//make sure that no duplicate emails are created, but it will allow the same email to be input if the user_ids match (not changing the email)
$q3 = "SELECT user_id FROM users where email = '$upd_email';";
$r3 = mysqli_query($dbc, $q3);
$row3 = mysqli_fetch_array($r3, MYSQLI_ASSOC);
$row3Count = mysqli_num_rows($r3);

if(($row3Count==0) || ($row3['user_id'] == $user_id)){
//update info query
$q2 = "UPDATE users
        SET first_name = '$upd_fname', last_name = '$upd_lname', email = '$upd_email', photo = '$upd_pfp'
        WHERE user_id = $user_id";
$r2 = mysqli_query($dbc, $q2);
}
else{
    echo"Please use a differnt email. Thank you.";
    
}
}
?>
    </div>
</section>
</main>

    </body>
</html>