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

/* @var $model nota\models\PaymentMethod */
?>

<div class="payment-method-form">
    <input type="hidden" id="payment-id" value="<?= $model->id ?? '' ?>">

    <div class="form-group">
        <label>Nama</label>
        <input type="text" id="payment-name" class="form-control" value="<?= $model->name ?? '' ?>">
    </div>

    <div class="form-group">
        <label>Kode</label>
        <input type="text" id="payment-code" class="form-control" value="<?= $model->code ?? '' ?>">
    </div>

    <div class="form-group">
        <label>Status</label>
        <select id="payment-active" class="form-control">
            <option value="1" <?= (isset($model->is_active) && $model->is_active==1)?'selected':'' ?>>Aktif</option>
            <option value="0" <?= (isset($model->is_active) && $model->is_active==0)?'selected':'' ?>>Nonaktif</option>
        </select>
    </div>

    <div class="form-group">
        <button id="btn-save-payment" class="btn btn-success">
            <?= isset($model->id)?'Update':'Simpan' ?>
        </button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
    </div>
</div>
<!-- SweetAlert2 CSS -->



<script>
if (! $.fn.DataTable.isDataTable('#table-payment-method')) {
    var paymentTable = $('#table-payment-method').DataTable({
        ajax: '<?= \yii\helpers\Url::to(['setting/payment-method-list']) ?>',
        columns: [
            { data: 'id' },
            { data: 'name' },
            { data: 'code' },
            { data: 'status' },
            { data: 'action', orderable:false, searchable:false }
        ]
    });
}

var csrfParam = $('meta[name=csrf-param]').attr('content');
var csrfToken = $('meta[name=csrf-token]').attr('content');

// Load modal Add/Edit
$('#modal-payment').on('show.bs.modal', function(event){
    var button = $(event.relatedTarget);
    var url    = button.attr('href');
    var id     = button.data('id');

    if(id){
        url += (url.indexOf('?')>-1 ? '&' : '?') + 'id=' + id;
    }

    $('#modal-payment-content').html('<div class="text-center p-3">Loading...</div>');
    $.get(url, function(data){
        $('#modal-payment-content').html(data);
    });
});

// Submit form via Ajax (Add/Edit)
// Submit form via Ajax (Add/Edit)
$(document).on('click','#btn-save-payment', function(e){

    e.preventDefault();

    var id = $('#payment-id').val();

    var postData = {
        name: $('#payment-name').val(),
        code: $('#payment-code').val(),
        is_active: $('#payment-active').val()
    };

    postData[csrfParam] = csrfToken;

    var url = id
        ? '<?= \yii\helpers\Url::to(["setting/edit-payment-method"]) ?>'
        : '<?= \yii\helpers\Url::to(["setting/add-payment-method"]) ?>';

    if(id) url += '&id=' + id;

    // === LOADING ===
    swal({
        title: "Menyimpan...",
        text: "Silakan tunggu",
        showConfirmButton: false,
        closeOnClickOutside: false,
        closeOnEsc: false,
        type: "info"
    });

    $.post(url, postData, function(res){

        swal.close();

        if(res.success){

            $('#modal-payment').modal('hide');

            paymentTable.ajax.reload(null,false);

            swal("Berhasil!", res.message, "success");

        } else {

            let msg = res.message || "Gagal menyimpan data!";

            // tampilkan error validasi Yii
            if(res.errors){
                msg += "\n\n";
                $.each(res.errors,function(k,v){
                    msg += "- " + v[0] + "\n";
                });
            }

            swal("Gagal!", msg, "error");
        }

    },'json')
    .fail(function(){

        swal.close();

        swal("Error!", "Terjadi kesalahan server!", "error");
    });

});


</script>

