<?php
declare(strict_types=1);

require_once __DIR__ . '/../app/init.php';
app_require_login();

$requestedCompanyId = (int) ($_GET['company_id'] ?? 0);
$companyId = app_effective_company_id($requestedCompanyId > 0 ? $requestedCompanyId : null, true);
if ($companyId !== null && $companyId > 0) {
    app_set_context_company_id($companyId);
}
$company = $companyId ? app_company_by_id((int) $companyId) : null;
$counts = app_module_counts($companyId);
$visibleBySlug = [];
foreach (app_visible_modules() as $module) {
    $moduleSlug = (string) ($module['slug'] ?? '');
    if ($moduleSlug !== '') {
        $visibleBySlug[$moduleSlug] = $module;
    }
}

$isArabic = app_ui_language() === 'ar';
$txt = static fn (string $en, string $ar): string => $isArabic ? $ar : $en;
$companyQuery = $companyId ? ('company_id=' . urlencode((string) $companyId)) : '';
$moduleCard = static function (string $slug, string $accent) use ($visibleBySlug, $counts, $companyQuery): ?array {
    $module = $visibleBySlug[$slug] ?? null;
    if (!is_array($module)) {
        return null;
    }
    return [
        'title' => (string) ($module['title'] ?? $slug),
        'subtitle' => (string) ($module['description'] ?? ''),
        'href' => 'module.php?module=' . urlencode($slug) . ($companyQuery !== '' ? '&' . $companyQuery : ''),
        'icon' => (string) ($module['icon'] ?? strtoupper(substr($slug, 0, 2))),
        'count' => (int) ($counts[$slug] ?? 0),
        'accent' => $accent,
    ];
};

$reportCards = [
    ['title' => $txt('Student Dashboard', 'لوحة حسابات الطلاب'), 'href' => 'student_reports.php' . ($companyQuery !== '' ? '?' . $companyQuery : ''), 'icon' => 'SR', 'accent' => '#0ea5e9', 'count' => (int) ($counts['student_invoices'] ?? 0)],
    ['title' => $txt('Student Statement', 'كشف حساب طالب'), 'href' => 'student_reports.php?report=statement' . ($companyQuery !== '' ? '&' . $companyQuery : ''), 'icon' => 'ST', 'accent' => '#2563eb', 'count' => (int) ($counts['students'] ?? 0)],
    ['title' => $txt('Receivables Aging', 'أعمار مديونية الطلاب'), 'href' => 'student_reports.php?report=aging' . ($companyQuery !== '' ? '&' . $companyQuery : ''), 'icon' => 'AG', 'accent' => '#dc2626', 'count' => (int) ($counts['student_invoices'] ?? 0)],
    ['title' => $txt('Collections', 'التحصيلات'), 'href' => 'student_reports.php?report=collections' . ($companyQuery !== '' ? '&' . $companyQuery : ''), 'icon' => 'RC', 'accent' => '#16a34a', 'count' => (int) ($counts['student_receipts'] ?? 0)],
];

$groups = [
    [
        'title' => $txt('Billing and Collection', 'الفواتير والتحصيل'),
        'cards' => array_filter([
            $moduleCard('student_invoices', '#2563eb'),
            $moduleCard('student_receipts', '#16a34a'),
            $moduleCard('student_refunds', '#f97316'),
        ]),
    ],
    [
        'title' => $txt('Students and Academic Structure', 'الطلاب والهيكل الدراسي'),
        'cards' => array_filter([
            $moduleCard('students', '#7c3aed'),
            $moduleCard('student_stages', '#0f766e'),
            $moduleCard('student_grades', '#0891b2'),
            $moduleCard('student_classes', '#f59e0b'),
        ]),
    ],
    [
        'title' => $txt('Fee Setup and GL Integration', 'تهيئة الرسوم والربط المحاسبي'),
        'cards' => array_filter([
            $moduleCard('student_revenue_types', '#be123c'),
            $moduleCard('student_fee_plans', '#9333ea'),
            $moduleCard('student_account_settings', '#475569'),
            $moduleCard('posting_account_settings', '#0f766e'),
        ]),
    ],
];
?>
<!doctype html>
<html <?= app_html_attrs() ?>>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?= app_escape((string) app_config('app_name')) ?> - <?= app_escape($txt('Student Accounts', 'حسابات الطلاب')) ?></title>
  <link rel="icon" href="assets/favicon.svg" type="image/svg+xml">
  <link rel="shortcut icon" href="assets/favicon.svg" type="image/svg+xml">
  <link rel="stylesheet" href="assets/styles.css">
