@extends('layouts/base_view')
@section('title')
{!! $variables['header_routes'] !!}
@endsection
@section('content')
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <div class="card card-outline card-warning">
                    <div class="card-header">
                        <h3 class="card-title">{!! $variables['judul'] !!}</h3>
                    </div>
                    <div class="card-body">
                        <form id="formID" method="post" action="{{ route('parameter_globals.store', $data->id) }}" enctype="multipart/form-data">
                            @csrf
                            <input type="hidden" name="version" value="{{$data->version}}">
                            <div class="row">
                                <div class="form-group col-md-6">
                                    <label for="key">Kunci</label>
                                    <input value="{{ old('key', $data->key ?? null) }}" type="text" class="form-control form-control-sm" name="key" placeholder="" required readonly>
                                    @if ($errors->has('key'))
                                    <span class="text-danger text-left">{{ $errors->first('key') }}</span>
                                    @endif
                                </div>
                                <div class="form-group col-md-6">
                                    <label for="value">Nilai</label>
                                    <input value="{{ old('value', $data->value ?? '') }}" type="text" class="form-control form-control-sm" name="value" placeholder="" required>
                                    @if ($errors->has('value'))
                                    <span class="text-danger text-left">{{ $errors->first('value') }}</span>
                                    @endif
                                </div>
                                <div class="form-group col-md-12">
                                    <label for="description">Deskripsi</label>
                                    <input value="{{ old('description', $data->description ?? '') }}" type="text" class="form-control form-control-sm" name="description" placeholder="" required>
                                    @if ($errors->has('description'))
                                    <span class="text-danger text-left">{{ $errors->first('description') }}</span>
                                    @endif
                                </div>
                            </div>

                            <button type="submit" class="btn btn-outline-info btn-sm button-cek-permission" data-permission="parameter_globals.store"><i class="fas fa-save"></i> Simpan</button>
                            <a href="{{ route('parameter_globals.index') }}" class="btn btn-outline-warning btn-sm" data='back' onclick="return notificationBeforeBack(event, this);"><i class="fas fa-arrow-left"></i> Kembali</a>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<script>
    $(document).ready(function() {
        var isSubmitting = false;
        $('#formID').validate({
            rules: {
                // Tambahkan rules kalau mau validasi sisi store juga
            },
            messages: {
                // Pesan custom untuk validasi
            },
            errorElement: 'span',
            errorPlacement: function(error, element) {
                error.addClass('invalid-feedback');
                element.closest('.form-group').append(error);
            },
            highlight: function(element) {
                $(element).addClass('is-invalid');
            },
            unhighlight: function(element) {
                $(element).removeClass('is-invalid');
            },
            submitHandler: function(form) {
                if (isSubmitting) return false;

                isSubmitting = true;
                $('#loaderkdnknz').show();

                $.ajax({
                        type: "POST",
                        url: $(form).attr('action'), // Gunakan attribute action dari form
                        data: $(form).serialize(), // Kirim seluruh isi form
                        dataType: "json",
                    })
                    .done(function(data) {
                        Swal.fire({
                            icon: data[0],
                            text: data[1],
                            showCancelButton: false,
                            confirmButtonColor: '#6c757d',
                            confirmButtonText: 'OK',
                            allowOutsideClick: false
                        }).then((result) => {
                            if (result.isConfirmed && data[0] == 'success') {
                                window.location.href = "{{ route('parameter_globals.index') }}";
                            }
                        });
                    })
                    .fail(function(xhr) {
                        let errorText = "Silakan hubungi tim terkait atau coba lagi.";

                        // Jika status 422 dan ada errors
                        if (xhr.status === 422 && xhr.responseJSON.errors) {
                            const errors = xhr.responseJSON.errors;
                            errorText = '';

                            for (const key in errors) {
                                if (errors.hasOwnProperty(key)) {
                                    errorText += `<strong>${key}</strong> : ${errors[key].join(', ')}<br>`;
                                }
                            }
                        } // Jika ada pesan error umum (misalnya 403, 500, dsb)
                        else if (xhr.responseJSON && xhr.responseJSON.message) {
                            errorText = xhr.responseJSON.message;
                        }

                        Swal.fire({
                            icon: "error",
                            title: "Validasi Gagal",
                            html: errorText, // <- pakai html, bukan text
                            confirmButtonColor: '#6c757d',
                            confirmButtonText: 'OK'
                        });
                    })
                    .always(function() {
                        isSubmitting = false;
                        $('#loaderkdnknz').hide();
                    });
            }
        });
    });
</script>

@endsection