{% extends 'base.html' %}
{% load humanize %}

{% block title %}Customer Request Details{% endblock %}

{% block content %}
<div class="container mx-auto px-4 sm:px-6 lg:px-8 py-6">
    <!-- Page Header -->
    <div class="mb-8">
        <div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
            <div>
                <h1 class="text-3xl font-bold text-gray-900 dark:text-white">Request #{{ customer_request.request_number
                    }}</h1>
                <p class="text-gray-600 dark:text-gray-400 mt-1">{{ customer_request.subject }}</p>
            </div>
            <div class="flex gap-2">
                <a href="{% url 'reports:customer_requests_list' %}"
                    class="inline-flex items-center px-4 py-2 text-sm font-medium text-gray-600 bg-gray-100 rounded-lg hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600 transition-colors">
                    <span>←</span> Back to Requests
                </a>
            </div>
        </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
        <!-- Request Details -->
        <div class="lg:col-span-2 space-y-6">
            <!-- Request Information -->
            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6">
                <h2 class="text-xl font-semibold text-gray-900 dark:text-white mb-6">Request Information</h2>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="block text-sm font-medium text-gray-600 dark:text-gray-400 mb-1">Customer</label>
                        <p class="text-lg font-semibold text-gray-900 dark:text-white">{{
                            customer_request.customer.get_full_name }}</p>
                        <p class="text-sm text-gray-600 dark:text-gray-400">{{ customer_request.customer.phone_number }}
                        </p>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-600 dark:text-gray-400 mb-1">Request
                            Type</label>
                        <p class="text-lg font-semibold text-gray-900 dark:text-white">{{
                            customer_request.get_request_type_display }}</p>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-600 dark:text-gray-400 mb-1">Priority</label>
                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-sm font-medium 
                            {% if customer_request.priority == 'critical' %}bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200
                            {% elif customer_request.priority == 'high' %}bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200
                            {% elif customer_request.priority == 'medium' %}bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200
                            {% else %}bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200{% endif %}">
                            {{ customer_request.get_priority_display }}
                        </span>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-600 dark:text-gray-400 mb-1">Status</label>
                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-sm font-medium 
                            {% if customer_request.status == 'pending' %}bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200
                            {% elif customer_request.status == 'in_progress' %}bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200
                            {% elif customer_request.status == 'resolved' %}bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200
                            {% else %}bg-gray-100 text-gray-800 dark:bg-gray-900 dark:text-gray-200{% endif %}">
                            {{ customer_request.get_status_display }}
                        </span>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-600 dark:text-gray-400 mb-1">Created</label>
                        <p class="text-gray-900 dark:text-white">{{ customer_request.created_at|date:"M d, Y H:i" }}</p>
                    </div>

                    {% if customer_request.assigned_to %}
                    <div>
                        <label class="block text-sm font-medium text-gray-600 dark:text-gray-400 mb-1">Assigned
                            To</label>
                        <p class="text-gray-900 dark:text-white">{{ customer_request.assigned_to.get_full_name }}</p>
                    </div>
                    {% endif %}
                </div>

                <div class="mt-6">
                    <label class="block text-sm font-medium text-gray-600 dark:text-gray-400 mb-2">Description</label>
                    <div class="bg-gray-50 dark:bg-gray-700 rounded-lg p-4">
                        <p class="text-gray-900 dark:text-white whitespace-pre-wrap">{{ customer_request.description }}
                        </p>
                    </div>
                </div>

                {% if customer_request.resolution_notes %}
                <div class="mt-6">
                    <label class="block text-sm font-medium text-gray-600 dark:text-gray-400 mb-2">Resolution
                        Notes</label>
                    <div
                        class="bg-green-50 dark:bg-green-900 border border-green-200 dark:border-green-700 rounded-lg p-4">
                        <p class="text-gray-900 dark:text-white whitespace-pre-wrap">{{
                            customer_request.resolution_notes }}</p>
                        {% if customer_request.resolved_by %}
                        <p class="text-sm text-green-600 dark:text-green-400 mt-2">
                            Resolved by {{ customer_request.resolved_by.get_full_name }} on {{
                            customer_request.resolved_at|date:"M d, Y H:i" }}
                        </p>
                        {% endif %}
                    </div>
                </div>
                {% endif %}
            </div>
        </div>

        <!-- Actions Sidebar -->
        <div class="space-y-6">
            <!-- Update Status -->
            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6">
                <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Update Status</h3>

                <form method="POST" class="space-y-4">
                    {% csrf_token %}
                    <input type="hidden" name="action" value="update_status">

                    <div>
                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Status</label>
                        <select name="status" required
                            class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
                            {% for value, label in status_choices %}
                            <option value="{{ value }}" {% if customer_request.status==value %}selected{% endif %}>{{
                                label }}</option>
                            {% endfor %}
                        </select>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Notes</label>
                        <textarea name="notes" rows="3"
                            class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white"
                            placeholder="Add notes about this status update..."></textarea>
                    </div>

                    <button type="submit"
                        class="w-full px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700 transition-colors">
                        Update Status
                    </button>
                </form>
            </div>

            <!-- Assign Request -->
            {% if customer_request.status != 'resolved' %}
            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6">
                <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Assign Request</h3>

                <form method="POST" class="space-y-4">
                    {% csrf_token %}
                    <input type="hidden" name="action" value="assign">

                    <div>
                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Assign To</label>
                        <select name="assigned_to"
                            class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
                            <option value="">Select Staff Member</option>
                            {% for staff in staff_members %}
                            <option value="{{ staff.id }}" {% if customer_request.assigned_to==staff %}selected{% endif
                                %}>
                                {{ staff.get_full_name }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>

                    <button type="submit"
                        class="w-full px-4 py-2 text-sm font-medium text-white bg-green-600 rounded-lg hover:bg-green-700 transition-colors">
                        Assign Request
                    </button>
                </form>
            </div>
            {% endif %}

            <!-- Request Timeline -->
            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6">
                <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Timeline</h3>

                <div class="space-y-4">
                    <div class="flex items-start space-x-3">
                        <div class="flex-shrink-0 w-2 h-2 bg-blue-500 rounded-full mt-2"></div>
                        <div>
                            <p class="text-sm font-medium text-gray-900 dark:text-white">Request Created</p>
                            <p class="text-xs text-gray-600 dark:text-gray-400">{{ customer_request.created_at|date:"M
                                d, Y H:i" }}</p>
                        </div>
                    </div>

                    {% if customer_request.assigned_to %}
                    <div class="flex items-start space-x-3">
                        <div class="flex-shrink-0 w-2 h-2 bg-yellow-500 rounded-full mt-2"></div>
                        <div>
                            <p class="text-sm font-medium text-gray-900 dark:text-white">Assigned to {{
                                customer_request.assigned_to.get_full_name }}</p>
                            <p class="text-xs text-gray-600 dark:text-gray-400">{{ customer_request.updated_at|date:"M
                                d, Y H:i" }}</p>
                        </div>
                    </div>
                    {% endif %}

                    {% if customer_request.status == 'resolved' %}
                    <div class="flex items-start space-x-3">
                        <div class="flex-shrink-0 w-2 h-2 bg-green-500 rounded-full mt-2"></div>
                        <div>
                            <p class="text-sm font-medium text-gray-900 dark:text-white">Request Resolved</p>
                            <p class="text-xs text-gray-600 dark:text-gray-400">{{ customer_request.resolved_at|date:"M
                                d, Y H:i" }}</p>
                        </div>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}