<?php
/**
 * CustomAstDB status page.
 * Vars from showPage():
 *   $rows    — array of [extension, name, dnd, cf, cfb, cfu]
 *   $summary — [total, with_dnd, with_cf, clear]
 */
?>
<div id="customastdb-wrapper">

    <div class="row">
        <div class="col-md-3">
            <div class="panel panel-default">
                <div class="panel-body text-center">
                    <div style="font-size:24px;font-weight:bold;"><?php echo (int)$summary['total']; ?></div>
                    <div class="text-muted">Extensions</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="panel panel-default">
                <div class="panel-body text-center">
                    <div style="font-size:24px;font-weight:bold;color:<?php echo $summary['with_dnd'] > 0 ? '#d9534f' : '#5cb85c'; ?>;"><?php echo (int)$summary['with_dnd']; ?></div>
                    <div class="text-muted">With DND on</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="panel panel-default">
                <div class="panel-body text-center">
                    <div style="font-size:24px;font-weight:bold;color:<?php echo $summary['with_cf'] > 0 ? '#f0ad4e' : '#5cb85c'; ?>;"><?php echo (int)$summary['with_cf']; ?></div>
                    <div class="text-muted">With any CF set</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="panel panel-default">
                <div class="panel-body text-center">
                    <div style="font-size:24px;font-weight:bold;color:#5cb85c;"><?php echo (int)$summary['clear']; ?></div>
                    <div class="text-muted">Clear (no DND, no CF)</div>
                </div>
            </div>
        </div>
    </div>

    <div class="row" style="margin-bottom:12px;">
        <div class="col-md-6">
            <div class="input-group">
                <span class="input-group-addon"><i class="fa fa-search"></i></span>
                <input type="text" id="cad-filter" class="form-control" placeholder="Filter by extension, name, or CF destination&hellip;" autocomplete="off">
            </div>
        </div>
        <div class="col-md-6 text-right">
            <a href="ajax.php?module=customastdb&command=csv" class="btn btn-default"><i class="fa fa-download"></i> Export CSV</a>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12">
            <table class="table table-striped table-condensed" id="cad-table">
                <thead>
                    <tr>
                        <th>Extension</th>
                        <th>Name</th>
                        <th>DND</th>
                        <th>CF (always)</th>
                        <th>CFB (busy)</th>
                        <th>CFU (no answer)</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                        <tr><td colspan="6" class="text-center text-muted"><em>No extensions found.</em></td></tr>
                    <?php else: foreach ($rows as $r): ?>
                        <?php
                            $hasCf = ($r['cf'] !== '' || $r['cfb'] !== '' || $r['cfu'] !== '');
                            // FreePBX legacy.js activates the tab whose data-name matches the URL hash on page load.
                            $extLink = 'config.php?display=extensions&extdisplay=' . urlencode($r['extension']) . '&view=form#customastdb';
                            $haystack = strtolower($r['extension'] . ' ' . $r['name'] . ' ' . $r['cf'] . ' ' . $r['cfb'] . ' ' . $r['cfu']);
                        ?>
                        <tr data-haystack="<?php echo htmlspecialchars($haystack); ?>">
                            <td><a href="<?php echo $extLink; ?>"><?php echo htmlspecialchars($r['extension']); ?></a></td>
                            <td><?php echo htmlspecialchars($r['name']); ?></td>
                            <td>
                                <?php if ($r['dnd']): ?>
                                    <span class="label label-danger">ON</span>
                                <?php else: ?>
                                    <span class="text-muted">&mdash;</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($r['cf'] !== ''): ?>
                                    <span class="label label-warning"><?php echo htmlspecialchars($r['cf']); ?></span>
                                <?php else: ?>
                                    <span class="text-muted">&mdash;</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($r['cfb'] !== ''): ?>
                                    <span class="label label-warning"><?php echo htmlspecialchars($r['cfb']); ?></span>
                                <?php else: ?>
                                    <span class="text-muted">&mdash;</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($r['cfu'] !== ''): ?>
                                    <span class="label label-warning"><?php echo htmlspecialchars($r['cfu']); ?></span>
                                <?php else: ?>
                                    <span class="text-muted">&mdash;</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <p class="text-muted" style="font-size:11px;">
        Click any extension to open its FreePBX Extensions edit page; the <strong>AstDB State</strong> tab there lets you toggle DND or set / clear the three Call Forward destinations. Source: <code>/var/lib/asterisk/astdb.sqlite3</code>, read live on every page load.
    </p>
</div>

<?php
$cssV = @filemtime(__DIR__ . '/../assets/css/customastdb.css') ?: '1';
$jsV  = @filemtime(__DIR__ . '/../assets/js/customastdb.js')  ?: '1';
?>
<link rel="stylesheet" href="modules/customastdb/assets/css/customastdb.css?v=<?php echo $cssV; ?>">
<script src="modules/customastdb/assets/js/customastdb.js?v=<?php echo $jsV; ?>"></script>
