<?php

require_once __DIR__ . '/../library/auth.php';

allow_roles([
    'admin',
    'teacher'
]);

$school_id =
    $_SESSION['school_id'];

/*
|--------------------------------------------------------------------------
| VALIDATE ID
|--------------------------------------------------------------------------
*/

$teacher_id = (int)(
    $_GET['id'] ?? 0
);

if ($teacher_id <= 0) {

    header(
        "Location: index.php?msg=Invalid teacher"
    );

    exit;
}

/*
|--------------------------------------------------------------------------
| PAGE CONFIG
|--------------------------------------------------------------------------
*/

$page_title = 'Staff Profile';

require_once __DIR__ . '/../include/header.php';

require_once __DIR__ . '/../include/sidebar.php';

/*
|--------------------------------------------------------------------------
| FETCH TEACHER
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("

    SELECT *

    FROM teachers

    WHERE id = ?
    AND school_id = ?

    LIMIT 1

");

$stmt->execute([

    $teacher_id,
    $school_id

]);

$teacher =
    $stmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| NOT FOUND
|--------------------------------------------------------------------------
*/

if (!$teacher) {

    header(
        "Location: index.php?msg=Teacher not found"
    );

    exit;
}

?>

<div class="content-wrapper">

    <section class="content-header">

        <div class="container-fluid">

            <h1>

                Teacher Profile

            </h1>

        </div>

    </section>

    <section class="content">

        <div class="container-fluid">

            <div class="row">

                <!-- LEFT -->

                <div class="col-md-4">

                    <div class="card card-primary card-outline">

                        <div class="card-body box-profile">

                            <div class="text-center">

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

                                    <img
                                        class="profile-user-img img-fluid img-circle"
                                        src="uploads/<?= htmlspecialchars($teacher['photo']) ?>"
                                        alt="Teacher Photo"
                                        style="
                                            width:120px;
                                            height:120px;
                                            object-fit:cover;
                                        "
                                    >

                                <?php else: ?>

                                    <img
                                        class="profile-user-img img-fluid img-circle"
                                        src="https://via.placeholder.com/120"
                                        alt="No Photo"
                                    >

                                <?php endif; ?>

                            </div>

                            <h3 class="profile-username text-center">

                                <?= htmlspecialchars($teacher['name']) ?>

                            </h3>

                            <p class="text-muted text-center">

                                <?= htmlspecialchars($teacher['qualification'] ?? '-') ?>

                            </p>

                            <ul class="list-group list-group-unbordered mb-3">

                                <li class="list-group-item">

                                    <b>Mobile</b>

                                    <span class="float-right">

                                        <?= htmlspecialchars($teacher['mobile_no'] ?? '-') ?>

                                    </span>

                                </li>

                                <li class="list-group-item">

                                    <b>Status</b>

                                    <span class="float-right">

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

                                            <span class="badge badge-success">

                                                Active

                                            </span>

                                        <?php else: ?>

                                            <span class="badge badge-danger">

                                                Inactive

                                            </span>

                                        <?php endif; ?>

                                    </span>

                                </li>

                                <li class="list-group-item">

                                    <b>Salary</b>

                                    <span class="float-right">

                                        ₹<?= htmlspecialchars($teacher['salary'] ?? '0') ?>

                                    </span>

                                </li>

                                <li class="list-group-item">

                                    <b>Joining Date</b>

                                    <span class="float-right">

                                        <?= htmlspecialchars($teacher['date_of_joining'] ?? '-') ?>

                                    </span>

                                </li>

                            </ul>

                            <a
                                href="edit.php?id=<?= $teacher['id'] ?>"
                                class="btn btn-primary btn-block"
                            >

                                <i class="fas fa-edit"></i>

                                Edit Teacher

                            </a>

                        </div>

                    </div>

                </div>

                <!-- RIGHT -->

                <div class="col-md-8">

                    <div class="card">

                        <div class="card-header p-2">

                            <ul class="nav nav-pills">

                                <li class="nav-item">

                                    <a
                                        class="nav-link active"
                                        href="#personal"
                                        data-toggle="tab"
                                    >

                                        Personal

                                    </a>

                                </li>

                                <li class="nav-item">

                                    <a
                                        class="nav-link"
                                        href="#education"
                                        data-toggle="tab"
                                    >

                                        Education

                                    </a>

                                </li>

                                <li class="nav-item">

                                    <a
                                        class="nav-link"
                                        href="#documents"
                                        data-toggle="tab"
                                    >

                                        Documents

                                    </a>

                                </li>

                            </ul>

                        </div>

                        <div class="card-body">

                            <div class="tab-content">

                                <!-- PERSONAL -->

                                <div class="active tab-pane" id="personal">

                                    <table class="table table-bordered">

                                        <tr>
                                            <th>Father Name</th>
                                            <td><?= htmlspecialchars($teacher['father_name'] ?? '-') ?></td>
                                        </tr>

                                        <tr>
                                            <th>Mother Name</th>
                                            <td><?= htmlspecialchars($teacher['mother_name'] ?? '-') ?></td>
                                        </tr>

                                        <tr>
                                            <th>Mobile Number</th>
                                            <td><?= htmlspecialchars($teacher['mobile_no'] ?? '-') ?></td>
                                        </tr>

                                        <tr>
                                            <th>Aadhaar Number</th>
                                            <td><?= htmlspecialchars($teacher['adhar_no'] ?? '-') ?></td>
                                        </tr>

                                        <tr>
                                            <th>Date of Birth</th>
                                            <td><?= htmlspecialchars($teacher['date_of_birth'] ?? '-') ?></td>
                                        </tr>

                                        <tr>
                                            <th>Marital Status</th>
                                            <td><?= htmlspecialchars($teacher['marital_status'] ?? '-') ?></td>
                                        </tr>

                                        <tr>
                                            <th>Address</th>
                                            <td><?= htmlspecialchars($teacher['address'] ?? '-') ?></td>
                                        </tr>

                                    </table>

                                </div>

                                <!-- EDUCATION -->

                                <div class="tab-pane" id="education">

                                    <table class="table table-bordered">

                                        <tr>
                                            <th>Qualification</th>
                                            <td><?= htmlspecialchars($teacher['qualification'] ?? '-') ?></td>
                                        </tr>

                                        <tr>
                                            <th>10th</th>
                                            <td>

                                                Year:
                                                <?= htmlspecialchars($teacher['tenth_year'] ?? '-') ?>

                                                |

                                                Percentage:
                                                <?= htmlspecialchars($teacher['tenth_percentage'] ?? '-') ?>

                                            </td>
                                        </tr>

                                        <tr>
                                            <th>12th</th>
                                            <td>

                                                Year:
                                                <?= htmlspecialchars($teacher['twelfth_year'] ?? '-') ?>

                                                |

                                                Percentage:
                                                <?= htmlspecialchars($teacher['twelfth_percentage'] ?? '-') ?>

                                            </td>
                                        </tr>

                                        <tr>
                                            <th>Graduation</th>
                                            <td>

                                                Year:
                                                <?= htmlspecialchars($teacher['grad_year'] ?? '-') ?>

                                                |

                                                Percentage:
                                                <?= htmlspecialchars($teacher['grad_percentage'] ?? '-') ?>

                                            </td>
                                        </tr>

                                        <tr>
                                            <th>Post Graduation</th>
                                            <td>

                                                Year:
                                                <?= htmlspecialchars($teacher['pg_year'] ?? '-') ?>

                                                |

                                                Percentage:
                                                <?= htmlspecialchars($teacher['pg_percentage'] ?? '-') ?>

                                            </td>
                                        </tr>

                                    </table>

                                </div>

                                <!-- DOCUMENTS -->

                                <div class="tab-pane" id="documents">

                                    <table class="table table-bordered">

                                        <tr>

                                            <th>Resume</th>

                                            <td>

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

                                                    <a
                                                        href="uploads/<?= htmlspecialchars($teacher['resume']) ?>"
                                                        target="_blank"
                                                        class="btn btn-sm btn-success"
                                                    >

                                                        <i class="fas fa-file-pdf"></i>

                                                        View Resume

                                                    </a>

                                                <?php else: ?>

                                                    No Resume Uploaded

                                                <?php endif; ?>

                                            </td>

                                        </tr>

                                    </table>

                                </div>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

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