@extends('admin.layouts.app')

@section('title', __('admin/orders.edit_title'))

@section('content')
<div class="container-fluid px-4">
    <x-admin.page-header
        :title="__('admin/orders.edit_title') . ' #' . $order->id"
        icon="fas fa-shopping-cart"
        :buttons="[
            ['label' => __('admin/orders.back_to_order'), 'route' => route('admin.orders.show', $order), 'icon' => 'fas fa-arrow-left', 'color' => 'outline-secondary'],
        ]"
    />

    <x-admin.alert />

    <form action="{{ route('admin.orders.update', $order) }}" method="POST">
        @csrf
        @method('PUT')

        <x-admin.two-column>
            <x-admin.card>
                <h5 class="card-title mb-4">{{ __('admin/orders.order_information') }}</h5>

                <div class="row mb-3">
                    <div class="col-md-6">
                        <label for="client_id" class="form-label">{{ __('admin/orders.fields_client') }} <span class="text-danger">*</span></label>
                        <select name="client_id" id="client_id" class="form-select @error('client_id') is-invalid @enderror" required>
                            <option value="">{{ __('admin/orders.placeholders_select_client') }}</option>
                            @foreach($clients as $client)
                                <option value="{{ $client->id }}" @selected(old('client_id', $order->client_id) == $client->id)>
                                    {{ $client->first_name }} {{ $client->last_name }} ({{ $client->email }})
                                </option>
                            @endforeach
                        </select>
                        @error('client_id')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="col-md-6">
                        <label for="currency_id" class="form-label">{{ __('admin/orders.fields_currency') }} <span class="text-danger">*</span></label>
                        <select name="currency_id" id="currency_id" class="form-select @error('currency_id') is-invalid @enderror" required>
                            <option value="">{{ __('admin/orders.placeholders_select_currency') }}</option>
                            @foreach($currencies as $currency)
                                <option value="{{ $currency->id }}" @selected(old('currency_id', $order->currency_id) == $currency->id)>
                                    {{ $currency->name }} ({{ $currency->code }})
                                </option>
                            @endforeach
                        </select>
                        @error('currency_id')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-md-6">
                        <label for="status" class="form-label">{{ __('admin/orders.fields_status') }} <span class="text-danger">*</span></label>
                        <select name="status" id="status" class="form-select @error('status') is-invalid @enderror" required>
                            <option value="pending" @selected(old('status', $order->status) == 'pending')>
                                {{ __('admin/orders.statuses_pending') }}
                            </option>
                            <option value="paid" @selected(old('status', $order->status) == 'paid')>
                                {{ __('admin/orders.statuses_paid') }}
                            </option>
                            <option value="cancelled" @selected(old('status', $order->status) == 'cancelled')>
                                {{ __('admin/orders.statuses_cancelled') }}
                            </option>
                            <option value="refunded" @selected(old('status', $order->status) == 'refunded')>
                                {{ __('admin/orders.statuses_refunded') }}
                            </option>
                        </select>
                        @error('status')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="col-md-6">
                        <label for="payment_method" class="form-label">{{ __('admin/orders.fields_payment_method') }}</label>
                        <input type="text" name="payment_method" id="payment_method"
                               class="form-control @error('payment_method') is-invalid @enderror"
                               value="{{ old('payment_method', $order->payment_method) }}">
                        @error('payment_method')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>

                <div class="mb-3">
                    <label for="notes" class="form-label">{{ __('admin/orders.fields_notes') }}</label>
                    <textarea name="notes" id="notes" rows="3"
                              class="form-control @error('notes') is-invalid @enderror">{{ old('notes', $order->notes) }}</textarea>
                    @error('notes')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>
            </x-admin.card>

            <x-admin.card class="mt-3" :title="__('admin/orders.order_items')" icon="fas fa-list">
                {{-- Sipariş kalemleri salt-okunur gösterilir; tutarlar create akışında
                     hesaplandığı için edit ekranında değiştirilmez. Aşağıdaki hidden
                     alanlar OrderUpdateRequest'in zorunlu items/tutar kurallarını
                     karşılar (PRC-485). --}}
                <div class="table-responsive">
                    <table class="table table-sm align-middle mb-0">
                        <thead>
                            <tr>
                                <th>{{ __('admin/orders.fields_type') }}</th>
                                <th>{{ __('admin/orders.fields_description') }}</th>
                                <th class="text-center">{{ __('admin/orders.fields_quantity') }}</th>
                                <th class="text-center">{{ __('admin/orders.fields_period') }}</th>
                                <th class="text-end">{{ __('admin/orders.fields_amount') }}</th>
                                <th class="text-end">{{ __('admin/orders.fields_total') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($order->items as $i => $item)
                                @php
                                    $itemType = in_array($item->type, ['product', 'domain'], true) ? $item->type : 'product';
                                    $itemRelationId = (int) ($item->relation_id ?? $item->product_id ?? 0);
                                    $itemDescription = $item->description ?: ($item->name ?: ('#' . $item->id));
                                    $itemPeriodType = in_array($item->period_type, ['month', 'year'], true) ? $item->period_type : 'year';
                                    $itemQuantity = (int) ($item->quantity ?: 1);
                                    $itemPeriod = (int) ($item->period ?: 1);
                                    $itemAmount = (float) ($item->amount ?? 0);
                                    $itemSetup = (float) ($item->setup_amount ?? 0);
                                    $itemTotal = (float) ($item->total_amount ?? $item->amount ?? 0);
                                @endphp
                                <tr>
                                    <td>{{ ucfirst($itemType) }}</td>
                                    <td>{{ $itemDescription }}</td>
                                    <td class="text-center">{{ $itemQuantity }}</td>
                                    <td class="text-center">{{ $itemPeriod }} {{ $itemPeriodType }}</td>
                                    <td class="text-end">{{ number_format($itemAmount, 2) }}</td>
                                    <td class="text-end">{{ number_format($itemTotal, 2) }}</td>
                                </tr>
                                <input type="hidden" name="items[{{ $i }}][type]" value="{{ $itemType }}">
                                <input type="hidden" name="items[{{ $i }}][relation_id]" value="{{ $itemRelationId }}">
                                <input type="hidden" name="items[{{ $i }}][description]" value="{{ $itemDescription }}">
                                <input type="hidden" name="items[{{ $i }}][amount]" value="{{ $itemAmount }}">
                                <input type="hidden" name="items[{{ $i }}][setup_amount]" value="{{ $itemSetup }}">
                                <input type="hidden" name="items[{{ $i }}][quantity]" value="{{ $itemQuantity }}">
                                <input type="hidden" name="items[{{ $i }}][period]" value="{{ $itemPeriod }}">
                                <input type="hidden" name="items[{{ $i }}][period_type]" value="{{ $itemPeriodType }}">
                                <input type="hidden" name="items[{{ $i }}][total_amount]" value="{{ $itemTotal }}">
                            @empty
                                <tr>
                                    <td colspan="6" class="text-muted text-center py-3">
                                        {{ __('admin/orders.no_items') }}
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>

                {{-- Tutar alanları (gerçek DB kolonları) — hidden passthrough --}}
                <input type="hidden" name="subtotal" value="{{ (float) $order->subtotal }}">
                <input type="hidden" name="tax_amount" value="{{ (float) $order->tax_amount }}">
                <input type="hidden" name="discount_amount" value="{{ (float) $order->discount_amount }}">
                <input type="hidden" name="setup_amount" value="{{ (float) $order->setup_amount }}">
                <input type="hidden" name="total_amount" value="{{ (float) $order->amount }}">
            </x-admin.card>

            <x-slot:sidebar>
                <x-admin.card>
                    <h5 class="card-title mb-4">{{ __('admin/orders.order_summary') }}</h5>

                    <dl class="row mb-0">
                        <dt class="col-sm-6">{{ __('admin/orders.fields_order_number') }}</dt>
                        <dd class="col-sm-6 text-end"><strong>{{ $order->order_number }}</strong></dd>

                        <dt class="col-sm-6">{{ __('admin/orders.fields_subtotal') }}</dt>
                        <dd class="col-sm-6 text-end">{{ $order->currency->prefix }}{{ number_format($order->subtotal, 2) }}{{ $order->currency->suffix }}</dd>

                        <dt class="col-sm-6">{{ __('admin/orders.fields_tax') }}</dt>
                        <dd class="col-sm-6 text-end">{{ $order->currency->prefix }}{{ number_format($order->tax, 2) }}{{ $order->currency->suffix }}</dd>

                        <dt class="col-sm-6">{{ __('admin/orders.fields_discount') }}</dt>
                        <dd class="col-sm-6 text-end">{{ $order->currency->prefix }}{{ number_format($order->discount, 2) }}{{ $order->currency->suffix }}</dd>

                        <dt class="col-sm-6 border-top pt-2 mt-2"><strong>{{ __('admin/orders.fields_total') }}</strong></dt>
                        <dd class="col-sm-6 text-end border-top pt-2 mt-2">
                            <strong>{{ $order->currency->prefix }}{{ number_format($order->total, 2) }}{{ $order->currency->suffix }}</strong>
                        </dd>
                    </dl>

                    <hr>

                    <div class="d-grid gap-2">
                        @can('update orders')
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save me-1"></i>
                                {{ __('admin/orders.save_changes') }}
                            </button>
                        @endcan
                        <a href="{{ route('admin.orders.show', $order) }}" class="btn btn-outline-secondary">
                            {{ __('admin/orders.cancel') }}
                        </a>
                    </div>
                </x-admin.card>

                <x-admin.card class="mt-3">
                    <h5 class="card-title mb-3">{{ __('admin/orders.order_details') }}</h5>
                    <small class="text-muted d-block mb-2">
                        <i class="fas fa-calendar me-1"></i>
                        {{ __('admin/orders.fields_created_at') }}: {{ $order->created_at->format('Y-m-d H:i') }}
                    </small>
                    <small class="text-muted d-block mb-2">
                        <i class="fas fa-clock me-1"></i>
                        {{ __('admin/orders.fields_updated_at') }}: {{ $order->updated_at->format('Y-m-d H:i') }}
                    </small>
                    @if($order->paid_date)
                        <small class="text-muted d-block">
                            <i class="fas fa-check-circle me-1"></i>
                            {{ __('admin/orders.fields_paid_date') }}: {{ $order->paid_date->format('Y-m-d H:i') }}
                        </small>
                    @endif
                </x-admin.card>
            </x-slot:sidebar>
        </x-admin.two-column>
    </form>
</div>
@endsection
