<?php

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

$school_id = $_SESSION['school_id'];
$session_id = get_current_session_id($pdo);

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

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

if (
    !isset($_GET['id']) ||
    !is_numeric($_GET['id'])
) {

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

    exit;
}

$student_id = (int) $_GET['id'];

/*
|--------------------------------------------------------------------------
| FETCH STUDENT
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT *
    FROM students
    WHERE id = ?
    AND school_id = ?
    LIMIT 1
");

$stmt->execute([
    $student_id,
    $school_id
]);

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

if (!$student) {

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

    exit;
}

/*
|--------------------------------------------------------------------------
| FETCH SESSION DATA
|--------------------------------------------------------------------------
*/

$sessionStmt = $pdo->prepare("
    SELECT *
    FROM student_sessions
    WHERE student_id = ?
    AND school_id = ?
    AND session_id = ?
    LIMIT 1
");

$sessionStmt->execute([
    $student_id,
    $school_id,
    $session_id
]);

$studentSession =
    $sessionStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH PARENT DATA
|--------------------------------------------------------------------------
*/

$parentStmt = $pdo->prepare("
    SELECT *
    FROM student_parents
    WHERE student_id = ?
    AND school_id = ?
    LIMIT 1
");

$parentStmt->execute([
    $student_id,
    $school_id
]);

$parentData =
    $parentStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH ADDRESS DATA
|--------------------------------------------------------------------------
*/

$addressStmt = $pdo->prepare("
    SELECT *
    FROM student_address
    WHERE student_id = ?
    AND school_id = ?
    LIMIT 1
");

$addressStmt->execute([
    $student_id,
    $school_id
]);

$addressData =
    $addressStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH DOCUMENT DATA
|--------------------------------------------------------------------------
*/

$documentStmt = $pdo->prepare("
    SELECT *
    FROM student_documents
    WHERE student_id = ?
    AND school_id = ?
    LIMIT 1
");

$documentStmt->execute([
    $student_id,
    $school_id
]);

$documentData =
    $documentStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH ACADEMIC DATA
|--------------------------------------------------------------------------
*/

$academicStmt = $pdo->prepare("
    SELECT *
    FROM student_academic_info
    WHERE student_id = ?
    AND school_id = ?
    LIMIT 1
");

$academicStmt->execute([
    $student_id,
    $school_id
]);

$academicData =
    $academicStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH CATEGORY DATA
|--------------------------------------------------------------------------
*/

$categoryStmt = $pdo->prepare("
    SELECT *
    FROM student_category_info
    WHERE student_id = ?
    AND school_id = ?
    LIMIT 1
");

$categoryStmt->execute([
    $student_id,
    $school_id
]);

$categoryData =
    $categoryStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH BANK DATA
|--------------------------------------------------------------------------
*/

$bankStmt = $pdo->prepare("
    SELECT *
    FROM student_bank_details
    WHERE student_id = ?
    AND school_id = ?
    LIMIT 1
");

$bankStmt->execute([
    $student_id,
    $school_id
]);

$bankData =
    $bankStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH CLASSES
|--------------------------------------------------------------------------
*/

$classStmt = $pdo->prepare("
    SELECT
        id,
        class_name,
        section
    FROM classes
    WHERE school_id = ?
    ORDER BY class_name ASC
");

$classStmt->execute([$school_id]);

$classes =
    $classStmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH SESSIONS
|--------------------------------------------------------------------------
*/

$academicSessionStmt = $pdo->prepare("
    SELECT
        id,
        session_name
    FROM academic_sessions
    WHERE school_id = ?
    ORDER BY id DESC
");

$academicSessionStmt->execute([$school_id]);

$sessions =
    $academicSessionStmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| OLD HELPER
|--------------------------------------------------------------------------
*/

function old($key, $default = '')
{
    return htmlspecialchars(
        $_SESSION['old'][$key] ?? $default
    );
}

$page_title = 'Edit Student';

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

?>

<div class="content-wrapper">

    <!-- PAGE HEADER -->

    <section class="content-header">

        <div class="container-fluid">

            <div class="row mb-2 align-items-center">

                <div class="col-sm-6">

                    <h1>

                        Edit Student

                    </h1>

                </div>

                <div class="col-sm-6 text-sm-right mt-2 mt-sm-0">

                    <a
                        href="index.php"
                        class="btn btn-secondary"
                    >

                        <i class="fas fa-arrow-left mr-1"></i>

                        Back to Students

                    </a>

                </div>

            </div>

        </div>

    </section>

    <!-- MAIN CONTENT -->

    <section class="content">

        <div class="container-fluid">

            <form
                action="process.php?action=edit"
                method="post"
                enctype="multipart/form-data"
            >

<input
    type="hidden"
    name="student_id"
    value="<?= $student['id'] ?>"
>

<input
    type="hidden"
    name="old_image"
    value="<?= $student['photo'] ?>"
>
<!-- =========================================================
1. BASIC INFORMATION
========================================================= -->

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

    <div class="card-header">

        <h3 class="card-title">

            <i class="fas fa-user mr-1"></i>

            Basic Information

        </h3>

    </div>

    <div class="card-body">

        <div class="row">

            <!-- FIRST NAME -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    First Name
                    <span class="text-danger">*</span>

                </label>

                <input
                    type="text"
                    name="first_name"
                    class="form-control"
                    value="<?= old(
                        'first_name',
                        $student['first_name']
                    ) ?>"
                    required
                >

            </div>

            <!-- LAST NAME -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Last Name

                </label>

                <input
                    type="text"
                    name="last_name"
                    class="form-control"
                    value="<?= old(
                        'last_name',
                        $student['last_name']
                    ) ?>"
                >

            </div>

            <!-- GENDER -->

            <div class="col-lg-2 col-md-4 col-12 form-group">

                <label>

                    Gender

                </label>

                <select
                    name="gender"
                    class="form-control"
                >

                    <option
                        value="Male"
                        <?= old(
                            'gender',
                            $student['gender']
                        ) == 'Male'
                            ? 'selected'
                            : '' ?>
                    >
                        Male
                    </option>

                    <option
                        value="Female"
                        <?= old(
                            'gender',
                            $student['gender']
                        ) == 'Female'
                            ? 'selected'
                            : '' ?>
                    >
                        Female
                    </option>

                    <option
                        value="Other"
                        <?= old(
                            'gender',
                            $student['gender']
                        ) == 'Other'
                            ? 'selected'
                            : '' ?>
                    >
                        Other
                    </option>

                </select>

            </div>

            <!-- DOB -->

            <div class="col-lg-2 col-md-4 col-12 form-group">

                <label>

                    Date of Birth

                </label>

                <input
                    type="date"
                    name="dob"
                    class="form-control"
                    value="<?= old(
                        'dob',
                        $student['dob']
                    ) ?>"
                >

            </div>

            <!-- BLOOD GROUP -->

            <div class="col-lg-2 col-md-4 col-12 form-group">

                <label>

                    Blood Group

                </label>

                <select
                    name="blood_group"
                    class="form-control"
                >

                    <option value="">
                        Select
                    </option>

                    <option value="A+" <?= old('blood_group', $student['blood_group']) == 'A+' ? 'selected' : '' ?>>A+</option>
                    <option value="A-" <?= old('blood_group', $student['blood_group']) == 'A-' ? 'selected' : '' ?>>A-</option>
                    <option value="B+" <?= old('blood_group', $student['blood_group']) == 'B+' ? 'selected' : '' ?>>B+</option>
                    <option value="B-" <?= old('blood_group', $student['blood_group']) == 'B-' ? 'selected' : '' ?>>B-</option>
                    <option value="AB+" <?= old('blood_group', $student['blood_group']) == 'AB+' ? 'selected' : '' ?>>AB+</option>
                    <option value="AB-" <?= old('blood_group', $student['blood_group']) == 'AB-' ? 'selected' : '' ?>>AB-</option>
                    <option value="O+" <?= old('blood_group', $student['blood_group']) == 'O+' ? 'selected' : '' ?>>O+</option>
                    <option value="O-" <?= old('blood_group', $student['blood_group']) == 'O-' ? 'selected' : '' ?>>O-</option>

                </select>

            </div>

            <!-- PHONE -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Mobile Number

                </label>

                <input
                    type="text"
                    name="phone"
                    maxlength="10"
                    class="form-control"
                    value="<?= old(
                        'phone',
                        $student['phone']
                    ) ?>"
                >

            </div>

            <!-- EMAIL -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Email Address

                </label>

                <input
                    type="email"
                    name="email"
                    class="form-control"
                    value="<?= old(
                        'email',
                        $student['email']
                    ) ?>"
                >

            </div>

            <!-- PHOTO -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Student Photo

                </label>

                <?php if (!empty($student['photo'])) { ?>

                    <div class="mb-2">

                        <img
                            src="<?= WEB_ROOT ?>student/uploads/<?= htmlspecialchars($student['photo']) ?>"
                            class="img-thumbnail"
                            style="
                                width:90px;
                                height:90px;
                                object-fit:cover;
                            "
                        >

                    </div>

                <?php } ?>

                <input
                    type="file"
                    name="image"
                    class="form-control"
                    accept=".jpg,.jpeg,.png,.webp"
                >

                <small class="text-muted">

                    JPG, PNG, WEBP only

                </small>

            </div>

        </div>

    </div>

</div>
<!-- =========================================================
2. ACADEMIC DETAILS
========================================================= -->

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

    <div class="card-header">

        <h3 class="card-title">

            <i class="fas fa-graduation-cap mr-1"></i>

            Academic Details

        </h3>

    </div>

    <div class="card-body">

        <div class="row">

            <!-- SESSION -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Academic Session
                    <span class="text-danger">*</span>

                </label>

                <select
                    name="session_id"
                    class="form-control"
                    required
                >

                    <?php foreach ($sessions as $session) { ?>

                        <option
                            value="<?= $session['id'] ?>"

                            <?= old(
                                'session_id',
                                $studentSession['session_id'] ?? ''
                            ) == $session['id']
                                ? 'selected'
                                : '' ?>
                        >

                            <?= htmlspecialchars(
                                $session['session_name']
                            ) ?>

                        </option>

                    <?php } ?>

                </select>

            </div>

            <!-- CLASS -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Class
                    <span class="text-danger">*</span>

                </label>

                <select
                    name="class_id"
                    id="class_id"
                    class="form-control"
                    required
                >

                    <option value="">
                        -- Select Class --
                    </option>

                    <?php foreach ($classes as $c) { ?>

                        <option
                            value="<?= $c['id'] ?>"

                            data-section="<?= htmlspecialchars(
                                $c['section'] ?? ''
                            ) ?>"

                            <?= old(
                                'class_id',
                                $studentSession['class_id'] ?? ''
                            ) == $c['id']
                                ? 'selected'
                                : '' ?>
                        >

                            <?= htmlspecialchars(
                                $c['class_name']
                            ) ?>

                        </option>

                    <?php } ?>

                </select>

            </div>

            <!-- SECTION -->

            <div class="col-lg-2 col-md-4 col-12 form-group">

                <label>

                    Section

                </label>

                <input
                    type="text"
                    name="section"
                    id="section"
                    class="form-control"
                    readonly

                    value="<?= old(
                        'section',
                        $studentSession['section'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- ROLL NUMBER -->

            <div class="col-lg-2 col-md-4 col-12 form-group">

                <label>

                    Roll No

                </label>

                <input
                    type="text"
                    name="roll_no"
                    class="form-control"

                    value="<?= old(
                        'roll_no',
                        $studentSession['roll_no'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- ADMISSION NUMBER -->

            <div class="col-lg-2 col-md-4 col-12 form-group">

                <label>

                    Admission No

                </label>

                <input
                    type="text"
                    name="admission_no"
                    class="form-control"

                    value="<?= old(
                        'admission_no',
                        $studentSession['admission_no'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- SCHOLAR NUMBER -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Scholar No

                </label>

                <input
                    type="text"
                    name="scholar_no"
                    class="form-control"

                    value="<?= old(
                        'scholar_no',
                        $student['enrollment_no']
                    ) ?>"
                >

            </div>

            <!-- ADMISSION DATE -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Admission Date

                </label>

                <input
                    type="date"
                    name="admission_date"
                    class="form-control"

                    value="<?= old(
                        'admission_date',
                        $studentSession['admission_date'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- PREVIOUS SCHOOL -->

            <div class="col-lg-6 col-md-12 col-12 form-group">

                <label>

                    Previous School

                </label>

                <input
                    type="text"
                    name="prev_school_name"
                    class="form-control"

                    value="<?= old(
                        'prev_school_name',
                        $academicData['prev_school_name'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- PREVIOUS CLASS -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Previous Class

                </label>

                <input
                    type="text"
                    name="prev_class_studied"
                    class="form-control"

                    value="<?= old(
                        'prev_class_studied',
                        $academicData['prev_class_studied'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- APPEARED EXAM -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Appeared Exam

                </label>

                <input
                    type="text"
                    name="appeared_prev_exam"
                    class="form-control"

                    value="<?= old(
                        'appeared_prev_exam',
                        $academicData['appeared_prev_exam'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- RESULT -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Previous Result

                </label>

                <input
                    type="text"
                    name="prev_exam_result"
                    class="form-control"

                    value="<?= old(
                        'prev_exam_result',
                        $academicData['prev_exam_result'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- MARKS -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Marks Percentage

                </label>

                <input
                    type="number"
                    step="0.01"
                    name="marks_percentage"
                    class="form-control"

                    value="<?= old(
                        'marks_percentage',
                        $academicData['marks_percentage'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- CLASS RANK -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Class Rank

                </label>

                <input
                    type="text"
                    name="class_rank"
                    class="form-control"

                    value="<?= old(
                        'class_rank',
                        $academicData['class_rank'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- ATTENDANCE -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Previous Attendance

                </label>

                <input
                    type="text"
                    name="attendance_prev_year"
                    class="form-control"

                    value="<?= old(
                        'attendance_prev_year',
                        $academicData['attendance_prev_year'] ?? ''
                    ) ?>"
                >

            </div>

        </div>

    </div>

</div>
<!-- =========================================================
3. GOVT & CATEGORY DETAILS
========================================================= -->

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

    <div class="card-header">

        <h3 class="card-title">

            <i class="fas fa-id-card mr-1"></i>

            Govt & Category Details

        </h3>

    </div>

    <div class="card-body">

        <div class="row">

            <!-- AADHAAR -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Aadhaar Number

                </label>

                <input
                    type="text"
                    maxlength="12"
                    name="aadhaar_no"
                    class="form-control"

                    value="<?= old(
                        'aadhaar_no',
                        $documentData['aadhaar_no'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- NAME AS PER AADHAAR -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Name As Per Aadhaar

                </label>

                <input
                    type="text"
                    name="name_as_per_aadhaar"
                    class="form-control"

                    value="<?= old(
                        'name_as_per_aadhaar',
                        $documentData['name_as_per_aadhaar'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- APAAR -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    APAAR ID

                </label>

                <input
                    type="text"
                    name="apaar_id"
                    class="form-control"

                    value="<?= old(
                        'apaar_id',
                        $documentData['apaar_id'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- PEN -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    PEN Number

                </label>

                <input
                    type="text"
                    name="pen_no"
                    class="form-control"

                    value="<?= old(
                        'pen_no',
                        $documentData['pen_no'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- SAMAGRA -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Samagra ID

                </label>

                <input
                    type="text"
                    name="samagra_id"
                    class="form-control"

                    value="<?= old(
                        'samagra_id',
                        $documentData['samagra_id'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- FAMILY SSSMID -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Family SSSMID

                </label>

                <input
                    type="text"
                    name="family_sssmid"
                    class="form-control"

                    value="<?= old(
                        'family_sssmid',
                        $documentData['family_sssmid'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- CASTE CERTIFICATE -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Digital Caste Certificate

                </label>

                <input
                    type="text"
                    name="digital_caste_cert_no"
                    class="form-control"

                    value="<?= old(
                        'digital_caste_cert_no',
                        $documentData['digital_caste_cert_no'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- CATEGORY -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Category

                </label>

                <select
                    name="category"
                    class="form-control"
                >

                    <option value="">
                        Select Category
                    </option>

                    <option value="General" <?= old('category', $categoryData['category'] ?? '') == 'General' ? 'selected' : '' ?>>General</option>

                    <option value="OBC" <?= old('category', $categoryData['category'] ?? '') == 'OBC' ? 'selected' : '' ?>>OBC</option>

                    <option value="SC" <?= old('category', $categoryData['category'] ?? '') == 'SC' ? 'selected' : '' ?>>SC</option>

                    <option value="ST" <?= old('category', $categoryData['category'] ?? '') == 'ST' ? 'selected' : '' ?>>ST</option>

                </select>

            </div>

            <!-- CASTE -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Caste

                </label>

                <input
                    type="text"
                    name="caste"
                    class="form-control"

                    value="<?= old(
                        'caste',
                        $categoryData['caste'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- RELIGION -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Religion

                </label>

                <input
                    type="text"
                    name="religion"
                    class="form-control"

                    value="<?= old(
                        'religion',
                        $categoryData['religion'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- MINORITY -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Minority Group

                </label>

                <input
                    type="text"
                    name="minority_group"
                    class="form-control"

                    value="<?= old(
                        'minority_group',
                        $categoryData['minority_group'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- MOTHER TONGUE -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Mother Tongue

                </label>

                <input
                    type="text"
                    name="mother_tongue"
                    class="form-control"

                    value="<?= old(
                        'mother_tongue',
                        $categoryData['mother_tongue'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- BPL -->

            <div class="col-lg-2 col-md-4 col-12 form-group">

                <label>

                    BPL

                </label>

                <select
                    name="is_bpl"
                    class="form-control"
                >

                    <option value="0">
                        No
                    </option>

                    <option
                        value="1"

                        <?= old(
                            'is_bpl',
                            $categoryData['is_bpl'] ?? 0
                        ) == 1
                            ? 'selected'
                            : '' ?>
                    >
                        Yes
                    </option>

                </select>

            </div>

            <!-- RTE -->

            <div class="col-lg-2 col-md-4 col-12 form-group">

                <label>

                    RTE

                </label>

                <select
                    name="is_rte"
                    class="form-control"
                >

                    <option value="0">
                        No
                    </option>

                    <option
                        value="1"

                        <?= old(
                            'is_rte',
                            $categoryData['is_rte'] ?? 0
                        ) == 1
                            ? 'selected'
                            : '' ?>
                    >
                        Yes
                    </option>

                </select>

            </div>

            <!-- EWS -->

            <div class="col-lg-2 col-md-4 col-12 form-group">

                <label>

                    EWS

                </label>

                <select
                    name="is_ews"
                    class="form-control"
                >

                    <option value="0">
                        No
                    </option>

                    <option
                        value="1"

                        <?= old(
                            'is_ews',
                            $categoryData['is_ews'] ?? 0
                        ) == 1
                            ? 'selected'
                            : '' ?>
                    >
                        Yes
                    </option>

                </select>

            </div>

        </div>

    </div>

</div>

<!-- =========================================================
4. PARENTS DETAILS
========================================================= -->

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

    <div class="card-header">

        <h3 class="card-title">

            <i class="fas fa-users mr-1"></i>

            Parents & Guardian Details

        </h3>

    </div>

    <div class="card-body">

        <div class="row">

            <!-- FATHER -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Father Name

                </label>

                <input
                    type="text"
                    name="father_name"
                    class="form-control"

                    value="<?= old(
                        'father_name',
                        $parentData['father_name'] ?? ''
                    ) ?>"
                >

            </div>

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Father Mobile

                </label>

                <input
                    type="text"
                    maxlength="10"
                    name="father_phone"
                    class="form-control"

                    value="<?= old(
                        'father_phone',
                        $parentData['father_phone'] ?? ''
                    ) ?>"
                >

            </div>

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Father Occupation

                </label>

                <input
                    type="text"
                    name="father_occupation"
                    class="form-control"

                    value="<?= old(
                        'father_occupation',
                        $parentData['father_occupation'] ?? ''
                    ) ?>"
                >

            </div>

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Father Income

                </label>

                <input
                    type="number"
                    step="0.01"
                    name="father_income"
                    class="form-control"

                    value="<?= old(
                        'father_income',
                        $parentData['father_income'] ?? ''
                    ) ?>"
                >

            </div>
			            <!-- MOTHER -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Mother Name

                </label>

                <input
                    type="text"
                    name="mother_name"
                    class="form-control"

                    value="<?= old(
                        'mother_name',
                        $parentData['mother_name'] ?? ''
                    ) ?>"
                >

            </div>

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Mother Mobile

                </label>

                <input
                    type="text"
                    maxlength="10"
                    name="mother_phone"
                    class="form-control"

                    value="<?= old(
                        'mother_phone',
                        $parentData['mother_phone'] ?? ''
                    ) ?>"
                >

            </div>

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Mother Occupation

                </label>

                <input
                    type="text"
                    name="mother_occupation"
                    class="form-control"

                    value="<?= old(
                        'mother_occupation',
                        $parentData['mother_occupation'] ?? ''
                    ) ?>"
                >

            </div>

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Mother Income

                </label>

                <input
                    type="number"
                    step="0.01"
                    name="mother_income"
                    class="form-control"

                    value="<?= old(
                        'mother_income',
                        $parentData['mother_income'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- GUARDIAN -->

            <div class="col-lg-4 col-md-6 col-12 form-group">

                <label>

                    Guardian Name

                </label>

                <input
                    type="text"
                    name="guardian_name"
                    class="form-control"

                    value="<?= old(
                        'guardian_name',
                        $parentData['guardian_name'] ?? ''
                    ) ?>"
                >

            </div>

            <div class="col-lg-4 col-md-6 col-12 form-group">

                <label>

                    Guardian Relation

                </label>

                <input
                    type="text"
                    name="guardian_relation"
                    class="form-control"

                    value="<?= old(
                        'guardian_relation',
                        $parentData['guardian_relation'] ?? ''
                    ) ?>"
                >

            </div>

            <div class="col-lg-4 col-md-6 col-12 form-group">

                <label>

                    Guardian Mobile

                </label>

                <input
                    type="text"
                    maxlength="10"
                    name="guardian_phone"
                    class="form-control"

                    value="<?= old(
                        'guardian_phone',
                        $parentData['guardian_phone'] ?? ''
                    ) ?>"
                >

            </div>

        </div>

    </div>

</div>

<!-- =========================================================
5. ADDRESS DETAILS
========================================================= -->

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

    <div class="card-header">

        <h3 class="card-title">

            <i class="fas fa-map-marker-alt mr-1"></i>

            Address Details

        </h3>

    </div>

    <div class="card-body">

        <div class="row">

            <!-- CURRENT ADDRESS -->

            <div class="col-lg-6 col-md-12 col-12 form-group">

                <label>

                    Current Address

                </label>

                <textarea
                    name="address"
                    rows="3"
                    class="form-control"
                ><?= old(
                    'address',
                    $addressData['address'] ?? ''
                ) ?></textarea>

            </div>

            <!-- PERMANENT ADDRESS -->

            <div class="col-lg-6 col-md-12 col-12 form-group">

                <label>

                    Permanent Address

                </label>

                <textarea
                    name="permanent_address"
                    rows="3"
                    class="form-control"
                ><?= old(
                    'permanent_address',
                    $addressData['permanent_address'] ?? ''
                ) ?></textarea>

            </div>

            <!-- STATE -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    State

                </label>

                <input
                    type="text"
                    name="state"
                    class="form-control"

                    value="<?= old(
                        'state',
                        $addressData['state'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- CITY -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    City

                </label>

                <input
                    type="text"
                    name="city"
                    class="form-control"

                    value="<?= old(
                        'city',
                        $addressData['city'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- VILLAGE -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Village

                </label>

                <input
                    type="text"
                    name="village"
                    class="form-control"

                    value="<?= old(
                        'village',
                        $addressData['village'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- PINCODE -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Pincode

                </label>

                <input
                    type="text"
                    maxlength="6"
                    name="pincode"
                    class="form-control"

                    value="<?= old(
                        'pincode',
                        $addressData['pincode'] ?? ''
                    ) ?>"
                >

            </div>

        </div>

    </div>

</div>

<!-- =========================================================
6. BANK & TRANSPORT DETAILS
========================================================= -->

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

    <div class="card-header">

        <h3 class="card-title">

            <i class="fas fa-university mr-1"></i>

            Bank & Transport Details

        </h3>

    </div>

    <div class="card-body">

        <div class="row">

            <!-- BANK ACCOUNT -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Bank Account Number

                </label>

                <input
                    type="text"
                    name="bank_acc_no"
                    class="form-control"

                    value="<?= old(
                        'bank_acc_no',
                        $bankData['bank_acc_no'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- BANK NAME -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Bank Name

                </label>

                <input
                    type="text"
                    name="bank_name"
                    class="form-control"

                    value="<?= old(
                        'bank_name',
                        $bankData['bank_name'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- IFSC -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    IFSC Code

                </label>

                <input
                    type="text"
                    name="ifsc_code"
                    class="form-control"

                    value="<?= old(
                        'ifsc_code',
                        $bankData['ifsc_code'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- OPENING BALANCE -->

            <div class="col-lg-3 col-md-6 col-12 form-group">

                <label>

                    Opening Balance

                </label>

                <input
                    type="number"
                    step="0.01"
                    name="opening_balance"
                    class="form-control"

                    value="<?= old(
                        'opening_balance',
                        $bankData['opening_balance'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- ANNUAL INCOME -->

            <div class="col-lg-4 col-md-6 col-12 form-group">

                <label>

                    Annual Family Income

                </label>

                <input
                    type="number"
                    step="0.01"
                    name="annual_income"
                    class="form-control"

                    value="<?= old(
                        'annual_income',
                        $categoryData['annual_income'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- TRANSPORT ROUTE -->

            <div class="col-lg-4 col-md-6 col-12 form-group">

                <label>

                    Transport Route

                </label>

                <input
                    type="text"
                    name="transport_route"
                    class="form-control"

                    value="<?= old(
                        'transport_route',
                        $bankData['transport_route'] ?? ''
                    ) ?>"
                >

            </div>

            <!-- TRANSPORT FEES -->

            <div class="col-lg-4 col-md-6 col-12 form-group">

                <label>

                    Transport Fees

                </label>

                <input
                    type="number"
                    step="0.01"
                    name="transport_fees"
                    class="form-control"

                    value="<?= old(
                        'transport_fees',
                        $bankData['transport_fees'] ?? ''
                    ) ?>"
                >

            </div>

        </div>

    </div>

</div>

<!-- =========================================================
SUBMIT BUTTONS
========================================================= -->

<div class="card">

    <div class="card-body">

        <div class="row">

            <div class="col-12 text-center text-md-right">

                <a
                    href="index.php"
                    class="btn btn-secondary px-4"
                >

                    <i class="fas fa-times mr-1"></i>

                    Cancel

                </a>

                <button
                    type="submit"
                    class="btn btn-primary px-4"
                >

                    <i class="fas fa-save mr-1"></i>

                    Update Student

                </button>

            </div>

        </div>

    </div>

</div>

</form>

</div>

</section>

</div>

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

<script>

const classDropdown =
    document.getElementById('class_id');

const sectionInput =
    document.getElementById('section');

if (classDropdown) {

    classDropdown.addEventListener(
        'change',
        function () {

            let selectedOption =
                this.options[this.selectedIndex];

            let section =
                selectedOption.getAttribute(
                    'data-section'
                ) || '';

            sectionInput.value = section;
        }
    );

    classDropdown.dispatchEvent(
        new Event('change')
    );
}

</script>