{% extends 'companies/superadmin/base.html' %}
{% load humanize %}
{% block content %}
<div class="content-header">
    <a href="{% url 'superadmin_company_list' %}" class="text-decoration-none mb-3 d-inline-block">
        <i class="fas fa-arrow-left me-1"></i> Back to Companies
    </a>
    <div class="d-flex justify-content-between align-items-center">
        <h2 class="mb-0">{{ company.name }}</h2>
        <div>
            {% if company.is_active %}
                <a href="{% url 'superadmin_company_toggle' company.slug %}" class="btn btn-danger shadow-sm">
                    <i class="fas fa-ban me-1"></i> Suspend Company
                </a>
            {% else %}
                <a href="{% url 'superadmin_company_toggle' company.slug %}" class="btn btn-success shadow-sm">
                    <i class="fas fa-check-circle me-1"></i> Activate Company
                </a>
            {% endif %}
        </div>
    </div>
</div>

<div class="row g-4 mb-4">
    <!-- Statistics Overview -->
    <div class="col-md-3">
        <div class="card border-0 shadow-sm" style="border-radius: 15px; background: linear-gradient(135deg, #e0f7fa 0%, #b2ebf2 100%);">
            <div class="card-body p-4 text-center">
                <i class="fas fa-chart-line fa-2x text-info mb-2"></i>
                <h6 class="text-muted fw-bold mb-1">Today's Sales</h6>
                <h4 class="mb-0 fw-bold">{{ cs.currency_symbol|default:"₦" }}{{ today_sales|floatformat:2|intcomma }}</h4>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-0 shadow-sm" style="border-radius: 15px; background: linear-gradient(135deg, #f3e5f5 0%, #e1bee7 100%);">
            <div class="card-body p-4 text-center">
                <i class="fas fa-shopping-cart fa-2x text-purple mb-2" style="color: #8e24aa;"></i>
                <h6 class="text-muted fw-bold mb-1">Today's Orders</h6>
                <h4 class="mb-0 fw-bold">{{ today_orders|intcomma }}</h4>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-0 shadow-sm" style="border-radius: 15px; background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%);">
            <div class="card-body p-4 text-center">
                <i class="fas fa-coins fa-2x text-success mb-2"></i>
                <h6 class="text-muted fw-bold mb-1">Total Revenue</h6>
                <h4 class="mb-0 fw-bold">{{ cs.currency_symbol|default:"₦" }}{{ total_revenue|floatformat:2|intcomma }}</h4>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-0 shadow-sm" style="border-radius: 15px; background: linear-gradient(135deg, #fff3e0 0%, #ffe0b2 100%);">
            <div class="card-body p-4 text-center">
                <i class="fas fa-boxes fa-2x text-warning mb-2"></i>
                <h6 class="text-muted fw-bold mb-1">Total Orders</h6>
                <h4 class="mb-0 fw-bold">{{ total_orders_count|intcomma }}</h4>
            </div>
        </div>
    </div>
</div>

<div class="row g-4">
    <!-- Company Details -->
    <div class="col-lg-7">
        <div class="card border-0 shadow-sm mb-4" style="border-radius: 20px;">
            <div class="card-header bg-white py-3 border-bottom-0">
                <h5 class="mb-0 fw-bold"><i class="fas fa-info-circle me-2 text-primary"></i>General Information</h5>
            </div>
            <div class="card-body p-4 pt-0">
                <div class="table-responsive">
                    <table class="table table-sm table-borderless align-middle mb-0">
                        <tr class="border-bottom">
                            <td class="py-3 text-muted" width="160">Company Slug</td>
                            <td class="py-3 fw-bold text-primary">/{{ company.slug }}/</td>
                        </tr>
                        <tr class="border-bottom">
                            <td class="py-3 text-muted">Email Address</td>
                            <td class="py-3">{{ company.email|default:"—" }}</td>
                        </tr>
                        <tr class="border-bottom">
                            <td class="py-3 text-muted">Phone Number</td>
                            <td class="py-3">{{ company.phone|default:"—" }}</td>
                        </tr>
                        <tr class="border-bottom">
                            <td class="py-3 text-muted">Default Currency</td>
                            <td class="py-3">{{ cs.currency_symbol }} ({{ cs.get_currency_display }})</td>
                        </tr>
                        <tr class="border-bottom">
                            <td class="py-3 text-muted">Registered On</td>
                            <td class="py-3">{{ company.created_at|date:"d M Y, H:i" }}</td>
                        </tr>
                        <tr class="border-bottom">
                            <td class="py-3 text-muted">Status</td>
                            <td class="py-3">
                                {% if company.is_active %}
                                    {% if company.is_expired %}
                                        <span class="badge bg-warning-subtle text-warning border border-warning-subtle px-3 rounded-pill">Expired</span>
                                    {% else %}
                                        <span class="badge bg-success-subtle text-success border border-success-subtle px-3 rounded-pill">Active</span>
                                    {% endif %}
                                {% else %}
                                    <span class="badge bg-danger-subtle text-danger border border-danger-subtle px-3 rounded-pill">Suspended</span>
                                {% endif %}
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>

        <div class="card border-0 shadow-sm" style="border-radius: 20px;">
            <div class="card-header bg-white py-3 border-bottom-0">
                <h5 class="mb-0 fw-bold"><i class="fas fa-external-link-alt me-2 text-info"></i>Access Control</h5>
            </div>
            <div class="card-body p-4 pt-0">
                <p class="text-muted">You can enter this company's workspace directly as a super-administrator to inspect data or help the tenant.</p>
                <div class="d-flex gap-3">
                    <a href="/{{ company.slug }}/dashboard/" class="btn btn-outline-primary px-4 rounded-pill" target="_blank">
                        <i class="fas fa-rocket me-2"></i> Open Workspace
                    </a>
                    {% if company.is_active %}
                        <a href="{% url 'superadmin_company_toggle' company.slug %}" class="btn btn-danger px-4 rounded-pill">
                            <i class="fas fa-ban me-2"></i> Suspend Access
                        </a>
                    {% else %}
                        <a href="{% url 'superadmin_company_toggle' company.slug %}" class="btn btn-success px-4 rounded-pill">
                            <i class="fas fa-check-circle me-2"></i> Restore Access
                        </a>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- Subscription Management -->
    <div class="col-lg-5">
        <div class="card border-0 shadow-sm" style="border-radius: 20px; background: linear-gradient(135deg, #ffffff 0%, #f9fafb 100%);">
            <div class="card-header bg-transparent py-3 border-bottom-0">
                <h5 class="mb-0 fw-bold"><i class="fas fa-crown me-2 text-warning"></i>Plan Management</h5>
            </div>
            <div class="card-body p-4 pt-0">
                <div class="mb-4">
                    <small class="text-muted text-uppercase fw-bold d-block mb-1">Current Plan</small>
                    <div class="h4 fw-bold">{{ company.plan.name|default:"Custom / No Plan" }}</div>
                    {% if company.plan %}
                        <div class="text-muted small">₦{{ company.plan.price_monthly|floatformat:2 }}/month</div>
                    {% endif %}
                </div>

                <div class="mb-4">
                    <small class="text-muted text-uppercase fw-bold d-block mb-1">Expiration</small>
                    <div class="h5 fw-bold {% if company.is_expired %}text-danger{% else %}text-dark{% endif %}">
                        {% if company.expiry_date %}
                            {{ company.expiry_date|date:"d M Y" }}
                            {% if company.is_expired %}(Expired){% endif %}
                        {% else %}
                            Lifetime / Never Expires
                        {% endif %}
                    </div>
                </div>

                <hr class="my-4">

                <h6 class="fw-bold mb-3">Update Subscription</h6>
                <form action="{% url 'superadmin_company_update_subscription' company.slug %}" method="POST">
                    {% csrf_token %}
                    <div class="mb-3">
                        <label class="form-label small text-muted">Change Plan</label>
                        <select name="plan" class="form-select" style="border-radius: 10px;">
                            <option value="">No Plan</option>
                            {% for p in plans %}
                                <option value="{{ p.id }}" {% if company.plan_id == p.id %}selected{% endif %}>
                                    {{ p.name }} (₦{{ p.price_monthly }}/mo)
                                </option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="mb-4">
                        <label class="form-label small text-muted">Update Expiry Date</label>
                        <input type="date" name="expiry_date" class="form-control" 
                               value="{{ company.expiry_date|date:'Y-m-d' }}" style="border-radius: 10px;">
                        <div class="form-text small">Leave blank for lifetime access.</div>
                    </div>

                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary rounded-pill py-2 shadow-sm">
                            <i class="fas fa-save me-1"></i> Save Changes
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}
