<div class="content-wrapper android-packages-prices"><main class="appkg-main">
  <header class="appkg-heading"><div><span>Plans & Speeds</span><h1>السرعات والباقات</h1><p>إدارة سرعات MikroTik وأحجام الاشتراك وأسعارها.</p></div><a href="app_settings.php"><i class="fa fa-cog"></i><b>الإعدادات</b></a></header>
  <?php if($msg): ?><div class="appkg-notice is-ok"><?= pp_h($msg) ?></div><?php endif; ?><?php if($warn): ?><div class="appkg-notice is-warn"><?= pp_h($warn) ?></div><?php endif; ?><?php if($err): ?><div class="appkg-notice is-error"><?= pp_h($err) ?></div><?php endif; ?>
  <nav class="appkg-shortcuts"><a href="subscribers_add.php"><i class="fa fa-user-plus"></i> مشترك جديد</a><a href="invoices_generate_month.php"><i class="fa fa-refresh"></i> فواتير الشهر</a></nav>
  <details class="appkg-card appkg-editor" <?= $editingSpeed?'open':'' ?>><summary><span><i class="fa fa-tachometer"></i><b><?= $editingSpeed?'تعديل سرعة':'إضافة سرعة جديدة' ?></b></span><i class="fa fa-angle-down"></i></summary><form method="post" data-ua-android-native-layout><input type="hidden" name="action" value="save_speed"><input type="hidden" name="id" value="<?= (int)($editingSpeed['id']??0) ?>"><label><span>اسم السرعة</span><input name="speed_name" value="<?= pp_h($editingSpeed['speed_name']??'') ?>" placeholder="مثال: 2M / 4M"></label><div class="appkg-grid"><label><span>رفع</span><input type="number" step="0.01" min="0.01" name="upload_rate" value="<?= pp_h($editingSpeed['upload_rate']??'') ?>" required></label><label><span>تحميل</span><input type="number" step="0.01" min="0.01" name="download_rate" value="<?= pp_h($editingSpeed['download_rate']??'') ?>" required></label></div><?php $u=sp_normalize_unit((string)($editingSpeed['rate_unit']??'M')); ?><label><span>الوحدة</span><select name="rate_unit"><option value="K" <?= $u==='K'?'selected':'' ?>>K</option><option value="M" <?= $u==='M'?'selected':'' ?>>M</option><option value="G" <?= $u==='G'?'selected':'' ?>>G</option></select></label><label><span>السعر الشهري</span><input type="number" step="0.01" min="0" name="price" value="<?= pp_h($editingSpeed['price']??'0') ?>"></label><label><span>اسم بروفايل MikroTik</span><input name="mikrotik_profile" value="<?= pp_h($editingSpeed['mikrotik_profile']??'') ?>" placeholder="فارغ = توليد تلقائي"></label><button><i class="fa fa-save"></i> <?= $editingSpeed?'حفظ التعديل':'إضافة السرعة' ?></button><?php if($editingSpeed): ?><a href="packages_prices.php">إلغاء التعديل</a><?php endif; ?></form></details>
  <details class="appkg-card appkg-editor" <?= $editingPackage?'open':'' ?>><summary><span><i class="fa fa-cube"></i><b><?= $editingPackage?'تعديل باقة':'إضافة باقة جديدة' ?></b></span><i class="fa fa-angle-down"></i></summary><form method="post" data-ua-android-native-layout><input type="hidden" name="action" value="save_package"><input type="hidden" name="id" value="<?= (int)($editingPackage['id']??0) ?>"><label><span>اسم الباقة</span><input name="package_name" value="<?= pp_h($editingPackage['package_name']??'') ?>" placeholder="مثال: 50 GB" required></label><div class="appkg-grid"><label><span>الحجم GB</span><input type="number" min="0" name="quota_gb" value="<?= pp_h($editingPackage['quota_gb']??'') ?>" placeholder="فارغ = مفتوح"></label><label><span>المدة / يوم</span><input type="number" min="1" name="validity_days" value="<?= pp_h($editingPackage['validity_days']??'30') ?>"></label></div><div class="appkg-grid"><label><span>السعر</span><input type="number" step="0.01" min="0" name="price" value="<?= pp_h($editingPackage['price']??'0') ?>" required></label><label><span>رسوم الاشتراك</span><input type="number" step="0.01" min="0" name="subscription_fee" value="<?= pp_h($editingPackage['subscription_fee']??'0') ?>"></label></div><button><i class="fa fa-save"></i> <?= $editingPackage?'حفظ التعديل':'إضافة الباقة' ?></button><?php if($editingPackage): ?><a href="packages_prices.php">إلغاء التعديل</a><?php endif; ?></form></details>
  <section class="appkg-list"><header><div><span>Speeds</span><h2>السرعات الحالية</h2></div><b><?= count($speeds) ?></b></header><?php if(!$speeds): ?><p class="appkg-empty">لا توجد سرعات بعد.</p><?php endif; ?><?php foreach($speeds as $s): ?><article><div class="appkg-rowhead"><div><small>#<?= (int)$s['id'] ?></small><h3><?= pp_h($s['speed_name']) ?></h3><span><?= pp_h($s['rate_limit']) ?></span></div><b><?= number_format((float)($s['price']??0),2) ?><small>شهرياً</small></b></div><div class="appkg-values"><span><small>رفع</small><b><?= pp_h(sp_format_rate_value($s['upload_rate']??0,(string)($s['rate_unit']??'M'))) ?></b></span><span><small>تحميل</small><b><?= pp_h(sp_format_rate_value($s['download_rate']??0,(string)($s['rate_unit']??'M'))) ?></b></span></div><p><?= pp_h($s['mikrotik_profile']) ?></p><div class="appkg-actions"><a href="packages_prices.php?edit_speed=<?= (int)$s['id'] ?>"><i class="fa fa-edit"></i> تعديل</a><details><summary><i class="fa fa-trash"></i> حذف</summary><form method="post"><input type="hidden" name="action" value="delete_speed"><input type="hidden" name="id" value="<?= (int)$s['id'] ?>"><button>تأكيد الحذف</button></form></details></div></article><?php endforeach; ?></section>
  <section class="appkg-list"><header><div><span>Packages</span><h2>الباقات الحالية</h2></div><b><?= count($packages) ?></b></header><?php if(!$packages): ?><p class="appkg-empty">لا توجد باقات بعد.</p><?php endif; ?><?php foreach($packages as $p): ?><article><div class="appkg-rowhead"><div><small>#<?= (int)$p['id'] ?></small><h3><?= pp_h($p['package_name']) ?></h3><span><?= ($p['quota_gb']===null||$p['quota_gb']==='')?'مفتوح':((int)$p['quota_gb'].' GB') ?></span></div><b><?= number_format((float)($p['price']??0),2) ?><small>السعر</small></b></div><div class="appkg-values"><span><small>رسوم الاشتراك</small><b><?= number_format((float)($p['subscription_fee']??0),2) ?></b></span><span><small>المدة</small><b><?= (int)($p['validity_days']??0) ?> يوم</b></span></div><div class="appkg-actions"><a href="packages_prices.php?edit_package=<?= (int)$p['id'] ?>"><i class="fa fa-edit"></i> تعديل</a><details><summary><i class="fa fa-trash"></i> حذف</summary><form method="post"><input type="hidden" name="action" value="delete_package"><input type="hidden" name="id" value="<?= (int)$p['id'] ?>"><button>تأكيد الحذف</button></form></details></div></article><?php endforeach; ?></section>
</main></div>
