<?php
use yii\helpers\Html;
$this->title = "Detail Nota";
?>
<div class="nota-view card p-4 shadow-sm" style="margin:auto;">
<h3>Detail Nota <?= $model->nomor ?></h3>

<p>

<p>
<?= Html::a('🖨 Cetak',['print','id'=>$model->id],[ 'class'=>'btn btn-primary','target'=>'_blank']) ?> 
<?= Html::a('PDF',['pdf','id'=>$model->id],[ 'class'=>'btn btn-warning','target'=>'_blank']) ?> 
<?= Html::a('💬 WhatsApp', '#', ['class'=>'btn btn-success','id'=>'btn-whatsapp']) ?> 
</p> 

<!-- <p>
    <?= Html::a('🖨 Print Nota', ['nota/print-auto', 'id'=>$model->id, 'print_id'=>2], ['class'=>'btn btn-success', 'target'=>'_blank']) ?>
</p>


// Contoh URL di browser atau link Yii2
<?= Html::a('📄 Cetak PDF A4', ['nota/print-pdf', 'id'=>$model->id, 'pdf_id'=>1, 'type'=>'pdf'], ['class'=>'btn btn-primary', 'target'=>'_blank']) ?>

// Misal 58 mm
<?= Html::a('🖨 Cetak Thermal 58mm', ['nota/print-pdf', 'id'=>$model->id, 'pdf_id'=>2, 'type'=>'print'], ['class'=>'btn btn-success', 'target'=>'_blank']) ?>

// Misal 80 mm
<?= Html::a('🖨 Cetak Thermal 80mm', ['nota/print-pdf', 'id'=>$model->id, 'pdf_id'=>3, 'type'=>'print'], ['class'=>'btn btn-success', 'target'=>'_blank']) ?> -->

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<?php
// Buat isi pesan lengkap seperti view
$message = "===== NOTA =====\n";
$message .= "Nota: {$model->nomor}\n";
$message .= "Pelanggan: {$model->pelanggan}\n";
$message .= "Tanggal: {$model->tanggal}\n";
$message .= "Metode Pembayaran: " . ($model->paymentMethod ? $model->paymentMethod->name : '-') . "\n";

if($model->paymentMethod && strtolower($model->paymentMethod->name) === 'tunai'){
    $message .= "Uang Diterima: Rp " . number_format($model->uang_diterima,0,',','.') . "\n";
    $message .= "Kembalian: Rp " . number_format($model->kembalian,0,',','.') . "\n";
}

$message .= "\n-- Daftar Barang --\n";
$totalBeforePPN = 0;
foreach($model->items as $i){
    $subtotal = $i->qty * $i->harga * (1 - $i->diskon/100);
    $totalBeforePPN += $subtotal;
    $message .= ($i->barang->nama) . " | Qty: {$i->qty} | Diskon: {$i->diskon}% | Harga: " . number_format($i->harga) . " | Subtotal: " . number_format($subtotal) . "\n";
}

$message .= "\nSubtotal: " . number_format($totalBeforePPN) . "\n";

if($model->voucher_id > 0){
    $message .= "Voucher: " . number_format($model->voucher) . "\n";
}
if($model->ppn > 0){
    $message .= "PPN 11%: " . number_format($model->ppn) . "\n";
}

$message .= "Grand Total: " . number_format($model->grand_total) . "\n";
$message .= "====================";
?>

<script>
$('#btn-whatsapp').on('click', function(e){
    e.preventDefault();

    var message = <?= json_encode($message) ?>;

    Swal.fire({
        title: 'Masukkan Nomor WhatsApp',
        input: 'text',
        inputPlaceholder: 'Contoh: 086701234567',
        showCancelButton: true,
        inputValidator: (value) => {
            if (!value) {
                return 'Nomor WA harus diisi!';
            }
        }
    }).then((result) => {
        if(result.isConfirmed){
            var nomor = result.value.replace(/\D/g,'');
            if(nomor.startsWith('0')){
                nomor = '62' + nomor.substring(1);
            }

            var pesan = encodeURIComponent(message);
            window.open('https://wa.me/' + nomor + '?text=' + pesan, '_blank');
        }
    });
});
</script>


</p>

<!-- Info Utama -->
<div class="d-flex justify-content-between flex-wrap mb-2">

    <!-- Kiri: Pelanggan & Tanggal -->
    <div>
        <p class="mb-1"><b>Pelanggan:</b> <?= Html::encode($model->pelanggan) ?></p>
        <p class="mb-0"><b>Tanggal:</b> <?= $model->tanggal ?></p>
    </div>

    <!-- Tengah: Metode Pembayaran -->
    <div class="text-center">
        <p class="mb-0"><b>Metode Pembayaran:</b> <?= $model->paymentMethod ? Html::encode($model->paymentMethod->name) : '-' ?></p>
    </div>

    <!-- Kanan: Uang Terima / Kembalian (Tunai) atau Rekening / Wallet Lain -->
    <div class="text-end">

        <?php if($model->paymentMethod && strtolower($model->paymentMethod->name) === 'tunai'): ?>
            <!-- Tunai -->
            <p class="mb-1"><b>Uang Terima:</b> <?= $model->uang_diterima ? 'Rp ' . number_format($model->uang_diterima,0,',','.') : '-' ?></p>
            <p class="mb-0"><b>Kembalian:</b> <?= $model->kembalian ? 'Rp ' . number_format($model->kembalian,0,',','.') : '-' ?></p>

        <?php else: ?>
            <?php
            // Ambil semua akun terkait metode pembayaran ini
            $accounts = \nota\models\PaymentSettings::find()
                        ->where(['payment_method_id' => $model->payment_method_id])
                        ->all();
            ?>
            <?php if($accounts): ?>
                <p class="mb-0"><b>Rekening / Wallet:</b></p>
                <?php foreach($accounts as $acc): ?>
                    <p class="mb-0">
                        <?= $acc->bank_name ?> - <?= $acc->account_number ?><?= $acc->owner_name ? ' a/n '.$acc->owner_name : '' ?>
                    </p>
                <?php endforeach; ?>
            <?php else: ?>
                <p class="mb-0">-</p>
            <?php endif; ?>
        <?php endif; ?>

    </div>

</div>


<hr>

<table class="table table-bordered">
<thead>
<tr>
  <th>Barang</th>
  <th>Qty</th>
  <th>Diskon %</th>
  <th>Harga</th>
  <th>Subtotal</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><?= $i->qty ?></td>
  <td><?= $i->diskon ?>%</td>
  <td><?= number_format($i->harga) ?></td>
  <td><?= number_format($subtotal) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
  <th colspan="4">Subtotal</th>
  <th><?= number_format($totalBeforePPN) ?></th>
</tr>

<?php if($model->voucher_id > 0): ?>
<tr>
  <th colspan="4">Voucher</th>
  <th><?= number_format($model->voucher) ?></th>
</tr>
<?php endif; ?>

<?php if($model->ppn > 0): ?>
<tr>
  <th colspan="4">PPN 11%</th>
  <th><?= number_format($model->ppn) ?></th>
</tr>
<?php endif; ?>

<tr>
  <th colspan="4">Grand Total</th>
  <th><?= number_format($model->grand_total) ?></th>
</tr>
</tfoot>
</table>
</div>