{include file="sections/header.tpl"}

<div class="container-fluid px-4 py-4">
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">Churn Analysis</h5>
        </div>
        <div class="card-body">
            <form method="get" class="mb-4">
                <div class="row">
                    <div class="col-md-3">
                        <label>Analysis Period (Days)</label>
                        <select name="period" class="form-control" onchange="this.form.submit()">
                            <option value="30" {if $period==30}selected{/if}>Last 30 Days</option>
                            <option value="60" {if $period==60}selected{/if}>Last 60 Days</option>
                            <option value="90" {if $period==90}selected{/if}>Last 90 Days</option>
                            <option value="180" {if $period==180}selected{/if}>Last 180 Days</option>
                            <option value="365" {if $period==365}selected{/if}>Last Year</option>
                        </select>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Monthly Churn Rate -->
    <div class="card mt-4">
        <div class="card-header">
            <h5 class="mb-0">Monthly Churn Rate</h5>
        </div>
        <div class="card-body table-responsive">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>Month</th>
                        <th>Churned Clients</th>
                        <th>Daily Average</th>
                    </tr>
                </thead>
                <tbody>
                    {foreach $monthlyChurn as $churn}
                    <tr>
                        <td>{$churn.month}</td>
                        <td>{$churn.churned_count}</td>
                        <td>{($churn.churned_count/30)|string_format:"%.1f"}</td>
                    </tr>
                    {foreachelse}
                    <tr><td colspan="3" class="text-center">No data available</td></tr>
                    {/foreach}
                </tbody>
            </table>
        </div>
    </div>

    <!-- Churn by Service Type -->
    <div class="row mt-4">
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Churn by Service Type</h5>
                </div>
                <div class="card-body table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>Service Type</th>
                                <th>Churned Clients</th>
                                <th>Percentage</th>
                            </tr>
                        </thead>
                        <tbody>
                            {foreach $serviceChurn as $service}
                            <tr>
                                <td>{$service.service_type}</td>
                                <td>{$service.churned_count}</td>
                                <td>
                                    <div class="progress">
                                        <div class="progress-bar bg-danger" style="width: {min(100, $service.churned_count/$monthlyChurn[0].churned_count*100)}%">
                                            {($service.churned_count/$monthlyChurn[0].churned_count*100)|string_format:"%.1f"}%
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            {foreachelse}
                            <tr><td colspan="3" class="text-center">No data available</td></tr>
                            {/foreach}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Top 10 Churned Plans</h5>
                </div>
                <div class="card-body table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>Plan Name</th>
                                <th>Churned Clients</th>
                            </tr>
                        </thead>
                        <tbody>
                            {foreach $planChurn as $plan}
                            <tr>
                                <td>{$plan.plan_name}</td>
                                <td>{$plan.churned_count}</td>
                            </tr>
                            {foreachelse}
                            <tr><td colspan="2" class="text-center">No data available</td></tr>
                            {/foreach}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

{include file="sections/footer.tpl"}
