<?php
require_once 'config/database.php';
secureSessionStart();
requireLogin();

$conn = getConnection();
$success = '';
$error = '';

// Get admin details
$admin = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM admin WHERE user_id = '{$_SESSION['admin_user']}'"));

// Handle password change
if (isset($_POST['change_password'])) {
    $current = $_POST['current_password'];
    $new = $_POST['new_password'];
    $confirm = $_POST['confirm_password'];
    
    // Verify current password (plain text for now - should be hashed)
    if ($current == $admin['password']) {
        if ($new == $confirm) {
            $stmt = mysqli_prepare($conn, "UPDATE admin SET password = ? WHERE id = ?");
            mysqli_stmt_bind_param($stmt, "si", $new, $admin['id']);
            
            if (mysqli_stmt_execute($stmt)) {
                $success = "Password changed successfully!";
                // Refresh admin data
                $admin = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM admin WHERE user_id = '{$_SESSION['admin_user']}'"));
            } else {
                $error = "Error changing password!";
            }
        } else {
            $error = "New passwords do not match!";
        }
    } else {
        $error = "Current password is incorrect!";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>Admin Profile - MLM System</title>
    
    <!-- Google Fonts -->
    <link href="https://fonts.gstatic.com" rel="preconnect">
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Nunito:300,300i,400,400i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">

    <!-- Vendor CSS Files -->
    <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
    <link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
    <link href="assets/vendor/remixicon/remixicon.css" rel="stylesheet">

    <!-- Template Main CSS File -->
    <link href="assets/css/style.css" rel="stylesheet">
</head>
<body>
    <?php include 'includes/header.php'; ?>
    <?php include 'includes/sidebar.php'; ?>

    <main id="main" class="main">
        <div class="pagetitle">
            <h1>Admin Profile</h1>
            <nav>
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                    <li class="breadcrumb-item active">Profile</li>
                </ol>
            </nav>
        </div>

        <section class="section profile">
            <div class="row">
                <div class="col-xl-4">
                    <div class="card">
                        <div class="card-body profile-card pt-4 d-flex flex-column align-items-center">
                            <img src="assets/img/admin-avatar.png" alt="Profile" class="rounded-circle" style="width: 120px; height: 120px;">
                            <h2><?php echo htmlspecialchars($admin['user_id']); ?></h2>
                            <h3>Administrator</h3>
                            <div class="social-links mt-2">
                                <a href="#" class="twitter"><i class="bi bi-twitter"></i></a>
                                <a href="#" class="facebook"><i class="bi bi-facebook"></i></a>
                                <a href="#" class="instagram"><i class="bi bi-instagram"></i></a>
                                <a href="#" class="linkedin"><i class="bi bi-linkedin"></i></a>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-xl-8">
                    <div class="card">
                        <div class="card-body pt-3">
                            <?php if ($success): ?>
                                <div class="alert alert-success"><?php echo $success; ?></div>
                            <?php endif; ?>
                            
                            <?php if ($error): ?>
                                <div class="alert alert-danger"><?php echo $error; ?></div>
                            <?php endif; ?>
                            
                            <ul class="nav nav-tabs nav-tabs-bordered">
                                <li class="nav-item">
                                    <button class="nav-link active" data-bs-toggle="tab" data-bs-target="#profile-overview">Overview</button>
                                </li>
                                <li class="nav-item">
                                    <button class="nav-link" data-bs-toggle="tab" data-bs-target="#profile-change-password">Change Password</button>
                                </li>
                            </ul>
                            
                            <div class="tab-content pt-2">
                                <div class="tab-pane fade show active profile-overview" id="profile-overview">
                                    <h5 class="card-title">Profile Details</h5>
                                    
                                    <div class="row">
                                        <div class="col-lg-3 col-md-4 label">Admin ID</div>
                                        <div class="col-lg-9 col-md-8"><?php echo $admin['id']; ?></div>
                                    </div>
                                    
                                    <div class="row">
                                        <div class="col-lg-3 col-md-4 label">Username</div>
                                        <div class="col-lg-9 col-md-8"><?php echo htmlspecialchars($admin['user_id']); ?></div>
                                    </div>
                                    
                                    <div class="row">
                                        <div class="col-lg-3 col-md-4 label">Account Type</div>
                                        <div class="col-lg-9 col-md-8">Super Admin</div>
                                    </div>
                                    
                                    <div class="row">
                                        <div class="col-lg-3 col-md-4 label">Last Login</div>
                                        <div class="col-lg-9 col-md-8"><?php echo date('d M Y H:i:s', $_SESSION['login_time'] ?? time()); ?></div>
                                    </div>
                                </div>
                                
                                <div class="tab-pane fade pt-3" id="profile-change-password">
                                    <form method="POST">
                                        <div class="row mb-3">
                                            <label for="currentPassword" class="col-md-4 col-lg-3 col-form-label">Current Password</label>
                                            <div class="col-md-8 col-lg-9">
                                                <input name="current_password" type="password" class="form-control" id="currentPassword" required>
                                            </div>
                                        </div>
                                        
                                        <div class="row mb-3">
                                            <label for="newPassword" class="col-md-4 col-lg-3 col-form-label">New Password</label>
                                            <div class="col-md-8 col-lg-9">
                                                <input name="new_password" type="password" class="form-control" id="newPassword" required>
                                            </div>
                                        </div>
                                        
                                        <div class="row mb-3">
                                            <label for="confirmPassword" class="col-md-4 col-lg-3 col-form-label">Confirm Password</label>
                                            <div class="col-md-8 col-lg-9">
                                                <input name="confirm_password" type="password" class="form-control" id="confirmPassword" required>
                                            </div>
                                        </div>
                                        
                                        <div class="text-center">
                                            <button type="submit" name="change_password" class="btn btn-primary">Change Password</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <?php include 'includes/footer.php'; ?>

    <!-- Vendor JS Files -->
    <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/main.js"></script>
</body>
</html>
<?php mysqli_close($conn); ?>