@extends('layouts.layoutMaster')

@section('title', __('Edit Purchase Order'))

@section('vendor-style')
@vite([
    'resources/assets/vendor/libs/select2/select2.scss',
    'resources/assets/vendor/libs/flatpickr/flatpickr.scss'
])
@endsection

@section('vendor-script')
@vite([
    'resources/assets/vendor/libs/select2/select2.js',
    'resources/assets/vendor/libs/flatpickr/flatpickr.js'
])
@endsection

@section('content')
<x-breadcrumb
    :title="__('Edit Purchase Order')"
    :breadcrumbs="[
        ['name' => __('System Core'), 'url' => route('systemcore.dashboard')],
        ['name' => __('Purchase Orders'), 'url' => route('systemcore.purchase-orders.index')],
        ['name' => __('Edit')]
    ]"
/>

<div class="row">
    <div class="col-md-12">
        <form id="purchaseOrderForm" action="{{ route('systemcore.purchase-orders.update', $purchaseOrder->id) }}" method="POST">
            @csrf
            @method('PUT')

            <div class="card mb-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">{{ __('Purchase Order Information') }}</h5>
                    <span class="badge bg-label-primary">{{ $purchaseOrder->order_number }}</span>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="supplier_id" class="form-label">{{ __('Supplier') }} <span class="text-danger">*</span></label>
                            <select class="form-select @error('supplier_id') is-invalid @enderror"
                                    id="supplier_id" name="supplier_id" required>
                                <option value="">{{ __('Select Supplier') }}</option>
                                @foreach($suppliers as $supplier)
                                    <option value="{{ $supplier->id }}" {{ old('supplier_id', $purchaseOrder->supplier_id) == $supplier->id ? 'selected' : '' }}>
                                        {{ $supplier->supplier_code }} - {{ $supplier->name }}
                                    </option>
                                @endforeach
                            </select>
                            @error('supplier_id')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="order_date" class="form-label">{{ __('Order Date') }} <span class="text-danger">*</span></label>
                            <input type="text" class="form-control flatpickr-date @error('order_date') is-invalid @enderror"
                                   id="order_date" name="order_date" value="{{ old('order_date', $purchaseOrder->order_date?->format('Y-m-d')) }}" required>
                            @error('order_date')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="expected_delivery_date" class="form-label">{{ __('Expected Delivery Date') }}</label>
                            <input type="text" class="form-control flatpickr-date @error('expected_delivery_date') is-invalid @enderror"
                                   id="expected_delivery_date" name="expected_delivery_date" value="{{ old('expected_delivery_date', $purchaseOrder->expected_delivery_date?->format('Y-m-d')) }}">
                            @error('expected_delivery_date')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="status" class="form-label">{{ __('Status') }} <span class="text-danger">*</span></label>
                            <select class="form-select @error('status') is-invalid @enderror"
                                    id="status" name="status" required>
                                <option value="draft" {{ old('status', $purchaseOrder->status->value) == 'draft' ? 'selected' : '' }}>{{ __('Draft') }}</option>
                                <option value="pending" {{ old('status', $purchaseOrder->status->value) == 'pending' ? 'selected' : '' }}>{{ __('Pending') }}</option>
                                <option value="confirmed" {{ old('status', $purchaseOrder->status->value) == 'confirmed' ? 'selected' : '' }}>{{ __('Confirmed') }}</option>
                                <option value="processing" {{ old('status', $purchaseOrder->status->value) == 'processing' ? 'selected' : '' }}>{{ __('Processing') }}</option>
                                <option value="shipped" {{ old('status', $purchaseOrder->status->value) == 'shipped' ? 'selected' : '' }}>{{ __('Shipped') }}</option>
                                <option value="delivered" {{ old('status', $purchaseOrder->status->value) == 'delivered' ? 'selected' : '' }}>{{ __('Delivered') }}</option>
                                <option value="completed" {{ old('status', $purchaseOrder->status->value) == 'completed' ? 'selected' : '' }}>{{ __('Completed') }}</option>
                                <option value="cancelled" {{ old('status', $purchaseOrder->status->value) == 'cancelled' ? 'selected' : '' }}>{{ __('Cancelled') }}</option>
                            </select>
                            @error('status')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="payment_status" class="form-label">{{ __('Payment Status') }} <span class="text-danger">*</span></label>
                            <select class="form-select @error('payment_status') is-invalid @enderror"
                                    id="payment_status" name="payment_status" required>
                                <option value="pending" {{ old('payment_status', $purchaseOrder->payment_status) == 'pending' ? 'selected' : '' }}>{{ __('Pending') }}</option>
                                <option value="partial" {{ old('payment_status', $purchaseOrder->payment_status) == 'partial' ? 'selected' : '' }}>{{ __('Partial') }}</option>
                                <option value="paid" {{ old('payment_status', $purchaseOrder->payment_status) == 'paid' ? 'selected' : '' }}>{{ __('Paid') }}</option>
                                <option value="overdue" {{ old('payment_status', $purchaseOrder->payment_status) == 'overdue' ? 'selected' : '' }}>{{ __('Overdue') }}</option>
                            </select>
                            @error('payment_status')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label class="form-label">{{ __('Total Amount') }}</label>
                            <input type="text" class="form-control" value="{{ number_format($purchaseOrder->total_amount, 2) }}" disabled>
                            <small class="text-muted">{{ __('Amount is calculated from order line items') }}</small>
                        </div>

                        <div class="col-md-12 mb-3">
                            <label for="notes" class="form-label">{{ __('Notes') }}</label>
                            <textarea class="form-control @error('notes') is-invalid @enderror"
                                      id="notes" name="notes" rows="4">{{ old('notes', $purchaseOrder->notes) }}</textarea>
                            @error('notes')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>

                    <div class="alert alert-info">
                        <i class="bx bx-info-circle me-2"></i>
                        {{ __('Line items management will be available in the next update. You can currently update the order details only.') }}
                    </div>
                </div>
            </div>

            <div class="card">
                <div class="card-body">
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="bx bx-save me-1"></i> {{ __('Update Purchase Order') }}
                        </button>
                        <a href="{{ route('systemcore.purchase-orders.index') }}" class="btn btn-label-secondary">
                            <i class="bx bx-x me-1"></i> {{ __('Cancel') }}
                        </a>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
@endsection

@section('page-script')
<script>
document.addEventListener('DOMContentLoaded', function () {
    // Initialize Select2 for supplier dropdown
    $('#supplier_id').select2({
        placeholder: '{{ __("Select Supplier") }}',
        allowClear: true
    });

    // Initialize Flatpickr for date inputs
    $('.flatpickr-date').flatpickr({
        dateFormat: 'Y-m-d',
        altInput: true,
        altFormat: 'F j, Y'
    });
});
</script>
@endsection
