<?php
/**
 * Single Student Result Form - Refactored for Bootstrap 5.3
 * Using AJAX via APIs/getStudentResultData.php and APIs/saveStudentResult.php
 */
?>

<div id="singleResultWrapper" dir="rtl">
    <!-- Navigation Tabs / Links -->
    <div class="d-flex flex-wrap gap-2 mb-4">
        <a href="resultFrm" class="btn btn-outline-primary" title="تمام طلباء کا نتیجہ درج کریں">
            <i class="bi bi-people me-1"></i>تمام طلباء کا نتیجہ
        </a>
        <a href="updtaeResult" class="btn btn-outline-secondary" title="نتیجہ میں تبدیلی کریں">
            <i class="bi bi-pencil-square me-1"></i>تبدیلی کریں
        </a>
        <a href="dashboard" class="btn btn-outline-dark">
            <i class="bi bi-house me-1"></i>ڈیش بورڈ
        </a>
    </div>

    <!-- Page Header -->
    <div class="mb-4">
        <h2 class="fw-bold mb-0 text-primary">
            <i class="bi bi-person-badge me-2"></i>طالبِ علم کا انفرادی رزلٹ (Single Student Result)
        </h2>
        <p class="text-muted small">
            <?php echo htmlspecialchars(M_Name); ?>
        </p>
    </div>

    <!-- Alert Placeholder -->
    <div id="formAlert"></div>

    <!-- Search Card -->
    <div class="card border-0 shadow-sm mb-4 bg-light">
        <div class="card-body p-4">
            <form id="searchForm" class="row g-3 align-items-end">
                <div class="col-md-4">
                    <label class="form-label fw-semibold">رجسٹریشن نمبر لکھیں</label>
                    <input type="text" id="registrationNo" name="registrationNo"
                        class="form-control form-control-lg text-start" dir="ltr" placeholder="مثلاً: 1234" required>
                </div>
                <div class="col-md-4">
                    <label class="form-label fw-semibold">درجہ منتخب کریں</label>
                    <?php $crud->darjaatCmb('darja', 'class="form-select form-select-lg" id="darja" required'); ?>
                </div>
                <div class="col-md-4">
                    <button type="submit" class="btn btn-primary btn-lg w-100 shadow-sm" id="btnSearch">
                        <i class="bi bi-search me-2"></i> تلاش کریں
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Result Entry Container (Hidden by default) -->
    <div id="resultEntryContainer" class="d-none">
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header bg-dark text-white py-3">
                <h5 class="card-title mb-0 fs-5">
                    <i class="bi bi-file-earmark-text me-2"></i> رزلٹ کی تفصیلات
                </h5>
            </div>
            <div class="card-body p-4">
                <!-- Info Displayed by JS -->
                <div id="studentInfoDisplay" class="alert alert-info border-0 mb-4 shadow-sm">
                </div>

                <form id="saveResultForm">
                    <div class="row g-3 mb-4">
                        <div class="col-md-4">
                            <label class="form-label fw-semibold">امتحان (Term)</label>
                            <select name="resultTerm" id="resultTerm" class="form-select form-select-lg">
                                <option value="1"> سہ ماہی (Quarterly) </option>
                                <option value="2"> شش ماہی (Half-Yearly) </option>
                                <option value="3"> سالانہ (Annual) </option>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label fw-semibold">تاریخِ ابتداء (Promotion Date)</label>
                            <input type="text" id="promotionDate" name="promotionDate"
                                class="form-control form-control-lg text-start hijri-picker" dir="ltr"
                                value="<?php echo htmlspecialchars($promotionDt); ?>" readonly required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label fw-semibold">تاریخِ انتہا (End Date)</label>
                            <input type="text" id="dateEnd" name="dateEnd"
                                class="form-control form-control-lg text-start hijri-picker" dir="ltr"
                                value="<?php echo htmlspecialchars($dateEd); ?>" readonly required>
                        </div>
                    </div>

                    <div class="table-responsive">
                        <table class="table table-hover table-bordered align-middle">
                            <thead class="table-light">
                                <tr>
                                    <th class="ps-3">مضمون (Subject)</th>
                                    <th class="text-center" style="width: 150px;">کل نمبرات</th>
                                    <th class="text-center" style="width: 200px;">حاصل کردہ نمبرات</th>
                                </tr>
                            </thead>
                            <tbody id="subjectsTableBody">
                                <!-- Injected by JS -->
                            </tbody>
                        </table>
                    </div>

                    <div class="mt-4 border-top pt-4">
                        <div class="row g-3">
                            <div class="col-md-12 mb-3">
                                <label for="edu_year_remarks" class="form-label fw-semibold">تعلیمی سال کی تفصیل
                                    (Remarks)</label>
                                <textarea class="form-control" name="edu_year_remarks" id="edu_year_remarks" rows="2"
                                    placeholder="مثلاً: تعلیمی سال 1445-1446ھ کے نتائج"></textarea>
                            </div>
                            <div class="col-md-12 d-flex justify-content-end">
                                <button type="submit" class="btn btn-success btn-lg px-5 shadow-sm" id="btnSave">
                                    <i class="bi bi-save me-2"></i>محفوظ کریں
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- JS Handling -->
    <script src="assets/js/result_frm_single_std.js"></script>
</div>