<?php
use yii\helpers\Html;
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Nota <?= $model->nomor ?></title>

<style>

/* ===== FONT & UMUM ===== */
body{
  font-family: monospace;
  font-size:12px;
}

.center{text-align:center;}
.right{text-align:right;}
.left{text-align:left;}
hr{border:none;border-top:1px dashed #000;}

/* ===== FORMAT A4 ===== */
.a4{width:210mm; margin:auto; padding:10px; font-size:14px;}
.a4 table{width:100%; border-collapse:collapse;}
.a4 th, .a4 td{padding:3px; font-size:13px;}
.a4 th{border-bottom:1px solid #000;}

/* ===== THERMAL 58mm ===== */
.thermal58{width:58mm; margin:auto; font-size:12px;}
.thermal58 table{width:100%; border-collapse:collapse;}
.thermal58 th, .thermal58 td{font-size:11px; padding:2px;}
@media print{button{display:none;}}

/* ===== THERMAL 80mm ===== */
.thermal80{width:80mm; margin:auto; font-size:12px;}
.thermal80 table{width:100%; border-collapse:collapse;}
.thermal80 th, .thermal80 td{font-size:11px; padding:2px;}

/* ===== A5 ===== */
.a5{width:148mm; margin:auto; font-size:13px;}
.a5 table{width:100%; border-collapse:collapse;}
.a5 th, .a5 td{padding:3px; font-size:12px;}

/* TABLE ITEM */
th,td{text-align:left;}
td.right{text-align:right;}
td.center{text-align:center;}

.thermal58, .thermal80{
  page-break-after: avoid;
}

</style>
</head>

<body>

<?php
if(empty($setting) || empty($setting->paper_size)){
    echo "<script>alert('Setting ukuran kertas belum diatur!'); window.close();</script>";
    return;
}

$formats = [$setting->paper_size];


foreach($formats as $format):
?>
<div class="<?= $format ?>" style="margin-bottom:30px; border:1px solid #000; padding:5px;">

  <!-- Header -->
  <div class="center">
    <strong><?= $cluster->vNama; ?></strong><br>
    <span style=" font-size:11px;"><?= $cluster->vAlamat; ?></span><br>
    Telp <?= $cluster->vTelp ?>
  </div>
  <hr>

  <!-- Info Nota -->
  No : <?= $model->nomor ?><br>
  Tgl: <?= date('d/m/Y H:i',strtotime($model->tanggal)) ?><br>
  Pelanggan: <?= Html::encode($model->pelanggan) ?> <br>
  Pembayaran: <?= $model->paymentMethod ? Html::encode($model->paymentMethod->name) : '-' ?><br>

  <?php
  // Untuk web / email bisa tampilkan rekening/wallet, tapi di thermal kita skip
  if(!in_array($format, ['thermal58','thermal80'])):
      $accounts = \nota\models\PaymentSettings::find()
                  ->where(['payment_method_id' => $model->payment_method_id])
                  ->all();
      if($accounts):
          echo '<b>Rekening / Wallet:</b><br>';
          foreach($accounts as $acc){
              echo ($acc->bank_name ? $acc->bank_name.' - ' : '') 
                 . $acc->account_number 
                 . ($acc->owner_name ? ' a/n '.$acc->owner_name : '') 
                 . '<br>';
          }
      endif;
  endif;
  ?>
  <hr>

  <!-- Item Table -->
  <table>
    <thead>
      <tr>
        <th class="nama">Item</th>
        <th class="qty center">Qty</th>
        <th class="diskon center">Disc%</th>
        <th class="harga right">Harga</th>
        <th class="sub right">Sub</th>
      </tr>
    </thead>
    <tbody>
    <?php 
    $totalBeforePPN = 0;
    foreach($model->items as $i):
        $subtotal = $i->qty * $i->harga * (1 - $i->diskon/100);
        $totalBeforePPN += $subtotal;
    ?>
      <tr>
        <td><?= Html::encode($i->barang->nama) ?></td>
        <td class="center"><?= $i->qty ?></td>
        <td class="center"><?= $i->diskon ?>%</td>
        <td class="right"><?= number_format($i->harga) ?></td>
        <td class="right"><?= number_format($subtotal) ?></td>
      </tr>
    <?php endforeach; ?>
    </tbody>
  </table>
  <hr>

  <!-- Total -->
  <table>
    <tr>
      <td><b>Subtotal</b></td>
      <td class="right"><b><?= number_format($totalBeforePPN) ?></b></td>
    </tr>

    <?php if($model->voucher_id > 0): ?>
    <tr>
      <td><b>Voucher</b></td>
      <td class="right"><b><?= number_format($model->voucher) ?></b></td>
    </tr>
    <?php endif; ?>

    <?php if($model->ppn > 0): ?>
    <tr>
      <td><b>PPN 11%</b></td>
      <td class="right"><b><?= number_format($model->ppn) ?></b></td>
    </tr>
    <?php endif; ?>

    <tr>
      <td><b>Grand Total</b></td>
      <td class="right"><b><?= number_format($model->grand_total) ?></b></td>
    </tr>

    <?php if($model->paymentMethod && strtolower($model->paymentMethod->name) == 'tunai'): ?>
    <tr>
      <td>Uang Diterima</td>
      <td class="right"><?= number_format($model->uang_diterima) ?></td>
    </tr>
    <tr>
      <td>Kembalian</td>
      <td class="right"><?= number_format($model->kembalian) ?></td>
    </tr>
    <?php endif; ?>
  </table>

  <hr>

  <!-- Footer -->
  <div class="center">
    Terima Kasih 🙏<br>
    Barang yang sudah dibeli tidak dapat dikembalikan
  </div>

  <br>
  <button onclick="window.print()">🖨 Cetak</button>
</div>
<?php endforeach; ?>

</body>
</html>
