<?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-ajax/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');

        swal({
            title:"Yakin ingin menghapus?",
            text:"Data tidak bisa dikembalikan!",
            icon:"warning",
            buttons:["Batal","Hapus"],
            dangerMode:true
        }).then(function(willDelete){
            if(willDelete){
                $.post('<?= Url::to(["nota-ajax/delete"]) ?>',{id:id,_csrf:'<?= Yii::$app->request->csrfToken ?>'}, function(res){
                    if(res.success){
                        swal("Terhapus!", res.message,"success");
                        notaTable.ajax.reload(null,false);
                    } else {
                        swal("Gagal!", res.message,"error");
                    }
                },'json').fail(function(){
                    swal("Error!","Kesalahan server","error");
                });
            }
        });
    });

});
</script>
