<div class="content-wrapper android-voucher-print">
  <main class="avp-main">
    <header class="avp-heading">
      <div><span>Print Studio</span><h1>طباعة الكروت</h1><p>اختر الدفعة والورق، ثم راجع الكروت قبل الطباعة.</p></div>
      <button type="button" data-avp-print><i class="fa fa-print"></i><b>طباعة</b></button>
    </header>
    <nav class="avp-nav" aria-label="صفحات الكروت">
      <a href="vouchers.php"><i class="fa fa-credit-card"></i><span>الكروت</span></a><a href="vouchers_generate.php"><i class="fa fa-magic"></i><span>توليد</span></a><a class="is-active" href="vouchers_print.php"><i class="fa fa-print"></i><span>طباعة</span></a><a href="vouchers_settings.php"><i class="fa fa-cog"></i><span>إعدادات</span></a>
    </nav>
    <section class="avp-card avp-controls">
      <div class="avp-title"><i class="fa fa-filter"></i><div><span>Print Options</span><h2>خيارات الطباعة</h2></div></div>
      <form method="get" data-ua-android-native-layout>
        <?php if($voucher_id>0): ?><input type="hidden" name="voucher_id" value="<?= $voucher_id ?>"><?php endif; ?>
        <?php if($ids_raw!==''): ?><input type="hidden" name="ids" value="<?= h($ids_raw) ?>"><?php endif; ?>
        <label><span>الدفعة</span><select name="batch" <?= ($voucher_id>0 || $ids_raw!=='')?'disabled':'' ?>><option value="0">أحدث 200 كرت</option><?php foreach($androidVoucherBatches as $printBatch): ?><option value="<?= (int)$printBatch['id'] ?>" <?= $batch===(int)$printBatch['id']?'selected':'' ?>>#<?= (int)$printBatch['id'] ?> — <?= h($printBatch['title']) ?> (<?= (int)$printBatch['qty'] ?>)</option><?php endforeach; ?></select></label>
        <div class="avp-grid"><label><span>نوع الورق</span><select name="paper"><option value="A4" <?= $paper==='A4'?'selected':'' ?>>A4</option><option value="80MM" <?= $paper==='80MM'?'selected':'' ?>>حراري 80mm</option><option value="58MM" <?= $paper==='58MM'?'selected':'' ?>>حراري 58mm</option></select></label><label><span>التصدير</span><select name="export"><option value="1" <?= $auto_export!==0?'selected':'' ?>>عند أول طباعة</option><option value="0" <?= $auto_export===0?'selected':'' ?>>بدون تصدير</option></select></label></div>
        <button type="submit"><i class="fa fa-eye"></i> تحديث المعاينة</button>
      </form>
    </section>
    <section class="avp-card avp-summary"><div><span>Preview</span><h2><?= $batch>0?'دفعة #'.$batch:($voucher_id>0?'كرت واحد':(!empty($selected_ids)?'الكروت المحددة':'أحدث الكروت')) ?></h2><p><?= h($title) ?></p></div><b><?= count($rows) ?></b></section>
    <?php if(!$rows): ?><div class="avp-empty"><i class="fa fa-inbox"></i><b>لا توجد كروت للطباعة</b><a href="vouchers_generate.php">توليد كروت جديدة</a></div><?php else: ?>
    <section class="avp-paper avp-paper--<?= h($paper_class) ?>" id="androidVoucherPrintArea">
      <div class="avp-sheet-head"><span>كروت الإنترنت</span><span><?= h($now) ?></span></div>
      <div class="avp-vouchers">
        <?php foreach($rows as $printVoucher):
          $paid = (!empty($printVoucher['payment_id']) || ($printVoucher['status']==='used'));
          $token = vouchers_ensure_qr_token($conn, (int)$printVoucher['id']);
          $qrText = $baseUrl . 'voucher_redeem.php?t=' . rawurlencode($token);
        ?>
        <article class="avp-voucher">
          <header><div><span>INTERNET VOUCHER</span><br><span>كرت إنترنت</span></div><span class="<?= $paid?'is-paid':'is-unpaid' ?>"><?= $paid?'مدفوع':'غير مدفوع' ?></span></header>
          <div class="avp-voucher-body"><div class="avp-credentials"><span>اسم المستخدم</span><div dir="ltr"><?= h($printVoucher['code']) ?></div><span>كلمة المرور</span><div dir="ltr"><?= h($printVoucher['passcode']) ?></div></div><div class="avp-qr" data-qr="<?= h($qrText) ?>"></div></div>
          <footer><span><?= !empty($printVoucher['duration_minutes'])?round(((int)$printVoucher['duration_minutes'])/60,2).' ساعة':'بدون مدة' ?></span><span><?= !empty($printVoucher['data_bytes'])?round(((int)$printVoucher['data_bytes'])/1073741824,2).' GB':'—' ?></span><span dir="ltr"><?= h($printVoucher['rate_limit']?:'—') ?></span></footer>
        </article>
        <?php endforeach; ?>
      </div>
      <div class="avp-sheet-foot"><span><?= h($admin_name) ?></span><span><?= h($op_id) ?></span></div>
    </section>
    <button class="avp-print-button" type="button" data-avp-print><i class="fa fa-print"></i><span>طباعة <?= count($rows) ?> كرت</span></button>
    <?php endif; ?>
  </main>
</div>
<script src="assets/js/qr_local.min.js"></script>
<script>
(function(){
  document.querySelectorAll('[data-avp-print]').forEach(button=>button.addEventListener('click',()=>window.print()));
  document.querySelectorAll('.avp-qr[data-qr]').forEach(element=>{try{const qr=QRLocal.create(element.dataset.qr||'',5),canvas=qr.toCanvas(180);canvas.style.width='100%';canvas.style.height='100%';canvas.style.display='block';element.appendChild(canvas);}catch(error){element.textContent='QR';}});
  <?php if($auto): ?>window.addEventListener('load',()=>setTimeout(()=>window.print(),300));<?php endif; ?>
})();
</script>
