@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('menus.store', ['prm_id' => $data->id]) }}">
                            @csrf

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="code" class="font-weight-bold">Kode</label>
                                        <small class="text-muted text-xs">*({{ $data->id > 0 ? 'Kode tidak dapat diubah' : 'Otomatis dibuat jika kosong' }})</small>
                                        <input value="{{ old('code', $data->code ?? '') }}" type="text" class="form-control form-control-sm" name="code" id="code" {{ $data->id > 0 ? 'readonly' : '' }}>
                                    </div>

                                    <div class="form-group">
                                        <label for="name" class="font-weight-bold">Nama <span class="text-danger">*</span></label>
                                        <input value="{{ old('name', $data->name ?? '') }}" type="text" class="form-control form-control-sm" name="name" id="name" required maxlength="255">
                                    </div>

                                    <div class="form-group">
                                        <label for="menu_group" class="font-weight-bold">Grup Menu</label>
                                        <small class="text-muted text-xs">*(Grup menu dari sistem ERP (QAD))</small>
                                        <input value="{{ old('menu_group', $data->menu_group ?? '') }}" type="text" class="form-control form-control-sm" name="menu_group" id="menu_group" maxlength="100">
                                    </div>

                                    <div class="form-group">
                                        <label for="plu_code" class="font-weight-bold">Kode PLU</label>
                                        <small class="text-muted text-xs">*(PLU code dari sistem ERP (QAD))</small>
                                        <input value="{{ old('plu_code', $data->plu_code ?? '') }}" type="text" class="form-control form-control-sm" name="plu_code" id="plu_code" maxlength="50">
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="category" class="font-weight-bold">Kategori</label>
                                        <input value="{{ old('category', $data->category ?? '') }}" type="text" class="form-control form-control-sm" name="category" id="category" maxlength="100">
                                    </div>

                                    <div class="form-group">
                                        <label for="price" class="font-weight-bold">Harga</label>
                                        <small class="text-muted text-xs">*(Harga menu dalam Rupiah)</small>
                                        <input value="{{ old('price', $data->price ?? '') }}" type="number" step="0.01" class="form-control form-control-sm" name="price" id="price" min="0">
                                    </div>

                                    <div class="form-group">
                                        <label for="description" class="font-weight-bold">Deskripsi</label>
                                        <textarea class="form-control form-control-sm" name="description" id="description" rows="3">{{ old('description', $data->description ?? '') }}</textarea>
                                    </div>

                                    <div class="form-group">
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" name="is_active" id="is_active" value="1" {{ old('is_active', $data->is_active ?? true) ? 'checked' : '' }}>
                                            <label class="form-check-label font-weight-bold" for="is_active">
                                                Aktif - Aktifkan menu ini
                                            </label>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- Action Buttons -->
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group">
                                        <button type="submit" class="btn btn-sm btn-info button-cek-permission" data-permission="menus.store">
                                            <i class="fa fa-save"></i> Simpan
                                        </button>
                                        <a href="{{ route('menus.index') }}" class="btn btn-sm btn-warning" data='back' onclick="return notificationBeforeBack(event, this);">
                                            <i class="fa fa-arrow-left"></i> Kembali
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
    $(document).ready(function() {
        $('#formID').on('submit', function(e) {
            e.preventDefault();

            const form = this;
            const formData = new FormData(form);

            // Show loading
            Swal.fire({
                title: 'Menyimpan...',
                allowOutsideClick: false,
                didOpen: () => {
                    Swal.showLoading();
                }
            });

            $.ajax({
                type: "POST",
                url: $(form).attr('action'),
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    Swal.fire({
                        title: 'Berhasil!',
                        text: 'Menu berhasil disimpan',
                        icon: 'success',
                        confirmButtonText: 'OK'
                    }).then(() => {
                        window.location.href = "{{ route('menus.index') }}";
                    });
                },
                error: function(xhr) {
                    let message = 'Terjadi kesalahan';
                    if (xhr.responseJSON && xhr.responseJSON.errors) {
                        const errors = xhr.responseJSON.errors;
                        message = Object.values(errors).flat().join('\n');
                    } else if (xhr.responseJSON && xhr.responseJSON.message) {
                        message = xhr.responseJSON.message;
                    }

                    Swal.fire({
                        title: 'Error!',
                        text: message,
                        icon: 'error',
                        confirmButtonText: 'OK'
                    });
                }
            });
        });
    });
</script>
@endsection