<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $model app\models\Member */
?>

<?php $form = ActiveForm::begin([
    'id' => 'form-member',
    'enableAjaxValidation' => false,
]); ?>

<div class="row">
    <div class="col-md-6">
        <?= $form->field($model, 'vNama')->textInput(['maxlength'=>100]) ?>
        <?= $form->field($model, 'email')->textInput(['type'=>'email']) ?>
        <?= $form->field($model, 'eType')->dropDownList([
            'ADMIN'=>'ADMIN',
            'KASIR'=>'KASIR',
            'SUPERVISOR'=>'SUPERVISOR',
            'BENDAHARA'=>'BENDAHARA'
        ]) ?>
    </div>
    <div class="col-md-6">
        <?= $form->field($model, 'eAdmin')->dropDownList([
            'Ya'=>'Ya',
            'Tidak'=>'Tidak'
        ]) ?>
        <?= $form->field($model, 'eSuperUser')->dropDownList([
            'Ya'=>'Ya',
            'Tidak'=>'Tidak'
        ]) ?>
        <?= $form->field($model, 'vHp')->textInput(['maxlength'=>50]) ?>
        <?= $form->field($model, 'vBlok')->textInput(['maxlength'=>100]) ?>
    </div>
</div>

<div class="mt-3">
    <?= Html::hiddenInput('id', $model->id) ?>
    <?= Html::button('Simpan', ['class'=>'btn btn-primary','id'=>'btn-save-member']) ?>
    <?= Html::button('Batal', ['class'=>'btn btn-secondary','data-dismiss'=>'modal']) ?>
</div>

<?php ActiveForm::end(); ?>

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

    var form = $('#form-member');
    var data = form.serialize();

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

    var url = form.find('input[name="id"]').val() 
        ? '<?= \yii\helpers\Url::to(["setting/edit-member"]) ?>' + '&id=' + form.find('input[name="id"]').val()
        : '<?= \yii\helpers\Url::to(["setting/add-member"]) ?>';

    swal({
        title: "Menyimpan...",
        showConfirmButton: false,
        closeOnClickOutside: false,
        icon: "info"
    });

    $.post(url, data, function(res){
        swal.close();
        if(res.success){
            $('#modal-member').modal('hide');
            if(window.memberTable) memberTable.ajax.reload(null,false);
            swal("Berhasil!", res.message, "success");
        } else {
            var msg = res.message || "Gagal menyimpan data!";
            swal("Gagal!", msg, "error");
        }
    }, 'json').fail(function(){
        swal.close();
        swal("Error!", "Kesalahan server!", "error");
    });

});
</script>
