@model LittleShop.DTOs.UpdateProductDto

@{
    ViewData["Title"] = "Edit Product";
    var categories = ViewData["Categories"] as IEnumerable<LittleShop.DTOs.CategoryDto>;
    var productId = ViewData["ProductId"];
    var productPhotos = ViewData["ProductPhotos"] as IEnumerable<LittleShop.DTOs.ProductPhotoDto>;
}

<div class="row mb-4">
    <div class="col">
        <h1><i class="fas fa-edit"></i> Edit Product</h1>
    </div>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-body">
                <form method="post" action="@Url.Action("Edit", new { id = productId })">
                    @Html.AntiForgeryToken()
                    @if (ViewData.ModelState[""] != null && ViewData.ModelState[""].Errors.Count > 0)
                    {
                        <div class="alert alert-danger" role="alert">
                            @foreach (var error in ViewData.ModelState[""].Errors)
                            {
                                <div>@error.ErrorMessage</div>
                            }
                        </div>
                    }
                    
                    <div class="mb-3">
                        <label for="Name" class="form-label">Product Name</label>
                        <input name="Name" id="Name" value="@Model?.Name" class="form-control" required />
                    </div>
                    
                    <div class="mb-3">
                        <label for="Description" class="form-label">Description</label>
                        <textarea name="Description" id="Description" class="form-control" rows="4" required>@Model?.Description</textarea>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label for="Price" class="form-label">Price (£)</label>
                                <input name="Price" id="Price" value="@Model?.Price" type="number" step="0.01" class="form-control" required />
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label for="StockQuantity" class="form-label">Stock Quantity</label>
                                <input name="StockQuantity" id="StockQuantity" value="@Model?.StockQuantity" type="number" min="0" class="form-control" required />
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label for="CategoryId" class="form-label">Category</label>
                                <select name="CategoryId" id="CategoryId" class="form-select" required>
                                    <option value="">Select a category</option>
                                    @if (categories != null)
                                    {
                                        @foreach (var category in categories)
                                        {
                                            <option value="@category.Id" selected="@(Model?.CategoryId == category.Id)">@category.Name</option>
                                        }
                                    }
                                </select>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="Weight" class="form-label">Weight/Volume</label>
                                <input name="Weight" id="Weight" value="@Model?.Weight" type="number" step="0.01" class="form-control" required />
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="WeightUnit" class="form-label">Unit</label>
                                <select name="WeightUnit" id="WeightUnit" class="form-select">
                                    <option value="0" selected="@(Model?.WeightUnit == LittleShop.Enums.ProductWeightUnit.Unit)">Unit</option>
                                    <option value="1" selected="@(Model?.WeightUnit == LittleShop.Enums.ProductWeightUnit.Micrograms)">Micrograms</option>
                                    <option value="2" selected="@(Model?.WeightUnit == LittleShop.Enums.ProductWeightUnit.Grams)">Grams</option>
                                    <option value="3" selected="@(Model?.WeightUnit == LittleShop.Enums.ProductWeightUnit.Ounces)">Ounces</option>
                                    <option value="4" selected="@(Model?.WeightUnit == LittleShop.Enums.ProductWeightUnit.Pounds)">Pounds</option>
                                    <option value="5" selected="@(Model?.WeightUnit == LittleShop.Enums.ProductWeightUnit.Millilitres)">Millilitres</option>
                                    <option value="6" selected="@(Model?.WeightUnit == LittleShop.Enums.ProductWeightUnit.Litres)">Litres</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <div class="form-check">
                            <input name="IsActive" type="checkbox" class="form-check-input" checked="@(Model?.IsActive == true)" value="true" />
                            <input name="IsActive" type="hidden" value="false" />
                            <label for="IsActive" class="form-check-label">Active</label>
                        </div>
                    </div>
                    
                    <div class="d-flex justify-content-between">
                        <a href="@Url.Action("Index")" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> Back to Products
                        </a>
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save"></i> Save Changes
                        </button>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- Photo Upload Section -->
        <div class="card mt-4">
            <div class="card-header">
                <h5><i class="fas fa-camera"></i> Product Photos</h5>
            </div>
            <div class="card-body">
                <form method="post" action="@Url.Action("UploadPhoto", new { id = productId })" enctype="multipart/form-data">
                    @Html.AntiForgeryToken()
                    <div class="row">
                        <div class="col-md-8">
                            <div class="mb-3">
                                <label for="file" class="form-label">Choose Photo</label>
                                <input name="file" id="file" type="file" class="form-control" accept="image/*" required />
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label for="altText" class="form-label">Alt Text</label>
                                <input name="altText" id="altText" class="form-control" placeholder="Image description" />
                            </div>
                        </div>
                    </div>
                    <button type="submit" class="btn btn-primary" id="upload-photo-btn">
                        <i class="fas fa-upload"></i> Upload Photo
                    </button>
                </form>
                
                @if (productPhotos != null && productPhotos.Any())
                {
                    <hr>
                    <h6><i class="fas fa-images"></i> Current Photos</h6>
                    <div class="row">
                        @foreach (var photo in productPhotos)
                        {
                            <div class="col-md-6 col-lg-4 mb-3">
                                <div class="card">
                                    <img src="@photo.FilePath" class="card-img-top" style="height: 150px; object-fit: cover;" alt="@photo.AltText">
                                    <div class="card-body p-2">
                                        <small class="text-muted">@photo.FileName</small>
                                        <form method="post" action="@Url.Action("DeletePhoto", new { id = productId, photoId = photo.Id })" class="mt-1">
                                            @Html.AntiForgeryToken()
                                            <button type="submit" class="btn btn-outline-danger btn-sm w-100" 
                                                    onclick="return confirm('Delete this photo?')">
                                                <i class="fas fa-trash"></i> Delete
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        }
                    </div>
                }
                else
                {
                    <div class="text-muted text-center py-3">
                        <i class="fas fa-camera fa-2x mb-2"></i>
                        <p>No photos uploaded yet. Upload your first photo above.</p>
                    </div>
                }
            </div>
        </div>
    </div>
    
    <div class="col-md-4">
        <div class="card">
            <div class="card-header">
                <h5><i class="fas fa-info-circle"></i> Product Information</h5>
            </div>
            <div class="card-body">
                <ul class="list-unstyled">
                    <li><strong>Name:</strong> Product identifier</li>
                    <li><strong>Description:</strong> Supports Unicode and emojis</li>
                    <li><strong>Price:</strong> Base price in GBP</li>
                    <li><strong>Weight/Volume:</strong> For shipping calculations</li>
                    <li><strong>Category:</strong> Product organization</li>
                    <li><strong>Status:</strong> Active products appear in catalog</li>
                </ul>
            </div>
        </div>
    </div>
