================================================================================
  PARENT PAYMENT - SHOW GRADE-LEVEL SUBJECTS (CORRECT APPROACH)
================================================================================

REQUIREMENT:
------------
Show ALL subjects available for the student's grade level, not just enrolled ones.
This allows parents to pay for additional subjects their child wants to take.

================================================================================
HOW IT WORKS:
================================================================================

GRADE-SUBJECT RELATIONSHIP:
---------------------------
The database has a grade_subject table that links:
- Grades (Grade 4, Grade 5, Form 1, Form 2, etc.)
- Subjects (Mathematics, English, SiSwati, etc.)

Example:
Grade 7 subjects might include:
- English Language
- SiSwati Language  
- Mathematics
- Science
- Social Studies
- Religious Education
- Physical Education
- ICT/Computer Studies

Form 1 subjects might include:
- English Language
- SiSwati Language
- Mathematics
- Integrated Science
- Social Studies
- Religious Education
- Agriculture
- Business Studies
- Design & Technology

================================================================================
THE SOLUTION:
================================================================================

SQL QUERY:
----------
SELECT s.id, s.name, s.code 
FROM subjects s
INNER JOIN grade_subject gs ON s.id = gs.subject_id
WHERE gs.grade_id = ?
ORDER BY s.name

LOGIC:
------
1. Get student's grade_id (e.g., Grade 7)
2. Find all subjects linked to that grade in grade_subject table
3. Display those subjects on payment page
4. Mark which ones student is already enrolled in
5. Parent can pay for new subjects or existing ones

================================================================================
USER EXPERIENCE:
================================================================================

EXAMPLE: Student in Grade 7
----------------------------

Payment Page Shows:
┌─────────────────────────────────────┐
│ Select Subjects                      │
│ These are the subjects available     │
│ for Grade 7. Already enrolled        │
│ subjects are marked.                 │
├─────────────────────────────────────┤
│ ☑ English (Enrolled)    E350/month  │ ← Already taking
│ ☐ SiSwati               E350/month  │ ← Can add
│ ☑ Mathematics (Enrolled) E350/month │ ← Already taking
│ ☐ Science               E350/month  │ ← Can add
│ ☐ Social Studies        E350/month  │ ← Can add
│ ☑ Physical Ed (Enrolled) E350/month │ ← Already taking
│ ☐ ICT                   E350/month  │ ← Can add
└─────────────────────────────────────┘

BENEFITS:
---------
✓ Parents see all grade-appropriate subjects
✓ Can enroll in new subjects by paying
✓ Can continue paying for existing subjects
✓ Clear which ones already enrolled
✓ Grade-level filtering (Form 1 subjects != Grade 7 subjects)

================================================================================
COMPARISON TO ALTERNATIVES:
================================================================================

APPROACH 1: Show ALL subjects in database
------------------------------------------
SELECT * FROM subjects ORDER BY name

PROBLEM:
- Shows Grade 4 subjects to Form 5 students
- Shows Form 3 subjects to Grade 6 students
- Not age-appropriate
- Overwhelming (50+ subjects)

APPROACH 2: Show only enrolled subjects
----------------------------------------
SELECT s.* FROM subjects s
JOIN student_subject ss ON s.id = ss.subject_id
WHERE ss.student_id = ?

PROBLEM:
- Can't add new subjects
- Limited to what student already has
- Parent must contact school to add subjects

APPROACH 3: Show grade-level subjects (CORRECT!)
-------------------------------------------------
SELECT s.* FROM subjects s
JOIN grade_subject gs ON s.id = gs.subject_id
WHERE gs.grade_id = ?

BENEFITS:
- Shows appropriate subjects for grade
- Parent can add new subjects
- Parent can pay for existing subjects
- Age-appropriate
- Manageable list (8-15 subjects typically)

================================================================================
HOW ENROLLMENT WORKS:
================================================================================

SCENARIO 1: Paying for existing subjects
-----------------------------------------
1. Student already enrolled in: Math, English
2. Payment page shows: Math (✓), English (✓), Science, History, etc.
3. Parent selects: Math, English
4. Pays
5. Payment recorded for those subjects

SCENARIO 2: Adding new subjects
--------------------------------
1. Student enrolled in: Math, English
2. Payment page shows: Math (✓), English (✓), Science, History, etc.
3. Parent selects: Math, English, Science (NEW!)
4. Pays
5. System automatically enrolls student in Science
6. Payment recorded
7. Student now enrolled in: Math, English, Science

SCENARIO 3: Mixed payment
--------------------------
1. Student enrolled in: Math, English
2. Parent selects: Math, Science, History (1 existing + 2 new)
3. System:
   - Keeps Math enrollment (already exists)
   - Adds Science enrollment (new)
   - Adds History enrollment (new)
   - Records payment for all 3

================================================================================
DATABASE STRUCTURE:
================================================================================

Tables Involved:
----------------

1. students
   - id
   - grade_id ← Links to grades table
   - full_name
   - etc.

2. grades
   - id
   - name (Grade 4, Grade 5, Form 1, etc.)

3. subjects
   - id
   - name (Mathematics, English, etc.)
   - code (G7-MAT-003, F1-ENG-001, etc.)

