<?php
require_once __DIR__ . '/../library/auth.php'; 
$school_id = $_SESSION['school_id'];
allow_roles(['admin']);
$stmt = $pdo->prepare("

    SELECT

        id,
        name,
        father_name,
        mother_name,
        qualification,

        tenth_year,
        tenth_percentage,

        twelfth_year,
        twelfth_percentage,

        grad_year,
        grad_percentage,

        pg_year,
        pg_percentage,

        marital_status,
        salary,
        mobile_no,
        adhar_no,

        address,
        photo,
        resume,
        status

    FROM teachers

    WHERE school_id = ?

    ORDER BY id DESC

");

$stmt->execute([$school_id]);
$teachers = $stmt->fetchAll(PDO::FETCH_ASSOC);
$page_title = 'Teachers';
require_once __DIR__ . '/../include/header.php';
require_once __DIR__ . '/../include/sidebar.php';
?>

<div class="content-wrapper">
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>Staff Directory</h1>
                </div>
                <div class="col-sm-6 text-right">
                    <a href="<?php echo WEB_ROOT; ?>teacher/add.php" class="btn btn-primary">
                        Add Staff
                    </a>
                </div>
            </div>
        </div>
    </section>

    <section class="content">
        <div class="container-fluid">
            <?php if (isset($_GET['msg'])): ?>
                <div class="alert alert-info">
                    <?php echo htmlspecialchars($_GET['msg']); ?>
                </div>
            <?php endif; ?>

            <div class="card">
                <div class="card-header">
                    <h3 class="card-title"> Staff Management </h3>
                </div>

                <div class="card-body table-responsive">
                    <table class="table table-hover align-middle table-bordered">
                        <thead class="bg-dark text-white">
                            <tr>
                                <th>#</th>
                                <th>Name</th>
                                <th>Father</th>
                                <th>Mother</th>
                                <th>Qualification</th>
                                <th>Mobile</th>
                                <th>Salary</th>
                                <th>Photo</th>
                                <th>Resume</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($teachers)): ?>
                                <?php $i = 1; foreach ($teachers as $teacher): ?>
                                    <tr>
                                        <td><?php echo $i++; ?></td>
                                        <td><?php echo htmlspecialchars($teacher['name'] ?? ''); ?></td>
                                        <td><?php echo htmlspecialchars($teacher['father_name'] ?? ''); ?></td>
                                        <td><?php echo htmlspecialchars($teacher['mother_name'] ?? ''); ?></td>
                                        <td><?php echo htmlspecialchars($teacher['qualification'] ?? ''); ?></td>
                                        <td><?php echo htmlspecialchars($teacher['mobile_no'] ?? ''); ?></td>
                                        <td>

											₹<?php echo number_format((float)($teacher['salary'] ?? 0)); ?>

										</td>
                                        <td>

																<?php if (!empty($teacher['photo'])): ?>

																	<img

																		src="<?php echo WEB_ROOT; ?>teacher/uploads/<?php echo htmlspecialchars($teacher['photo']); ?>"

																		class="img-thumbnail rounded-circle"

																		style="
																			width:60px;
																			height:60px;
																			object-fit:cover;
																		"

																		alt="Teacher Photo"
																	>

																<?php else: ?>

																	<img

																		src="https://via.placeholder.com/60"

																		class="img-thumbnail rounded-circle"

																		alt="No Photo"
																	>

																<?php endif; ?>

															</td>
                                       <td>

											<?php if (!empty($teacher['resume'])): ?>

												<a

													href="<?php echo WEB_ROOT; ?>teacher/uploads/<?php echo htmlspecialchars($teacher['resume']); ?>"

													target="_blank"

													class="btn btn-sm btn-success"
												>

													View Resume

												</a>

											<?php else: ?>

												<span class="text-muted">

													No Resume

												</span>

											<?php endif; ?>

										</td>
                                        <td>

											<?php if (($teacher['status'] ?? '') === 'Active'): ?>

												<span class="badge badge-success px-3 py-2">

													Active

												</span>

											<?php else: ?>

												<span class="badge badge-danger px-3 py-2">

													Inactive

												</span>

											<?php endif; ?>

										</td>
                                        <td>

														<div class="btn-group">

															<a

																href="<?php echo WEB_ROOT; ?>teacher/view.php?id=<?php echo (int)$teacher['id']; ?>"

																class="btn btn-sm btn-secondary"
															>

																View

															</a>

															<a

																href="<?php echo WEB_ROOT; ?>teacher/edit.php?id=<?php echo (int)$teacher['id']; ?>"

																class="btn btn-sm btn-warning"
															>

																Edit

															</a>

															<form

																action="<?php echo WEB_ROOT; ?>teacher/process.php"

																method="POST"

																class="d-inline"

																onsubmit="return confirm('Delete this teacher?')"
															>

																<input
																	type="hidden"
																	name="action"
																	value="delete"
																>

																<input
																	type="hidden"
																	name="teacher_id"
																	value="<?php echo (int)$teacher['id']; ?>"
																>

																<button
																	type="submit"
																	class="btn btn-sm btn-danger"
																>

																	Delete

																</button>

															</form>

														</div>

													</td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="11" class="text-center text-muted">No teachers found.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </section>
</div>

<?php
require_once __DIR__ . '/../include/footer.php';
?>