<?php

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

$school_id = $_SESSION['school_id'];

allow_roles(['admin']);

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

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

if ($teacher_id <= 0) {

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

    exit;
}

/*
|--------------------------------------------------------------------------
| 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;
}

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

$page_title = 'Edit Teacher';

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>Edit Teacher</h1>
                </div>
            </div>
        </div>
    </section>

    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <div class="card card-primary">
                        <div class="card-header">
                            <h3 class="card-title">Update Teacher: <?php echo htmlspecialchars($teacher['name'] ?? ''); ?></h3>
                        </div>

                        <form action="<?php echo WEB_ROOT; ?>teacher/process.php?action=edit" method="post" enctype="multipart/form-data">
                            <input type="hidden" name="teacher_id" value="<?php echo (int)$teacher['id']; ?>">
                            <input type="hidden" name="old_photo" value="<?php echo htmlspecialchars($teacher['photo'] ?? ''); ?>">
                            <input type="hidden" name="old_resume" value="<?php echo htmlspecialchars($teacher['resume'] ?? ''); ?>">

                            <div class="card-body">
                                <?php if (isset($_GET['msg'])): ?>
                                    <div class="alert alert-info alert-dismissible fade show">
                                        <button type="button" class="close" data-dismiss="alert">&times;</button>
                                        <?php echo htmlspecialchars($_GET['msg']); ?>
                                    </div>
                                <?php endif; ?>

                                <div class="row">
                                    <!-- Personal Info -->
                                    <div class="col-md-6">
                                        <h6 class="text-muted mb-3 border-bottom pb-2"><i class="fas fa-user"></i> Personal Information</h6>
                                        <div class="form-group">
                                            <label>Name <span class="text-danger">*</span></label>
                                            <input type="text" name="name" class="form-control" required
                                                   value="<?php echo htmlspecialchars($teacher['name'] ?? ''); ?>">
                                        </div>

                                        <div class="form-group">
                                            <label>Father's Name</label>
                                            <input type="text" name="father_name" class="form-control"
                                                   value="<?php echo htmlspecialchars($teacher['father_name'] ?? ''); ?>">
                                        </div>

                                        <div class="form-group">
                                            <label>Mother's Name</label>
                                            <input type="text" name="mother_name" class="form-control"
                                                   value="<?php echo htmlspecialchars($teacher['mother_name'] ?? ''); ?>">
                                        </div>

                                        <div class="form-group">
                                            <label>Date of Birth</label>
                                            <input type="date" name="date_of_birth" class="form-control"
                                                   value="<?php echo htmlspecialchars($teacher['date_of_birth'] ?? ''); ?>">
                                        </div>

                                        <div class="form-group">
                                            <label>Marital Status</label>
                                            <div class="form-check form-check-inline">
                                                <input class="form-check-input" type="radio" name="marital_status" id="single" value="Single"
                                                       <?php echo (($teacher['marital_status'] ?? '') === 'Single') ? 'checked' : ''; ?>>
                                                <label class="form-check-label" for="single">Single</label>
                                            </div>
                                            <div class="form-check form-check-inline">
                                                <input class="form-check-input" type="radio" name="marital_status" id="married" value="Married"
                                                       <?php echo (($teacher['marital_status'] ?? '') === 'Married') ? 'checked' : ''; ?>>
                                                <label class="form-check-label" for="married">Married</label>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- Academic Qualifications -->
                                    <div class="col-md-6">
                                        <h6 class="text-muted mb-3 border-bottom pb-2"><i class="fas fa-graduation-cap"></i> Academic Qualifications</h6>
                                        <div class="form-group">
                                            <label>Qualification</label>
                                            <input type="text" name="qualification" class="form-control"
                                                   value="<?php echo htmlspecialchars($teacher['qualification'] ?? ''); ?>"
                                                   placeholder="e.g., B.Ed, M.Sc">
                                        </div>

                                        <div class="row">
                                            <div class="col-6 form-group">
                                                <label>10th Year</label>
                                                <input type="text" name="tenth_year" class="form-control" maxlength="4"
                                                       value="<?php echo htmlspecialchars($teacher['tenth_year'] ?? ''); ?>">
                                            </div>
                                            <div class="col-6 form-group">
                                                <label>10th %</label>
                                                <input type="text" name="tenth_percentage" class="form-control"
                                                       value="<?php echo htmlspecialchars($teacher['tenth_percentage'] ?? ''); ?>">
                                            </div>
                                        </div>

                                        <div class="row">
                                            <div class="col-6 form-group">
                                                <label>12th Year</label>
                                                <input type="text" name="twelfth_year" class="form-control" maxlength="4"
                                                       value="<?php echo htmlspecialchars($teacher['twelfth_year'] ?? ''); ?>">
                                            </div>
                                            <div class="col-6 form-group">
                                                <label>12th %</label>
                                                <input type="text" name="twelfth_percentage" class="form-control"
                                                       value="<?php echo htmlspecialchars($teacher['twelfth_percentage'] ?? ''); ?>">
                                            </div>
                                        </div>

                                        <div class="row">
                                            <div class="col-6 form-group">
                                                <label>Grad Year</label>
                                                <input type="text" name="grad_year" class="form-control" maxlength="4"
                                                       value="<?php echo htmlspecialchars($teacher['grad_year'] ?? ''); ?>">
                                            </div>
                                            <div class="col-6 form-group">
                                                <label>Grad %</label>
                                                <input type="text" name="grad_percentage" class="form-control"
                                                       value="<?php echo htmlspecialchars($teacher['grad_percentage'] ?? ''); ?>">
                                            </div>
                                        </div>

                                        <div class="row">
                                            <div class="col-6 form-group">
                                                <label>PG Year</label>
                                                <input type="text" name="pg_year" class="form-control" maxlength="4"
                                                       value="<?php echo htmlspecialchars($teacher['pg_year'] ?? ''); ?>">
                                            </div>
                                            <div class="col-6 form-group">
                                                <label>PG %</label>
                                                <input type="text" name="pg_percentage" class="form-control"
                                                       value="<?php echo htmlspecialchars($teacher['pg_percentage'] ?? ''); ?>">
                                            </div>
                                        </div>
                                    </div>

                                    <!-- Contact & Service -->
                                    <div class="col-md-6">
                                        <h6 class="text-muted mb-3 border-bottom pb-2"><i class="fas fa-phone"></i> Contact & Service</h6>
                                        <div class="form-group">
                                            <label>Mobile No.</label>
                                            <input type="text" name="mobile_no" class="form-control" maxlength="10"
                                                   value="<?php echo htmlspecialchars($teacher['mobile_no'] ?? ''); ?>">
                                        </div>

                                        <div class="form-group">
                                            <label>Aadhar No.</label>
                                            <input type="text" name="adhar_no" class="form-control" maxlength="12"
                                                   value="<?php echo htmlspecialchars($teacher['adhar_no'] ?? ''); ?>">
                                        </div>

                                        <div class="form-group">
                                            <label>Address</label>
                                            <textarea name="address" class="form-control" rows="3"><?php echo htmlspecialchars($teacher['address'] ?? ''); ?></textarea>
                                        </div>

                                        <div class="form-group">
                                            <label>Salary (₹)</label>
                                            <input type="text" name="salary" class="form-control"
                                                   value="<?php echo htmlspecialchars($teacher['salary'] ?? ''); ?>">
                                        </div>

                                        <div class="form-group">
                                            <label>Date of Joining</label>
                                            <input type="date" name="date_of_joining" class="form-control"
                                                   value="<?php echo htmlspecialchars($teacher['date_of_joining'] ?? ''); ?>">
                                        </div>

                                        <div class="form-group">
                                            <label>Date of Leaving</label>
                                            <input type="date" name="date_of_leaving" class="form-control"
                                                   value="<?php echo htmlspecialchars($teacher['date_of_leaving'] ?? ''); ?>">
                                        </div>
                                    </div>

                                    <!-- Media Uploads -->
                                    <div class="col-md-6">
                                        <h6 class="text-muted mb-3 border-bottom pb-2"><i class="fas fa-images"></i> Media</h6>
                                        
                                        <div class="form-group">
                                            <label>Current Photo</label>
                                            <div class="mb-2">
                                                <?php if (!empty($teacher['photo'])): ?>
                                                    <img src="<?php echo WEB_ROOT; ?>teacher/uploads/<?php echo htmlspecialchars($teacher['photo']); ?>" 
                                                         class="img-thumbnail" width="150" alt="Teacher Photo">
                                                <?php else: ?>
                                                    <span class="text-muted">No photo uploaded</span>
                                                <?php endif; ?>
                                            </div>
                                            <input type="file" name="photo" class="form-control" accept="image/*">
                                            <small class="text-muted">JPG, PNG, WebP (Max 2MB)</small>
                                        </div>

                                        <div class="form-group">
                                            <label>Current Resume</label>
                                            <div class="mb-2">
                                                <?php if (!empty($teacher['resume'])): ?>
                                                    <a href="<?php echo WEB_ROOT; ?>teacher/uploads/<?php echo htmlspecialchars($teacher['resume']); ?>" 
                                                       class="btn btn-sm btn-outline-primary" target="_blank">
                                                        <i class="fas fa-file-pdf"></i> View Resume
                                                    </a>
                                                <?php else: ?>
                                                    <span class="text-muted">No resume uploaded</span>
                                                <?php endif; ?>
                                            </div>
                                            <input type="file" name="resume" class="form-control" accept="application/pdf">
                                            <small class="text-muted">PDF only</small>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="card-footer">
                                <button type="submit" class="btn btn-success btn-lg">
                                    <i class="fas fa-save"></i> Update Teacher
                                </button>
                                <a href="<?php echo WEB_ROOT; ?>teacher/index.php" class="btn btn-secondary btn-lg">
                                    <i class="fas fa-arrow-left"></i> Back to Teachers
                                </a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

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