<?php
session_start();
if (!isset($_SESSION['doctor_id'])) { header("Location: login.php"); exit(); }
include '../include/config.php';
$doctor_id = $_SESSION['doctor_id'];

$msg = '';
$error = '';

// Handle profile photo upload
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['profile_photo'])) {
    $target_dir = "../uploads/";
    
    // Create directory if not exists
    if (!file_exists($target_dir)) {
        mkdir($target_dir, 0777, true);
    }
    
    $file_extension = strtolower(pathinfo($_FILES['profile_photo']['name'], PATHINFO_EXTENSION));
    $allowed = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
    
    if (in_array($file_extension, $allowed)) {
        $new_filename = "doctor_" . $doctor_id . "_" . time() . "." . $file_extension;
        $target_file = $target_dir . $new_filename;
        
        if (move_uploaded_file($_FILES['profile_photo']['tmp_name'], $target_file)) {
            $profile_path = "uploads/" . $new_filename;
            $stmt = $conn->prepare("UPDATE doctors SET profile_photo = ? WHERE doctor_id = ?");
            $stmt->bind_param("si", $profile_path, $doctor_id);
            if ($stmt->execute()) {
                $msg = "Profile photo updated successfully!";
                $_SESSION['doctor_profile_photo'] = $profile_path;
            } else {
                $error = "Database update failed.";
            }
            $stmt->close();
        } else {
            $error = "Failed to upload image.";
        }
    } else {
        $error = "Only JPG, PNG, GIF files are allowed.";
    }
}

// Handle credentials update
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['credentials'])) {
    $credentials = $conn->real_escape_string($_POST['credentials']);
    $conn->query("UPDATE doctors SET credentials = '$credentials' WHERE doctor_id = $doctor_id");
    $_SESSION['doctor_credentials'] = $credentials;
    $msg = "Credentials updated.";
}

$doctor = $conn->query("SELECT * FROM doctors WHERE doctor_id = $doctor_id")->fetch_assoc();
$profile_pic = !empty($doctor['profile_photo']) ? '../' . $doctor['profile_photo'] : 'https://ui-avatars.com/api/?background=0f6b4c&color=fff&name=' . urlencode($doctor['name']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Doctor Profile - UHD</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
    <style>
        body { background: #0a3d2f; font-family: 'Segoe UI', sans-serif; }
        .navbar { background: #064e3b; }
        .card { border-radius: 20px; background: rgba(255,255,255,0.95); }
        .profile-pic { width: 120px; height: 120px; border-radius: 50%; object-fit: cover; border: 3px solid #0f6b4c; }
    </style>
</head>
<body>
<nav class="navbar navbar-dark"><div class="container"><span class="navbar-brand"><i class="fas fa-user-md"></i> Doctor Profile</span><a href="dashboard.php" class="btn btn-light btn-sm">Back</a></div></nav>
<div class="container mt-4">
    <div class="card">
        <div class="card-header bg-success text-white">Edit Profile</div>
        <div class="card-body">
            <?php if($msg): ?>
                <div class="alert alert-success"><?= htmlspecialchars($msg) ?></div>
            <?php endif; ?>
            <?php if($error): ?>
                <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
            <?php endif; ?>
            
            <div class="text-center mb-4">
                <img src="<?= $profile_pic ?>" class="profile-pic mb-2">
                <form method="POST" enctype="multipart/form-data">
                    <input type="file" name="profile_photo" accept="image/*" class="form-control d-inline-block w-auto" style="display: inline-block; width: auto;">
                    <button type="submit" class="btn btn-primary btn-sm">Upload Photo</button>
                </form>
            </div>
            
            <form method="POST">
                <div class="mb-3">
                    <label>Credentials (Qualifications)</label>
                    <textarea name="credentials" class="form-control" rows="4"><?= htmlspecialchars($doctor['credentials'] ?? '') ?></textarea>
                </div>
                <button type="submit" class="btn btn-primary">Save Credentials</button>
            </form>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>