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

<div class="row">
    <div class="col-sm-12">
        <div class="card card-hovered mb20 card-primary">
            <div class="card-title">
                <div class="btn-group pull-right">
                    <a class="btn btn-info btn-xs" title="{Lang::T('Export to CSV')}" href="{$_url}customers/csv-prepaid"
                        onclick="return confirm('{Lang::T('This will export to CSV?')}')">
                        <span class="glyphicon glyphicon-download" aria-hidden="true"></span> {Lang::T('CSV')}
                    </a>
                </div>
                {if in_array($_admin['user_type'], ['SuperAdmin', 'Admin'])}
                    <!-- Combined Form: Filter by Router, Status, and Sync -->
                    <form id="combined-form" method="post" action="{$_url}plan/sync">
                        <div class="row row-no-gutters" style="padding: 5px;">
                            <!-- Router Selection for Sync and Filter -->
                            <div class="col-sm-3 col-md-3">
                                <select class="form-control" id="router" name="router" title="{Lang::T('Filter by Router')}">
                                    <option value="">{Lang::T('All Routers')}</option> <!-- Option for all routers -->
                                    {foreach $routers as $r}
                                        <option value="{$r}" {if $router eq $r }selected{/if}>{$r}</option>
                                    {/foreach}
                                </select>
                            </div>
            
                            <!-- Status Selection for Sync and Filter -->
                            <div class="col-sm-3 col-md-3">
                                <select class="form-control" id="status" name="status" title="{Lang::T('Filter by Status')}">
                                    <option value="">{Lang::T('All Status')}</option>
                                    <option value="on" {if $status eq 'on' }selected{/if}>{Lang::T('Active')}</option>
                                    <option value="off" {if $status eq 'off' }selected{/if}>{Lang::T('Expired')}</option>
                                </select>
                            </div>
            
                            <!-- Sync Button -->
                            <div class="col-sm-2 col-md-2">
                                <button class="btn btn-primary btn-block" type="submit">
                                    <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span> {Lang::T('Sync')}
                                </button>
                            </div>
            
                            <!-- Filter Button (Submit to filter the clients) -->
                            <div class="col-sm-2 col-md-2">
                                <button class="btn btn-primary btn-block" type="submit" name="filter_action" value="1">
                                    <span class="glyphicon glyphicon-search" aria-hidden="true"></span> {Lang::T('Filter Clients')}
                                </button>
                            </div>
                        </div>
                    </form>
                {/if}
            
                <!-- Export to CSV Button -->
                
            </div>
            
            <!-- Card Body: Search and Recharge Options -->
            <div class="card-body">
                <div class="md-whiteframe-z1 mb20 text-center" style="padding: 15px;">
                    <div class="col-md-8">
                        <form id="site-search" method="post" action="{$_url}plan/list/">
                            <div class="input-group">
                                <div class="input-group-addon">
                                    <span class="fa fa-search"></span>
                                </div>
                                <input type="text" name="search" class="form-control"
                                    placeholder="{Lang::T('Search by Username')}..." value="{$search}" title="{Lang::T('Search by Username')}">
                                <div class="input-group-btn">
                                    <button class="btn btn-success" type="submit">{Lang::T('Search')}</button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="col-md-4">
                        <a href="{$_url}plan/recharge" class="btn btn-primary btn-block"><i
                                class="ion ion-android-add"> </i> {Lang::T('Recharge Account')}</a>
                    </div>
                </div>
            </div>
            
            

                <!-- Client Data Table -->
                <div class="table-responsive">
                    <table id="datatable" class="table table-bordered table-striped table-condensed">
                        <thead>
                            <tr>
                                <th>{Lang::T('Username')}</th>
                                <th>{Lang::T('Plan Name')}</th>
                                <th>{Lang::T('Type')}</th>
                                <th>{Lang::T('Created On')}</th>
                                <th>{Lang::T('Expires On')}</th>
                                <th>{Lang::T('Method')}</th>
                                <th>{Lang::T('Routers')}</th>
                                <th>{Lang::T('Status')}</th>
                                <th>{Lang::T('Manage')}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {if $d|@count == 0}
                                <tr><td colspan="9">{Lang::T('No clients found')}</td></tr>
                            {else}
                                {foreach $d as $ds}
                                    <tr>
                                        <td><a href="{$_url}customers/viewu/{$ds['username']}">{$ds['username']}</a></td>
                                        <td>{$ds['namebp']}</td>
                                        <td>{$ds['type']}</td>
                                        <td><span style="color: green;">{Lang::dateAndTimeFormat($ds.recharged_on, $ds.recharged_time)}</span></td>
                                        <td><span style="color: red;">{Lang::dateAndTimeFormat($ds.expiration, $ds.time)}</span></td>
                                        <td>{$ds.method}</td>
                                        <td>{$ds['routers']}</td>
                                        <td>
                                            {if $ds['status']=='off'}
                                                <i class="fa fa-circle" style="color: red;" title="{Lang::T('Expired')}"></i>
                                            {else}
                                                <i class="fa fa-circle" style="color: green;" title="{Lang::T('Active')}"></i>
                                            {/if}
                                        </td>
                                        <td>
                                            <a href="{$_url}plan/edit/{$ds['id']}" class="btn btn-warning btn-xs" style="color: black;" title="{Lang::T('Edit Plan')}">{Lang::T('Edit')}</a>
                                            {if in_array($_admin['user_type'],['SuperAdmin','Admin'])}
                                                <a href="{$_url}plan/delete/{$ds['id']}" id="{$ds['id']}"
                                                    onclick="return confirm('{Lang::T('Delete')}?')"
                                                    class="btn btn-danger btn-xs" title="{Lang::T('Delete Plan')}"><i class="glyphicon glyphicon-trash"></i> {Lang::T('Delete')}</a>
                                            {/if}
                                            {if $ds['status']=='off' && $_c['extend_expired']}
                                                <a href="javascript:extend('{$ds['id']}')" class="btn btn-info btn-xs" title="{Lang::T('Extend Expired Plan')}">{Lang::T('Extend')}</a>
                                            {/if}
                                        </td>
                                    </tr>
                                {/foreach}
                            {/if}
                        </tbody>
                    </table>
                </div>
                
                <!-- Pagination -->
                {include file="pagination.tpl"}
            </div>
        </div>
    </div>
</div>

<script>
// Add client-side validation for the filter form
$("#combined-form").on('submit', function (e) {
    var router = $("#router").val();
    var status = $("#status").val();
    if (!router && !status) {
        alert("{Lang::T('Please select at least one filter: Router or Status.')}"); // Alert if neither filter is selected
        e.preventDefault(); // Prevent form submission
    }
});

// Extend plan functionality with confirmation
function extend(idP) {
    var res = prompt("{Lang::T('Extend for how many days?')}", "3");
    if(res){
        if(confirm("{Lang::T('Extend for')} " + res + " {Lang::T('days?')}")){
            window.location.href = "{$_url}plan/extend/" + idP + "/" + res + "&stoken={App::getToken()}";
        }
    }
}
</script>

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