@extends('layouts.app')

@section('title', 'Edit Model')

@section('actions')
<a href="{{ route('settings.models.index') }}" class="btn btn-secondary">
    <i class="fas fa-arrow-left"></i> Back to Models
</a>
@endsection

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-edit me-2 text-primary"></i>Edit Model: {{ $model->name }}</h5>
                </div>

                @if(session('error'))
                <div class="alert alert-danger m-3">
                    <i class="fas fa-exclamation-circle me-2"></i> {{ session('error') }}
                </div>
                @endif

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

                <div class="card-body">
                    <form action="{{ route('settings.models.update', $model) }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        @method('PUT')

                        <div class="row">
                            <div class="col-md-8">
                                <!-- Basic Information -->
                                <div class="card mb-3">
                                    <div class="card-header bg-light">
                                        <h6 class="mb-0"><i class="fas fa-info-circle me-2"></i>Basic Information</h6>
                                    </div>
                                    <div class="card-body">
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <label for="name" class="form-label">Model Name <span class="text-danger">*</span></label>
                                                <input type="text" name="name" id="name" 
                                                       class="form-control @error('name') is-invalid @enderror" 
                                                       value="{{ old('name', $model->name) }}" required>
                                                @error('name')
                                                <div class="invalid-feedback">{{ $message }}</div>
                                                @enderror
                                            </div>

                                            <div class="col-md-6 mb-3">
                                                <label for="code" class="form-label">Model Code <span class="text-danger">*</span></label>
                                                <input type="text" name="code" id="code" 
                                                       class="form-control @error('code') is-invalid @enderror" 
                                                       value="{{ old('code', $model->code) }}" required>
                                                <small class="text-muted">Unique code for the model</small>
                                                @error('code')
                                                <div class="invalid-feedback">{{ $message }}</div>
                                                @enderror
                                            </div>

                                            <div class="col-md-6 mb-3">
                                                <label for="brand_id" class="form-label">Brand <span class="text-danger">*</span></label>
                                                <select name="brand_id" id="brand_id" 
                                                        class="form-select @error('brand_id') is-invalid @enderror" required>
                                                    <option value="">Select Brand</option>
                                                    @foreach($brands as $brand)
                                                    <option value="{{ $brand->id }}" {{ old('brand_id', $model->brand_id) == $brand->id ? 'selected' : '' }}>
                                                        {{ $brand->name }}
                                                    </option>
                                                    @endforeach
                                                </select>
                                                @error('brand_id')
                                                <div class="invalid-feedback">{{ $message }}</div>
                                                @enderror
                                            </div>

                                            <div class="col-md-6 mb-3">
                                                <label for="category_id" class="form-label">Category <span class="text-danger">*</span></label>
                                                <select name="category_id" id="category_id" 
                                                        class="form-select @error('category_id') is-invalid @enderror" required>
                                                    <option value="">Select Category</option>
                                                    @foreach($categories as $category)
                                                    <option value="{{ $category->id }}" {{ old('category_id', $model->category_id) == $category->id ? 'selected' : '' }}>
                                                        {{ $category->name }}
                                                    </option>
                                                    @endforeach
                                                </select>
                                                @error('category_id')
                                                <div class="invalid-feedback">{{ $message }}</div>
                                                @enderror
                                            </div>

                                            <div class="col-md-12 mb-3">
                                                <label for="specifications" class="form-label">Specifications</label>
                                                <textarea name="specifications" id="specifications" 
                                                          class="form-control @error('specifications') is-invalid @enderror" 
                                                          rows="4">{{ old('specifications', $model->specifications) }}</textarea>
                                                <small class="text-muted">Enter specifications in JSON format or plain text</small>
                                                @error('specifications')
                                                <div class="invalid-feedback">{{ $message }}</div>
                                                @enderror
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="col-md-4">
                                <!-- Status -->
                                <div class="card mb-3">
                                    <div class="card-header bg-light">
                                        <h6 class="mb-0"><i class="fas fa-toggle-on me-2"></i>Status</h6>
                                    </div>
                                    <div class="card-body">
                                        <div class="mb-3">
                                            <div class="form-check form-switch">
                                                <input type="checkbox" name="status" id="status" 
                                                       class="form-check-input" value="1" {{ old('status', $model->status) ? 'checked' : '' }}>
                                                <label class="form-check-label" for="status">Active</label>
                                            </div>
                                            <small class="text-muted">Inactive models won't be available for products</small>
                                        </div>
                                    </div>
                                </div>

                                <!-- Model Image -->
                                <div class="card mb-3">
                                    <div class="card-header bg-light">
                                        <h6 class="mb-0"><i class="fas fa-image me-2"></i>Model Image</h6>
                                    </div>
                                    <div class="card-body">
                                        @if($model->image)
                                        <div class="text-center mb-3">
                                            <img src="{{ asset('storage/models/'.$model->image) }}" 
                                                 alt="{{ $model->name }}" class="img-fluid rounded" style="max-height: 120px;">
                                            <div class="mt-2">
                                                <small class="text-muted">Current image</small>
                                            </div>
                                        </div>
                                        @endif
                                        
                                        <div class="mb-3">
                                            <label for="image" class="form-label">Update Image</label>
                                            <input type="file" name="image" id="image" 
                                                   class="form-control @error('image') is-invalid @enderror" 
                                                   accept="image/*">
                                            <small class="text-muted">Max size: 2MB. Allowed: jpeg, png, jpg, gif</small>
                                            @error('image')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                            @enderror
                                        </div>
                                        <div id="image-preview" class="text-center mt-2" style="display: none;">
                                            <img src="#" alt="Preview" class="img-fluid rounded" style="max-height: 120px;">
                                        </div>
                                    </div>
                                </div>

                                <!-- Products Count -->
                                <div class="card mb-3">
                                    <div class="card-header bg-light">
                                        <h6 class="mb-0"><i class="fas fa-box me-2"></i>Products</h6>
                                    </div>
                                    <div class="card-body">
                                        <div class="text-center">
                                            <h3>{{ $model->products_count ?? $model->products()->count() }}</h3>
                                            <p class="text-muted">Products using this model</p>
                                            
                                            @if(($model->products_count ?? $model->products()->count()) > 0)
                                            <div class="alert alert-warning mt-2">
                                                <i class="fas fa-exclamation-triangle me-2"></i>
                                                This model is used by products. Changing it may affect those products.
                                            </div>
                                            @endif
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="text-center mt-4">
                            <button type="submit" class="btn btn-primary px-5">
                                <i class="fas fa-save me-2"></i>Update Model
                            </button>
                            <a href="{{ route('settings.models.index') }}" class="btn btn-secondary px-5 ms-2">
                                <i class="fas fa-times me-2"></i>Cancel
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
$(document).ready(function() {
    // Image preview
    $('#image').change(function() {
        const file = this.files[0];
        if (file) {
            let reader = new FileReader();
            reader.onload = function(event) {
                $('#image-preview').show();
                $('#image-preview img').attr('src', event.target.result);
            }
            reader.readAsDataURL(file);
        } else {
            $('#image-preview').hide();
        }
    });

    // Auto-generate code from name (optional)
    $('#name').on('input', function() {
        // You can add auto-generation logic here if needed
    });

    // Form validation
    $('form').on('submit', function() {
        // Add any custom validation here
        return true;
    });
});
</script>
@endsection