@extends('layouts.admin-simple')

@section('title', 'Edit Supplier Delivery')

@section('breadcrumb')
    <li class="breadcrumb-item">Warehouse</li>
    <li class="breadcrumb-item"><a href="{{ route('warehouse.supplier-delivery.index') }}">Supplier Delivery</a></li>
    <li class="breadcrumb-item active">Edit Delivery</li>
@endsection

@section('page-title')
    <div class="d-flex justify-content-between align-items-center w-100">
        <div class="page-title-left">
            <h3 class="mb-1">Edit Supplier Delivery</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">Update LPO and delivery reference details</p>
        </div>
        <div class="page-actions-right d-flex align-items-center">
            <a href="{{ route('warehouse.supplier-delivery.index') }}" class="btn btn-sm btn-outline-secondary">
                <i class="material-icons mr-1" style="font-size: 16px;">arrow_back</i>
                Back to List
            </a>
        </div>
    </div>
@endsection

@section('content')
    <form action="{{ route('warehouse.supplier-delivery.update', $operation) }}" method="POST">
        @csrf
        @method('PUT')

        <div class="row">
            <div class="col-lg-8">
                <div class="card mb-3">
                    <div class="card-header">
                        <h6 class="card-title mb-0 d-flex align-items-center">
                            <i class="material-icons mr-2">edit_note</i>
                            Editable Details
                        </h6>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="lpo_number">LPO Number</label>
                                    <input type="text"
                                           name="lpo_number"
                                           id="lpo_number"
                                           class="form-control @error('lpo_number') is-invalid @enderror"
                                           value="{{ old('lpo_number', $operation->lpo_number) }}"
                                           placeholder="Enter LPO Number">
                                    @error('lpo_number')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="lpo_status">LPO Status</label>
                                    <select name="lpo_status" id="lpo_status" class="form-control @error('lpo_status') is-invalid @enderror">
                                        @foreach([
                                            'pending' => 'Pending',
                                            'approved' => 'Approved',
                                            'partially_delivered' => 'Partially Delivered',
                                            'completed' => 'Completed',
                                            'cancelled' => 'Cancelled',
                                        ] as $value => $label)
                                            <option value="{{ $value }}" {{ old('lpo_status', $operation->lpo_status ?? 'pending') === $value ? 'selected' : '' }}>
                                                {{ $label }}
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('lpo_status')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="delivery_note_number">Delivery Note Number</label>
                                    <input type="text"
                                           name="delivery_note_number"
                                           id="delivery_note_number"
                                           class="form-control @error('delivery_note_number') is-invalid @enderror"
                                           value="{{ old('delivery_note_number', $operation->delivery_note_number) }}"
                                           placeholder="Supplier delivery note number">
                                    @error('delivery_note_number')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="form-group mb-0">
                            <label for="notes">Notes</label>
                            <textarea name="notes"
                                      id="notes"
                                      rows="4"
                                      class="form-control @error('notes') is-invalid @enderror"
                                      placeholder="Internal notes">{{ old('notes', $operation->notes) }}</textarea>
                            @error('notes')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>
                </div>

                <div class="card mb-3">
                    <div class="card-header">
                        <h6 class="card-title mb-0 d-flex align-items-center">
                            <i class="material-icons mr-2">inventory_2</i>
                            Delivery Items
                        </h6>
                    </div>
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-sm table-hover mb-0">
                                <thead class="thead-light">
                                    <tr>
                                        <th>Item Code</th>
                                        <th>Description</th>
                                        <th class="text-right">Qty</th>
                                        <th class="text-right">Unit Price</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse($operation->items as $item)
                                        <tr>
                                            <td>{{ $item->item->item_code ?? '-' }}</td>
                                            <td>{{ $item->item->item_description ?? '-' }}</td>
                                            <td class="text-right">{{ number_format($item->quantity_delivered ?? 0, 2) }}</td>
                                            <td class="text-right">{{ number_format($item->unit_price_aed ?? $item->unit_price ?? 0, 2) }}</td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="4" class="text-center text-muted py-4">No items found.</td>
                                        </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-lg-4">
                <div class="card mb-3">
                    <div class="card-header">
                        <h6 class="card-title mb-0 d-flex align-items-center">
                            <i class="material-icons mr-2">info</i>
                            Delivery Summary
                        </h6>
                    </div>
                    <div class="card-body">
                        <dl class="row mb-0">
                            <dt class="col-5">Operation</dt>
                            <dd class="col-7">{{ $operation->operation_number }}</dd>

                            <dt class="col-5">Date</dt>
                            <dd class="col-7">{{ optional($operation->operation_date)->format('d-M-Y') ?? '-' }}</dd>

                            <dt class="col-5">Supplier</dt>
                            <dd class="col-7">{{ $operation->supplier->name ?? '-' }}</dd>

                            <dt class="col-5">Division</dt>
                            <dd class="col-7">{{ $operation->projectDivision->division_name ?? $operation->division ?? '-' }}</dd>

                            <dt class="col-5">Status</dt>
                            <dd class="col-7">{{ ucfirst(str_replace('_', ' ', $operation->status)) }}</dd>

                            <dt class="col-5">Items</dt>
                            <dd class="col-7">{{ $operation->items->count() }}</dd>
                        </dl>
                    </div>
                </div>

                <div class="card">
                    <div class="card-body d-flex justify-content-end">
                        <a href="{{ route('warehouse.supplier-delivery.index') }}" class="btn btn-outline-secondary mr-2">Cancel</a>
                        <button type="submit" class="btn btn-primary">
                            <i class="material-icons mr-1" style="font-size: 16px;">save</i>
                            Save
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </form>
@endsection
