{% extends 'myadmin/base_myadmin.html' %}

{% block title %}Property Care Visits | MyAdmin{% endblock %}

{% block content %}
<style>
    .visit-admin-page {
        min-height: 100vh;
        background:
            radial-gradient(circle at top left, rgba(251, 191, 36, 0.08), transparent 30%),
            radial-gradient(circle at top right, rgba(15, 118, 110, 0.08), transparent 32%),
            linear-gradient(180deg, #f8fafc 0%, #ffffff 100%);
    }

    .visit-hero {
        border-radius: 36px;
        padding: 2.5rem 2.3rem;
        background:
            radial-gradient(circle at top right, rgba(251, 191, 36, 0.15), transparent 34%),
            linear-gradient(135deg, #052e25, #0f766e);
        color: #fff;
        box-shadow: 0 28px 65px rgba(15, 23, 42, 0.18);
    }

    .visit-mini {
        display: inline-flex;
        gap: .45rem;
        align-items: center;
        border-radius: 999px;
        padding: .48rem .95rem;
        background: rgba(255,255,255,.14);
        border: 1px solid rgba(255,255,255,.22);
        color: #fef3c7;
        text-transform: uppercase;
        letter-spacing: .05em;
        font-size: .84rem;
        font-weight: 900;
        margin-bottom: 1rem;
    }

    .visit-hero h1 {
        font-size: clamp(2.1rem, 4vw, 3.35rem);
        font-weight: 950;
        letter-spacing: -.04em;
        margin-bottom: .75rem;
    }

    .visit-hero p {
        max-width: 900px;
        color: rgba(255,255,255,.88);
        line-height: 1.75;
        margin-bottom: 0;
    }

    .stat-card,
    .filter-card,
    .visit-table-card {
        border-radius: 30px;
        background: #fff;
        border: 1px solid #e2e8f0;
        box-shadow: 0 18px 42px rgba(15, 23, 42, 0.08);
    }

    .stat-card {
        padding: 1.35rem;
        height: 100%;
    }

    .stat-label {
        color: #64748b;
        font-size: .85rem;
        text-transform: uppercase;
        letter-spacing: .05em;
        font-weight: 900;
        margin-bottom: .4rem;
    }

    .stat-value {
        color: #0f172a;
        font-size: 2rem;
        font-weight: 950;
        line-height: 1;
        margin-bottom: 0;
    }

    .filter-card {
        padding: 1.35rem;
    }

    .filter-card .form-control,
    .filter-card .form-select {
        min-height: 48px;
        border-radius: 16px;
    }

    .filter-submit-btn {
        min-height: 48px;
        border-radius: 999px;
        font-weight: 900;
    }

    .visit-table-card {
        overflow: hidden;
    }

    .visit-table-card .table {
        margin-bottom: 0;
    }

    .visit-table-card thead th {
        background: #f8fafc;
        color: #334155;
        font-size: .84rem;
        text-transform: uppercase;
        letter-spacing: .04em;
        font-weight: 900;
        padding: 1rem;
        white-space: nowrap;
    }

    .visit-table-card tbody td {
        padding: 1rem;
        vertical-align: middle;
    }

    .visit-title {
        color: #0f172a;
        font-weight: 900;
        margin-bottom: .2rem;
    }

    .visit-subtitle {
        color: #64748b;
        font-size: .9rem;
    }

    .status-pill {
        display: inline-flex;
        align-items: center;
        gap: .4rem;
        border-radius: 999px;
        padding: .42rem .78rem;
        font-size: .8rem;
        font-weight: 900;
        white-space: nowrap;
    }

    .status-scheduled {
        background: #dbeafe;
        color: #1d4ed8;
    }

    .status-in-progress {
        background: #fef3c7;
        color: #92400e;
    }

    .status-completed {
        background: #dcfce7;
        color: #166534;
    }

    .status-cancelled,
    .status-rescheduled {
        background: #fee2e2;
        color: #991b1b;
    }

    .owner-visible-pill {
        background: #ecfdf5;
        color: #166534;
    }

    .not-visible-pill {
        background: #e2e8f0;
        color: #475569;
    }

    .table-action-btn {
        border-radius: 999px;
        font-weight: 800;
        white-space: nowrap;
    }

    .empty-box {
        border-radius: 28px;
        padding: 2rem;
        border: 1px dashed rgba(15, 118, 110, .35);
        background: #fff;
        color: #475569;
    }

    @media (max-width: 767.98px) {
        .visit-hero {
            padding: 2rem 1.35rem;
            border-radius: 28px;
        }

        .filter-card {
            padding: 1.15rem;
        }
    }
</style>

<section class="visit-admin-page py-5">
    <div class="container">

        <div class="visit-hero mb-4">
            <span class="visit-mini">
                <i class="bi bi-clipboard2-pulse-fill"></i>
                Property Care Operations
            </span>

            <h1>Maintenance Visits</h1>

            <p>
                Review all scheduled, in-progress, completed, cancelled, and rescheduled property care visits.
                Use this area as the operational entry point for visit execution and photo evidence tracking.
            </p>
        </div>

        <div class="row g-4 mb-4">
            <div class="col-6 col-lg-3">
                <div class="stat-card">
                    <div class="stat-label">Total Visits</div>
                    <div class="stat-value">{{ total_visits }}</div>
                </div>
            </div>

            <div class="col-6 col-lg-3">
                <div class="stat-card">
                    <div class="stat-label">Scheduled</div>
                    <div class="stat-value text-primary">{{ scheduled_visits }}</div>
                </div>
            </div>

            <div class="col-6 col-lg-3">
                <div class="stat-card">
                    <div class="stat-label">In Progress</div>
                    <div class="stat-value text-warning">{{ in_progress_visits }}</div>
                </div>
            </div>

            <div class="col-6 col-lg-3">
                <div class="stat-card">
                    <div class="stat-label">Completed</div>
                    <div class="stat-value text-success">{{ completed_visits }}</div>
                </div>
            </div>
        </div>

        <div class="filter-card mb-4">
            <form method="get">
                <div class="row g-3 align-items-end">

                    <div class="col-lg-5">
                        <label class="form-label fw-bold">Search</label>
                        <input
                            type="text"
                            name="q"
                            value="{{ search_query }}"
                            class="form-control"
                            placeholder="Visit title, property, owner, notes..."
                        >
                    </div>

                    <div class="col-md-6 col-lg-3">
                        <label class="form-label fw-bold">Visit Status</label>
                        <select name="status" class="form-select">
                            <option value="">All Statuses</option>
                            {% for value, label in visit_status_choices %}
                                <option value="{{ value }}"
                                    {% if selected_status == value %}selected{% endif %}>
                                    {{ label }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="col-md-6 col-lg-3">
                        <label class="form-label fw-bold">Visit Type</label>
                        <select name="type" class="form-select">
                            <option value="">All Types</option>
                            {% for value, label in visit_type_choices %}
                                <option value="{{ value }}"
                                    {% if selected_type == value %}selected{% endif %}>
                                    {{ label }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="col-lg-1">
                        <button type="submit"
                                class="btn btn-dark w-100 filter-submit-btn">
                            <i class="bi bi-search"></i>
                        </button>
                    </div>

                </div>
            </form>
        </div>

        {% if visits %}
            <div class="visit-table-card">
                <div class="table-responsive">
                    <table class="table align-middle">
                        <thead>
                            <tr>
                                <th>Visit</th>
                                <th>Property</th>
                                <th>Type</th>
                                <th>Status</th>
                                <th>Scheduled</th>
                                <th>Visited By</th>
                                <th>Visibility</th>
                                <th class="text-end">Action</th>
                            </tr>
                        </thead>

                        <tbody>
                            {% for visit in visits %}
                                <tr>
                                    <td>
                                        <div class="visit-title">
                                            {{ visit.visit_title }}
                                        </div>
                                        <div class="visit-subtitle">
                                            Created {{ visit.created_at|date:"M d, Y" }}
                                        </div>
                                    </td>

                                    <td>
                                        <strong>{{ visit.managed_property.property_name }}</strong><br>
                                        <span class="text-muted small">
                                            {{ visit.managed_property.owner.full_name|default:visit.managed_property.owner.email }}
                                        </span>
                                    </td>

                                    <td>
                                        {{ visit.get_visit_type_display }}
                                    </td>

                                    <td>
                                        <span class="status-pill status-{{ visit.visit_status|slugify }}">
                                            {{ visit.get_visit_status_display }}
                                        </span>
                                    </td>

                                    <td>
                                        {% if visit.scheduled_date %}
                                            {{ visit.scheduled_date|date:"M d, Y" }}
                                            {% if visit.scheduled_time %}
                                                <br>
                                                <span class="text-muted small">
                                                    {{ visit.scheduled_time|time:"H:i" }}
                                                </span>
                                            {% endif %}
                                        {% else %}
                                            <span class="text-muted">Not set</span>
                                        {% endif %}
                                    </td>

                                    <td>
                                        {% if visit.visited_by %}
                                            {{ visit.visited_by.get_full_name|default:visit.visited_by.username }}
                                        {% else %}
                                            <span class="text-muted">Not assigned</span>
                                        {% endif %}
                                    </td>

                                    <td>
                                        {% if visit.is_owner_visible %}
                                            <span class="status-pill owner-visible-pill">
                                                Owner Visible
                                            </span>
                                        {% else %}
                                            <span class="status-pill not-visible-pill">
                                                Internal
                                            </span>
                                        {% endif %}
                                    </td>

                                    <td class="text-end">
                                        <div class="d-flex justify-content-end gap-2">
                                            <a href="{% url 'myadmin:property_care_visit_detail' visit.pk %}"
                                            class="btn btn-outline-dark btn-sm table-action-btn">
                                                Open
                                            </a>

                                            <a href="{% url 'myadmin:property_care_visit_update' visit.pk %}"
                                            class="btn btn-outline-primary btn-sm table-action-btn">
                                                Edit
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        {% else %}
            <div class="empty-box">
                <h4 class="fw-bold mb-2">No maintenance visits found</h4>
                <p class="mb-0">
                    Try a different search or filter. New maintenance visits can be created from a managed property review or operational workflow.
                </p>
            </div>
        {% endif %}

    </div>
</section>
{% endblock %}