<?php

namespace App\Http\Controllers\AdminSekolah;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class MuridController extends Controller
{
    /**
     * Get the school ID for the current admin
     */
    private function getSchoolId()
    {
        $user = Auth::user();
        
        if ($user->employee && $user->employee->activeAssignment) {
            return $user->employee->activeAssignment->school_id;
        }
        
        return null;
    }

    /**
     * Get school data
     */
    private function getSchool()
    {
        $user = Auth::user();
        
        if ($user->employee && $user->employee->activeAssignment) {
            return $user->employee->activeAssignment->school;
        }
        
        return null;
    }

    /**
     * Display student list
     */
    public function index()
    {
        $school = $this->getSchool();
        
        if (!$school) {
            return redirect()->route('dashboard')->with('error', 'Anda belum memiliki sekolah yang ditugaskan.');
        }

        // Dummy data untuk sementara - nanti bisa diintegrasikan dengan tabel students jika ada
        $murid = collect([
            [
                'id' => 1,
                'nisn' => '0012345678',
                'nis' => '12345',
                'nama' => 'Ahmad Rizki Pratama',
                'jenis_kelamin' => 'L',
                'kelas' => 'X MIPA 1',
                'tahun_masuk' => '2024',
                'status' => 'Aktif'
            ],
            [
                'id' => 2,
                'nisn' => '0012345679',
                'nis' => '12346',
                'nama' => 'Siti Nurhaliza',
                'jenis_kelamin' => 'P',
                'kelas' => 'X MIPA 1',
                'tahun_masuk' => '2024',
                'status' => 'Aktif'
            ],
            [
                'id' => 3,
                'nisn' => '0012345680',
                'nis' => '12347',
                'nama' => 'Budi Santoso',
                'jenis_kelamin' => 'L',
                'kelas' => 'XI IPS 2',
                'tahun_masuk' => '2023',
                'status' => 'Aktif'
            ],
            [
                'id' => 4,
                'nisn' => '0012345681',
                'nis' => '12348',
                'nama' => 'Dewi Lestari',
                'jenis_kelamin' => 'P',
                'kelas' => 'XII MIPA 3',
                'tahun_masuk' => '2022',
                'status' => 'Aktif'
            ],
        ]);

        // Statistics
        $stats = [
            'total' => $murid->count(),
            'aktif' => $murid->where('status', 'Aktif')->count(),
            'laki_laki' => $murid->where('jenis_kelamin', 'L')->count(),
            'perempuan' => $murid->where('jenis_kelamin', 'P')->count(),
        ];

        return view('admin-sekolah.murid', [
            'murid' => $murid,
            'stats' => $stats,
            'school' => $school,
        ]);
    }
}
