<?php
/**
 * admin/finance_tabs.php
 * Unified navigation for Billing & Accounting.
 */

$ua_fin_today = date('Y-m-d');
$ua_fin_month = date('Y-m');
$ua_fin_current = basename($_SERVER['PHP_SELF'] ?? '');

$ua_fin_groups = [
  'summary' => [
    'title' => 'الملخص المالي',
    'icon'  => 'fa-line-chart',
    'href'  => 'finance_reports.php',
    'tabs'  => [
      'summary'      => ['label' => 'الملخص',       'href' => 'finance_reports.php', 'icon' => 'fa-pie-chart'],
      'cashbox'      => ['label' => 'الأرصدة اليومية', 'href' => 'finance_daily_balances.php', 'icon' => 'fa-archive'],
      'day_report'   => ['label' => 'تقرير اليوم',  'href' => 'finance_day_report.php?day=' . urlencode($ua_fin_today), 'icon' => 'fa-calendar-check-o'],
      'close'        => ['label' => 'إغلاق الشهر',  'href' => 'finance_month_close.php', 'icon' => 'fa-lock'],
      'month_report' => ['label' => 'تقرير الشهر',  'href' => 'finance_month_report.php?month=' . urlencode($ua_fin_month), 'icon' => 'fa-bar-chart'],
    ],
  ],
  'billing' => [
    'title' => 'الفواتير',
    'icon'  => 'fa-file-text-o',
    'href'  => 'invoices.php',
    'tabs'  => [
      'invoices'               => ['label' => 'الفواتير',       'href' => 'invoices.php', 'icon' => 'fa-list-alt'],
      'invoice_add'            => ['label' => 'فاتورة جديدة',   'href' => 'invoices_add.php', 'icon' => 'fa-plus'],
      'gen'                    => ['label' => 'دورة الفوترة',   'href' => 'invoices_generate_month.php', 'icon' => 'fa-refresh'],
      'billing_message_requests' => ['label' => 'رسائل التسديد', 'href' => 'billing_message_requests.php', 'icon' => 'fa-envelope'],
      'billing_limit_requests' => ['label' => 'طلبات التخفيض',  'href' => 'billing_limit_requests.php', 'icon' => 'fa-tachometer'],
      'billing_cut_requests'   => ['label' => 'طلبات القطع',    'href' => 'billing_cut_requests.php', 'icon' => 'fa-ban'],
      'print_preview'          => ['label' => 'الطباعة',        'href' => 'print_preview.php?type=page', 'icon' => 'fa-print'],
    ],
  ],
  'movement' => [
    'title' => 'الحركة المالية',
    'icon'  => 'fa-exchange',
    'href'  => 'finance_payments.php',
    'tabs'  => [
      'payments' => ['label' => 'الدفعات',  'href' => 'finance_payments.php', 'icon' => 'fa-arrow-circle-down'],
      'expenses' => ['label' => 'المدفوعات الخارجة', 'href' => 'finance_expenses.php', 'icon' => 'fa-arrow-circle-up'],
    ],
  ],
  'purchases' => [
    'title' => 'المشتريات',
    'icon'  => 'fa-shopping-bag',
    'href'  => 'purchases_report.php',
    'tabs'  => [
      'purchases_report' => ['label' => 'جرد المشتريات', 'href' => 'purchases_report.php', 'icon' => 'fa-clipboard'],
      'purchases_new'    => ['label' => 'مشتريات جديدة', 'href' => 'purchases_new.php', 'icon' => 'fa-plus-circle'],
    ],
  ],
  'equity' => [
    'title' => 'الشركاء ورأس المال',
    'icon'  => 'fa-users',
    'href'  => 'finance_partners.php',
    'tabs'  => [
      'partners' => ['label' => 'الشركاء ورأس المال', 'href' => 'finance_partners.php', 'icon' => 'fa-users'],
      'purchases_new' => ['label' => 'شراء رأسمالي', 'href' => 'purchases_new.php', 'icon' => 'fa-building-o'],
      'close' => ['label' => 'إغلاق الشهر', 'href' => 'finance_month_close.php', 'icon' => 'fa-lock'],
    ],
  ],
];

