{% extends "base.html" %}

{% block content %}
    <h1>Manage Clients</h1>
    <a href="{{ url_for('admin.create_client') }}" class="btn btn-primary mb-3">Add New Client</a>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Date of Birth</th>
                <th>Gender</th>
                <th>Assigned Consultant</th>
                <th>Status</th>
                <th>Actions</th>
                <th>Assign Assessments</th>
            </tr>
        </thead>
        <tbody>
            {% for client in clients %}
            <tr>
                <td>{{ client.name }}</td>
                <td>{{ client.email }}</td>
                <td>{{ client.date_of_birth.strftime('%Y-%m-%d') }}</td>
                <td>{{ client.gender }}</td>
                <td>{{ client.referred_by.username if client.referred_by else 'Unassigned' }}</td>
                <td>
                    {% if client.is_active %}
                        <span class="badge badge-success">Active</span>
                    {% else %}
                        <span class="badge badge-danger">Suspended</span>
                    {% endif %}
                </td>
                <td>
                    <a href="{{ url_for('admin.edit_client', client_id=client.id) }}" class="btn btn-sm btn-warning">Edit</a>
                    {% if client.is_active %}
                        <form action="{{ url_for('admin.suspend_client', client_id=client.id) }}" method="POST" style="display:inline;">
                            <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to suspend this client?');">Suspend</button>
                        </form>
                    {% else %}
                        <form action="{{ url_for('admin.activate_client', client_id=client.id) }}" method="POST" style="display:inline;">
                            <button type="submit" class="btn btn-sm btn-success" onclick="return confirm('Are you sure you want to activate this client?');">Activate</button>
                        </form>
                    {% endif %}
                    <form action="{{ url_for('admin.delete_client', client_id=client.id) }}" method="POST" style="display:inline;">
                        <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this client? This action cannot be undone.');">Delete</button>
                    </form>
                </td>
                <td>
                    <form action="{{ url_for('admin.assign_multiple_assessments_to_client', client_id=client.id) }}" method="POST" id="assessmentForm{{ client.id }}">
                        <div class="form-group">
                            {% for assessment in assessments %}
                            <div class="form-check">
                                <input class="form-check-input assessment-checkbox" type="checkbox" name="assessment_ids" value="{{ assessment.id }}" id="assessment{{ assessment.id }}_client{{ client.id }}">
                                <label class="form-check-label" for="assessment{{ assessment.id }}_client{{ client.id }}">
                                    {{ assessment.title }}
                                </label>
                            </div>
                            {% endfor %}
                        </div>
                        <button type="submit" name="action" value="email" class="btn btn-sm btn-primary">Assign & Send Email</button>
                        <button type="submit" name="action" value="link" class="btn btn-sm btn-secondary">Assign & Generate Link</button>
                    </form>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
{% endblock %}

{% block scripts %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Add validation for all assessment forms
    const forms = document.querySelectorAll('form[id^="assessmentForm"]');
    
    forms.forEach(function(form) {
        form.addEventListener('submit', function(e) {
            const checkboxes = form.querySelectorAll('input[name="assessment_ids"]:checked');
            
            if (checkboxes.length === 0) {
                e.preventDefault();
                alert('Please select at least one assessment to assign.');
                return false;
            }
        });
    });
});
</script>
{% endblock %}