4. grade_subject (MAPPING TABLE)
   - grade_id ← Which grade
   - subject_id ← Which subject
   - (Defines which subjects available for which grades)

5. student_subject (ENROLLMENT TABLE)
   - student_id ← Which student
   - subject_id ← Which subject
   - enrolled_at
   - (Records actual enrollments)

QUERY PATH:
-----------
student → grade_id → grade_subject → subjects
         ↓
    student_subject (marks which already enrolled)

================================================================================
CODE BREAKDOWN:
================================================================================

STEP 1: Get student details including grade
--------------------------------------------
$stmt = $pdo->prepare("
    SELECT s.*, g.name as grade_name 
    FROM students s
    JOIN grades g ON s.grade_id = g.id
    WHERE s.id = ?
");
$stmt->execute([$student_id]);
$student = $stmt->fetch();

Result: 
$student['grade_id'] = 7 (for example)
$student['grade_name'] = 'Grade 7'


STEP 2: Get subjects for that grade
------------------------------------
$stmt = $pdo->prepare("
    SELECT s.id, s.name, s.code 
    FROM subjects s
    INNER JOIN grade_subject gs ON s.id = gs.subject_id
    WHERE gs.grade_id = ?
    ORDER BY s.name
");
$stmt->execute([$student['grade_id']]);
$subjects = $stmt->fetchAll();

Result: Array of subjects for Grade 7


STEP 3: Get enrolled subjects to mark them
-------------------------------------------
$stmt = $pdo->prepare("
    SELECT subject_id FROM student_subject WHERE student_id = ?
");
$stmt->execute([$student_id]);
$enrolled_subjects = array_column($stmt->fetchAll(), 'subject_id');

Result: [1, 5, 8] (IDs of enrolled subjects)


STEP 4: Display with enrollment status
---------------------------------------
foreach ($subjects as $subject) {
    $is_enrolled = in_array($subject['id'], $enrolled_subjects);
    
    // Show checkbox
    // If $is_enrolled, mark with "Currently Enrolled" badge
}

================================================================================
EDGE CASES:
================================================================================

CASE 1: No subjects configured for grade
-----------------------------------------
Display: "No subjects available. No subjects have been configured for 
         Grade 7 yet."

Action: Admin needs to configure grade-subject relationships


CASE 2: Student moves to new grade
-----------------------------------
- Payment page automatically shows new grade's subjects
- Old grade subjects no longer shown
- Student keeps enrollment in subjects that exist in both grades


CASE 3: Subject removed from grade
-----------------------------------
- Subject no longer appears on payment page
- Student keeps enrollment if already enrolled
- Can't pay for it anymore (which is correct)

================================================================================
ADMIN CONFIGURATION:
================================================================================

For this to work, admin must configure grade-subject relationships:

In admin panel:
1. Go to Configure Subjects
2. Select Grade (e.g., Grade 7)
3. Assign subjects to that grade
4. Saves to grade_subject table

SQL Example:
INSERT INTO grade_subject (grade_id, subject_id) VALUES
(7, 1),  -- Grade 7, Mathematics
(7, 2),  -- Grade 7, English
(7, 3),  -- Grade 7, SiSwati
... etc

This determines which subjects parents can pay for!

================================================================================
FILES CHANGED:
================================================================================

parent/make_payment.php
- Lines 37-53: Query to get grade-level subjects
- Lines 252-263: Updated help text and messages

================================================================================
TESTING:
================================================================================

TEST 1: Student in Grade 7
---------------------------
1. Login as parent
2. Select student in Grade 7
3. Click Make Payment
4. Should see Grade 7 subjects only
5. Enrolled subjects marked with badge
6. Can select and pay for any

TEST 2: Student in Form 1
--------------------------
1. Login as parent
2. Select student in Form 1
3. Click Make Payment
4. Should see Form 1 subjects only (different from Grade 7)
5. Verify correct subjects for that level

TEST 3: Add new subject
-----------------------
1. Student enrolled in: Math, English
2. Parent selects: Math, English, Science
3. Pay
4. Check database: student_subject table
5. Should have 3 enrollments now
6. Science should auto-enroll

TEST 4: No subjects configured
-------------------------------
1. Create new grade with no subjects assigned
2. Make payment for student in that grade
3. Should see: "No subjects available" message

================================================================================
UPLOAD TO SERVER:
================================================================================

FILE: parent/make_payment.php
LOCAL: C:\xampp\htdocs\Multi-Tanent\parent\make_payment.php

UPLOAD TO:
public_html/test.melanegroup.com/Multi-Tanent/parent/make_payment.php

================================================================================
VERIFICATION AFTER UPLOAD:
================================================================================

1. Login as parent
2. Go to Make Payment for each child
3. Verify:
   ✓ Only see subjects for that child's grade
   ✓ Enrolled subjects are marked
   ✓ Can select and pay for subjects
   ✓ Can add new subjects by paying
   ✓ Different grades show different subjects

================================================================================
SUMMARY:
================================================================================

LOGIC: Show all subjects configured for the student's grade level
METHOD: Use grade_subject table to filter by grade_id
RESULT: Age-appropriate subject list with enrollment status
BENEFIT: Parents can pay for existing or add new subjects

This is the correct approach for multi-grade schools! 🎉

================================================================================

