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

{% block title %}Final Care Plan | {{ property.property_name }} | MyAdmin{% endblock %}

{% block content %}
<style>
    .plan-hero {
        border-radius: 36px;
        background:
            radial-gradient(circle at top right, rgba(251, 191, 36, 0.14), transparent 34%),
            linear-gradient(135deg, #052e25, #0f766e);
        color: #fff;
        padding: 2.8rem 2.4rem;
        box-shadow: 0 28px 65px rgba(15, 23, 42, 0.18);
    }

    .plan-hero h1 {
        font-weight: 900;
        letter-spacing: -0.04em;
        font-size: clamp(2.1rem, 4vw, 3.3rem);
    }

    .plan-hero p {
        color: rgba(255, 255, 255, 0.88);
        line-height: 1.78;
        max-width: 920px;
    }

    .plan-card {
        background: #fff;
        border-radius: 32px;
        border: 1px solid #e2e8f0;
        box-shadow: 0 20px 46px rgba(15, 23, 42, 0.09);
        overflow: hidden;
    }

    .plan-card-header {
        padding: 1.55rem 1.7rem 1.15rem;
        border-bottom: 1px solid #e2e8f0;
    }

    .plan-card-header h2,
    .plan-card-header h3 {
        color: #0f172a;
        font-weight: 900;
    }

    .plan-card-header p {
        color: #64748b;
        line-height: 1.65;
        margin-bottom: 0;
    }

    .plan-card-body {
        padding: 1.7rem;
    }

    .plan-summary-box {
        border-radius: 28px;
        background:
            radial-gradient(circle at top right, rgba(251, 191, 36, 0.10), transparent 30%),
            linear-gradient(135deg, #f8fafc, #ecfdf5);
        border: 1px solid rgba(15, 118, 110, 0.18);
        padding: 1.45rem;
        height: 100%;
    }

    .plan-summary-label {
        color: #0f766e;
        font-size: 0.82rem;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        font-weight: 900;
        margin-bottom: 0.45rem;
    }

    .plan-summary-value {
        color: #052e25;
        font-size: 2.2rem;
        line-height: 1;
        font-weight: 950;
        letter-spacing: -0.035em;
        margin-bottom: 0.4rem;
    }

    .plan-summary-copy {
        color: #475569;
        line-height: 1.6;
        margin-bottom: 0;
    }

    .service-plan-line {
        border-radius: 28px;
        background: #fff;
        border: 1px solid #e2e8f0;
        box-shadow: 0 14px 30px rgba(15, 23, 42, 0.06);
        padding: 1.35rem;
        margin-bottom: 1.2rem;
    }

    .service-plan-line:last-child {
        margin-bottom: 0;
    }

    .service-title {
        color: #0f172a;
        font-weight: 900;
        font-size: 1.15rem;
        margin-bottom: 0.35rem;
    }

    .service-request-note {
        color: #64748b;
        line-height: 1.62;
        font-size: 0.92rem;
        margin-bottom: 1rem;
    }

    .inclusion-box {
        border-radius: 20px;
        background: #f8fafc;
        border: 1px solid #e2e8f0;
        padding: 1rem;
        height: 100%;
    }

    .form-label {
        color: #0f172a;
        font-weight: 800;
    }

    .form-control,
    .form-select {
        border-radius: 16px;
        border: 1px solid #dbe4ee;
        min-height: 50px;
    }

    textarea.form-control {
        min-height: 105px;
    }

    .status-pill {
        display: inline-flex;
        align-items: center;
        gap: 0.45rem;
        border-radius: 999px;
        padding: 0.48rem 0.86rem;
        font-size: 0.82rem;
        font-weight: 900;
    }

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

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

    .sticky-action-box {
        position: sticky;
        top: 105px;
    }

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

        .plan-card-header,
        .plan-card-body {
            padding-left: 1.3rem;
            padding-right: 1.3rem;
        }

        .sticky-action-box {
            position: static;
        }
    }
</style>

<section class="py-5">
    <div class="container">

        <div class="plan-hero mb-4">
            <h1 class="mb-2">
                Final Care Plan: {{ property.property_name }}
            </h1>

            <p class="mb-3">
                Convert the owner’s property details, service selections, and provisional calculator estimate
                into Ensolanka’s final operational care plan.
            </p>

            <div class="d-flex flex-wrap gap-2">
                <a href="{% url 'myadmin:property_care_property_review_detail' property.pk %}"
                   class="btn btn-light rounded-pill fw-bold">
                    <i class="bi bi-arrow-left me-2"></i>
                    Back to Property Review
                </a>
                <a href="{% url 'myadmin:property_care_visit_create' property.pk %}"
                    class="btn btn-info rounded-pill fw-bold">
                    <i class="bi bi-clipboard2-pulse-fill me-2"></i>
                    Create Maintenance Visit
                </a>
                {% if care_plan.plan_status == 'finalized' %}
                    <span class="status-pill status-finalized">
                        <i class="bi bi-check-circle-fill"></i>
                        Finalized Plan
                    </span>
                {% else %}
                    <span class="status-pill status-draft">
                        <i class="bi bi-pencil-square"></i>
                        Draft Plan
                    </span>
                {% endif %}
            </div>
        </div>


        <div class="row g-4 mb-4">
            <div class="col-md-4">
                <div class="plan-summary-box">
                    <div class="plan-summary-label">
                        Provisional Calculator Estimate
                    </div>

                    <div class="plan-summary-value">
                        ${{ care_plan.provisional_estimate_snapshot_usd|default:"0.00" }}
                    </div>

                    <p class="plan-summary-copy">
                        Snapshot of the owner-side calculator amount available when the care plan was prepared.
                    </p>
                </div>
            </div>

            <div class="col-md-4">
                <div class="plan-summary-box">
                    <div class="plan-summary-label">
                        Final Approved Monthly Fee
                    </div>

                    <div class="plan-summary-value">
                        ${{ care_plan.final_monthly_fee_usd|default:"0.00" }}
                    </div>

                    <p class="plan-summary-copy">
                        Calculated from the included final care-plan service lines below.
                    </p>
                </div>
            </div>

            <div class="col-md-4">
                <div class="plan-summary-box">
                    <div class="plan-summary-label">
                        Plan Start Date
                    </div>

                    <div class="plan-summary-value" style="font-size: 1.55rem;">
                        {{ care_plan.start_date|date:"M d, Y"|default:"Not set" }}
                    </div>

                    <p class="plan-summary-copy">
                        Required before the plan can be finalized.
                    </p>
                </div>
            </div>
        </div>


        <form method="post" id="care-plan-draft-form">
            {% csrf_token %}

            <div class="row g-4">

                <div class="col-lg-8">

                    <div class="plan-card mb-4">
                        <div class="plan-card-header">
                            <h2 class="mb-2">Plan Header</h2>
                            <p>
                                Define the approved plan start date, general exclusions, and final plan notes.
                            </p>
                        </div>

                        <div class="plan-card-body">

                            <div class="mb-4">
                                <label class="form-label">
                                    {{ form.start_date.label }}
                                </label>
                                {{ form.start_date }}
                                {{ form.start_date.errors }}
                            </div>

                            <div class="mb-4">
                                <label class="form-label">
                                    {{ form.general_exclusions.label }}
                                </label>
                                {{ form.general_exclusions }}
                                {{ form.general_exclusions.errors }}
                            </div>

                            <div>
                                <label class="form-label">
                                    {{ form.plan_notes.label }}
                                </label>
                                {{ form.plan_notes }}
                                {{ form.plan_notes.errors }}
                            </div>

                        </div>
                    </div>


                    <div class="plan-card">
                        <div class="plan-card-header">
                            <h2 class="mb-2">Included Services & Exclusions</h2>
                            <p>
                                Review each owner-selected service, decide whether it is included,
                                set the final monthly amount, and record any exclusion reason.
                            </p>
                        </div>

                        <div class="plan-card-body">

                            {% if service_rows %}

                                {% for row in service_rows %}
                                    <div class="service-plan-line">

                                        <div class="service-title">
                                            {{ row.service.name }}
                                        </div>

                                        <div class="service-request-note">
                                            <strong>Owner Request:</strong>
                                            {{ row.selection.get_frequency_display }}
                                            · Requested visits/month:
                                            {{ row.selection.requested_visits_per_month }}

                                            {% if row.selection.estimated_monthly_amount_usd %}
                                                · Calculator estimate:
                                                ${{ row.selection.estimated_monthly_amount_usd }}
                                            {% endif %}

                                            {% if row.selection.owner_note %}
                                                <br>
                                                <strong>Owner note:</strong>
                                                {{ row.selection.owner_note }}
                                            {% endif %}
                                        </div>

                                        <div class="row g-3">

                                            <div class="col-md-4">
                                                <div class="inclusion-box">
                                                    <div class="form-check">
                                                        <input class="form-check-input"
                                                               type="checkbox"
                                                               name="included_service_selection_ids"
                                                               value="{{ row.selection.id }}"
                                                               id="include_{{ row.selection.id }}"
                                                               {% if row.is_included %}checked{% endif %}>

                                                        <label class="form-check-label fw-bold"
                                                               for="include_{{ row.selection.id }}">
                                                            Include in final plan
                                                        </label>
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="col-md-4">
                                                <label class="form-label">
                                                    Final Frequency
                                                </label>

                                                <select name="final_frequency_{{ row.selection.id }}"
                                                        class="form-select">
                                                    {% for value, label in frequency_choices %}
                                                        <option value="{{ value }}"
                                                            {% if row.line and row.line.final_frequency == value %}
                                                                selected
                                                            {% elif not row.line and row.selection.frequency == value %}
                                                                selected
                                                            {% endif %}>
                                                            {{ label }}
                                                        </option>
                                                    {% endfor %}
                                                </select>
                                            </div>

                                            <div class="col-md-4">
                                                <label class="form-label">
                                                    Final Visits / Month
                                                </label>

                                                <input type="number"
                                                       name="final_visits_per_month_{{ row.selection.id }}"
                                                       class="form-control"
                                                       min="0"
                                                       max="31"
                                                       value="{% if row.line %}{{ row.line.final_visits_per_month }}{% else %}{{ row.selection.requested_visits_per_month }}{% endif %}">
                                            </div>

                                            <div class="col-md-4">
                                                <label class="form-label">
                                                    Final Monthly Amount USD
                                                </label>

                                                <input type="number"
                                                       step="0.01"
                                                       min="0"
                                                       name="final_monthly_amount_{{ row.selection.id }}"
                                                       class="form-control"
                                                       value="{{ row.default_final_amount }}">
                                            </div>

                                            <div class="col-md-8">
                                                <label class="form-label">
                                                    Staff Line Note
                                                </label>

                                                <input type="text"
                                                       name="line_note_{{ row.selection.id }}"
                                                       class="form-control"
                                                       value="{% if row.line %}{{ row.line.line_note }}{% endif %}"
                                                       placeholder="Why this service is included or how it will be handled">
                                            </div>

                                            <div class="col-12">
                                                <label class="form-label">
                                                    Exclusion Reason
                                                </label>

                                                <textarea name="exclusion_reason_{{ row.selection.id }}"
                                                          class="form-control"
                                                          placeholder="Required only when this owner-selected service is not included">{% if row.line %}{{ row.line.exclusion_reason }}{% endif %}</textarea>
                                            </div>

                                        </div>
                                    </div>
                                {% endfor %}

                            {% else %}

                                <div class="alert alert-warning rounded-4 mb-0">
                                    No owner-selected services are available for this property.
                                    Ask the owner to select services before finalizing a care plan.
                                </div>

                            {% endif %}

                        </div>
                    </div>

                </div>


                <div class="col-lg-4">
                    <div class="sticky-action-box">

                        <div class="plan-card mb-4">
                            <div class="plan-card-header">
                                <h3 class="mb-0">Save Draft</h3>
                            </div>

                            <div class="plan-card-body">
                                <p class="text-muted">
                                    Save the current plan header and all service-line decisions.
                                </p>

                                <button type="submit"
                                        class="btn btn-primary btn-lg rounded-pill fw-bold w-100">
                                    <i class="bi bi-save-fill me-2"></i>
                                    Save Care Plan Draft
                                </button>
                            </div>
                        </div>

                        <div class="plan-card">
                            <div class="plan-card-header">
                                <h3 class="mb-0">Finalize Plan</h3>
                            </div>

                            <div class="plan-card-body">

                                {% if care_plan.plan_status == 'finalized' %}
                                    <div class="alert alert-success rounded-4 mb-0">
                                        This Property Care Plan has already been finalized.
                                    </div>
                                {% else %}
                                    <p class="text-muted">
                                        Finalize only after the start date, included services,
                                        final amounts, exclusions, and notes are complete.
                                    </p>

                                    <button type="submit"
                                            form="care-plan-finalize-form"
                                            class="btn btn-success btn-lg rounded-pill fw-bold w-100"
                                            onclick="return confirm('Finalize this Property Care Plan? This confirms the staff-approved plan.');">
                                        <i class="bi bi-check2-circle me-2"></i>
                                        Finalize Care Plan
                                    </button>
                                {% endif %}

                            </div>
                        </div>

                    </div>
                </div>

            </div>
        </form>
            {% if care_plan.plan_status != 'finalized' %}
                <form method="post"
                    id="care-plan-finalize-form"
                    action="{% url 'myadmin:property_care_final_plan_finalize' property.pk %}">
                    {% csrf_token %}
                </form>
            {% endif %}
    </div>
</section>
{% endblock %}