@extends('layouts.app')
@section('title', 'Inventory Settings')
@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
    <li class="breadcrumb-item active">Inventory Settings</li>
@endsection
@section('content')
<div class="page-header">
    <div>
        <h1 class="page-title">Inventory Settings</h1>
        <p class="page-subtitle">Configure inventory behaviour, tracking and modules</p>
    </div>
</div>

@include('settings._tabs', ['active' => 'inventory'])

@if(session('success'))
<div class="alert alert-success alert-dismissible d-flex gap-2 mb-4">
    <i class="bi bi-check-circle flex-shrink-0"></i><div>{{ session('success') }}</div>
    <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert"></button>
</div>
@endif

<form method="POST" action="{{ route('settings.inventory.update') }}" id="invForm">
@csrf @method('PUT')
<div class="row g-4">

    <div class="col-12 col-xl-6">
        <div class="card mb-4">
            <div class="card-header"><i class="bi bi-boxes me-2 text-primary"></i>Inventory Behaviour</div>
            <div class="card-body">

                <div class="p-3 rounded-2 border mb-3 {{ $settings['allow_negative_inventory']==='1' ? 'border-warning-subtle bg-warning-subtle' : 'border-success-subtle bg-success-subtle' }}">
                    <div class="form-check form-switch mb-1">
                        <input class="form-check-input" type="checkbox" name="allow_negative_inventory" value="1" id="negInv" role="switch" {{ $settings['allow_negative_inventory']==='1' ? 'checked' : '' }}>
                        <label class="form-check-label fw-semibold" for="negInv">Allow Negative Inventory</label>
                    </div>
                    <div class="text-muted" style="font-size:12px">
                        When <strong>OFF</strong> (recommended): any transaction that would make an inventory item stock balance negative is blocked.
                        When <strong>ON</strong>: negative stock is allowed (backorder scenario).
                    </div>
                </div>

                <div class="p-3 rounded-2 border mb-0 bg-light">
                    <div class="form-check form-switch mb-1">
                        <input class="form-check-input" type="checkbox" name="use_fifo_valuation" value="1" id="fifoVal" role="switch" {{ $settings['use_fifo_valuation']==='1' ? 'checked' : '' }}>
                        <label class="form-check-label fw-semibold" for="fifoVal">Use FIFO Valuation Method</label>
                    </div>
                    <div class="text-muted" style="font-size:12px">
                        When <strong>ON</strong>: First In First Out (FIFO) is the default valuation method.
                        When <strong>OFF</strong>: Weighted Average Cost (WAC) is used. Can be overridden per item.
                    </div>
                    <div class="mt-2 badge" style="background:#EBF3FB;color:#1B4F8A;font-size:11px">
                        Current: <strong>{{ $settings['use_fifo_valuation']==='1' ? 'FIFO' : 'Weighted Average' }}</strong>
                    </div>
                </div>

            </div>
        </div>
    </div>

    <div class="col-12 col-xl-6">
        <div class="card mb-4">
            <div class="card-header"><i class="bi bi-gear-wide-connected me-2 text-primary"></i>Manufacturing Module</div>
            <div class="card-body">
                <div class="p-3 rounded-2 border bg-light">
                    <div class="form-check form-switch mb-1">
                        <input class="form-check-input" type="checkbox" name="use_manufacturing" value="1" id="useMfg" role="switch" {{ ($settings['use_manufacturing'] ?? '0') === '1' ? 'checked' : '' }}>
                        <label class="form-check-label fw-semibold" for="useMfg">Use Manufacturing Module</label>
                    </div>
                    <div class="text-muted" style="font-size:12px">
                        When <strong>ON</strong>: Manufacturing appears in the top menu and sidebar with
                        Bill of Materials and Production Orders.
                        When <strong>OFF</strong>: the Manufacturing menu is hidden.
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-12 col-xl-6">
        <div class="card mb-4">
            <div class="card-header"><i class="bi bi-toggles me-2 text-primary"></i>Optional Modules &amp; Tracking</div>
            <div class="card-body">

                <div class="p-3 rounded-2 border mb-3 bg-light">
                    <div class="form-check form-switch mb-1">
                        <input class="form-check-input" type="checkbox" name="use_multi_warehouse" value="1" id="multiWh" role="switch" {{ $settings['use_multi_warehouse']==='1' ? 'checked' : '' }}>
                        <label class="form-check-label fw-semibold" for="multiWh">Use Multi-Warehouse</label>
                    </div>
                    <div class="text-muted" style="font-size:12px">When <strong>OFF</strong>: warehouse selection is hidden from all transaction forms and reports.</div>
                </div>

                <div class="p-3 rounded-2 border mb-3 bg-light">
                    <div class="form-check form-switch mb-1">
                        <input class="form-check-input" type="checkbox" name="use_serial_number" value="1" id="useSerial" role="switch" {{ $settings['use_serial_number']==='1' ? 'checked' : '' }}>
                        <label class="form-check-label fw-semibold" for="useSerial">Use Serial Number Tracking</label>
                    </div>
                    <div class="text-muted" style="font-size:12px">When <strong>OFF</strong>: serial number fields and reports are hidden.</div>
                </div>

                <div class="p-3 rounded-2 border mb-0 bg-light">
                    <div class="form-check form-switch mb-1">
                        <input class="form-check-input" type="checkbox" name="use_lot_number" value="1" id="useLot" role="switch" {{ $settings['use_lot_number']==='1' ? 'checked' : '' }}>
                        <label class="form-check-label fw-semibold" for="useLot">Use Lot / Batch Number Tracking</label>
                    </div>
                    <div class="text-muted" style="font-size:12px">When <strong>OFF</strong>: lot/batch fields and expiry date tracking are hidden.</div>
                </div>

            </div>
        </div>
    </div>

    <div class="col-12">
        <div class="card" style="background:#f0f9ff;border:1px solid #bae6fd">
            <div class="card-body py-3">
                <div class="fw-semibold mb-2" style="font-size:13px;color:#0369a1">
                    <i class="bi bi-info-circle me-2"></i>Active Inventory Configuration
                </div>
                <div class="row g-2" style="font-size:13px">
                    <div class="col-6 col-md-4"><span class="fw-semibold">Negative Inventory:</span> <span class="{{ $settings['allow_negative_inventory']==='1' ? 'text-warning' : 'text-success' }}">{{ $settings['allow_negative_inventory']==='1' ? 'Allowed' : 'Blocked (recommended)' }}</span></div>
                    <div class="col-6 col-md-4"><span class="fw-semibold">Multi-Warehouse:</span> <span class="{{ $settings['use_multi_warehouse']==='1' ? 'text-success' : 'text-secondary' }}">{{ $settings['use_multi_warehouse']==='1' ? 'Enabled' : 'Disabled' }}</span></div>
                    <div class="col-6 col-md-4"><span class="fw-semibold">Manufacturing:</span> <span class="{{ $settings['use_manufacturing']==='1' ? 'text-success' : 'text-secondary' }}">{{ $settings['use_manufacturing']==='1' ? 'Enabled' : 'Disabled' }}</span></div>
                    <div class="col-6 col-md-4"><span class="fw-semibold">Serial Tracking:</span> <span class="{{ $settings['use_serial_number']==='1' ? 'text-success' : 'text-secondary' }}">{{ $settings['use_serial_number']==='1' ? 'Enabled' : 'Disabled' }}</span></div>
                    <div class="col-6 col-md-4"><span class="fw-semibold">Lot Tracking:</span> <span class="{{ $settings['use_lot_number']==='1' ? 'text-success' : 'text-secondary' }}">{{ $settings['use_lot_number']==='1' ? 'Enabled' : 'Disabled' }}</span></div>
                    <div class="col-6 col-md-4"><span class="fw-semibold">Valuation:</span> <span class="text-primary">{{ $settings['use_fifo_valuation']==='1' ? 'FIFO' : 'Weighted Average' }}</span></div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-12">
        <div class="d-flex gap-3 justify-content-end">
            <a href="{{ route('dashboard') }}" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary px-4" id="saveBtn">
                <i class="bi bi-check-circle me-1"></i>Save Inventory Settings
            </button>
        </div>
    </div>
</div>
</form>
@endsection
@push('scripts')
<script>
document.getElementById('invForm').addEventListener('submit',function(){
    var btn=document.getElementById('saveBtn');
    btn.innerHTML='<span class="spinner-border spinner-border-sm me-2"></span>Saving...';
    btn.disabled=true;
});
</script>
@endpush
