# Complaint/Inquiry Form Back Link Fix

## Problem

The "Back to Complaints" and "Back to Inquiries" links in the create forms were always pointing to the generic complaint/inquiry list views (`complaints:complaint_list` and `complaints:inquiry_list`). This caused issues for Source Users who should be redirected to their filtered views instead.

## Solution

Made the back links user-type aware by:
1. Adding `source_user` variable to the template context in both create views
2. Updating the form templates to check if the user is a Source User
3. Redirecting to the appropriate list view based on user type

## Changes Made

### 1. Updated Complaint Form Template (`templates/complaints/complaint_form.html`)

**Page Header Back Link:**
```django
{% if source_user %}
<a href="{% url 'px_sources:source_user_complaint_list' %}" class="btn btn-outline-secondary btn-sm mb-3">
    <i class="bi bi-arrow-left me-1"></i> {{ _("Back to My Complaints")}}
</a>
{% else %}
<a href="{% url 'complaints:complaint_list' %}" class="btn btn-outline-secondary btn-sm mb-3">
    <i class="bi bi-arrow-left me-1"></i> {{ _("Back to Complaints")}}
</a>
{% endif %}
```

**Cancel Button:**
```django
{% if source_user %}
<a href="{% url 'px_sources:source_user_complaint_list' %}" class="btn btn-outline-secondary">
    <i class="bi bi-x-circle me-2"></i>{{ _("Cancel") }}
</a>
{% else %}
<a href="{% url 'complaints:complaint_list' %}" class="btn btn-outline-secondary">
    <i class="bi bi-x-circle me-2"></i>{{ _("Cancel") }}
</a>
{% endif %}
```

### 2. Updated Inquiry Form Template (`templates/complaints/inquiry_form.html`)

**Page Header Back Link:**
```django
{% if source_user %}
<a href="{% url 'px_sources:source_user_inquiry_list' %}" class="btn btn-outline-secondary btn-sm mb-3">
    <i class="bi bi-arrow-left me-1"></i> {{ _("Back to My Inquiries")}}
</a>
{% else %}
<a href="{% url 'complaints:inquiry_list' %}" class="btn btn-outline-secondary btn-sm mb-3">
    <i class="bi bi-arrow-left me-1"></i> {{ _("Back to Inquiries")}}
</a>
{% endif %}
```

**Cancel Button:**
```django
{% if source_user %}
<a href="{% url 'px_sources:source_user_inquiry_list' %}" class="btn btn-outline-secondary">
    <i class="bi bi-x-circle me-2"></i>{{ _("Cancel") }}
</a>
{% else %}
<a href="{% url 'complaints:inquiry_list' %}" class="btn btn-outline-secondary">
    <i class="bi bi-x-circle me-2"></i>{{ _("Cancel") }}
</a>
{% endif %}
```

### 3. Updated Complaint Create View (`apps/complaints/ui_views.py`)

```python
@login_required
@require_http_methods(["GET", "POST"])
def complaint_create(request):
    """Create new complaint with AI-powered classification"""
    # Determine base layout based on user type
    from apps.px_sources.models import SourceUser
    source_user = SourceUser.objects.filter(user=request.user).first()
    base_layout = 'layouts/source_user_base.html' if source_user else 'layouts/base.html'
    
    # ... form handling code ...
    
    context = {
        'hospitals': hospitals,
        'base_layout': base_layout,
        'source_user': source_user,  # Added to context
    }
```

### 4. Updated Inquiry Create View (`apps/complaints/ui_views.py`)

```python
@login_required
@require_http_methods(["GET", "POST"])
def inquiry_create(request):
    """Create new inquiry"""
    from .models import Inquiry
    from apps.organizations.models import Patient
    
    # Determine base layout based on user type
    from apps.px_sources.models import SourceUser
    source_user = SourceUser.objects.filter(user=request.user).first()
    base_layout = 'layouts/source_user_base.html' if source_user else 'layouts/base.html'
    
    # ... form handling code ...
    
    context = {
        'hospitals': hospitals,
        'base_layout': base_layout,
        'source_user': source_user,  # Added to context
    }
```

## Behavior

### For Source Users:
- **Back to My Complaints** → Redirects to `/px-sources/complaints/` (filtered view)
- **Back to My Inquiries** → Redirects to `/px-sources/inquiries/` (filtered view)
- Uses Source User base layout

### For Regular Users (PX Admin, Hospital Admin, etc.):
- **Back to Complaints** → Redirects to `/complaints/list/` (generic list)
- **Back to Inquiries** → Redirects to `/complaints/inquiries/` (generic list)
- Uses regular base layout

## Testing

### Test as Source User:
1. Login as a Source User
2. Navigate to "My Complaints" or "My Inquiries"
3. Click "Create Complaint" or "Create Inquiry"
4. Fill in form details (or leave blank)
5. Click "Cancel" or "Back to My Complaints/Inquiries" link
6. Verify you are redirected to the filtered view (`/px-sources/complaints/` or `/px-sources/inquiries/`)

### Test as Regular User:
1. Login as a PX Admin or Hospital Admin
2. Navigate to Complaints or Inquiries list
3. Click "Create Complaint" or "Create Inquiry"
4. Click "Cancel" or "Back to Complaints/Inquiries" link
5. Verify you are redirected to the generic list (`/complaints/list/` or `/complaints/inquiries/`)

## Files Modified

1. `templates/complaints/complaint_form.html` - Updated back links
2. `templates/complaints/inquiry_form.html` - Updated back links
3. `apps/complaints/ui_views.py` - Added `source_user` to context in both create views

## Related Documentation

- `SOURCE_USER_FILTERED_VIEWS_IMPLEMENTATION.md` - Documentation for Source User filtered views
- `SOURCE_USER_BASE_LAYOUT_IMPLEMENTATION.md` - Documentation for Source User base layout
- `SOURCE_USER_LOGIN_REDIRECT_IMPLEMENTATION.md` - Documentation for Source User login redirect

## Summary

This fix ensures that both Source Users and regular users have a seamless experience when creating complaints and inquiries. The back links now intelligently redirect users to the appropriate list view based on their user type, maintaining data isolation for Source Users while providing full access for administrators.