<x-layouts.app.sidebar>
    <div class="max-w-7xl mx-auto py-6 px-4 sm:px-6 lg:px-8">
        <!-- Settings Header -->
        <div class="mb-8">
            <h1 class="text-3xl font-bold text-zinc-900 dark:text-white">Settings</h1>
        </div>

        <!-- Settings Navigation -->
        <x-settings-navigation />

        <!-- Settings Form -->
        <form method="POST" action="{{ route('settings.inventory.update') }}" class="space-y-8">
            @csrf
            @method('PUT')

            <!-- Stock Management Section -->
            <div class="bg-white dark:bg-zinc-800 border border-zinc-200 dark:border-zinc-700 rounded-lg p-6">
                <div class="mb-6">
                    <h2 class="text-xl font-semibold text-zinc-900 dark:text-white">Stock Management</h2>
                    <p class="text-zinc-600 dark:text-zinc-400">Configure inventory tracking and stock level alerts</p>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Low Stock Threshold *</label>
                        <input type="number" min="1" name="low_stock_threshold" 
                               value="{{ old('low_stock_threshold', $settings->low_stock_threshold ?? '5') }}" required 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Alert when stock falls below this level</p>
                        @error('low_stock_threshold')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Critical Stock Threshold *</label>
                        <input type="number" min="1" name="critical_stock_threshold" 
                               value="{{ old('critical_stock_threshold', $settings->critical_stock_threshold ?? '2') }}" required 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Critical stock level requiring immediate attention</p>
                        @error('critical_stock_threshold')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Default Reorder Quantity *</label>
                        <input type="number" min="1" name="default_reorder_quantity" 
                               value="{{ old('default_reorder_quantity', $settings->default_reorder_quantity ?? '25') }}" required 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Default quantity to reorder</p>
                        @error('default_reorder_quantity')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Default Lead Time (days) *</label>
                        <input type="number" min="1" name="default_lead_time_days" 
                               value="{{ old('default_lead_time_days', $settings->default_lead_time_days ?? '7') }}" required 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Expected delivery time for orders</p>
                        @error('default_lead_time_days')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Default Markup Percentage *</label>
                        <input type="number" step="0.01" min="0" name="default_markup_percentage" 
                               value="{{ old('default_markup_percentage', $settings->default_markup_percentage ?? '35.00') }}" required 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Default markup percentage for parts</p>
                        @error('default_markup_percentage')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Preferred Supplier Count *</label>
                        <input type="number" min="1" name="preferred_supplier_count" 
                               value="{{ old('preferred_supplier_count', $settings->preferred_supplier_count ?? '3') }}" required 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Number of preferred suppliers to maintain</p>
                        @error('preferred_supplier_count')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Minimum Order Amount *</label>
                        <input type="number" step="0.01" min="0" name="minimum_order_amount" 
                               value="{{ old('minimum_order_amount', $settings->minimum_order_amount ?? '50.00') }}" required 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Minimum amount for supplier orders</p>
                        @error('minimum_order_amount')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>
                </div>

                <div class="mt-6 space-y-4">
                    <div class="flex items-center">
                        <input type="checkbox" name="enable_low_stock_alerts" value="1" 
                               {{ old('enable_low_stock_alerts', $settings->enable_low_stock_alerts ?? true) ? 'checked' : '' }}
                               class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-zinc-300 rounded" />
                        <label class="ml-3 text-sm font-medium text-zinc-700 dark:text-zinc-300">Enable Low Stock Alerts</label>
                    </div>

                    <div class="flex items-center">
                        <input type="checkbox" name="enable_automatic_reorder" value="1" 
                               {{ old('enable_automatic_reorder', $settings->enable_auto_reorder ?? false) ? 'checked' : '' }}
                               class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-zinc-300 rounded" />
                        <label class="ml-3 text-sm font-medium text-zinc-700 dark:text-zinc-300">Enable Automatic Reorder</label>
                    </div>

                    <div class="flex items-center">
                        <input type="checkbox" name="track_serial_numbers" value="1" 
                               {{ old('track_serial_numbers', $settings->track_serial_numbers ?? false) ? 'checked' : '' }}
                               class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-zinc-300 rounded" />
                        <label class="ml-3 text-sm font-medium text-zinc-700 dark:text-zinc-300">Track Serial Numbers</label>
                    </div>

                    <div class="flex items-center">
                        <input type="checkbox" name="enable_barcode_scanning" value="1" 
                               {{ old('enable_barcode_scanning', $settings->enable_barcode_scanning ?? false) ? 'checked' : '' }}
                               class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-zinc-300 rounded" />
                        <label class="ml-3 text-sm font-medium text-zinc-700 dark:text-zinc-300">Enable Barcode Scanning</label>
                    </div>
                </div>
            </div>

            <!-- Pricing Rules Section -->
            <div class="bg-white dark:bg-zinc-800 border border-zinc-200 dark:border-zinc-700 rounded-lg p-6">
                <div class="mb-6">
                    <h2 class="text-xl font-semibold text-zinc-900 dark:text-white">Pricing Rules</h2>
                    <p class="text-zinc-600 dark:text-zinc-400">Configure markup percentages and pricing strategies</p>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Default Part Markup (%) *</label>
                        <input type="number" step="0.01" min="0" max="1000" name="default_part_markup" 
                               value="{{ old('default_part_markup', $settings->default_part_markup ?? '50.00') }}" required 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Default markup percentage for parts</p>
                        @error('default_part_markup')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Core Charge Percentage (%) *</label>
                        <input type="number" step="0.01" min="0" max="100" name="core_charge_percentage" 
                               value="{{ old('core_charge_percentage', $settings->core_charge_percentage ?? '10.00') }}" required 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Default core charge as percentage of part cost</p>
                        @error('core_charge_percentage')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Shop Supply Fee (%) *</label>
                        <input type="number" step="0.01" min="0" max="25" name="shop_supply_fee" 
                               value="{{ old('shop_supply_fee', $settings->shop_supply_fee ?? '5.00') }}" required 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Shop supply fee as percentage of total</p>
                        @error('shop_supply_fee')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Environmental Fee ($) *</label>
                        <input type="number" step="0.01" min="0" max="100" name="environmental_fee" 
                               value="{{ old('environmental_fee', $settings->environmental_fee ?? '5.00') }}" required 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Fixed environmental disposal fee</p>
                        @error('environmental_fee')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Waste Oil Fee ($) *</label>
                        <input type="number" step="0.01" min="0" max="50" name="waste_oil_fee" 
                               value="{{ old('waste_oil_fee', $settings->waste_oil_fee ?? '3.00') }}" required 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Fee for waste oil disposal</p>
                        @error('waste_oil_fee')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Tire Disposal Fee ($) *</label>
                        <input type="number" step="0.01" min="0" max="25" name="tire_disposal_fee" 
                               value="{{ old('tire_disposal_fee', $settings->tire_disposal_fee ?? '5.00') }}" required 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Fee per tire for disposal</p>
                        @error('tire_disposal_fee')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>
                </div>

                <div class="mt-6 space-y-4">
                    <div class="flex items-center">
                        <input type="checkbox" name="enable_volume_discounts" value="1" 
                               {{ old('enable_volume_discounts', $settings->enable_volume_discounts ?? false) ? 'checked' : '' }}
                               class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-zinc-300 rounded" />
                        <label class="ml-3 text-sm font-medium text-zinc-700 dark:text-zinc-300">Enable Volume Discounts</label>
                    </div>
                    <p class="ml-7 text-xs text-zinc-500 dark:text-zinc-400">Apply volume-based pricing discounts</p>

                    <div class="flex items-center">
                        <input type="checkbox" name="enable_seasonal_pricing" value="1" 
                               {{ old('enable_seasonal_pricing', $settings->enable_seasonal_pricing ?? false) ? 'checked' : '' }}
                               class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-zinc-300 rounded" />
                        <label class="ml-3 text-sm font-medium text-zinc-700 dark:text-zinc-300">Enable Seasonal Pricing</label>
                    </div>
                    <p class="ml-7 text-xs text-zinc-500 dark:text-zinc-400">Apply seasonal pricing adjustments</p>

                    <div class="flex items-center">
                        <input type="checkbox" name="enable_customer_specific_pricing" value="1" 
                               {{ old('enable_customer_specific_pricing', $settings->enable_customer_specific_pricing ?? false) ? 'checked' : '' }}
                               class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-zinc-300 rounded" />
                        <label class="ml-3 text-sm font-medium text-zinc-700 dark:text-zinc-300">Enable Customer-Specific Pricing</label>
                    </div>
                    <p class="ml-7 text-xs text-zinc-500 dark:text-zinc-400">Allow custom pricing for specific customers</p>
                </div>
            </div>

            <!-- Supplier Settings Section -->
            <div class="bg-white dark:bg-zinc-800 border border-zinc-200 dark:border-zinc-700 rounded-lg p-6">
                <div class="mb-6">
                    <h2 class="text-xl font-semibold text-zinc-900 dark:text-white">Supplier Settings</h2>
                    <p class="text-zinc-600 dark:text-zinc-400">Configure default supplier preferences and ordering settings</p>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Default Supplier Payment Terms *</label>
                        <select name="default_payment_terms" required 
                                class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white">
                            <option value="NET_30" {{ old('default_payment_terms', $settings->default_payment_terms ?? 'NET_30') === 'NET_30' ? 'selected' : '' }}>Net 30</option>
                            <option value="NET_15" {{ old('default_payment_terms', $settings->default_payment_terms ?? 'NET_30') === 'NET_15' ? 'selected' : '' }}>Net 15</option>
                            <option value="NET_10" {{ old('default_payment_terms', $settings->default_payment_terms ?? 'NET_30') === 'NET_10' ? 'selected' : '' }}>Net 10</option>
                            <option value="COD" {{ old('default_payment_terms', $settings->default_payment_terms ?? 'NET_30') === 'COD' ? 'selected' : '' }}>COD</option>
                        </select>
                        @error('default_payment_terms')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Preferred Ordering Method *</label>
                        <select name="preferred_ordering_method" required 
                                class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white">
                            <option value="EMAIL" {{ old('preferred_ordering_method', $settings->preferred_ordering_method ?? 'EMAIL') === 'EMAIL' ? 'selected' : '' }}>Email</option>
                            <option value="PHONE" {{ old('preferred_ordering_method', $settings->preferred_ordering_method ?? 'EMAIL') === 'PHONE' ? 'selected' : '' }}>Phone</option>
                            <option value="ONLINE" {{ old('preferred_ordering_method', $settings->preferred_ordering_method ?? 'EMAIL') === 'ONLINE' ? 'selected' : '' }}>Online</option>
                            <option value="FAX" {{ old('preferred_ordering_method', $settings->preferred_ordering_method ?? 'EMAIL') === 'FAX' ? 'selected' : '' }}>Fax</option>
                        </select>
                        @error('preferred_ordering_method')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Minimum Order Amount ($)</label>
                        <input type="number" step="0.01" min="0" name="minimum_order_amount" 
                               value="{{ old('minimum_order_amount', $settings->minimum_order_amount ?? '50.00') }}" 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Minimum order amount for suppliers</p>
                        @error('minimum_order_amount')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">Free Shipping Threshold ($)</label>
                        <input type="number" step="0.01" min="0" name="free_shipping_threshold" 
                               value="{{ old('free_shipping_threshold', $settings->free_shipping_threshold ?? '200.00') }}" 
                               class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-zinc-700 dark:text-white" />
                        <p class="mt-1 text-xs text-zinc-500 dark:text-zinc-400">Order amount for free shipping</p>
                        @error('free_shipping_threshold')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>
                </div>

                <div class="mt-6 space-y-4">
                    <div class="flex items-center">
                        <input type="checkbox" name="require_po_approval" value="1" 
                               {{ old('require_po_approval', $settings->require_po_approval ?? true) ? 'checked' : '' }}
                               class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-zinc-300 rounded" />
                        <label class="ml-3 text-sm font-medium text-zinc-700 dark:text-zinc-300">Require Purchase Order Approval</label>
                    </div>
                    <p class="ml-7 text-xs text-zinc-500 dark:text-zinc-400">Require manager approval for purchase orders</p>

                    <div class="flex items-center">
                        <input type="checkbox" name="enable_dropship" value="1" 
                               {{ old('enable_dropship', $settings->enable_dropship ?? false) ? 'checked' : '' }}
                               class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-zinc-300 rounded" />
                        <label class="ml-3 text-sm font-medium text-zinc-700 dark:text-zinc-300">Enable Drop-shipping</label>
                    </div>
                    <p class="ml-7 text-xs text-zinc-500 dark:text-zinc-400">Allow parts to be shipped directly to customers</p>

                    <div class="flex items-center">
                        <input type="checkbox" name="enable_backorders" value="1" 
                               {{ old('enable_backorders', $settings->enable_backorders ?? true) ? 'checked' : '' }}
                               class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-zinc-300 rounded" />
                        <label class="ml-3 text-sm font-medium text-zinc-700 dark:text-zinc-300">Enable Backorders</label>
                    </div>
                    <p class="ml-7 text-xs text-zinc-500 dark:text-zinc-400">Allow ordering parts that are out of stock</p>
                </div>
            </div>

            <!-- Form Actions -->
            <div class="flex justify-end space-x-3">
                <a href="{{ route('settings.general') }}" 
                   class="px-4 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md text-zinc-700 dark:text-zinc-300 hover:bg-zinc-50 dark:hover:bg-zinc-700">
                    Cancel
                </a>
                <button type="submit" 
                        class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500">
                    Save Inventory Settings
                </button>
            </div>
        </form>
    </div>
</x-layouts.app.sidebar>
