{% extends "layouts/base.html" %}
{% load i18n %}
{% load static %}

{% block title %}PX Action Center - PX360{% endblock %}

{% block extra_css %}
<style>
    .view-tabs {
        background: #fff;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 10px;
        margin-bottom: 20px;
    }
    .view-tabs .nav-link {
        color: #6c757d;
        border-radius: 6px;
        padding: 8px 16px;
        margin: 0 4px;
    }
    .view-tabs .nav-link.active {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
    }
    .filter-panel {
        background: #f8f9fa;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
    }
    .filter-panel.collapsed {
        padding: 10px 20px;
    }
    .filter-panel.collapsed .filter-body {
        display: none;
    }
    .table-toolbar {
        background: #fff;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 15px;
        margin-bottom: 15px;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .status-badge {
        padding: 4px 12px;
        border-radius: 12px;
        font-size: 0.85rem;
        font-weight: 500;
    }
    .status-open { background: #e3f2fd; color: #1976d2; }
    .status-in_progress { background: #fff3e0; color: #f57c00; }
    .status-pending_approval { background: #fff9c4; color: #f57f17; }
    .status-approved { background: #e8f5e9; color: #388e3c; }
    .status-closed { background: #f5f5f5; color: #616161; }
    .status-cancelled { background: #ffebee; color: #d32f2f; }
    
    .severity-badge {
        padding: 4px 12px;
        border-radius: 12px;
        font-size: 0.85rem;
        font-weight: 500;
    }
    .severity-low { background: #e8f5e9; color: #388e3c; }
    .severity-medium { background: #fff3e0; color: #f57c00; }
    .severity-high { background: #ffebee; color: #d32f2f; }
    .severity-critical { background: #880e4f; color: #fff; }
    
    .source-badge {
        padding: 4px 10px;
        border-radius: 10px;
        font-size: 0.75rem;
        font-weight: 500;
    }
    .source-survey { background: #e1f5fe; color: #01579b; }
    .source-complaint { background: #fce4ec; color: #880e4f; }
    .source-social_media { background: #f3e5f5; color: #4a148c; }
    .source-call_center { background: #e8f5e9; color: #1b5e20; }
    
    .overdue-badge {
        background: #d32f2f;
        color: white;
        padding: 2px 8px;
        border-radius: 10px;
        font-size: 0.75rem;
        font-weight: 600;
        margin-left: 8px;
    }
    
    .escalation-badge {
        background: #ff6f00;
        color: white;
        padding: 2px 8px;
        border-radius: 10px;
        font-size: 0.75rem;
        font-weight: 600;
        margin-left: 8px;
    }
    
    .action-row:hover {
        background: #f8f9fa;
        cursor: pointer;
    }
    
    .stat-card {
        border-left: 4px solid;
        transition: transform 0.2s;
    }
    .stat-card:hover {
        transform: translateY(-2px);
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">
                <i class="bi bi-lightning-fill text-warning me-2"></i>
                PX Action Center
            </h2>
            <p class="text-muted mb-0">Track and manage patient experience improvement actions</p>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-2">
            <div class="card stat-card border-primary">
                <div class="card-body py-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1 small">{% trans "Total" %}</h6>
                            <h4 class="mb-0">{{ stats.total }}</h4>
                        </div>
                        <i class="bi bi-list-ul text-primary" style="font-size: 1.5rem;"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card stat-card border-info">
                <div class="card-body py-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1 small">{% trans "Open" %}</h6>
                            <h4 class="mb-0">{{ stats.open }}</h4>
                        </div>
                        <i class="bi bi-folder2-open text-info" style="font-size: 1.5rem;"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card stat-card border-warning">
                <div class="card-body py-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1 small">{% trans "In Progress" %}</h6>
                            <h4 class="mb-0">{{ stats.in_progress }}</h4>
                        </div>
                        <i class="bi bi-hourglass-split text-warning" style="font-size: 1.5rem;"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card stat-card border-danger">
                <div class="card-body py-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1 small">{% trans "Overdue" %}</h6>
                            <h4 class="mb-0 text-danger">{{ stats.overdue }}</h4>
                        </div>
                        <i class="bi bi-exclamation-triangle-fill text-danger" style="font-size: 1.5rem;"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card stat-card border-success">
                <div class="card-body py-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1 small">{% trans "Pending Approval" %}</h6>
                            <h4 class="mb-0">{{ stats.pending_approval }}</h4>
                        </div>
                        <i class="bi bi-check-circle text-success" style="font-size: 1.5rem;"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card stat-card border-secondary">
                <div class="card-body py-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1 small">{% trans "My Actions" %}</h6>
                            <h4 class="mb-0">{{ stats.my_actions }}</h4>
                        </div>
                        <i class="bi bi-person-check text-secondary" style="font-size: 1.5rem;"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- View Tabs -->
    <div class="view-tabs">
        <ul class="nav nav-pills">
            <li class="nav-item">
                <a class="nav-link {% if current_view == 'all' %}active{% endif %}" 
                   href="?view=all">All Actions</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if current_view == 'my_actions' %}active{% endif %}" 
                   href="?view=my_actions">My Actions</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if current_view == 'overdue' %}active{% endif %}" 
                   href="?view=overdue">Overdue</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if current_view == 'escalated' %}active{% endif %}" 
                   href="?view=escalated">Escalated</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if current_view == 'pending_approval' %}active{% endif %}" 
                   href="?view=pending_approval">Pending Approval</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if current_view == 'from_surveys' %}active{% endif %}" 
                   href="?view=from_surveys">From Surveys</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if current_view == 'from_complaints' %}active{% endif %}" 
                   href="?view=from_complaints">From Complaints</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if current_view == 'from_social' %}active{% endif %}" 
                   href="?view=from_social">From Social</a>
            </li>
        </ul>
    </div>

    <!-- Filter Panel -->
    <div class="filter-panel" id="filterPanel">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h5 class="mb-0">
                <i class="bi bi-funnel me-2"></i>Filters
            </h5>
            <button class="btn btn-sm btn-outline-secondary" onclick="toggleFilters()">
                <i class="bi bi-chevron-up" id="filterToggleIcon"></i>
            </button>
        </div>
        
        <div class="filter-body">
            <form method="get" action="{% url 'actions:action_list' %}" id="filterForm">
                <input type="hidden" name="view" value="{{ current_view }}">
                <div class="row g-3">
                    <!-- Search -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Search" %}</label>
                        <input type="text" class="form-control" name="search" 
                               placeholder="{% trans 'Title, description...' %}" 
                               value="{{ filters.search }}">
                    </div>
                    
                    <!-- Status -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Status" %}</label>
                        <select class="form-select" name="status">
                            <option value="">All Statuses</option>
                            {% for value, label in status_choices %}
                            <option value="{{ value }}" {% if filters.status == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <!-- Severity -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Severity" %}</label>
                        <select class="form-select" name="severity">
                            <option value="">All Severities</option>
                            <option value="low" {% if filters.severity == 'low' %}selected{% endif %}>Low</option>
                            <option value="medium" {% if filters.severity == 'medium' %}selected{% endif %}>Medium</option>
                            <option value="high" {% if filters.severity == 'high' %}selected{% endif %}>High</option>
                            <option value="critical" {% if filters.severity == 'critical' %}selected{% endif %}>Critical</option>
                        </select>
                    </div>
                    
                    <!-- Priority -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Priority" %}</label>
                        <select class="form-select" name="priority">
                            <option value="">All Priorities</option>
                            <option value="low" {% if filters.priority == 'low' %}selected{% endif %}>Low</option>
                            <option value="medium" {% if filters.priority == 'medium' %}selected{% endif %}>Medium</option>
                            <option value="high" {% if filters.priority == 'high' %}selected{% endif %}>High</option>
                            <option value="urgent" {% if filters.priority == 'urgent' %}selected{% endif %}>Urgent</option>
                        </select>
                    </div>
                    
                    <!-- Category -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Category" %}</label>
                        <select class="form-select" name="category">
                            <option value="">All Categories</option>
                            <option value="clinical_quality" {% if filters.category == 'clinical_quality' %}selected{% endif %}>Clinical Quality</option>
                            <option value="patient_safety" {% if filters.category == 'patient_safety' %}selected{% endif %}>Patient Safety</option>
                            <option value="service_quality" {% if filters.category == 'service_quality' %}selected{% endif %}>Service Quality</option>
                            <option value="staff_behavior" {% if filters.category == 'staff_behavior' %}selected{% endif %}>Staff Behavior</option>
                            <option value="facility" {% if filters.category == 'facility' %}selected{% endif %}>Facility & Environment</option>
                            <option value="process_improvement" {% if filters.category == 'process_improvement' %}selected{% endif %}>Process Improvement</option>
                            <option value="other" {% if filters.category == 'other' %}selected{% endif %}>Other</option>
                        </select>
                    </div>
                    
                    <!-- Source Type -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Source" %}</label>
                        <select class="form-select" name="source_type">
                            <option value="">All Sources</option>
                            {% for value, label in source_choices %}
                            <option value="{{ value }}" {% if filters.source_type == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <!-- Hospital -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Hospital" %}</label>
                        <select class="form-select" name="hospital">
                            <option value="">All Hospitals</option>
                            {% for hospital in hospitals %}
                            <option value="{{ hospital.id }}" {% if filters.hospital == hospital.id|stringformat:"s" %}selected{% endif %}>
                                {{ hospital.name_en }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <!-- Department -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Department" %}</label>
                        <select class="form-select" name="department">
                            <option value="">All Departments</option>
                            {% for dept in departments %}
                            <option value="{{ dept.id }}" {% if filters.department == dept.id|stringformat:"s" %}selected{% endif %}>
                                {{ dept.name_en }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <!-- Assigned To -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Assigned To" %}</label>
                        <select class="form-select" name="assigned_to">
                            <option value="">All Users</option>
                            {% for user_obj in assignable_users %}
                            <option value="{{ user_obj.id }}" {% if filters.assigned_to == user_obj.id|stringformat:"s" %}selected{% endif %}>
                                {{ user_obj.get_full_name }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <!-- Date Range -->
                    <div class="col-md-3">
                        <label class="form-label">{% trans "Date From" %}</label>
                        <input type="date" class="form-control" name="date_from" value="{{ filters.date_from }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">{% trans "Date To" %}</label>
                        <input type="date" class="form-control" name="date_to" value="{{ filters.date_to }}">
                    </div>
                </div>
                
                <div class="mt-3 d-flex gap-2">
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-search me-1"></i> Apply Filters
                    </button>
                    <a href="{% url 'actions:action_list' %}?view={{ current_view }}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-1"></i> Clear
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Table Toolbar -->
    <div class="table-toolbar">
        <div>
            <span class="text-muted">
                Showing {{ page_obj.start_index }} to {{ page_obj.end_index }} of {{ page_obj.paginator.count }} actions
            </span>
        </div>
        <div class="d-flex gap-2">
            <button class="btn btn-sm btn-outline-primary" onclick="exportData('csv')">
                <i class="bi bi-file-earmark-spreadsheet me-1"></i> Export CSV
            </button>
            <button class="btn btn-sm btn-outline-primary" onclick="exportData('excel')">
                <i class="bi bi-file-earmark-excel me-1"></i> Export Excel
            </button>
        </div>
    </div>

    <!-- Actions Table -->
    <div class="card">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th style="width: 50px;">
                                <input type="checkbox" class="form-check-input" id="selectAll">
                            </th>
                            <th>{% trans "ID" %}</th>
                            <th>{% trans "Title" %}</th>
                            <th>{% trans "Source" %}</th>
                            <th>{% trans "Category" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Severity" %}</th>
                            <th>{% trans "Hospital" %}</th>
                            <th>{% trans "Assigned To" %}</th>
                            <th>{% trans "Due Date" %}</th>
                            <th>{% trans "Created" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for action in actions %}
                        <tr class="action-row" onclick="window.location='{% url 'actions:action_detail' action.id %}'">
                            <td onclick="event.stopPropagation();">
                                <input type="checkbox" class="form-check-input action-checkbox" 
                                       value="{{ action.id }}">
                            </td>
                            <td>
                                <small class="text-muted">#{{ action.id|slice:":8" }}</small>
                            </td>
                            <td>
                                <div class="d-flex align-items-center">
                                    {{ action.title|truncatewords:8 }}
                                    {% if action.is_overdue %}
                                    <span class="overdue-badge">OVERDUE</span>
                                    {% endif %}
                                    {% if action.escalation_level > 0 %}
                                    <span class="escalation-badge">L{{ action.escalation_level }}</span>
                                    {% endif %}
                                </div>
                            </td>
                            <td>
                                <span class="source-badge source-{{ action.source_type }}">
                                    {{ action.get_source_type_display }}
                                </span>
                            </td>
                            <td>
                                <span class="badge bg-secondary">{{ action.get_category_display }}</span>
                            </td>
                            <td>
                                <span class="status-badge status-{{ action.status }}">
                                    {{ action.get_status_display }}
                                </span>
                            </td>
                            <td>
                                <span class="severity-badge severity-{{ action.severity }}">
                                    {{ action.get_severity_display }}
                                </span>
                            </td>
                            <td>
                                <small>{{ action.hospital.name_en|truncatewords:3 }}</small>
                            </td>
                            <td>
                                {% if action.assigned_to %}
                                <small>{{ action.assigned_to.get_full_name }}</small>
                                {% else %}
                                <span class="text-muted"><em>Unassigned</em></span>
                                {% endif %}
                            </td>
                            <td>
                                <small class="{% if action.is_overdue %}text-danger fw-bold{% endif %}">
                                    {{ action.due_at|date:"M d, Y H:i" }}
                                </small>
                            </td>
                            <td>
                                <small class="text-muted">{{ action.created_at|date:"M d, Y" }}</small>
                            </td>
                            <td onclick="event.stopPropagation();">
                                <div class="btn-group btn-group-sm">
                                    <a href="{% url 'actions:action_detail' action.id %}" 
                                       class="btn btn-outline-primary" title="{% trans 'View' %}">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="12" class="text-center py-5">
                                <i class="bi bi-inbox" style="font-size: 3rem; color: #ccc;"></i>
                                <p class="text-muted mt-3">No actions found</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <nav aria-label="Actions pagination" class="mt-4">
        <ul class="pagination justify-content-center">
            {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page=1&view={{ current_view }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-double-left"></i>
                </a>
            </li>
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.previous_page_number }}&view={{ current_view }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-left"></i>
                </a>
            </li>
            {% endif %}
            
            {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                <li class="page-item active"><span class="page-link">{{ num }}</span></li>
                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ num }}&view={{ current_view }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                        {{ num }}
                    </a>
                </li>
                {% endif %}
            {% endfor %}
            
            {% if page_obj.has_next %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.next_page_number }}&view={{ current_view }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-right"></i>
                </a>
            </li>
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}&view={{ current_view }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-double-right"></i>
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %}

{% block extra_js %}
<script>
function toggleFilters() {
    const panel = document.getElementById('filterPanel');
    const icon = document.getElementById('filterToggleIcon');
    panel.classList.toggle('collapsed');
    icon.classList.toggle('bi-chevron-up');
    icon.classList.toggle('bi-chevron-down');
}

function exportData(format) {
    const params = new URLSearchParams(window.location.search);
    params.set('export', format);
    window.location.href = '{% url "actions:action_list" %}?' + params.toString();
}

// Select all checkbox
document.getElementById('selectAll')?.addEventListener('change', function() {
    const checkboxes = document.querySelectorAll('.action-checkbox');
    checkboxes.forEach(cb => cb.checked = this.checked);
});
</script>
{% endblock %}
