<?php
session_start();

// Include access control functions
require_once '../includes/access_control.php';
require_once '../config/database.php';

// Check if user is logged in and has appropriate role
if (!isset($_SESSION['user_id']) || ($_SESSION['user_role'] !== 'admin' && $_SESSION['user_role'] !== 'teacher')) {
    header('Location: ../login.php');
    exit();
}

$user_role = $_SESSION['user_role'];
$user_id = $_SESSION['user_id'];

// Validate ID
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) {
    http_response_code(400);
    echo 'Invalid teacher id';
    exit();
}

// Fetch teacher with linked user
$sql = "
    SELECT t.*, u.username AS user_username, u.email AS user_email
    FROM teachers t
    LEFT JOIN users u ON t.user_id = u.id
    WHERE t.id = ?
";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id]);
$teacher = $stmt->fetch();

if (!$teacher) {
    http_response_code(404);
    echo 'Teacher not found';
    exit();
}

function fmt_date($d) { return !empty($d) ? date('d-m-Y', strtotime($d)) : '-'; }
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>শিক্ষক প্রোফাইল - <?php echo htmlspecialchars($teacher['first_name'] . ' ' . $teacher['last_name']); ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="../assets/css/style.css" rel="stylesheet">
    <style>
        @media print {
            @page { size: A4; margin: 12mm; }
            body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
            .no-print, .navbar, .btn { display: none !important; }
            .card, .card-header, .card-body { border: 0 !important; box-shadow: none !important; }
            .table th { background: #000 !important; color: #fff !important; }
        }
        .profile-photo {
            width: 120px; height: 120px; object-fit: cover; border-radius: 8px; border: 1px solid #ddd;
        }
        .label-cell { width: 220px; }
    </style>
</head>
<body>
    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary no-print">
        <div class="container-fluid">
            <a class="navbar-brand" href="<?php echo ($_SESSION['user_role'] == 'teacher') ? 'dashboard.php' : '../index.php'; ?>">
                <i class="fas fa-graduation-cap"></i> স্কুল ম্যানেজমেন্ট সিস্টেম
            </a>
            <div class="navbar-nav ms-auto">
                <a class="nav-link" href="list.php"><i class="fas fa-list"></i> শিক্ষক তালিকা</a>
                <a class="nav-link" href="../logout.php"><i class="fas fa-sign-out-alt"></i> লগআউট</a>
            </div>
        </div>
    </nav>

    <div class="container-fluid mt-4">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <div>
                            <h5 class="mb-0"><i class="fas fa-chalkboard-teacher"></i>
                                শিক্ষক প্রোফাইল: <?php echo htmlspecialchars($teacher['first_name'] . ' ' . $teacher['last_name']); ?>
                            </h5>
                            <small class="text-muted">Employee ID: <strong><?php echo htmlspecialchars($teacher['employee_id']); ?></strong></small>
                        </div>
                        <div class="no-print">
                            <button class="btn btn-info btn-sm" onclick="window.print()"><i class="fas fa-print"></i> প্রিন্ট</button>
                            <?php if ($_SESSION['user_role'] == 'admin'): ?>
                            <a href="edit.php?id=<?php echo $teacher['id']; ?>" class="btn btn-warning btn-sm"><i class="fas fa-edit"></i> সম্পাদনা</a>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="row g-4">
                            <div class="col-md-3 text-center">
                                <?php if (!empty($teacher['photo'])): ?>
                                    <img src="../<?php echo $teacher['photo']; ?>" alt="Photo" class="profile-photo">
                                <?php else: ?>
                                    <div class="d-flex align-items-center justify-content-center bg-light" style="width:120px;height:120px;border:1px solid #ddd;border-radius:8px;">
                                        <i class="fas fa-user fa-3x text-muted"></i>
                                    </div>
                                <?php endif; ?>
                            </div>
                            <div class="col-md-9">
                                <table class="table table-bordered table-sm mb-0">
                                    <tbody>
                                        <tr>
                                            <th class="label-cell">নাম</th>
                                            <td><?php echo htmlspecialchars($teacher['first_name'] . ' ' . $teacher['last_name']); ?></td>
                                        </tr>
                                        <tr>
                                            <th>ইউজারনেম</th>
                                            <td><?php echo htmlspecialchars($teacher['user_username'] ?: '-'); ?></td>
                                        </tr>
                                        <tr>
                                            <th>ইমেইল</th>
                                            <td><?php echo htmlspecialchars($teacher['email'] ?: '-'); ?></td>
                                        </tr>
                                        <tr>
                                            <th>ফোন</th>
                                            <td><?php echo htmlspecialchars($teacher['phone'] ?: '-'); ?></td>
                                        </tr>
                                        <tr>
                                            <th>জন্ম তারিখ</th>
                                            <td><?php echo fmt_date($teacher['date_of_birth']); ?></td>
                                        </tr>
                                        <tr>
                                            <th>লিঙ্গ</th>
                                            <td><?php echo htmlspecialchars($teacher['gender'] ?: '-'); ?></td>
                                        </tr>
                                        <tr>
                                            <th>ঠিকানা</th>
                                            <td><?php echo nl2br(htmlspecialchars($teacher['address'] ?: '-')); ?></td>
                                        </tr>
                                        <tr>
                                            <th>শিক্ষাগত যোগ্যতা</th>
                                            <td><?php echo htmlspecialchars($teacher['qualification'] ?: '-'); ?></td>
                                        </tr>
                                        <tr>
                                            <th>অভিজ্ঞতা</th>
                                            <td><?php echo (int)$teacher['experience_years'] > 0 ? ((int)$teacher['experience_years'] . ' বছর') : 'নতুন'; ?></td>
                                        </tr>
                                        <tr>
                                            <th>যোগদান</th>
                                            <td><?php echo fmt_date($teacher['joining_date']); ?></td>
                                        </tr>
                                        <tr>
                                            <th>বেতন</th>
                                            <td><?php echo (float)$teacher['salary'] > 0 ? ('৳' . number_format((float)$teacher['salary'], 0)) : '-'; ?></td>
                                        </tr>
                                        <tr>
                                            <th>অবস্থা</th>
                                            <td>
                                                <?php if ($teacher['status'] === 'active'): ?>
                                                    <span class="badge bg-success">সক্রিয়</span>
                                                <?php else: ?>
                                                    <span class="badge bg-danger">নিষ্ক্রিয়</span>
                                                <?php endif; ?>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>

                        <!-- Signature Area for print/detail -->
                        <div class="mt-5 d-print-block" style="page-break-inside: avoid;">
                            <div class="row text-center">
                                <div class="col-4">
                                    <div style="border-top:1px solid #000; margin-top:60px; padding-top:6px;">টিচারের স্বাক্ষর</div>
                                </div>
                                <div class="col-4">
                                    <div style="border-top:1px solid #000; margin-top:60px; padding-top:6px;">শাখা প্রধান</div>
                                </div>
                                <div class="col-4">
                                    <div style="border-top:1px solid #000; margin-top:60px; padding-top:6px;">প্রধান শিক্ষক</div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>