<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use kartik\select2\Select2;
use nota\models\PaymentMethod;
?>

<?php $form = ActiveForm::begin(['id'=>'form-payment-setting']); ?>


<?php
        $formatList = ArrayHelper::map(
            PaymentMethod::find()->all(),
            'id',
            'name'
        );
        ?>
        <?= $form->field($model, 'payment_method_id')->dropDownList(
            $formatList,
            ['prompt' => 'Pilih Format']
        )->label('Pilih Format Nomor Nota') ?>
<div id="rekeningFields">
    <?= $form->field($model,'account_number')->textInput() ?>
    <?= $form->field($model,'owner_name')->textInput() ?>
    <?= $form->field($model,'bank_name')->textInput() ?>
</div>


<div class="text-right mt-3">
    <?= Html::submitButton('Simpan', ['class'=>'btn btn-primary', 'id'=>'btn-save-payment-setting']) ?>
</div>

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

<script>
function toggleRekening() {
    var selected = $('#paymentsettings-payment_method_id option:selected').text().toLowerCase();

    if (
        selected.includes('transfer') ||
        selected.includes('ovo') ||
        selected.includes('dana') ||
        selected.includes('gopay') ||
        selected.includes('shopee')
    ) {
        $('#rekeningFields').show();
    } else {
        $('#rekeningFields').hide();
    }
}

$('#paymentsettings-payment_method_id').change(toggleRekening);
toggleRekening(); // jalankan saat load
</script>

<script>
$(document).off('click','#btn-save-payment-setting').on('click','#btn-save-payment-setting', function(e){

    e.preventDefault();

    var form = $('#form-payment-setting');

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

    swal({
        title: "Menyimpan...",
        text: "Silakan tunggu",
        buttons: false,
        closeOnClickOutside: false,
        closeOnEsc: false,
        icon: "info"
    });

    $.post(form.attr('action'), form.serialize(), function(res){

        swal.close();

        if(res.success){

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

            pdfTable2.ajax.reload(null,false);

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

        } else {

            swal("Gagal!", res.message || "Gagal menyimpan", "error");

        }

    }).fail(function(){

        swal("Error!", "Kesalahan server", "error");

    });

});

</script>