@extends('layouts.admin-simple')

@section('title', 'Edit Indirect Good')

@section('breadcrumb')
    <li class="breadcrumb-item">Warehouse</li>
    <li class="breadcrumb-item">Indirect Goods</li>
    <li class="breadcrumb-item"><a href="{{ route('warehouse.indirect-goods.catalog.index') }}">Catalog</a></li>
    <li class="breadcrumb-item active">Edit</li>
@endsection

@section('page-title')
    <div class="d-flex justify-content-between align-items-center w-100">
        <div class="page-title-left">
            <h3 class="mb-1">Edit Indirect Good</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">Update indirect good information</p>
        </div>
    </div>
@endsection

@section('content')
<div class="card shadow mb-4">
    <div class="card-header py-3 d-flex justify-content-between align-items-center">
        <h6 class="m-0 font-weight-bold text-primary">Edit Indirect Good Information</h6>
        <span class="text-muted">{{ $good->ig_code }}</span>
    </div>

    <div class="card-body">
        @if ($errors->any())
            <div class="alert alert-danger">
                <ul class="mb-0">
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <form action="{{ route('warehouse.indirect-goods.catalog.update', $good->id) }}" method="POST">
            @csrf
            @method('PUT')
            <input type="hidden" name="opening_inventory_id" value="{{ old('opening_inventory_id', optional($openingInventory)->id) }}">

            <!-- Basic Information -->
            <div class="mb-4">
                <h6 class="text-primary font-weight-bold mb-3">Basic Information</h6>
            </div>

            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="ig_code" class="form-label">IG Code <span class="text-danger">*</span></label>
                        <input type="text" name="ig_code" id="ig_code"
                               class="form-control text-uppercase @error('ig_code') is-invalid @enderror"
                               value="{{ old('ig_code', $good->ig_code) }}"
                               maxlength="50" required>
                        @error('ig_code')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                <div class="col-md-6">
                    @php
                        $categoryOptions = collect($categories)->map(fn($cat) => ['value' => $cat->id, 'text' => $cat->category_name])->prepend(['value' => '', 'text' => 'Select Category'])->toArray();
                    @endphp
                    <x-warehouse.select2-dropdown
                        name="category_id"
                        id="category_id"
                        label="Category"
                        placeholder="Select Category"
                        :options="$categoryOptions"
                        :selected="old('category_id', $good->category_id)"
                        required="true"
                    />
                </div>
            </div>

            <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        <label for="ig_description" class="form-label">Description <span class="text-danger">*</span></label>
                        <input type="text" name="ig_description" id="ig_description"
                               class="form-control @error('ig_description') is-invalid @enderror"
                               value="{{ old('ig_description', $good->ig_description) }}" required>
                        @error('ig_description')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-6">
                    @php
                        $unitOptions = $units->toArray();
                        array_unshift($unitOptions, ['value' => '', 'text' => 'Select Unit']);
                    @endphp
                    <x-warehouse.select2-dropdown
                        name="unit_id"
                        id="unit_id"
                        label="Unit of Measure"
                        placeholder="Select Unit"
                        :options="$unitOptions"
                        :selected="old('unit_id', $good->unit_id)"
                        required="true"
                    />
                </div>
                <div class="col-md-6">
                    <x-warehouse.select2-dropdown
                        name="status"
                        id="status"
                        label="Status"
                        placeholder="Select Status"
                        :options="[
                            ['value' => 'active', 'text' => 'Active'],
                            ['value' => 'inactive', 'text' => 'Inactive']
                        ]"
                        :selected="old('status', $good->status)"
                        required="true"
                    />
                </div>
            </div>

            <!-- Stock Level Information -->
            <div class="mb-4 mt-4">
                <h6 class="text-primary font-weight-bold mb-3">Stock Level Information</h6>
            </div>

            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <label for="min_stock_level" class="form-label">Min Stock Level</label>
                        <input type="number" name="min_stock_level" id="min_stock_level"
                               class="form-control @error('min_stock_level') is-invalid @enderror"
                               value="{{ old('min_stock_level', $good->min_stock_level) }}"
                               min="0" step="0.01">
                        @error('min_stock_level')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label for="max_stock_level" class="form-label">Max Stock Level</label>
                        <input type="number" name="max_stock_level" id="max_stock_level"
                               class="form-control @error('max_stock_level') is-invalid @enderror"
                               value="{{ old('max_stock_level', $good->max_stock_level) }}"
                               min="0" step="0.01">
                        @error('max_stock_level')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label for="reorder_point" class="form-label">Reorder Point</label>
                        <input type="number" name="reorder_point" id="reorder_point"
                               class="form-control @error('reorder_point') is-invalid @enderror"
                               value="{{ old('reorder_point', $good->reorder_point) }}"
                               min="0" step="0.01">
                        @error('reorder_point')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
            </div>

            <!-- Opening / Current Stock Batch -->
            <div class="mb-4 mt-4">
                <h6 class="text-primary font-weight-bold mb-3">Opening / Current Stock Batch</h6>
                @if($good->inventories->count() > 1)
                    <small class="text-muted d-block mb-3">
                        This form edits the first warehouse inventory batch. Other batches remain available from inventory and adjustment screens.
                    </small>
                @endif
            </div>

            <div class="row">
                <div class="col-md-3">
                    <div class="form-group">
                        <label for="opening_quantity" class="form-label">Opening / Current Qty</label>
                        <input type="number" name="opening_quantity" id="opening_quantity"
                               class="form-control @error('opening_quantity') is-invalid @enderror"
                               value="{{ old('opening_quantity', optional($openingInventory)->quantity_available) }}"
                               min="0" step="0.01">
                        @error('opening_quantity')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        <label for="opening_unit_price" class="form-label">Unit Price</label>
                        <input type="number" name="opening_unit_price" id="opening_unit_price"
                               class="form-control @error('opening_unit_price') is-invalid @enderror"
                               value="{{ old('opening_unit_price', optional($openingInventory)->unit_price) }}"
                               min="0" step="0.01">
                        @error('opening_unit_price')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        <label for="opening_batch_number" class="form-label">Batch Number</label>
                        <input type="text" name="opening_batch_number" id="opening_batch_number"
                               class="form-control @error('opening_batch_number') is-invalid @enderror"
                               value="{{ old('opening_batch_number', optional($openingInventory)->batch_number) }}"
                               maxlength="50">
                        @error('opening_batch_number')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        <label for="opening_invoice_number" class="form-label">Invoice Number</label>
                        <input type="text" name="opening_invoice_number" id="opening_invoice_number"
                               class="form-control @error('opening_invoice_number') is-invalid @enderror"
                               value="{{ old('opening_invoice_number', optional($openingInventory)->invoice_number) }}"
                               maxlength="100">
                        @error('opening_invoice_number')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-3">
                    <div class="form-group">
                        <label for="opening_location" class="form-label">Location</label>
                        <input type="text" name="opening_location" id="opening_location"
                               class="form-control @error('opening_location') is-invalid @enderror"
                               value="{{ old('opening_location', optional($openingInventory)->location) }}"
                               maxlength="100">
                        @error('opening_location')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        <label for="opening_invoice_date" class="form-label">Invoice Date</label>
                        <input type="date" name="opening_invoice_date" id="opening_invoice_date"
                               class="form-control @error('opening_invoice_date') is-invalid @enderror"
                               value="{{ old('opening_invoice_date', optional(optional($openingInventory)->invoice_date)->format('Y-m-d')) }}">
                        @error('opening_invoice_date')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        <label for="opening_production_date" class="form-label">Production Date</label>
                        <input type="date" name="opening_production_date" id="opening_production_date"
                               class="form-control @error('opening_production_date') is-invalid @enderror"
                               value="{{ old('opening_production_date', optional(optional($openingInventory)->production_date)->format('Y-m-d')) }}">
                        @error('opening_production_date')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        <label for="opening_expiry_date" class="form-label">Expiry Date</label>
                        <input type="date" name="opening_expiry_date" id="opening_expiry_date"
                               class="form-control @error('opening_expiry_date') is-invalid @enderror"
                               value="{{ old('opening_expiry_date', optional(optional($openingInventory)->expiry_date)->format('Y-m-d')) }}">
                        @error('opening_expiry_date')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
            </div>

            <!-- Additional Information -->
            <div class="mb-4 mt-4">
                <h6 class="text-primary font-weight-bold mb-3">Additional Information</h6>
            </div>

            <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        <label for="notes" class="form-label">Notes</label>
                        <textarea name="notes" id="notes" rows="3"
                                  class="form-control @error('notes') is-invalid @enderror"
                                  placeholder="Optional notes about this item">{{ old('notes', $good->notes) }}</textarea>
                        @error('notes')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
            </div>

            <div class="d-flex justify-content-between mt-4 pt-3 border-top">
                <a href="{{ route('warehouse.indirect-goods.catalog.show', $good->id) }}" class="btn btn-secondary">
                    <i class="material-icons mr-1" style="font-size: 16px;">cancel</i>
                    Cancel
                </a>
                <button type="submit" class="btn btn-primary">
                    <i class="material-icons mr-1" style="font-size: 16px;">save</i>
                    Update Indirect Good
                </button>
            </div>
        </form>
    </div>
</div>
@endsection
