@extends('layouts.layoutMaster')

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

<form id="salesOrderForm" action="{{ route('systemcore.sales-orders.store') }}" method="POST">
    @csrf

    <!-- First Row: Order Information + Summary -->
    <div class="row">
        <!-- Left Column: Order Information -->
        <div class="col-lg-8 col-md-12">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">
                        <i class="bx bx-file me-1"></i>{{ __('Order Information') }}
                    </h5>
                </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 select2 @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') == $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="payment_term_id" class="form-label">{{ __('Payment Terms') }}</label>
                            <select class="form-select select2 @error('payment_term_id') is-invalid @enderror"
                                    id="payment_term_id" name="payment_term_id">
                                <option value="">{{ __('Select Payment Terms') }}</option>
                                @foreach($paymentTerms as $term)
                                    <option value="{{ $term->id }}" {{ old('payment_term_id') == $term->id ? 'selected' : '' }}>
                                        {{ $term->name }} ({{ $term->days }} {{ __('days') }})
                                    </option>
                                @endforeach
                            </select>
                            @error('payment_term_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', date('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') }}">
                            @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', 'draft') == 'draft' ? 'selected' : '' }}>{{ __('Draft') }}</option>
                                <option value="pending" {{ old('status') == 'pending' ? 'selected' : '' }}>{{ __('Pending') }}</option>
                                <option value="confirmed" {{ old('status') == 'confirmed' ? 'selected' : '' }}>{{ __('Confirmed') }}</option>
                                <option value="processing" {{ old('status') == 'processing' ? 'selected' : '' }}>{{ __('Processing') }}</option>
                                <option value="shipped" {{ old('status') == 'shipped' ? 'selected' : '' }}>{{ __('Shipped') }}</option>
                                <option value="delivered" {{ old('status') == 'delivered' ? 'selected' : '' }}>{{ __('Delivered') }}</option>
                                <option value="completed" {{ old('status') == 'completed' ? 'selected' : '' }}>{{ __('Completed') }}</option>
                                <option value="cancelled" {{ old('status') == 'cancelled' ? 'selected' : '' }}>{{ __('Cancelled') }}</option>
                            </select>
                            @error('status')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </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="3">{{ old('notes') }}</textarea>
                            @error('notes')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Right Column: Summary & Payment -->
        <div class="col-lg-4 col-md-12">
            <!-- Order Summary Card -->
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">
                        <i class="bx bx-calculator me-1"></i>{{ __('Order Summary') }}
                    </h5>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <label class="form-label">{{ __('Subtotal') }}</label>
                        <input type="text" class="form-control" id="displaySubtotal" value="0.00" readonly>
                        <input type="hidden" name="subtotal" id="subtotal" value="0">
                    </div>

                    <div class="mb-3">
                        <label for="tax_amount" class="form-label">{{ __('Tax Amount') }}</label>
                        <input type="number" step="0.01" class="form-control" id="tax_amount" name="tax_amount" value="{{ old('tax_amount', 0) }}" readonly>
                    </div>

                    <div class="mb-3">
                        <label for="discount_amount" class="form-label">{{ __('Additional Discount') }}</label>
                        <input type="number" step="0.01" class="form-control" id="discount_amount" name="discount_amount" value="{{ old('discount_amount', 0) }}">
                    </div>

                    <div class="mb-3">
                        <label for="shipping_cost" class="form-label">{{ __('Shipping Cost') }}</label>
                        <input type="number" step="0.01" class="form-control" id="shipping_cost" name="shipping_cost" value="{{ old('shipping_cost', 0) }}">
                    </div>

                    <hr>

                    <div class="mb-3">
                        <label class="form-label fw-bold">{{ __('Total Amount') }}</label>
                        <input type="text" class="form-control fw-bold text-primary fs-5" id="displayTotalAmount" value="0.00" readonly>
                        <input type="hidden" name="total_amount" id="total_amount" value="0">
                    </div>

                    <hr>

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

                    <div class="mb-3">
                        <label for="payment_method" class="form-label">{{ __('Payment Method') }}</label>
                        <select class="form-select @error('payment_method') is-invalid @enderror"
                                id="payment_method" name="payment_method">
                            <option value="">{{ __('Select Method') }}</option>
                            <option value="cash" {{ old('payment_method') == 'cash' ? 'selected' : '' }}>{{ __('Cash') }}</option>
                            <option value="credit_card" {{ old('payment_method') == 'credit_card' ? 'selected' : '' }}>{{ __('Credit Card') }}</option>
                            <option value="bank_transfer" {{ old('payment_method') == 'bank_transfer' ? 'selected' : '' }}>{{ __('Bank Transfer') }}</option>
                            <option value="check" {{ old('payment_method') == 'check' ? 'selected' : '' }}>{{ __('Check') }}</option>
                            <option value="other" {{ old('payment_method') == 'other' ? 'selected' : '' }}>{{ __('Other') }}</option>
                        </select>
                        @error('payment_method')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
            </div>

            <!-- Action Buttons Card -->
            <div class="card">
                <div class="card-body">
                    <button type="submit" class="btn btn-primary w-100 mb-2">
                        <i class="bx bx-save me-1"></i> {{ __('Save Sales Order') }}
                    </button>
                    <a href="{{ route('systemcore.sales-orders.index') }}" class="btn btn-label-secondary w-100">
                        <i class="bx bx-x me-1"></i> {{ __('Cancel') }}
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Second Row: Order Items (Full Width) -->
    <div class="row mt-4">
        <div class="col-12">
            <div class="card mb-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">
                        <i class="bx bx-list-ul me-1"></i>{{ __('Order Items') }}
                    </h5>
                    <button type="button" class="btn btn-sm btn-primary" id="addLineItem">
                        <i class="bx bx-plus me-1"></i>{{ __('Add Item') }}
                    </button>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered" id="lineItemsTable">
                            <thead class="table-light">
                                <tr>
                                    <th style="width: 30%">{{ __('Product') }}</th>
                                    <th style="width: 12%">{{ __('Quantity') }}</th>
                                    <th style="width: 12%">{{ __('Unit Price') }}</th>
                                    <th style="width: 15%">{{ __('Tax') }}</th>
                                    <th style="width: 12%">{{ __('Discount') }}</th>
                                    <th style="width: 13%">{{ __('Total') }}</th>
                                    <th style="width: 6%"></th>
                                </tr>
                            </thead>
                            <tbody id="lineItemsContainer">
                                <!-- Line items will be dynamically added here -->
                            </tbody>
                        </table>
                    </div>
                    <div class="text-muted mt-2">
                        <i class="bx bx-info-circle me-1"></i>
                        {{ __('Click "Add Item" to add products to this order') }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
@endsection

@section('page-script')
<script>
    // Pass tax configurations to JavaScript
    window.taxConfigurations = @json($taxConfigurations);

    // Page data for translations and URLs
    window.pageData = {
        urls: {
            productsSearch: @json(route('systemcore.products.search'))
        },
        labels: {
            selectCustomer: @json(__('Select Customer')),
            selectProduct: @json(__('Select Product')),
            warning: @json(__('Warning')),
            add_at_least_one_item: @json(__('Please add at least one item to the order')),
            select_all_products: @json(__('Please select a product for all line items')),
            noTax: @json(__('No Tax')),
            searchProducts: @json(__('Search products...'))
        }
    };
</script>
@vite(['Modules/SystemCore/resources/assets/js/sales-orders-form.js'])
@endsection
