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

{% block title %}Action #{{ action.id|slice:":8" }} - PX360{% endblock %}

{% block extra_css %}
<style>
    .action-header {
        {#background: linear-gradient(135deg, #616161 0%, #616161 100%);#}
        {#color: white;#}
        padding: 20px;
        border-radius: 8px;
        margin-bottom: 30px;
    }
    .sla-progress-card {
        background: rgba(255,255,255,0.2);
        padding: 20px;
        border-radius: 8px;
    }
    .sla-progress-card.overdue {
        background: #d32f2f;
    }
    .progress-custom {
        height: 30px;
        border-radius: 15px;
        background: rgba(255,255,255,0.3);
    }
    .progress-bar-custom {
        border-radius: 15px;
        font-weight: 600;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .status-badge {
        padding: 6px 16px;
        border-radius: 20px;
        font-size: 0.9rem;
        font-weight: 600;
    }
    .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: 6px 16px;
        border-radius: 20px;
        font-size: 0.9rem;
        font-weight: 600;
    }
    .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: 6px 14px;
        border-radius: 15px;
        font-size: 0.85rem;
        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; }
    
    .timeline {
        position: relative;
        padding-left: 30px;
    }
    .timeline::before {
        content: '';
        position: absolute;
        left: 8px;
        top: 0;
        bottom: 0;
        width: 2px;
        background: #dee2e6;
    }
    .timeline-item {
        position: relative;
        padding-bottom: 30px;
    }
    .timeline-item::before {
        content: '';
        position: absolute;
        left: -26px;
        top: 5px;
        width: 16px;
        height: 16px;
        border-radius: 50%;
        background: #fff;
        border: 3px solid #f57c00;
        z-index: 1;
    }
    .timeline-item.status_change::before {
        border-color: #1976d2;
    }
    .timeline-item.assignment::before {
        border-color: #388e3c;
    }
    .timeline-item.escalation::before {
        border-color: #d32f2f;
    }
    .timeline-item.approval::before {
        border-color: #4caf50;
    }
    .timeline-item.evidence::before {
        border-color: #ff9800;
    }
    
    .evidence-card {
        border-left: 4px solid #ff9800;
        transition: transform 0.2s;
    }
    .evidence-card:hover {
        transform: translateX(5px);
    }
    
    .info-label {
        font-weight: 600;
        color: #6c757d;
        font-size: 0.85rem;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }
    
    .info-value {
        font-size: 1rem;
        color: #212529;
        margin-top: 4px;
    }
    
    .escalation-badge {
        background: #ff6f00;
        color: white;
        padding: 4px 12px;
        border-radius: 12px;
        font-size: 0.85rem;
        font-weight: 600;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Back Button -->
    <div class="mb-3">
        <a href="{% url 'actions:action_list' %}" class="btn btn-outline-secondary btn-sm">
            <i class="bi bi-arrow-left me-1"></i> {% trans "Back to Actions" %}
        </a>
    </div>

    <!-- Action Header -->
    <div class="card p-2">
        <div class="row align-items-center">
            <div class="col-md-8">
                <div class="d-flex align-items-center mb-2">
                    <h3 class="mb-0 me-3">{{ action.title }}</h3>
                    <span class="status-badge status-{{ action.status }}">
                        {{ action.get_status_display }}
                    </span>
                    <span class="severity-badge severity-{{ action.severity }} ms-2">
                        {{ action.get_severity_display }}
                    </span>
                    {% if action.escalation_level > 0 %}
                    <span class="escalation-badge ms-2">
                        <i class="bi bi-arrow-up-circle me-1"></i>Level {{ action.escalation_level }}
                    </span>
                    {% endif %}
                </div>
                <p class="mb-2">
                    <i class="bi bi-hash me-1"></i>
                    <strong>ID:</strong> {{ action.id|slice:":8" }}
                    <span class="mx-2">|</span>
                    <span class="source-badge source-{{ action.source_type }}">
                        {{ action.get_source_type_display }}
                    </span>
                </p>
                <p class="mb-0">
                    <i class="bi bi-hospital me-1"></i>
                    <strong>Hospital:</strong> {{ action.hospital.name_en }}
                    {% if action.department %}
                    <span class="mx-2">|</span>
                    <i class="bi bi-building me-1"></i>
                    <strong>Department:</strong> {{ action.department.name_en }}
                    {% endif %}
                </p>
            </div>
            <div class="col-md-4">
                <div class="sla-progress-card {% if action.is_overdue %}overdue{% endif %}">
                    <div class="mb-2">
                        <i class="bi bi-clock-history me-1"></i>
                        <strong>SLA Progress</strong>
                    </div>
                    <div class="progress progress-custom mb-2">
                        <div class="progress-bar progress-bar-custom {% if action.is_overdue %}bg-danger{% elif sla_progress > 80 %}bg-warning{% else %}bg-success{% endif %}" 
                             style="width: {{ sla_progress }}%">
                            {{ sla_progress }}%
                        </div>
                    </div>
                    <div class="d-flex justify-content-between">
                        <small>Due: {{ action.due_at|date:"M d, Y H:i" }}</small>
                        {% if action.is_overdue %}
                        <small><strong>OVERDUE</strong></small>
                        {% else %}
                        <small>{{ action.due_at|timeuntil }} left</small>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- Main Content -->
        <div class="col-lg-8">
            <!-- Tabs -->
            <ul class="nav nav-tabs mb-3" id="actionTabs" role="tablist">
                <li class="nav-item" role="presentation">
                    <button class="nav-link active" id="details-tab" data-bs-toggle="tab" 
                            data-bs-target="#details" type="button" role="tab">
                        <i class="bi bi-info-circle me-1"></i> {% trans "Details" %}
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="logs-tab" data-bs-toggle="tab" 
                            data-bs-target="#logs" type="button" role="tab">
                        <i class="bi bi-clock-history me-1"></i> Activity Log ({{ logs.count }})
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="evidence-tab" data-bs-toggle="tab" 
                            data-bs-target="#evidence" type="button" role="tab">
                        <i class="bi bi-file-earmark-check me-1"></i> Evidence ({{ evidence_attachments.count }})
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="attachments-tab" data-bs-toggle="tab" 
                            data-bs-target="#attachments" type="button" role="tab">
                        <i class="bi bi-paperclip me-1"></i> Attachments ({{ attachments.count }})
                    </button>
                </li>
            </ul>

            <!-- Tab Content -->
            <div class="tab-content" id="actionTabsContent">
                <!-- Details Tab -->
                <div class="tab-pane fade show active" id="details" role="tabpanel">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title mb-4">{% trans "Action Details" %}</h5>
                            
                            <div class="row mb-3">
                                <div class="col-md-6">
                                    <div class="info-label">Category</div>
                                    <div class="info-value">
                                        <span class="badge bg-secondary">{{ action.get_category_display }}</span>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="info-label">Priority</div>
                                    <div class="info-value">
                                        <span class="badge bg-info">{{ action.get_priority_display }}</span>
                                    </div>
                                </div>
                            </div>
                            
                            <hr>
                            
                            <div class="mb-3">
                                <div class="info-label">Description</div>
                                <div class="info-value mt-2">
                                    <p class="mb-0">{{ action.description|linebreaks }}</p>
                                </div>
                            </div>
                            
                            {% if action.action_plan %}
                            <hr>
                            <div class="mb-3">
                                <div class="info-label">Action Plan</div>
                                <div class="info-value mt-2">
                                    <div class="alert alert-info">
                                        {{ action.action_plan|linebreaks }}
                                    </div>
                                </div>
                            </div>
                            {% endif %}
                            
                            {% if action.outcome %}
                            <hr>
                            <div class="mb-3">
                                <div class="info-label">Outcome</div>
                                <div class="info-value mt-2">
                                    <div class="alert alert-success">
                                        {{ action.outcome|linebreaks }}
                                    </div>
                                </div>
                            </div>
                            {% endif %}
                            
                            <hr>
                            
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="info-label">Created</div>
                                    <div class="info-value">{{ action.created_at|date:"M d, Y H:i" }}</div>
                                </div>
                                <div class="col-md-6">
                                    <div class="info-label">Last Updated</div>
                                    <div class="info-value">{{ action.updated_at|date:"M d, Y H:i" }}</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Activity Log Tab -->
                <div class="tab-pane fade" id="logs" role="tabpanel">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title mb-4">{% trans "Activity Log" %}</h5>
                            
                            {% if logs %}
                            <div class="timeline">
                                {% for log in logs %}
                                <div class="timeline-item {{ log.log_type }}">
                                    <div class="card mb-3">
                                        <div class="card-body">
                                            <div class="d-flex justify-content-between align-items-start mb-2">
                                                <div>
                                                    <span class="badge bg-primary">{{ log.get_log_type_display }}</span>
                                                    {% if log.created_by %}
                                                    <span class="text-muted ms-2">
                                                        by {{ log.created_by.get_full_name }}
                                                    </span>
                                                    {% endif %}
                                                </div>
                                                <small class="text-muted">
                                                    {{ log.created_at|date:"M d, Y H:i" }}
                                                </small>
                                            </div>
                                            <p class="mb-0">{{ log.message }}</p>
                                            {% if log.old_status and log.new_status %}
                                            <div class="mt-2">
                                                <span class="status-badge status-{{ log.old_status }}">
                                                    {{ log.old_status }}
                                                </span>
                                                <i class="bi bi-arrow-right mx-2"></i>
                                                <span class="status-badge status-{{ log.new_status }}">
                                                    {{ log.new_status }}
                                                </span>
                                            </div>
                                            {% endif %}
                                        </div>
                                    </div>
                                </div>
                                {% endfor %}
                            </div>
                            {% else %}
                            <div class="text-center py-5">
                                <i class="bi bi-clock-history" style="font-size: 3rem; color: #ccc;"></i>
                                <p class="text-muted mt-3">No activity log entries yet</p>
                            </div>
                            {% endif %}
                        </div>
                    </div>
                </div>

                <!-- Evidence Tab -->
                <div class="tab-pane fade" id="evidence" role="tabpanel">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title mb-4">{% trans "Evidence Files" %}</h5>
                            
                            {% if evidence_attachments %}
                            <div class="list-group mb-3">
                                {% for evidence in evidence_attachments %}
                                <div class="list-group-item evidence-card">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div>
                                            <i class="bi bi-file-earmark-check text-warning me-2"></i>
                                            <strong>{{ evidence.filename }}</strong>
                                            <span class="badge bg-warning text-dark ms-2">Evidence</span>
                                            <br>
                                            <small class="text-muted">
                                                Uploaded by {{ evidence.uploaded_by.get_full_name }} 
                                                on {{ evidence.created_at|date:"M d, Y H:i" }}
                                                ({{ evidence.file_size|filesizeformat }})
                                            </small>
                                            {% if evidence.description %}
                                            <br>
                                            <small>{{ evidence.description }}</small>
                                            {% endif %}
                                        </div>
                                        <a href="{{ evidence.file.url }}" class="btn btn-sm btn-outline-primary" 
                                           target="_blank" download>
                                            <i class="bi bi-download"></i>
                                        </a>
                                    </div>
                                </div>
                                {% endfor %}
                            </div>
                            {% else %}
                            <div class="text-center py-5">
                                <i class="bi bi-file-earmark-check" style="font-size: 3rem; color: #ccc;"></i>
                                <p class="text-muted mt-3">No evidence files uploaded yet</p>
                                {% if action.requires_approval and action.status != 'closed' %}
                                <p class="text-warning">
                                    <i class="bi bi-exclamation-triangle me-1"></i>
                                    Evidence is required before requesting approval
                                </p>
                                {% endif %}
                            </div>
                            {% endif %}
                        </div>
                    </div>
                </div>

                <!-- Attachments Tab -->
                <div class="tab-pane fade" id="attachments" role="tabpanel">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title mb-4">{% trans "All Attachments" %}</h5>
                            
                            {% if attachments %}
                            <div class="list-group">
                                {% for attachment in attachments %}
                                <div class="list-group-item">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div>
                                            <i class="bi bi-file-earmark me-2"></i>
                                            <strong>{{ attachment.filename }}</strong>
                                            {% if attachment.is_evidence %}
                                            <span class="badge bg-warning text-dark ms-2">Evidence</span>
                                            {% endif %}
                                            <br>
                                            <small class="text-muted">
                                                Uploaded by {{ attachment.uploaded_by.get_full_name }} 
                                                on {{ attachment.created_at|date:"M d, Y H:i" }}
                                                ({{ attachment.file_size|filesizeformat }})
                                            </small>
                                            {% if attachment.description %}
                                            <br>
                                            <small>{{ attachment.description }}</small>
                                            {% endif %}
                                        </div>
                                        <a href="{{ attachment.file.url }}" class="btn btn-sm btn-outline-primary" 
                                           target="_blank" download>
                                            <i class="bi bi-download"></i>
                                        </a>
                                    </div>
                                </div>
                                {% endfor %}
                            </div>
                            {% else %}
                            <div class="text-center py-5">
                                <i class="bi bi-paperclip" style="font-size: 3rem; color: #ccc;"></i>
                                <p class="text-muted mt-3">No attachments</p>
                            </div>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Sidebar Actions -->
        <div class="col-lg-4">
            <!-- Approval Required -->
            {% if action.status == 'pending_approval' and can_approve %}
            <div class="card mb-3 border-success">
                <div class="card-header bg-success text-white">
                    <h6 class="mb-0"><i class="bi bi-check-circle me-2"></i>{% trans "Approval Required" %}</h6>
                </div>
                <div class="card-body">
                    <p class="mb-3">This action is awaiting your approval.</p>
                    <form method="post" action="{% url 'actions:action_approve' action.id %}">
                        {% csrf_token %}
                        <button type="submit" class="btn btn-success w-100">
                            <i class="bi bi-check-circle me-1"></i> Approve Action
                        </button>
                    </form>
                </div>
            </div>
            {% endif %}
            
            <!-- Quick Actions -->
            {% if can_edit %}
            <div class="card mb-3">
                <div class="card-header bg-primary text-white">
                    <h6 class="mb-0"><i class="bi bi-lightning-fill me-2"></i>{% trans "Quick Actions" %}</h6>
                </div>
                <div class="card-body">
                    <!-- Assign -->
                    <form method="post" action="{% url 'actions:action_assign' action.id %}" class="mb-3">
                        {% csrf_token %}
                        <label class="form-label">{% trans "Assign To" %}</label>
                        <div class="input-group">
                            <select name="user_id" class="form-select" required>
                                <option value="">Select user...</option>
                                {% for user_obj in assignable_users %}
                                <option value="{{ user_obj.id }}" 
                                        {% if action.assigned_to and action.assigned_to.id == user_obj.id %}selected{% endif %}>
                                    {{ user_obj.get_full_name }}
                                </option>
                                {% endfor %}
                            </select>
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-person-check"></i>
                            </button>
                        </div>
                    </form>
                    
                    <!-- Change Status -->
                    <form method="post" action="{% url 'actions:action_change_status' action.id %}" class="mb-3">
                        {% csrf_token %}
                        <label class="form-label">{% trans "Change Status" %}</label>
                        <select name="status" class="form-select mb-2" required>
                            {% for value, label in status_choices %}
                            <option value="{{ value }}" {% if action.status == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                        <textarea name="note" class="form-control mb-2" rows="2" 
                                  placeholder="{% trans 'Optional note...' %}"></textarea>
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="bi bi-arrow-repeat me-1"></i> Update Status
                        </button>
                    </form>
                    
                    <!-- Escalate -->
                    <button type="button" class="btn btn-danger w-100" data-bs-toggle="modal" 
                            data-bs-target="#escalateModal">
                        <i class="bi bi-arrow-up-circle me-1"></i> Escalate
                    </button>
                </div>
            </div>
            {% endif %}
            
            <!-- Add Note -->
            <div class="card mb-3">
                <div class="card-header bg-success text-white">
                    <h6 class="mb-0"><i class="bi bi-chat-left-text me-2"></i>{% trans "Add Note" %}</h6>
                </div>
                <div class="card-body">
                    <form method="post" action="{% url 'actions:action_add_note' action.id %}">
                        {% csrf_token %}
                        <textarea name="note" class="form-control mb-2" rows="3" 
                                  placeholder="{% trans 'Enter your note...' %}" required></textarea>
                        <button type="submit" class="btn btn-success w-100">
                            <i class="bi bi-plus-circle me-1"></i> Add Note
                        </button>
                    </form>
                </div>
            </div>
            
            <!-- Assignment Info -->
            <div class="card mb-3">
                <div class="card-header">
                    <h6 class="mb-0"><i class="bi bi-info-circle me-2"></i>{% trans "Assignment Info" %}</h6>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <div class="info-label">Assigned To</div>
                        <div class="info-value">
                            {% if action.assigned_to %}
                            {{ action.assigned_to.get_full_name }}
                            <br>
                            <small class="text-muted">
                                Assigned {{ action.assigned_at|date:"M d, Y H:i" }}
                            </small>
                            {% else %}
                            <span class="text-muted">Unassigned</span>
                            {% endif %}
                        </div>
                    </div>
                    
                    {% if action.approved_by %}
                    <div class="mb-3">
                        <div class="info-label">Approved By</div>
                        <div class="info-value">
                            {{ action.approved_by.get_full_name }}
                            <br>
                            <small class="text-muted">
                                {{ action.approved_at|date:"M d, Y H:i" }}
                            </small>
                        </div>
                    </div>
                    {% endif %}
                    
                    {% if action.closed_by %}
                    <div class="mb-0">
                        <div class="info-label">Closed By</div>
                        <div class="info-value">
                            {{ action.closed_by.get_full_name }}
                            <br>
                            <small class="text-muted">
                                {{ action.closed_at|date:"M d, Y H:i" }}
                            </small>
                        </div>
                    </div>
                    {% endif %}
                </div>
            </div>
            
            <!-- SLA Info -->
            <div class="card">
                <div class="card-header bg-warning text-dark">
                    <h6 class="mb-0"><i class="bi bi-clock-history me-2"></i>{% trans "SLA Information" %}</h6>
                </div>
                <div class="card-body">
                    <div class="mb-2">
                        <strong>Due Date:</strong><br>
                        <span class="{% if action.is_overdue %}text-danger{% endif %}">
                            {{ action.due_at|date:"M d, Y H:i" }}
                        </span>
                    </div>
                    {% if action.escalated_at %}
                    <div class="mb-2">
                        <strong>Escalated:</strong><br>
                        {{ action.escalated_at|date:"M d, Y H:i" }}
                        <span class="badge bg-danger ms-1">Level {{ action.escalation_level }}</span>
                    </div>
                    {% endif %}
                    {% if action.reminder_sent_at %}
                    <div class="mb-0">
                        <strong>Reminder Sent:</strong><br>
                        {{ action.reminder_sent_at|date:"M d, Y H:i" }}
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Escalate Modal -->
<div class="modal fade" id="escalateModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="post" action="{% url 'actions:action_escalate' action.id %}">
                {% csrf_token %}
                <div class="modal-header">
                    <h5 class="modal-title">{% trans "Escalate Action" %}</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="alert alert-warning">
                        <i class="bi bi-exclamation-triangle me-2"></i>
                        This will escalate the action to the next level.
                        {% if action.escalation_level > 0 %}
                        Current level: {{ action.escalation_level }}
                        {% endif %}
                    </div>
                    <div class="mb-3">
                        <label class="form-label">{% trans "Reason for Escalation" %}</label>
                        <textarea name="reason" class="form-control" rows="3" 
                                  placeholder="{% trans 'Explain why this action needs escalation...' %}" required></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
                    <button type="submit" class="btn btn-danger">
                        <i class="bi bi-arrow-up-circle me-1"></i> Escalate
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}
