@extends('layouts.layoutMaster')

@section('title', __('Edit Sales 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 Sales Order')"
    :breadcrumbs="[
        ['name' => __('System Core'), 'url' => route('systemcore.dashboard')],
        ['name' => __('Sales Orders'), 'url' => route('systemcore.sales-orders.index')],
        ['name' => __('Edit')]
    ]"
/>

<div class="row">
    <div class="col-md-12">
        <form id="salesOrderForm" action="{{ route('systemcore.sales-orders.update', $salesOrder->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">{{ __('Sales Order Information') }}</h5>
                    <span class="badge bg-label-primary">{{ $salesOrder->order_number }}</span>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="customer_id" class="form-label">{{ __('Customer') }} <span class="text-danger">*</span></label>
                            <select class="form-select @error('customer_id') is-invalid @enderror"
                                    id="customer_id" name="customer_id" required>
                                <option value="">{{ __('Select Customer') }}</option>
                                @foreach($customers as $customer)
                                    <option value="{{ $customer->id }}" {{ old('customer_id', $salesOrder->customer_id) == $customer->id ? 'selected' : '' }}>
                                        {{ $customer->customer_code }} - {{ $customer->name }}
                                    </option>
                                @endforeach
                            </select>
                            @error('customer_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', $salesOrder->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="delivery_date" class="form-label">{{ __('Delivery Date') }}</label>
                            <input type="text" class="form-control flatpickr-date @error('delivery_date') is-invalid @enderror"
                                   id="delivery_date" name="delivery_date" value="{{ old('delivery_date', $salesOrder->delivery_date?->format('Y-m-d')) }}">
                            @error('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', $salesOrder->status->value) == 'draft' ? 'selected' : '' }}>{{ __('Draft') }}</option>
                                <option value="pending" {{ old('status', $salesOrder->status->value) == 'pending' ? 'selected' : '' }}>{{ __('Pending') }}</option>
                                <option value="confirmed" {{ old('status', $salesOrder->status->value) == 'confirmed' ? 'selected' : '' }}>{{ __('Confirmed') }}</option>
                                <option value="processing" {{ old('status', $salesOrder->status->value) == 'processing' ? 'selected' : '' }}>{{ __('Processing') }}</option>
                                <option value="shipped" {{ old('status', $salesOrder->status->value) == 'shipped' ? 'selected' : '' }}>{{ __('Shipped') }}</option>
                                <option value="delivered" {{ old('status', $salesOrder->status->value) == 'delivered' ? 'selected' : '' }}>{{ __('Delivered') }}</option>
                                <option value="completed" {{ old('status', $salesOrder->status->value) == 'completed' ? 'selected' : '' }}>{{ __('Completed') }}</option>
                                <option value="cancelled" {{ old('status', $salesOrder->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', $salesOrder->payment_status) == 'pending' ? 'selected' : '' }}>{{ __('Pending') }}</option>
                                <option value="partial" {{ old('payment_status', $salesOrder->payment_status) == 'partial' ? 'selected' : '' }}>{{ __('Partial') }}</option>
                                <option value="paid" {{ old('payment_status', $salesOrder->payment_status) == 'paid' ? 'selected' : '' }}>{{ __('Paid') }}</option>
                                <option value="overdue" {{ old('payment_status', $salesOrder->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($salesOrder->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', $salesOrder->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 Sales Order') }}
                        </button>
                        <a href="{{ route('systemcore.sales-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 customer dropdown
    $('#customer_id').select2({
        placeholder: '{{ __("Select Customer") }}',
        allowClear: true
    });

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