<?php
require_once '../../config.php';
require_once '../../functions.php';

check_user_auth();
$user_id = $_SESSION['user_id'];

// Today stats for this user
$today = ['total' => 0, 'sent' => 0, 'failed' => 0];
try {
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM sms_logs WHERE user_id = ? AND DATE(sent_at) = CURDATE()");
    $stmt->execute([$user_id]);
    $today['total'] = $stmt->fetchColumn();
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM sms_logs WHERE user_id = ? AND DATE(sent_at) = CURDATE() AND status='sent'");
    $stmt->execute([$user_id]);
    $today['sent'] = $stmt->fetchColumn();
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM sms_logs WHERE user_id = ? AND DATE(sent_at) = CURDATE() AND status='failed'");
    $stmt->execute([$user_id]);
    $today['failed'] = $stmt->fetchColumn();
} catch (Exception $e) {
}

$pageTitle = "SMS History - User Panel";
include '../includes/header.php';
include '../includes/user_sidebar.php';
include '../includes/navbar.php';
?>

<div class="page-header">
    <h1 class="page-title">📡 Sent History</h1>
    <div>
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
            <li class="breadcrumb-item active">History</li>
        </ol>
    </div>
</div>

<div class="stats-summary-bar fade-in-up">
    <div class="stats-summary-item">
        <div class="ss-value text-primary"><?php echo $today['total']; ?></div>
        <div class="ss-label">Today Total</div>
    </div>
    <div class="stats-summary-item">
        <div class="ss-value" style="color:var(--ui-emerald);"><?php echo $today['sent']; ?></div>
        <div class="ss-label">Today Sent</div>
    </div>
    <div class="stats-summary-item">
        <div class="ss-value" style="color:var(--ui-rose);"><?php echo $today['failed']; ?></div>
        <div class="ss-label">Today Failed</div>
    </div>
</div>

<div class="row">
    <div class="col-12 fade-in-up">
        <div class="card">
            <div class="card-header d-flex align-items-center">
                <h3 class="card-title mb-0">My SMS Logs</h3>
                <button class="btn btn-sm btn-outline-primary ms-auto" id="refreshBtn"><i
                        class="fe fe-refresh-cw"></i></button>
            </div>
            <div class="card-body">
                <div class="filter-bar">
                    <div class="form-group">
                        <label class="form-label" style="font-size:0.75rem;">Status</label>
                        <select class="form-select form-select-sm" id="filterStatus" style="min-width:100px;">
                            <option value="">All</option>
                            <option value="sent">Sent</option>
                            <option value="failed">Failed</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label class="form-label" style="font-size:0.75rem;">From</label>
                        <input type="date" class="form-control form-control-sm" id="filterDateFrom">
                    </div>
                    <div class="form-group">
                        <label class="form-label" style="font-size:0.75rem;">To</label>
                        <input type="date" class="form-control form-control-sm" id="filterDateTo">
                    </div>
                    <div class="form-group" style="align-self:flex-end;">
                        <button class="btn btn-sm btn-modern-primary" id="applyFilters"><i
                                class="fe fe-filter me-1"></i>Filter</button>
                    </div>
                </div>
                <div class="table-responsive">
                    <table class="table table-bordered table-hover" id="user-monitor-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>To / Actions</th>
                                <th>Message</th>
                                <th>Status</th>
                                <th>Time</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
$extra_scripts = '
    <link href="' . asset_url('assets/plugins/datatable/css/dataTables.bootstrap5.css') . '" rel="stylesheet" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />
    <script src="' . asset_url('assets/plugins/datatable/js/jquery.dataTables.min.js') . '"></script>
    <script src="' . asset_url('assets/plugins/datatable/js/dataTables.bootstrap5.js') . '"></script>
    <script src="' . asset_url('assets/plugins/datatable/js/dataTables.buttons.min.js') . '"></script>
    <script src="' . asset_url('assets/plugins/datatable/js/buttons.bootstrap5.min.js') . '"></script>
    <script src="' . asset_url('assets/plugins/datatable/js/jszip.min.js') . '"></script>
    <script src="' . asset_url('assets/plugins/datatable/pdfmake/pdfmake.min.js') . '"></script>
    <script src="' . asset_url('assets/plugins/datatable/pdfmake/vfs_fonts.js') . '"></script>
    <script src="' . asset_url('assets/plugins/datatable/js/buttons.html5.min.js') . '"></script>
    <script src="' . asset_url('assets/plugins/datatable/js/buttons.print.min.js') . '"></script>

    <script>
    $(document).ready(function() {
        var table = $("#user-monitor-table").DataTable({
            processing: true, serverSide: true,
            ajax: {
                url: "' . base_url('public/api/get_user_monitor_logs.php') . '",
                type: "GET",
                data: function(d) {
                    d.status_filter = $("#filterStatus").val();
                    d.date_from = $("#filterDateFrom").val();
                    d.date_to = $("#filterDateTo").val();
                }
            },
            columns: [
                { data: 0 }, { data: 1 }, { data: 2, orderable: false }, { data: 3 }, { data: 4 }
            ],
            order: [[0, "desc"]], pageLength: 25,
            language: { searchPlaceholder: "Search...", search: "",
                processing: "<div class=\"spinner-modern\" style=\"margin:20px auto;\"></div>" },
            dom: "Blfrtip",
            buttons: [
                { extend: "csv", className: "btn btn-sm btn-outline-primary" },
                { extend: "excel", className: "btn btn-sm btn-outline-primary" },
                { extend: "print", className: "btn btn-sm btn-outline-primary" }
            ]
        });
        $("#applyFilters").click(function() { table.ajax.reload(); });
        $("#refreshBtn").click(function() { table.ajax.reload(null, false); });
    });
    </script>';
include '../includes/footer.php';
?>