</head>
<body>
<div class="app-shell has-sidebar">
  <?= app_sidebar_nav_html('student_accounts', null, $companyId) ?>
  <div class="workspace-page">
    <section class="workspace-hero workspace-hero--students">
      <div>
        <a class="workspace-back" href="home.php<?= $companyId ? '?company_id=' . (int) $companyId : '' ?>"><?= app_escape($txt('Back to Home', 'رجوع للرئيسية')) ?></a>
        <h1><?= app_escape($txt('Student Accounts', 'حسابات الطلاب')) ?></h1>
        <p><?= app_escape($txt('A dedicated school-fee workspace for students, guardians, Saudi stages and grades, invoices, receipts, refunds, reports, and automatic GL posting.', 'مساحة مستقلة لحسابات الطلاب: بيانات الطلاب وأولياء الأمور والمراحل والصفوف السعودية والفواتير والتحصيل والسندات والتقارير والقيود المحاسبية الآلية.')) ?></p>
      </div>
      <img src="<?= app_escape(app_logo_path_for_header($company)) ?>" alt="Company Logo">
    </section>

    <section class="workspace-kpis">
      <div class="workspace-kpi"><span><?= app_escape($txt('Students', 'الطلاب')) ?></span><strong><?= app_escape(number_format((int) ($counts['students'] ?? 0))) ?></strong></div>
      <div class="workspace-kpi"><span><?= app_escape($txt('Invoices', 'الفواتير')) ?></span><strong><?= app_escape(number_format((int) ($counts['student_invoices'] ?? 0))) ?></strong></div>
      <div class="workspace-kpi"><span><?= app_escape($txt('Receipts', 'سندات القبض')) ?></span><strong><?= app_escape(number_format((int) ($counts['student_receipts'] ?? 0))) ?></strong></div>
      <div class="workspace-kpi"><span><?= app_escape($txt('Revenue Types', 'أنواع الإيرادات')) ?></span><strong><?= app_escape(number_format((int) ($counts['student_revenue_types'] ?? 0))) ?></strong></div>
    </section>

    <section class="workspace-section">
      <div class="workspace-section-head">
        <h2><?= app_escape($txt('Student Reports', 'تقارير حسابات الطلاب')) ?></h2>
        <span><?= app_escape($txt('Statements, receivables, collections, revenue analysis, and aging.', 'كشوف حساب ومديونيات وتحصيلات وتحليل إيرادات وأعمار مديونية.')) ?></span>
      </div>
      <div class="workspace-card-grid">
        <?php foreach ($reportCards as $card): ?>
          <a class="workspace-card" href="<?= app_escape((string) $card['href']) ?>" style="--workspace-accent: <?= app_escape((string) $card['accent']) ?>;">
            <span class="workspace-card-icon"><?= app_escape((string) $card['icon']) ?></span>
            <strong><?= app_escape((string) $card['title']) ?></strong>
            <small><?= app_escape(number_format((int) $card['count'])) ?> <?= app_escape($txt('records', 'سجلات')) ?></small>
          </a>
        <?php endforeach; ?>
      </div>
    </section>

    <?php foreach ($groups as $group): ?>
      <section class="workspace-section">
        <div class="workspace-section-head">
          <h2><?= app_escape((string) $group['title']) ?></h2>
          <span><?= app_escape(number_format(count((array) $group['cards']))) ?> <?= app_escape($txt('modules', 'وحدات')) ?></span>
        </div>
        <div class="workspace-card-grid">
          <?php foreach ((array) $group['cards'] as $card): ?>
            <a class="workspace-card" href="<?= app_escape((string) $card['href']) ?>" style="--workspace-accent: <?= app_escape((string) $card['accent']) ?>;">
              <span class="workspace-card-icon"><?= app_escape((string) $card['icon']) ?></span>
              <strong><?= app_escape((string) $card['title']) ?></strong>
              <small><?= app_escape(number_format((int) $card['count'])) ?> <?= app_escape($txt('records', 'سجلات')) ?></small>
            </a>
          <?php endforeach; ?>
        </div>
      </section>
    <?php endforeach; ?>
  </div>
</div>
<script src="assets/app.js"></script>
</body>
</html>
