<div class="content-wrapper android-sectors" data-android-sectors>
  <main class="as-main">
    <header class="as-heading">
      <div>
        <span>VLAN / PPPoE</span>
        <h1>إدارة القطاعات</h1>
        <p>إدارة VLAN وربط كل قطاع بالراوتر ونطاق عناوين PPPoE الخاص به.</p>
      </div>
      <a href="routers.php"><i class="fa fa-server"></i><b>الراوترات</b></a>
    </header>

    <div class="as-stats">
      <div><strong><?= (int)$sector_total ?></strong><span>القطاعات</span></div>
      <div class="is-green"><strong><?= (int)$sector_enabled ?></strong><span>مفعّل</span></div>
      <div class="is-cyan"><strong><?= (int)$sector_with_pool ?></strong><span>مع Pool</span></div>
      <div class="is-gold"><strong><?= (int)$sector_vlan_max ?></strong><span>أعلى VLAN</span></div>
    </div>

    <?php if ($msg): ?><div class="as-alert is-<?= h($msg_type) ?>"><i class="fa fa-info-circle"></i><span><?= h($msg) ?></span></div><?php endif; ?>

    <section class="as-section">
      <div class="as-section-head">
        <div><span>التوزيع الحالي</span><h2>قائمة القطاعات</h2></div>
        <a href="wizard_network.php"><i class="fa fa-magic"></i> المعالج</a>
      </div>

      <div class="as-list">
        <?php foreach ($sectors as $sector): ?>
          <?php $sid=(int)($sector['id']??0); $enabled=!empty($sector['is_enabled']); $hasPool=trim((string)($sector['pppoe_pool_range']??''))!==''; ?>
          <article class="as-sector-card <?= $enabled ? 'is-enabled' : 'is-disabled' ?>">
            <header>
              <span class="as-sector-icon"><i class="fa fa-sitemap"></i></span>
              <div><h3><?= h((string)($sector['name'] ?? ('Sector '.$sid))) ?></h3><p>#<?= $sid ?> · VLAN <?= (int)($sector['vlan_id']??0) ?></p></div>
              <span class="as-status"><i></i><?= $enabled ? 'مفعّل' : 'متوقف' ?></span>
            </header>

            <div class="as-sector-data">
              <div><span>الراوتر</span><strong><?= h((string)($sector['router_name'] ?? '—')) ?></strong></div>
              <div><span>Trunk</span><strong dir="ltr"><?= h((string)($sector['trunk_interface'] ?? '—')) ?></strong></div>
              <div><span>Bridge</span><strong dir="ltr"><?= h((string)($sector['bridge_name'] ?? '—')) ?></strong></div>
            </div>

            <div class="as-pool <?= $hasPool ? 'has-pool' : '' ?>">
              <i class="fa fa-database"></i>
              <div><span><?= $hasPool ? h((string)($sector['pppoe_pool_name'] ?: 'PPPoE Pool')) : 'لم يتم تحديد Pool' ?></span><strong dir="ltr"><?= $hasPool ? h((string)$sector['pppoe_pool_range']) : 'يمكن اقتراح نطاق تلقائياً' ?></strong></div>
            </div>

            <footer>
              <a href="sectors.php?edit=<?= $sid ?>"><i class="fa fa-pencil"></i><span>تعديل</span></a>
              <form method="post"><input type="hidden" name="action" value="apply_sector"><input type="hidden" name="id" value="<?= $sid ?>"><button class="is-apply" type="submit"><i class="fa fa-check"></i><span>تطبيق</span></button></form>
              <form method="post"><input type="hidden" name="action" value="suggest_pool"><input type="hidden" name="id" value="<?= $sid ?>"><button type="submit"><i class="fa fa-lightbulb-o"></i><span>اقتراح Pool</span></button></form>
              <form method="post" onsubmit="return confirm('حذف القطاع؟');"><input type="hidden" name="action" value="delete_sector"><input type="hidden" name="id" value="<?= $sid ?>"><button class="is-delete" type="submit"><i class="fa fa-trash"></i><span>حذف</span></button></form>
            </footer>
          </article>
        <?php endforeach; ?>
        <?php if (!$sectors): ?><div class="as-empty"><i class="fa fa-sitemap"></i><h3>لا توجد قطاعات</h3><p>استخدم النموذج التالي لإضافة أول قطاع.</p></div><?php endif; ?>
      </div>
    </section>

    <section class="as-form-card" id="sectorForm">
      <div class="as-section-head">
        <div><span><?= $edit ? 'تحديث إعدادات القطاع' : 'قطاع جديد' ?></span><h2><?= $edit ? 'تعديل القطاع' : 'إضافة قطاع' ?></h2></div>
        <?php if ($edit): ?><a href="sectors.php"><i class="fa fa-times"></i> إلغاء</a><?php endif; ?>
      </div>
      <form method="post" data-ua-android-native-layout>
        <input type="hidden" name="action" value="save_sector"><input type="hidden" name="id" value="<?= h($edit['id'] ?? 0) ?>">
        <label><span>اسم القطاع</span><input name="name" value="<?= h($edit['name'] ?? '') ?>" placeholder="Tower1 - SectorA" required></label>
        <div class="as-form-pair">
          <label><span>VLAN ID</span><input name="vlan_id" inputmode="numeric" dir="ltr" value="<?= h($edit['vlan_id'] ?? '') ?>" placeholder="10" required></label>
          <label><span>الراوتر</span><select name="router_id" required><option value="">اختر الراوتر</option><?php foreach($routers as $router): ?><?php $selected=(int)($edit['router_id']??0)===(int)$router['id']; ?><option value="<?= h($router['id']) ?>" <?= $selected?'selected':'' ?>><?= h($router['name']) ?></option><?php endforeach; ?></select></label>
        </div>
        <div class="as-form-pair">
          <label><span>Trunk Interface</span><input name="trunk_interface" dir="ltr" value="<?= h($edit['trunk_interface'] ?? 'ether2') ?>"></label>
          <label><span>Bridge Name</span><input name="bridge_name" dir="ltr" value="<?= h($edit['bridge_name'] ?? 'bridge-pppoe') ?>"></label>
        </div>

        <div class="as-form-divider"><i class="fa fa-database"></i><div><strong>إعدادات PPPoE</strong><span>يمكن تركها فارغة ثم استخدام زر اقتراح Pool.</span></div></div>
        <label><span>اسم Pool</span><input name="pppoe_pool_name" dir="ltr" value="<?= h($edit['pppoe_pool_name'] ?? '') ?>" placeholder="pool_vlan10"></label>
        <label><span>نطاق Pool</span><input name="pppoe_pool_range" dir="ltr" value="<?= h($edit['pppoe_pool_range'] ?? '') ?>" placeholder="10.10.10.2-10.10.10.254"></label>
        <div class="as-form-pair">
          <label><span>Local Address</span><input name="pppoe_local_ip" dir="ltr" value="<?= h($edit['pppoe_local_ip'] ?? '') ?>" placeholder="10.10.10.1"></label>
          <label><span>Server Name</span><input name="pppoe_server_name" dir="ltr" value="<?= h($edit['pppoe_server_name'] ?? '') ?>" placeholder="pppoe_vlan10"></label>
        </div>
        <label><span>Service Name</span><input name="pppoe_service" dir="ltr" value="<?= h($edit['pppoe_service'] ?? '') ?>" placeholder="ISP-VLAN10"></label>
        <label class="as-enabled"><input type="checkbox" name="is_enabled" <?= (!isset($edit)||($edit['is_enabled']??1))?'checked':'' ?>><span>القطاع مفعّل</span></label>
        <label><span>ملاحظات</span><textarea name="notes" rows="2"><?= h($edit['notes'] ?? '') ?></textarea></label>
        <button class="as-save" type="submit"><i class="fa fa-save"></i><span><?= $edit ? 'حفظ التعديلات' : 'إضافة القطاع' ?></span></button>
      </form>
    </section>
  </main>
</div>