$ua_fin_page_map = [
  'finance_reports.php'          => ['summary', 'summary'],
  'finance_cashbox.php'          => ['cashbox', 'summary'],
  'finance_daily_balances.php'   => ['cashbox', 'summary'],
  'finance_day_report.php'       => ['day_report', 'summary'],
  'finance_month_close.php'      => ['close', 'summary'],
  'finance_month_report.php'     => ['month_report', 'summary'],
  'invoices.php'                 => ['invoices', 'billing'],
  'invoices_add.php'             => ['invoice_add', 'billing'],
  'invoices_generate_month.php'  => ['gen', 'billing'],
  'billing_message_requests.php' => ['billing_message_requests', 'billing'],
  'billing_limit_requests.php'   => ['billing_limit_requests', 'billing'],
  'billing_cut_requests.php'     => ['billing_cut_requests', 'billing'],
  'print_preview.php'            => ['print_preview', 'billing'],
  'print_invoice.php'            => ['invoices', 'billing'],
  'pay_invoice.php'              => ['invoices', 'billing'],
  'unpay_invoice.php'            => ['invoices', 'billing'],
  'finance_payments.php'         => ['payments', 'movement'],
  'finance_expenses.php'         => ['expenses', 'movement'],
  'purchases_report.php'         => ['purchases_report', 'purchases'],
  'purchases_new.php'            => ['purchases_new', 'purchases'],
  'purchases_view.php'           => ['purchases_report', 'purchases'],
  'finance_partners.php'         => ['partners', 'equity'],
];

if (!isset($finance_active) || trim((string)$finance_active) === '') {
  $finance_active = $ua_fin_page_map[$ua_fin_current][0] ?? 'summary';
}

if (!isset($finance_group) || !isset($ua_fin_groups[$finance_group])) {
  $finance_group = $ua_fin_page_map[$ua_fin_current][1] ?? 'summary';
  if ($finance_group === 'summary') {
    foreach ($ua_fin_groups as $group_key => $group) {
      if (isset($group['tabs'][$finance_active])) {
        $finance_group = $group_key;
        break;
      }
    }
  }
}

$ua_fin_section = $ua_fin_groups[$finance_group] ?? $ua_fin_groups['summary'];
$tabs = $ua_fin_section['tabs'] ?? [];

if (!function_exists('ua_fin_nav_h')) {
  function ua_fin_nav_h($value): string {
    return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
  }
}
?>
<nav class="ua-fin-nav ua-fin-nav--compact ua-ftabs-wrap" aria-label="الفوترة والمحاسبة">
  <div class="ua-fin-nav__panel">
    <div class="ua-fin-nav__heading">
      <span class="ua-fin-nav__heading-icon" aria-hidden="true"><i class="fa <?= ua_fin_nav_h($ua_fin_section['icon'] ?? 'fa-folder-o') ?>"></i></span>
      <strong><?= ua_fin_nav_h($ua_fin_section['title'] ?? '') ?></strong>
    </div>

    <div class="ua-fin-nav__links" aria-label="<?= ua_fin_nav_h($ua_fin_section['title'] ?? 'القائمة الحالية') ?>">
      <?php foreach ($tabs as $key => $tab): ?>
        <?php $is_active = ($finance_active === $key); ?>
        <a class="ua-fin-nav__link<?= $is_active ? ' is-active' : '' ?>"
           href="<?= ua_fin_nav_h($tab['href'] ?? '#') ?>"
           <?= $is_active ? 'aria-current="page"' : '' ?>>
          <i class="fa <?= ua_fin_nav_h($tab['icon'] ?? 'fa-circle-o') ?>" aria-hidden="true"></i>
          <span><?= ua_fin_nav_h($tab['label'] ?? '') ?></span>
        </a>
      <?php endforeach; ?>
    </div>
  </div>
</nav>
