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

$this->title = "Daftar Nota";
?>

<p>
    <?= Html::a('Tambah Nota', ['nota/create'], ['class'=>'btn btn-success']) ?>
</p>

<table id="table-nota" class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>ID</th>
            <th>Nomor</th>
            <th>Pelanggan</th>
            <th>Tanggal</th>
            <th>Total</th>
            <th>PPN</th>
            <th>Aksi</th>
        </tr>
    </thead>
</table>

<script>
$(document).ready(function(){
    var notaTable = $('#table-nota').DataTable({
        ajax: '<?= Url::to(["nota/list"]) ?>',
        columns: [
            { data:'id' },
            { data:'nomor' },
            { data:'pelanggan' },
            { data:'tanggal' },
            { data:'total', render: $.fn.dataTable.render.number(',') },
            { data:'ppn', render: $.fn.dataTable.render.number(',') },
            { data:'action', orderable:false, searchable:false }
        ]
    });

    // Hapus nota via ajax
    $(document).on('click', '.btn-delete-nota', function(e){
    e.preventDefault();

    var id = $(this).data('id');
    var csrfParam = $('meta[name=csrf-param]').attr('content');
    var csrfToken = $('meta[name=csrf-token]').attr('content');
    var postData = {};
    postData[csrfParam] = csrfToken;
    postData['id'] = id;

    swal({
        title: "Yakin ingin menghapus nota ini?",
        text: "Data yang dihapus tidak bisa dikembalikan!",
        icon: "warning",
        buttons: ["Batal", "Hapus"],
        dangerMode: true,
    }).then(function(willDelete){
        if(willDelete){
            $.ajax({
                url: '<?= \yii\helpers\Url::to(["nota/delete"]) ?>',
                type: 'post',
                data: postData,
                success: function(res){
                    if(res.success){
                        // Reload DataTable jika ada
                        if (typeof notaTable !== 'undefined') {
                            notaTable.ajax.reload(null,false);
                        }
                        swal("Terhapus!", res.message || "Nota berhasil dihapus", "success");
                    } else {
                        swal("Gagal!", res.message || "Gagal menghapus nota", "error");
                    }
                },
                error: function(){
                    swal("Error!", "Terjadi kesalahan server!", "error");
                }
            });
        }
    });
});


});
</script>
