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

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

{% block extra_css %}
<style>
    .inquiry-header {
        background: linear-gradient(135deg, #17a2b8 0%, #138496 100%);
        color: white;
        padding: 30px;
        border-radius: 12px;
        margin-bottom: 30px;
    }
    .due-date-badge {
        background: rgba(255,255,255,0.2);
        padding: 15px;
        border-radius: 8px;
        text-align: center;
    }
    .due-date-badge.overdue {
        background: #d32f2f;
    }
    .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-resolved { background: #e8f5e9; color: #388e3c; }
    .status-closed { background: #f5f5f5; color: #616161; }
    
    .priority-badge {
        padding: 6px 16px;
        border-radius: 20px;
        font-size: 0.9rem;
        font-weight: 600;
    }
    .priority-low { background: #e8f5e9; color: #388e3c; }
    .priority-medium { background: #fff3e0; color: #f57c00; }
    .priority-high { background: #ffebee; color: #d32f2f; }
    .priority-urgent { background: #880e4f; color: #fff; }
    
    .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 #17a2b8;
        z-index: 1;
    }
    .timeline-item.status_change::before {
        border-color: #f57c00;
    }
    .timeline-item.response::before {
        border-color: #388e3c;
    }
    .timeline-item.note::before {
        border-color: #1976d2;
    }
    
    .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;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Back Button -->
    <div class="mb-3">
        <a href="{% url 'complaints:inquiry_list' %}" class="btn btn-outline-secondary btn-sm">
            <i class="bi bi-arrow-left me-1"></i> {{ _("Back to Inquiries")}}
        </a>
    </div>

    <!-- Inquiry Header -->
    <div class="card p-3">
        <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">{{ inquiry.subject }}</h3>
                    <span class="status-badge status-{{ inquiry.status }}">
                        {{ inquiry.get_status_display }}
                    </span>
                    {% if inquiry.priority %}
                    <span class="priority-badge priority-{{ inquiry.priority }} ms-2">
                        {{ inquiry.get_priority_display }}
                    </span>
                    {% endif %}
                </div>
                <p class="mb-2">
                    <i class="bi bi-hash me-1"></i>
                    <strong>{{ _("ID") }}:</strong> {{ inquiry.id|slice:":8" }}
                    {% if inquiry.patient %}
                    <span class="mx-2">|</span>
                    <i class="bi bi-person-fill me-1"></i>
                    <strong>{{ _("Patient") }}:</strong> {{ inquiry.patient.get_full_name }} ({{ _("MRN") }}: {{ inquiry.patient.mrn }})
                    {% else %}
                    <span class="mx-2">|</span>
                    <i class="bi bi-person me-1"></i>
                    <strong>{{ _("Contact") }}:</strong> {{ inquiry.contact_name|default:inquiry.contact_email }}
                    {% endif %}
                </p>
                <p class="mb-0">
                    <i class="bi bi-hospital me-1"></i>
                    <strong>{{ _("Hospital") }}:</strong> {{ inquiry.hospital.name_en }}
                    {% if inquiry.department %}
                    <span class="mx-2">|</span>
                    <i class="bi bi-building me-1"></i>
                    <strong>{{ _("Department") }}:</strong> {{ inquiry.department.name_en }}
                    {% endif %}
                </p>
            </div>
            <div class="col-md-4">
                {% if inquiry.due_date %}
                <div class="due-date-badge {% if inquiry.is_overdue %}overdue{% endif %}">
                    <div class="mb-1">
                        <i class="bi bi-clock-history me-1"></i>
                        <strong>{{ _("Due Date")}}</strong>
                    </div>
                    <h4 class="mb-0">{{ inquiry.due_date|date:"M d, Y H:i" }}</h4>
                    {% if inquiry.is_overdue %}
                    <div class="mt-2">
                        <i class="bi bi-exclamation-triangle-fill me-1"></i>
                        <strong>{{ _("OVERDUE") }}</strong>
                    </div>
                    {% else %}
                    <small>{{ inquiry.due_date|timeuntil }} {{ _("remaining") }}</small>
                    {% endif %}
                </div>
                {% endif %}
            </div>
        </div>
    </div>

    <div class="row">
        <!-- Main Content -->
        <div class="col-lg-8">
            <!-- Tabs -->
            <ul class="nav nav-tabs mb-3" id="inquiryTabs" 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> {{ _("Details") }}
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="timeline-tab" data-bs-toggle="tab" 
                            data-bs-target="#timeline" type="button" role="tab">
                        <i class="bi bi-clock-history me-1"></i> {{ _("Timeline") }} ({{ timeline.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="inquiryTabsContent">
                <!-- 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 "Inquiry 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">{{ inquiry.get_category_display }}</span>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="info-label">{{ _("Source") }}</div>
                                    <div class="info-value">
                                        {% if inquiry.source %}
                                        {{ inquiry.get_source_display }}
                                        {% else %}
                                        <span class="text-muted">{{ _("N/A")}}</span>
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                            
                            <div class="row mb-3">
                                <div class="col-md-6">
                                    <div class="info-label">{{ _("Channel") }}</div>
                                    <div class="info-value">
                                        {% if inquiry.channel %}
                                        {{ inquiry.get_channel_display }}
                                        {% else %}
                                        <span class="text-muted">{{ _("N/A")}}</span>
                                        {% endif %}
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="info-label">{{ _("Assigned To")}}</div>
                                    <div class="info-value">
                                        {% if inquiry.assigned_to %}
                                        {{ inquiry.assigned_to.get_full_name }}
                                        {% else %}
                                        <span class="text-muted">{{ _("Unassigned") }}</span>
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                            
                            <hr>
                            
                            <div class="mb-3">
                                <div class="info-label">{{ _("Message") }}</div>
                                <div class="info-value mt-2">
                                    <p class="mb-0">{{ inquiry.message|linebreaks }}</p>
                                </div>
                            </div>
                            
                            {% if inquiry.response %}
                            <hr>
                            <div class="mb-3">
                                <div class="info-label">{{ _("Response") }}</div>
                                <div class="info-value mt-2">
                                    <div class="alert alert-success">
                                        <p class="mb-2">{{ inquiry.response|linebreaks }}</p>
                                        <small class="text-muted">
                                            {{ _("Responded by")}} {{ inquiry.responded_by.get_full_name }}
                                            {{ _("on") }} {{ inquiry.responded_at|date:"M d, Y H:i" }}
                                        </small>
                                    </div>
                                </div>
                            </div>
                            {% endif %}
                            
                            <hr>
                            
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="info-label">{{ _("Created") }}</div>
                                    <div class="info-value">{{ inquiry.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">{{ inquiry.updated_at|date:"M d, Y H:i" }}</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Timeline Tab -->
                <div class="tab-pane fade" id="timeline" role="tabpanel">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title mb-4">{% trans "Activity Timeline" %}</h5>
                            
                            {% if timeline %}
                            <div class="timeline">
                                {% for update in timeline %}
                                <div class="timeline-item {{ update.update_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">{{ update.get_update_type_display }}</span>
                                                    {% if update.created_by %}
                                                    <span class="text-muted ms-2">
                                                        by {{ update.created_by.get_full_name }}
                                                    </span>
                                                    {% endif %}
                                                </div>
                                                <small class="text-muted">
                                                    {{ update.created_at|date:"M d, Y H:i" }}
                                                </small>
                                            </div>
                                            <p class="mb-0">{{ update.message }}</p>
                                            {% if update.old_status and update.new_status %}
                                            <div class="mt-2">
                                                <span class="status-badge status-{{ update.old_status }}">
                                                    {{ update.old_status }}
                                                </span>
                                                <i class="bi bi-arrow-right mx-2"></i>
                                                <span class="status-badge status-{{ update.new_status }}">
                                                    {{ update.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 timeline entries yet")}}</p>
                            </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 "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>
                                            <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">
            <!-- Quick Actions -->
            {% if can_edit %}
            <div class="card mb-3">
                <div class="card-header bg-info 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 'complaints:inquiry_assign' inquiry.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 inquiry.assigned_to and inquiry.assigned_to.id == user_obj.id %}selected{% endif %}>
                                    {{ user_obj.get_full_name }}
                                </option>
                                {% endfor %}
                            </select>
                            <button type="submit" class="btn btn-info">
                                <i class="bi bi-person-check"></i>
                            </button>
                        </div>
                    </form>
                    
                    <!-- Change Status -->
                    <form method="post" action="{% url 'complaints:inquiry_change_status' inquiry.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 inquiry.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-info w-100">
                            <i class="bi bi-arrow-repeat me-1"></i> {{ _("Update Status")}}
                        </button>
                    </form>
                </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 'complaints:inquiry_add_note' inquiry.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>
            
            <!-- Response Form -->
            {% if can_edit and inquiry.status != 'resolved' and inquiry.status != 'closed' %}
            <div class="card mb-3">
                <div class="card-header bg-primary text-white">
                    <h6 class="mb-0"><i class="bi bi-reply-fill me-2"></i>{% trans "Respond to Inquiry" %}</h6>
                </div>
                <div class="card-body">
                    <form method="post" action="{% url 'complaints:inquiry_respond' inquiry.id %}">
                        {% csrf_token %}
                        <textarea name="response" class="form-control mb-2" rows="4" 
                                  placeholder="{% trans 'Enter your response...' %}" required></textarea>
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="bi bi-send me-1"></i> {{ _("Send Response")}}
                        </button>
                    </form>
                </div>
            </div>
            {% endif %}
            
            <!-- Contact Information -->
            <div class="card mb-3">
                <div class="card-header">
                    <h6 class="mb-0"><i class="bi bi-info-circle me-2"></i>{% trans "Contact Information" %}</h6>
                </div>
                <div class="card-body">
                    {% if inquiry.patient %}
                        <div class="mb-3">
                            <div class="info-label">{{ _("Patient")}}</div>
                            <div class="info-value">
                                {{ inquiry.patient.get_full_name }}
                                <br>
                                <small class="text-muted">{{ _("MRN") }}: {{ inquiry.patient.mrn }}</small>
                            </div>
                        </div>
                        
                        {% if inquiry.patient.phone %}
                        <div class="mb-3">
                            <div class="info-label">{{ _("Phone")}}</div>
                            <div class="info-value">{{ inquiry.patient.phone }}</div>
                        </div>
                        {% endif %}
                        
                        {% if inquiry.patient.email %}
                        <div class="mb-3">
                            <div class="info-label">{{ _("Email")}}</div>
                            <div class="info-value">{{ inquiry.patient.email }}</div>
                        </div>
                        {% endif %}
                    {% else %}
                        {% if inquiry.contact_name %}
                        <div class="mb-3">
                            <div class="info-label">{{ _("Name")}}</div>
                            <div class="info-value">{{ inquiry.contact_name }}</div>
                        </div>
                        {% endif %}
                        
                        {% if inquiry.contact_phone %}
                        <div class="mb-3">
                            <div class="info-label">{{ _("Phone")}}</div>
                            <div class="info-value">{{ inquiry.contact_phone }}</div>
                        </div>
                        {% endif %}
                        
                        {% if inquiry.contact_email %}
                        <div class="mb-3">
                            <div class="info-label">{{ _("Email")}}</div>
                            <div class="info-value">{{ inquiry.contact_email }}</div>
                        </div>
                        {% endif %}
                    {% endif %}
                </div>
            </div>
            
            <!-- Assignment Info -->
            <div class="card">
                <div class="card-header">
                    <h6 class="mb-0"><i class="bi bi-person-check 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 inquiry.assigned_to %}
                            {{ inquiry.assigned_to.get_full_name }}
                            <br>
                            <small class="text-muted">
                                {{ _("Assigned") }} {{ inquiry.assigned_at|date:"M d, Y H:i"|default:_("N/A") }}
                            </small>
                            {% else %}
                            <span class="text-muted">{{ _("Unassigned") }}</span>
                            {% endif %}
                        </div>
                    </div>
                    
                    {% if inquiry.responded_by %}
                    <div class="mb-3">
                        <div class="info-label">{{ _("Responded By")}}</div>
                        <div class="info-value">
                            {{ inquiry.responded_by.get_full_name }}
                            <br>
                            <small class="text-muted">
                                {{ inquiry.responded_at|date:"M d, Y H:i" }}
                            </small>
                        </div>
                    </div>
                    {% endif %}
                    
                    {% if inquiry.resolved_by %}
                    <div class="mb-0">
                        <div class="info-label">{{ _("Resolved By")}}</div>
                        <div class="info-value">
                            {{ inquiry.resolved_by.get_full_name }}
                            <br>
                            <small class="text-muted">
                                {{ inquiry.resolved_at|date:"M d, Y H:i" }}
                            </small>
                        </div>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