</div>

@section Scripts {
    <script>
    // Photo upload enhancement
    document.addEventListener('DOMContentLoaded', function() {
        const fileInput = document.getElementById('file');
        const uploadBtn = document.getElementById('upload-photo-btn');
        const uploadForm = uploadBtn.closest('form');
        
        // Preview selected file
        fileInput.addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                console.log('Photo selected:', file.name, file.size, 'bytes');
                
                // Validate file type
                if (!file.type.startsWith('image/')) {
                    alert('Please select an image file (JPG, PNG, GIF, etc.)');
                    fileInput.value = '';
                    return;
                }
                
                // Validate file size (max 5MB)
                if (file.size > 5 * 1024 * 1024) {
                    alert('File too large. Please select an image smaller than 5MB.');
                    fileInput.value = '';
                    return;
                }
                
                uploadBtn.classList.remove('btn-primary');
                uploadBtn.classList.add('btn-success');
                uploadBtn.innerHTML = '<i class="fas fa-check"></i> Ready to Upload';
            } else {
                uploadBtn.classList.remove('btn-success');
                uploadBtn.classList.add('btn-primary');
                uploadBtn.innerHTML = '<i class="fas fa-upload"></i> Upload Photo';
            }
        });
        
        // Upload progress feedback
        uploadForm.addEventListener('submit', function(e) {
            if (!fileInput.files[0]) {
                e.preventDefault();
                alert('Please select a photo first');
                return;
            }
            
            uploadBtn.disabled = true;
            uploadBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Uploading...';
        });
    });
    </script>
}