@extends('layouts.app')

@section('title', 'Edit Warehouse Bin')

@section('content')
<div class="page-content">
    <div class="row">
        <div class="col-md-12">
            <div class="page-title-box">
                <div class="page-title-right">
                    <ol class="breadcrumb m-0">
                        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
                        <li class="breadcrumb-item"><a href="{{ route('warehouse-bins.index') }}">Warehouse Bins</a></li>
                        <li class="breadcrumb-item active">Edit Bin: {{ $warehouseBin->code }}</li>
                    </ol>
                </div>
                <h4 class="page-title">Edit Warehouse Bin</h4>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <form action="{{ route('warehouse-bins.update', $warehouseBin) }}" method="POST">
                        @csrf
                        @method('PUT')

                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label">Bin Code <span class="text-danger">*</span></label>
                                <input type="text" name="code" class="form-control @error('code') is-invalid @enderror"
                                       value="{{ old('code', $warehouseBin->code) }}" required maxlength="255">
                                @error('code')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                                <small class="form-text text-muted">Unique code for this bin (e.g., A01, B02)</small>
                            </div>

                            <div class="col-md-6">
                                <label class="form-label">Warehouse <span class="text-danger">*</span></label>
                                <select name="warehouse_id" class="form-select @error('warehouse_id') is-invalid @enderror" required>
                                    <option value="">Select Warehouse</option>
                                    @foreach($warehouses as $warehouse)
                                        <option value="{{ $warehouse->id }}" {{ old('warehouse_id', $warehouseBin->warehouse_id) == $warehouse->id ? 'selected' : '' }}>
                                            {{ $warehouse->name }} ({{ $warehouse->code }})
                                        </option>
                                    @endforeach
                                </select>
                                @error('warehouse_id')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="col-md-6">
                                <label class="form-label">Bin Type <span class="text-danger">*</span></label>
                                <select name="type" class="form-select @error('type') is-invalid @enderror" required>
                                    <option value="">Select Type</option>
                                    <option value="receipt" {{ old('type', $warehouseBin->type) == 'receipt' ? 'selected' : '' }}>Receipt</option>
                                    <option value="general" {{ old('type', $warehouseBin->type) == 'general' ? 'selected' : '' }}>General</option>
                                    <option value="dispatch" {{ old('type', $warehouseBin->type) == 'dispatch' ? 'selected' : '' }}>Dispatch</option>
                                    <option value="quality" {{ old('type', $warehouseBin->type) == 'quality' ? 'selected' : '' }}>Quality</option>
                                    <option value="damaged" {{ old('type', $warehouseBin->type) == 'damaged' ? 'selected' : '' }}>Damaged</option>
                                </select>
                                @error('type')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="col-md-6">
                                <div class="form-check mt-4">
                                    <input type="hidden" name="is_active" value="0">
                                    <input type="checkbox" name="is_active" class="form-check-input @error('is_active') is-invalid @enderror"
                                           value="1" {{ old('is_active', $warehouseBin->is_active) ? 'checked' : '' }}>
                                    <label class="form-check-label">Active Bin</label>
                                    <small class="form-text text-muted d-block">Inactive bins cannot be used for stock operations.</small>
                                    @error('is_active')
                                        <div class="invalid-feedback d-block">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>

                            <div class="col-12">
                                <div class="d-flex gap-2">
                                    <button type="submit" class="btn btn-primary">Update Bin</button>
                                    <a href="{{ route('warehouse-bins.index') }}" class="btn btn-secondary">Cancel</a>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
