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

<p>
<?php $user = Yii::$app->user->identity; ?>
<?php if(in_array($user->eType,['ADMIN','KASIR'])): ?>
    <?= Html::a('Tambah Transaksi', Url::to(['nota/create']), [
        'class'=>'btn btn-success btn-sm',
        'data-toggle'=>'modal',
        'data-target'=>'#modal-nota'
    ]) ?>
<?php endif; ?>
</p>

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

<!-- Modal Ajax Add/Edit -->
<div class="modal fade" id="modal-nota">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-body" id="modal-nota-content"></div>
        </div>
    </div>
</div>

<?php
$csrfParam = Yii::$app->request->csrfParam;
$csrfToken = Yii::$app->request->csrfToken;
$js = <<<JS

var notaTable = $('#table-nota').DataTable({
    ajax: '".Url::to(['nota/index'])."',
    columns:[
        {data:'id'},
        {data:'tanggal'},
        {data:'customer_name'},
        {data:'total'},
        {data:'action', orderable:false, searchable:false}
    ]
});

// Modal Add/Edit
$('#modal-nota').on('show.bs.modal', function(event){
    var button = $(event.relatedTarget);
    var url = button.attr('href');
    $('#modal-nota-content').html('<div class="text-center p-3">Loading...</div>');
    $.get(url, function(data){
        $('#modal-nota-content').html(data);
    });
});

// Hapus 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/delete'])."', {id:id, $csrfParam:'$csrfToken'}, function(res){
                if(res.success){
                    notaTable.ajax.reload(null,false);
                    swal("Terhapus!", res.message, "success");
                } else {
                    swal("Gagal!", res.message, "error");
                }
            },'json');
        }
    });
});

JS;

$this->registerJs($js);
?>
