@extends('admin.layouts.app')

@section('title', __('admin/product-groups.buttons_edit'))

@section('content')
<div class="container-fluid px-4">
    <x-admin.page-header
        :title="__('admin/product-groups.buttons_edit')"
        icon="fas fa-layer-group"
        :buttons="[
            ['label' => __('admin/product-groups.buttons_back'), 'route' => route('admin.product-groups.index'), 'icon' => 'fas fa-arrow-left', 'color' => 'outline-secondary'],
        ]"
    />

    <x-admin.two-column :main-cols="8" :side-cols="4">
        <x-admin.card :title="__('admin/product-groups.fields_group_details')">
            <form id="editGroupForm" action="{{ route('admin.product-groups.update', $productGroup->id) }}" method="POST">
                @csrf
                @method('PUT')

                <x-admin.alert />

                <div class="mb-3">
                    <label for="name" class="form-label">{{ __('admin/product-groups.fields_name') }}</label>
                    <div class="input-group">
                        <input type="text" class="form-control @error('name') is-invalid @enderror" id="name" name="name" value="{{ old('name', $productGroup->name) }}" required>
                        <button type="button"
                                data-translate="name"
                                data-translate-model="ProductGroup"
                                data-translate-id="{{ $productGroup->id }}"
                                data-translate-name="{{ __('admin/product-groups.fields_name') }}"
                                data-translate-target="#name"
                                title="{{ __('admin/common.translations_manage_translations') }}">
                        </button>
                    </div>
                    @error('name')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>

                <div class="mb-3">
                    <label for="slug" class="form-label">{{ __('admin/product-groups.fields_slug') }}</label>
                    <input type="text" class="form-control @error('slug') is-invalid @enderror" id="slug" name="slug" value="{{ old('slug', $productGroup->slug) }}" readonly>
                    @error('slug')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                    <div class="form-text">{{ __('admin/product-groups.fields_slug_help') }}</div>
                </div>

                <div class="mb-3">
                    <label for="template" class="form-label">{{ __('admin/product-groups.fields_template') }}</label>
                    <input type="text" class="form-control @error('template') is-invalid @enderror" id="template" name="template" value="{{ old('template', $productGroup->template) }}">
                    @error('template')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>

                <div class="mb-3">
                    <label for="gateways" class="form-label">{{ __('admin/product-groups.fields_gateways') }}</label>
                    <input type="text" class="form-control @error('gateways') is-invalid @enderror" id="gateways" name="gateways" value="{{ old('gateways', $productGroup->gateways) }}">
                    @error('gateways')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                    <div class="form-text">{{ __('admin/product-groups.fields_gateways_help') }}</div>
                </div>

                <div class="mb-3">
                    <label for="icon" class="form-label">{{ __('admin/product-groups.fields_icon') }}</label>
                    <input type="text" class="form-control @error('icon') is-invalid @enderror" id="icon" name="icon" value="{{ old('icon', $productGroup->icon) }}" placeholder="fas fa-layer-group">
                    @error('icon')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>

                <div class="row">
                    <div class="col-md-6">
                        <div class="mb-3">
                            <label for="order" class="form-label">{{ __('admin/product-groups.fields_order') }}</label>
                            <input type="number" class="form-control @error('order') is-invalid @enderror" id="order" name="order" value="{{ old('order', $productGroup->order) }}">
                            @error('order')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="mb-3">
                            <label class="form-label">{{ __('admin/product-groups.fields_hidden') }}</label>
                            <div class="form-check form-switch mt-2">
                                <input class="form-check-input @error('hidden') is-invalid @enderror" type="checkbox" id="hidden" name="hidden" value="1" {{ old('hidden', $productGroup->hidden) ? 'checked' : '' }}>
                                <label class="form-check-label" for="hidden">{{ __('admin/product-groups.fields_hidden_label') }}</label>
                            </div>
                            @error('hidden')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>
                </div>

                
                <div class="row">
                    <div class="col-md-6">
                        <div class="mb-3">
                            <label class="form-label">{{ __('admin/product-groups.fields_proration_enabled') }}</label>
                            @php $grpProration = old('proration_enabled', $productGroup->proration_enabled); @endphp
                            <select class="form-select @error('proration_enabled') is-invalid @enderror" name="proration_enabled">
                                <option value="" {{ $grpProration === null ? 'selected' : '' }}>{{ __('admin/product-groups.proration_inherit') }}</option>
                                <option value="1" {{ $grpProration === true || $grpProration === 1 ? 'selected' : '' }}>{{ __('admin/product-groups.proration_on') }}</option>
                                <option value="0" {{ $grpProration === false || $grpProration === 0 ? 'selected' : '' }}>{{ __('admin/product-groups.proration_off') }}</option>
                            </select>
                            @error('proration_enabled') <div class="invalid-feedback">{{ $message }}</div> @enderror
                            <div class="form-text small">{{ __('admin/product-groups.proration_enabled_help') }}</div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="mb-3">
                            <label class="form-label">{{ __('admin/product-groups.fields_billing_anchor_day') }}</label>
                            <input type="number" min="1" max="31" class="form-control @error('billing_anchor_day') is-invalid @enderror"
                                   name="billing_anchor_day" value="{{ old('billing_anchor_day', $productGroup->billing_anchor_day) }}"
                                   placeholder="{{ __('admin/product-groups.billing_anchor_day_placeholder') }}">
                            @error('billing_anchor_day') <div class="invalid-feedback">{{ $message }}</div> @enderror
                            <div class="form-text small">{{ __('admin/product-groups.billing_anchor_day_help') }}</div>
                        </div>
                    </div>
                </div>

                <x-admin.form-actions :cancel-url="route('admin.product-groups.index')" :submit-text="__('admin/product-groups.buttons_save')" />
            </form>
        </x-admin.card>

        <x-slot:sidebar>
            <x-admin.card :title="__('admin/product-groups.fields_group_info')">
                <div class="mb-3">
                    <label class="form-label fw-bold">{{ __('admin/product-groups.fields_id') }}:</label>
                    <p>{{ $productGroup->id }}</p>
                </div>

                <div class="mb-3">
                    <label class="form-label fw-bold">{{ __('admin/product-groups.fields_created_at') }}:</label>
                    <p>{{ $productGroup->created_at->format($dateTimeFormat) }}</p>
                </div>

                <div class="mb-3">
                    <label class="form-label fw-bold">{{ __('admin/product-groups.fields_updated_at') }}:</label>
                    <p>{{ $productGroup->updated_at->format($dateTimeFormat) }}</p>
                </div>

                <div class="mb-3">
                    <label class="form-label fw-bold">{{ __('admin/product-groups.fields_product_count') }}:</label>
                    <p>{{ $productGroup->products_count ?? 0 }}</p>
                </div>
            </x-admin.card>
        </x-slot:sidebar>
    </x-admin.two-column>
</div>
@endsection

@push('scripts')
@vite('resources/js/admin/product-groups/edit.js')
@endpush