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

<div class="row">
    <div class="col-12">
        <div class="panel panel-hovered mb20 panel-primary">
            <div class="panel-heading">
                {if in_array($_admin['user_type'], ['SuperAdmin', 'Admin'])}
                <div class="btn-group pull-right">
                    <!-- Sync Button for All Routers -->
                    <a class="btn btn-primary btn-xs" title="Sync Customer Active Plan to Mikrotik" href="{$_url}plan/sync"
                        onclick="return ask(this, 'This will sync/send Customer active plan to Mikrotik?')">
                        <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span> Sync All Routers
                    </a>
                </div>
                {/if}
                &nbsp;
                {Lang::T('Active Customers')}
            </div>

            <!-- Combined Form for Filter 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-12 col-sm-6 col-md-3 mb-2">
                        <select class="form-control" id="router" name="router" title="{Lang::T('Filter by Router')}" aria-label="{Lang::T('Filter by Router')}">
                            <option value="">{Lang::T('All Routers')}</option>
                            {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-12 col-sm-6 col-md-3 mb-2">
                        <select class="form-control" id="status" name="status" title="{Lang::T('Filter by Status')}" aria-label="{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-12 col-md-2 mb-2">
                        <button class="btn btn-primary btn-block" type="submit" aria-label="{Lang::T('Sync')}"
                            onclick="return ask(this, 'This will sync/send Customer active plan to Mikrotik?')">
                            <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span> Sync
                        </button>
                    </div>
            
                    <!-- Filter Button (Submit to filter the clients) -->
                    <div class="col-12 col-md-3 mb-2">
                        <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>
            </form>

            <!-- Search Form for Active Customers -->
            <form id="site-search" method="post" action="{$_url}plan/list/">
                <div class="panel-body">
                    <div class="row row-no-gutters" style="padding: 5px">
                        <div class="col-12 col-sm-6 col-md-3 mb-2">
                            <div class="input-group">
                                <div class="input-group-btn">
                                    <a class="btn btn-danger" title="{Lang::T('Clear Search Query')}" href="{$_url}plan/list">
                                        <span class="glyphicon glyphicon-remove-circle"></span> Clear
                                    </a>
                                </div>
                                <input type="text" name="search" class="form-control"
                                    placeholder="{Lang::T('Search')}..." value="{$search}" aria-label="{Lang::T('Search')}">
                            </div>
                        </div>

                        <!-- Router Filter -->
                        <div class="col-12 col-sm-6 col-md-2 mb-2">
                            <select class="form-control" id="router" name="router" aria-label="{Lang::T('Filter by Router')}">
                                <option value="">{Lang::T('Router')}</option>
                                {foreach $routers as $r}
                                <option value="{$r}" {if $router eq $r}selected{/if}>{$r}</option>
                                {/foreach}
                            </select>
                        </div>

                        <!-- Plan Filter -->
                        <div class="col-12 col-sm-6 col-md-2 mb-2">
                            <select class="form-control" id="plan" name="plan" aria-label="{Lang::T('Filter by Plan Name')}">
                                <option value="">{Lang::T('Plan Name')}</option>
                                {foreach $plans as $p}
                                <option value="{$p['id']}" {if $plan eq $p['id']}selected{/if}>{$p['name_plan']}</option>
                                {/foreach}
                            </select>
                        </div>

                        <!-- Status Filter -->
                        <div class="col-12 col-sm-6 col-md-2 mb-2">
                            <select class="form-control" id="status" name="status" aria-label="{Lang::T('Filter by Status')}">
                                <option value="-">{Lang::T('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>

                        <!-- Search Button -->
                        <div class="col-12 col-md-2 mb-2">
                            <button class="btn btn-success btn-block" type="submit" aria-label="{Lang::T('Search')}">
                                <span class="fa fa-search"></span>
                            </button>
                        </div>

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

            <!-- Table for Active Customers -->
            <div class="table-responsive">
                <div style="margin-left: 5px; margin-right: 5px;">&nbsp;
                    <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><a href="{$_url}routers/list">{Lang::T('Router')}</a></th>
                                <th>{Lang::T('Status')}</th>
                                <th>{Lang::T('Manage')}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {foreach $d as $ds}
                            <tr {if $ds['status']=='off'}class="danger"{/if}>
                                <td>
                                    {if $ds['customer_id'] == '0'}
                                    <a href="{$_url}plan/voucher/&search={$ds['username']}">{$ds['username']}</a>
                                    {else}
                                    <a href="{$_url}customers/viewu/{$ds['username']}">{$ds['username']}</a>
                                    {/if}
                                </td>
                                <td>
                                    {if $ds['type'] == 'Hotspot'}
                                        <a href="{$_url}services/edit/{$ds['plan_id']}">{$ds['namebp']}</a>
                                    {elseif $ds['type'] == 'PPPOE'}
                                        <a href="{$_url}services/pppoe-edit/{$ds['plan_id']}">{$ds['namebp']}</a>
                                    {elseif $ds['type'] == 'VPN'}
                                        <a href="{$_url}services/vpn-edit/{$ds['plan_id']}">{$ds['namebp']}</a>
                                    {/if}
                                </td>
                                <td>{$ds['type']}</td>
                                <td>{Lang::dateAndTimeFormat($ds['recharged_on'],$ds['recharged_time'])}</td>
                                <td>{Lang::dateAndTimeFormat($ds['expiration'],$ds['time'])}</td>
                                <td>{$ds['method']}</td>
                                <td>{$ds['routers']}</td>

                                <!-- Status Column with Color Change -->
                                <td class="{if $ds['status'] == 'on'}text-success{else}text-danger{/if}">
                                    {if $ds['status'] == 'on'}{Lang::T('Active')}{else}{Lang::T('Expired')}{/if}
                                </td>

                                <td>
                                    <a href="{$_url}plan/edit/{$ds['id']}" class="btn btn-warning btn-xs" style="color: black;" aria-label="{Lang::T('Edit')}">
                                        {Lang::T('Edit')}
                                    </a>
                                    {if in_array($_admin['user_type'], ['SuperAdmin', 'Admin'])}
                                    <a href="{$_url}plan/delete/{$ds['id']}" id="{$ds['id']}"
                                        onclick="return ask(this, '{Lang::T('Delete')}?')" class="btn btn-danger btn-xs" aria-label="{Lang::T('Delete')}">
                                        <i class="glyphicon glyphicon-trash"></i>
                                    </a>
                                    {/if}
                                    {if $ds['status']=='off' && $_c['extend_expired']}
                                    <a href="javascript:extend('{$ds['id']}')" class="btn btn-info btn-xs" aria-label="{Lang::T('Extend')}">
                                        {Lang::T('Extend')}
                                    </a>
                                    {/if}
                                </td>
                            </tr>
                            {/foreach}
                        </tbody>
                    </table>
                </div>
            </div>

            {include file="pagination.tpl"}
        </div>
    </div>
</div>

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

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