<?php
/**
 * ============================================================
 * CAPAPP IT SOLUTION
 * EXPIRED / RENEWAL DOMAINS
 * ============================================================
 */

declare(strict_types=1);

session_start();
require_once 'db.php';


/* ============================================================
   LOGIN CHECK
============================================================ */

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}


/* ============================================================
   HELPER
============================================================ */

function e($value): string
{
    return htmlspecialchars(
        (string)$value,
        ENT_QUOTES,
        'UTF-8'
    );
}


/* ============================================================
   GET DOMAINS
   Expired + next 30 days
============================================================ */

$sql = "
    SELECT
        *,
        DATEDIFF(expiry_date, CURDATE()) AS days_left
    FROM domains
    WHERE expiry_date <= DATE_ADD(CURDATE(), INTERVAL 30 DAY)
    ORDER BY expiry_date ASC
";

$result = mysqli_query($conn, $sql);

if (!$result) {
    die(
        "Database Error: " .
        e(mysqli_error($conn))
    );
}


/* ============================================================
   SUMMARY
============================================================ */

$totalDomains = 0;
$expiredCount = 0;
$todayCount   = 0;
$upcomingCount = 0;


/*
 * Store rows so we can also use them for search
 */
$domains = [];

while ($row = mysqli_fetch_assoc($result)) {

    $domains[] = $row;

    $totalDomains++;

    $days = (int)$row['days_left'];

    if ($days < 0) {

        $expiredCount++;

    } elseif ($days === 0) {

        $todayCount++;

    } else {

        $upcomingCount++;

    }
}

?>


<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta
    name="viewport"
    content="width=device-width, initial-scale=1.0">

<title>Expired Domains | CAPAPP</title>


<!-- Bootstrap -->

<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
rel="stylesheet">


<!-- Bootstrap Icons -->

<link
href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css"
rel="stylesheet">


<style>

/* ============================================================
   ROOT
============================================================ */

:root{

    --primary:#2563eb;
    --primary-dark:#1d4ed8;

    --navy:#0f172a;
    --navy-2:#1e293b;

    --bg:#eef4ff;

    --white:#ffffff;

    --text:#0f172a;

    --muted:#64748b;

    --border:#e2e8f0;

    --danger:#dc2626;

    --danger-light:#fee2e2;

    --warning:#f59e0b;

    --warning-light:#fef3c7;

    --success:#16a34a;

    --success-light:#dcfce7;

    --shadow:
        0 10px 35px
        rgba(15,23,42,.07);

    --shadow-lg:
        0 20px 50px
        rgba(15,23,42,.12);
}


/* ============================================================
   RESET
============================================================ */

*{
    margin:0;
    padding:0;
    box-sizing:border-box;
}

html{
    scroll-behavior:smooth;
}

body{

    font-family:
        "Segoe UI",
        Arial,
        sans-serif;

    background:
        linear-gradient(
            135deg,
            #eef4ff 0%,
            #f8fbff 50%,
            #eef2ff 100%
        );

    color:var(--text);

    min-height:100vh;
}


/* ============================================================
   SIDEBAR
============================================================ */

.sidebar{

    position:fixed;

    left:0;
    top:0;

    width:260px;
    height:100vh;

    padding:20px 15px;

    background:
        linear-gradient(
            180deg,
            #0f172a 0%,
            #172554 55%,
            #1e293b 100%
        );

    z-index:1000;

    overflow-y:auto;

    transition:.3s ease;

    box-shadow:
        10px 0 35px
        rgba(15,23,42,.12);
}


.sidebar::-webkit-scrollbar{
    width:5px;
}

.sidebar::-webkit-scrollbar-thumb{

    background:#334155;

    border-radius:10px;
}


/* ============================================================
   LOGO
============================================================ */

.logo-box{

    text-align:center;

    padding:
        8px
        8px
        22px;

    margin-bottom:12px;

    border-bottom:
        1px solid
        rgba(255,255,255,.08);
}

.logo-icon{

    width:58px;
    height:58px;

    margin:0 auto 10px;

    border-radius:18px;

    display:flex;

    align-items:center;
    justify-content:center;

    color:#fff;

    font-size:28px;

    background:
        linear-gradient(
            135deg,
            #2563eb,
            #7c3aed
        );

    box-shadow:
        0 12px 25px
        rgba(37,99,235,.35);
}

.logo-box h2{

    color:#fff;

    font-size:25px;

    font-weight:800;

    margin:0;
}

.logo-box p{

    color:#94a3b8;

    font-size:12px;

    margin:4px 0 0;
}


/* ============================================================
   MENU
============================================================ */

.menu-title{

    color:#64748b;

    text-transform:uppercase;

    font-size:10px;

    font-weight:800;

    letter-spacing:1.5px;

    padding:
        9px
        14px;
}

.menu a{

    display:flex;

    align-items:center;

    gap:13px;

    color:#cbd5e1;

    text-decoration:none;

    padding:
        13px
        14px;

    margin-bottom:6px;

    border-radius:13px;

    font-size:14px;

    font-weight:600;

    transition:.25s ease;

    position:relative;
}

.menu a i{

    width:22px;

    text-align:center;

    font-size:19px;
}

.menu a:hover{

    background:
        rgba(37,99,235,.18);

    color:#fff;

    transform:
        translateX(3px);
}

.menu a.active{

    background:
        linear-gradient(
            135deg,
            #2563eb,
            #1d4ed8
        );

    color:#fff;

    box-shadow:
        0 8px 20px
        rgba(37,99,235,.25);
}

.menu a.active::before{

    content:"";

    position:absolute;

    left:-15px;

    top:12px;

    width:4px;

    height:28px;

    background:#60a5fa;

    border-radius:
        0 5px 5px 0;
}


/* ============================================================
   MAIN
============================================================ */

.main{

    margin-left:260px;

    padding:25px;

    min-height:100vh;
}


/* ============================================================
   TOPBAR
============================================================ */

.topbar{

    background:#fff;

    border:
        1px solid
        rgba(226,232,240,.8);

    border-radius:20px;

    padding:
        15px
        20px;

    margin-bottom:22px;

    display:flex;

    align-items:center;

    justify-content:space-between;

    gap:15px;

    box-shadow:var(--shadow);
}

.topbar-left{

    display:flex;

    align-items:center;

    gap:13px;
}

.menu-btn{

    display:none;

    width:42px;
    height:42px;

    border:0;

    border-radius:12px;

    background:#eff6ff;

    color:var(--primary);

    font-size:22px;

    align-items:center;

    justify-content:center;

    cursor:pointer;
}

.page-icon{

    width:48px;
    height:48px;

    border-radius:14px;

    display:flex;

    align-items:center;
    justify-content:center;

    background:#fff1f2;

    color:#dc2626;

    font-size:23px;
}

.topbar h1{

    margin:0;

    font-size:21px;

    font-weight:800;
}

.topbar-subtitle{

    color:var(--muted);

    font-size:12px;

    margin-top:2px;
}

.admin-box{

    display:flex;

    align-items:center;

    gap:10px;
}

.admin-text{

    text-align:right;
}

.admin-name{

    font-size:13px;

    font-weight:700;
}

.admin-role{

    color:var(--muted);

    font-size:11px;
}

.admin-icon{

    width:42px;
    height:42px;

    border-radius:50%;

    display:flex;

    align-items:center;
    justify-content:center;

    color:#fff;

    font-size:19px;

    background:
        linear-gradient(
            135deg,
            #2563eb,
            #7c3aed
        );
}


/* ============================================================
   SUMMARY CARDS
============================================================ */

.summary-grid{

    display:grid;

    grid-template-columns:
        repeat(4,1fr);

    gap:17px;

    margin-bottom:22px;
}

.summary-card{

    background:#fff;

    border-radius:19px;

    padding:20px;

    min-height:135px;

    border:
        1px solid
        rgba(226,232,240,.8);

    box-shadow:var(--shadow);

    position:relative;

    overflow:hidden;

    transition:.25s ease;
}

.summary-card:hover{

    transform:
        translateY(-4px);

    box-shadow:var(--shadow-lg);
}

.summary-card::after{

    content:"";

    position:absolute;

    right:-35px;

    bottom:-45px;

    width:120px;
    height:120px;

    border-radius:50%;

    background:currentColor;

    opacity:.05;
}

.summary-top{

    display:flex;

    align-items:center;

    justify-content:space-between;
}

.summary-label{

    color:var(--muted);

    font-size:12px;

    font-weight:700;
}

.summary-card h2{

    margin:
        14px 0 0;

    font-size:30px;

    font-weight:800;
}

.summary-note{

    color:var(--muted);

    font-size:10px;

    margin-top:3px;
}

.summary-icon{

    width:46px;
    height:46px;

    border-radius:14px;

    display:flex;

    align-items:center;
    justify-content:center;

    font-size:22px;
}


/* Total */

.total-card{
    color:#2563eb;
}

.total-card .summary-icon{

    color:#2563eb;

    background:#eff6ff;
}


/* Expired */

.expired-card{
    color:#dc2626;
}

.expired-card .summary-icon{

    color:#dc2626;

    background:#fef2f2;
}


/* Today */

.today-card{
    color:#d97706;
}

.today-card .summary-icon{

    color:#d97706;

    background:#fffbeb;
}


/* Upcoming */

.upcoming-card{
    color:#16a34a;
}

.upcoming-card .summary-icon{

    color:#16a34a;

    background:#ecfdf5;
}


/* ============================================================
   CONTENT CARD
============================================================ */

.content-card{

    background:#fff;

    border-radius:22px;

    box-shadow:var(--shadow);

    border:
        1px solid
        rgba(226,232,240,.8);

    overflow:hidden;
}


/* ============================================================
   CARD HEADER
============================================================ */

.card-header-modern{

    padding:
        19px
        22px;

    display:flex;

    align-items:center;

    justify-content:space-between;

    gap:15px;

    border-bottom:
        1px solid
        #eef2f7;
}

.card-title-box{

    display:flex;

    align-items:center;

    gap:11px;
}

.card-title-icon{

    width:42px;
    height:42px;

    border-radius:12px;

    background:#eff6ff;

    color:#2563eb;

    display:flex;

    align-items:center;
    justify-content:center;

    font-size:20px;
}

.card-title-box h3{

    margin:0;

    font-size:17px;

    font-weight:800;
}

.card-title-box p{

    margin:2px 0 0;

    color:var(--muted);

    font-size:11px;
}


/* ============================================================
   SEARCH
============================================================ */

.search-box{

    position:relative;

    width:280px;
}

.search-box i{

    position:absolute;

    left:13px;

    top:50%;

    transform:
        translateY(-50%);

    color:#94a3b8;

    font-size:16px;
}

.search-box input{

    width:100%;

    height:42px;

    border:
        1px solid
        #dbe3ee;

    border-radius:11px;

    padding:
        0 13px
        0 39px;

    outline:none;

    font-size:12px;

    transition:.2s;
}

.search-box input:focus{

    border-color:#2563eb;

    box-shadow:
        0 0 0 4px
        rgba(37,99,235,.08);
}


/* ============================================================
   TABLE WRAPPER
============================================================ */

.table-wrap{

    width:100%;

    overflow-x:auto;

    padding:0 15px 15px;
}


/* ============================================================
   TABLE
============================================================ */

.modern-table{

    width:100%;

    min-width:1050px;

    border-collapse:separate;

    border-spacing:0 8px;
}

.modern-table thead th{

    background:#f8fafc;

    color:#64748b;

    font-size:10px;

    text-transform:uppercase;

    letter-spacing:.6px;

    font-weight:800;

    padding:
        13px
        14px;

    border-top:
        1px solid
        #eef2f7;

    border-bottom:
        1px solid
        #eef2f7;

    white-space:nowrap;
}

.modern-table thead th:first-child{

    border-radius:
        10px 0 0 10px;
}

.modern-table thead th:last-child{

    border-radius:
        0 10px 10px 0;
}

.modern-table tbody tr{

    transition:.2s ease;
}

.modern-table tbody tr:hover{

    transform:
        translateY(-2px);
}

.modern-table tbody td{

    padding:
        13px
        14px;

    background:#fff;

    border-top:
        1px solid
        #eef2f7;

    border-bottom:
        1px solid
        #eef2f7;

    font-size:12px;

    vertical-align:middle;
}

.modern-table tbody tr td:first-child{

    border-left:
        1px solid
        #eef2f7;

    border-radius:
        12px 0 0 12px;
}

.modern-table tbody tr td:last-child{

    border-right:
        1px solid
        #eef2f7;

    border-radius:
        0 12px 12px 0;
}


/* ============================================================
   EXPIRED ROW
============================================================ */

.modern-table tbody tr.expired-row td{

    background:
        linear-gradient(
            90deg,
            #fff7f7,
            #fff
        );
}


/* ============================================================
   TODAY ROW
============================================================ */

.modern-table tbody tr.today-row td{

    background:
        linear-gradient(
            90deg,
            #fffbeb,
            #fff
        );
}


/* ============================================================
   UPCOMING ROW
============================================================ */

.modern-table tbody tr.upcoming-row td{

    background:
        linear-gradient(
            90deg,
            #f0fdf4,
            #fff
        );
}


/* ============================================================
   ID
============================================================ */

.id-badge{

    width:32px;
    height:32px;

    display:inline-flex;

    align-items:center;
    justify-content:center;

    border-radius:9px;

    background:#f1f5f9;

    color:#475569;

    font-size:11px;

    font-weight:800;
}


/* ============================================================
   DOMAIN
============================================================ */

.domain-info{

    display:flex;

    align-items:center;

    gap:9px;
}

.domain-icon{

    width:37px;
    height:37px;

    border-radius:10px;

    display:flex;

    align-items:center;
    justify-content:center;

    background:#eff6ff;

    color:#2563eb;

    font-size:17px;

    flex-shrink:0;
}

.domain-name{

    font-weight:800;

    color:#0f172a;

    max-width:210px;

    overflow:hidden;

    text-overflow:ellipsis;

    white-space:nowrap;
}

.domain-label{

    display:block;

    color:#94a3b8;

    font-size:9px;

    margin-top:2px;
}


/* ============================================================
   CLIENT
============================================================ */

.client-info{

    display:flex;

    align-items:center;

    gap:8px;
}

.client-avatar{

    width:35px;
    height:35px;

    border-radius:50%;

    display:flex;

    align-items:center;
    justify-content:center;

    background:#f0f9ff;

    color:#0369a1;

    font-size:15px;

    flex-shrink:0;
}

.client-name{

    font-weight:700;

    color:#334155;

    max-width:170px;

    overflow:hidden;

    text-overflow:ellipsis;

    white-space:nowrap;
}


/* ============================================================
   MOBILE
============================================================ */

.mobile-link{

    display:inline-flex;

    align-items:center;

    gap:7px;

    text-decoration:none;

    background:#ecfdf5;

    color:#15803d;

    padding:
        8px
        10px;

    border-radius:10px;

    font-size:11px;

    font-weight:700;

    transition:.2s;
}

.mobile-link:hover{

    background:#dcfce7;

    color:#166534;
}


/* ============================================================
   EXPIRY
============================================================ */

.expiry-date{

    display:inline-flex;

    align-items:center;

    gap:7px;

    background:#f8fafc;

    color:#334155;

    padding:
        8px
        10px;

    border-radius:10px;

    font-size:11px;

    font-weight:700;
}

.expiry-date i{

    color:#2563eb;
}


/* ============================================================
   DAYS
============================================================ */

.days-box{

    display:inline-flex;

    align-items:center;

    gap:6px;

    padding:
        7px
        10px;

    border-radius:10px;

    font-size:11px;

    font-weight:800;
}

.days-expired{

    color:#b91c1c;

    background:#fee2e2;
}

.days-today{

    color:#b45309;

    background:#fef3c7;
}

.days-upcoming{

    color:#15803d;

    background:#dcfce7;
}


/* ============================================================
   STATUS
============================================================ */

.status-badge{

    display:inline-flex;

    align-items:center;

    gap:6px;

    padding:
        7px
        11px;

    border-radius:30px;

    font-size:10px;

    font-weight:800;

    white-space:nowrap;
}

.status-expired{

    color:#fff;

    background:#dc2626;

    box-shadow:
        0 5px 12px
        rgba(220,38,38,.18);
}

.status-today{

    color:#78350f;

    background:#fbbf24;
}

.status-upcoming{

    color:#fff;

    background:#16a34a;
}


/* ============================================================
   WHATSAPP BUTTON
============================================================ */

.whatsapp-btn{

    display:inline-flex;

    align-items:center;

    justify-content:center;

    gap:7px;

    padding:
        8px
        12px;

    border:0;

    border-radius:10px;

    background:#22c55e;

    color:#fff;

    text-decoration:none;

    font-size:11px;

    font-weight:800;

    white-space:nowrap;

    transition:.2s;

    box-shadow:
        0 5px 12px
        rgba(34,197,94,.18);
}

.whatsapp-btn:hover{

    color:#fff;

    background:#16a34a;

    transform:
        translateY(-2px);
}

.whatsapp-btn i{

    font-size:15px;
}


/* ============================================================
   EMPTY STATE
============================================================ */

.empty-state{

    text-align:center;

    padding:
        65px
        20px;
}

.empty-icon{

    width:70px;
    height:70px;

    border-radius:20px;

    margin:0 auto 15px;

    display:flex;

    align-items:center;
    justify-content:center;

    background:#ecfdf5;

    color:#16a34a;

    font-size:30px;
}

.empty-state h4{

    font-size:18px;

    font-weight:800;

    margin-bottom:5px;
}

.empty-state p{

    color:var(--muted);

    font-size:12px;

    margin:0;
}


/* ============================================================
   FOOTER
============================================================ */

.card-footer-modern{

    padding:
        15px
        22px;

    border-top:
        1px solid
        #eef2f7;

    background:#f8fafc;

    display:flex;

    align-items:center;

    justify-content:space-between;

    font-size:11px;

    color:var(--muted);
}

.footer-status{

    display:flex;

    align-items:center;

    gap:7px;
}

.live-dot{

    width:8px;
    height:8px;

    border-radius:50%;

    background:#22c55e;

    box-shadow:
        0 0 0 4px
        rgba(34,197,94,.12);
}


/* ============================================================
   SIDEBAR OVERLAY
============================================================ */

.sidebar-overlay{

    display:none;

    position:fixed;

    inset:0;

    background:
        rgba(15,23,42,.55);

    z-index:999;

    backdrop-filter:blur(2px);
}


/* ============================================================
   RESPONSIVE
============================================================ */

@media(max-width:1100px){

    .summary-grid{

        grid-template-columns:
            repeat(2,1fr);
    }

}


@media(max-width:991px){

    .sidebar{

        left:-100%;

        width:280px;
    }

    .sidebar.active{

        left:0;
    }

    .sidebar-overlay.active{

        display:block;
    }

    .main{

        margin-left:0;

        padding:15px;
    }

    .menu-btn{

        display:flex;
    }

    .topbar{

        padding:
            13px
            15px;
    }

}


@media(max-width:700px){

    .summary-grid{

        grid-template-columns:1fr;
    }

    .card-header-modern{

        align-items:flex-start;

        flex-direction:column;
    }

    .search-box{

        width:100%;
    }

}


@media(max-width:576px){

    .main{

        padding:10px;
    }

    .topbar{

        border-radius:15px;

        margin-bottom:14px;
    }

    .page-icon{

        width:40px;
        height:40px;

        font-size:19px;
    }

    .topbar h1{

        font-size:17px;
    }

    .topbar-subtitle{

        display:none;
    }

    .admin-text{

        display:none;
    }

    .admin-icon{

        width:38px;
        height:38px;
    }

    .summary-card{

        min-height:120px;

        padding:17px;
    }

    .summary-card h2{

        font-size:26px;
    }

    .content-card{

        border-radius:17px;
    }

    .card-header-modern{

        padding:16px;
    }

    .table-wrap{

        padding:
            0 8px 10px;
    }

    .card-footer-modern{

        padding:
            13px
            15px;

        flex-direction:column;

        gap:8px;

        text-align:center;
    }

}


/* ============================================================
   PRINT
============================================================ */

@media print{

    .sidebar,
    .topbar,
    .search-box,
    .card-footer-modern{

        display:none !important;
    }

    .main{

        margin:0;

        padding:0;
    }

    body{

        background:#fff;
    }

    .summary-grid{

        display:none;
    }

    .content-card{

        box-shadow:none;

        border:1px solid #ddd;
    }

}

</style>

</head>


<body>


<!-- =========================================================
     SIDEBAR
========================================================= -->

<div
    class="sidebar"
    id="sidebar">


    <div class="logo-box">

        <div class="logo-icon">

            <i class="bi bi-grid-1x2-fill"></i>

        </div>

        <h2>CAPAPP</h2>

        <p>
            Domain Management Panel
        </p>

    </div>


    <div class="menu">


        <div class="menu-title">
            Main Menu
        </div>


        <a href="dashboard.php">

            <i class="bi bi-speedometer2"></i>

            <span>Dashboard</span>

        </a>


        <a href="index.php">

            <i class="bi bi-globe2"></i>

            <span>All Domains</span>

        </a>


        <a href="add-domain.php">

            <i class="bi bi-plus-circle"></i>

            <span>Add Domain</span>

        </a>


        <div class="menu-title mt-3">
            Finance
        </div>


        <a href="domain-charges.php">

            <i class="bi bi-cash-stack"></i>

            <span>Domain Charges</span>

        </a>


        <a href="add-payment.php">

            <i class="bi bi-wallet2"></i>

            <span>Receive Payment</span>

        </a>


        <a href="domain-ledger.php">

            <i class="bi bi-journal-text"></i>

            <span>Balance Sheet</span>

        </a>


        <a href="bill.php">

            <i class="bi bi-receipt"></i>

            <span>Generate Invoice</span>

        </a>


        <div class="menu-title mt-3">
            Management
        </div>


        <a
            href="expired-domains.php"
            class="active">

            <i class="bi bi-clock-history"></i>

            <span>Expired Domains</span>

        </a>


        <a href="logout.php">

            <i class="bi bi-box-arrow-right"></i>

            <span>Logout</span>

        </a>


    </div>

</div>


<!-- MOBILE OVERLAY -->

<div
    class="sidebar-overlay"
    id="sidebarOverlay"
    onclick="closeMenu()">
</div>


<!-- =========================================================
     MAIN
========================================================= -->

<div class="main">


    <!-- TOPBAR -->

    <div class="topbar">


        <div class="topbar-left">


            <button
                type="button"
                class="menu-btn"
                onclick="toggleMenu()">

                <i class="bi bi-list"></i>

            </button>


            <div class="page-icon">

                <i class="bi bi-exclamation-triangle-fill"></i>

            </div>


            <div>

                <h1>
                    Expired & Renewal Domains
                </h1>

                <div class="topbar-subtitle">

                    Monitor expired domains and upcoming renewals

                </div>

            </div>

        </div>


        <div class="admin-box">


            <div class="admin-text">

                <div class="admin-name">
                    Admin
                </div>

                <div class="admin-role">
                    Administrator
                </div>

            </div>


            <div class="admin-icon">

                <i class="bi bi-person-fill"></i>

            </div>


        </div>

    </div>


    <!-- =====================================================
         SUMMARY CARDS
    ====================================================== -->

    <div class="summary-grid">


        <!-- TOTAL -->

        <div class="summary-card total-card">

            <div class="summary-top">

                <div>

                    <div class="summary-label">
                        Total Alerts
                    </div>

                    <h2>
                        <?= number_format($totalDomains) ?>
                    </h2>

                    <div class="summary-note">
                        Expired + next 30 days
                    </div>

                </div>


                <div class="summary-icon">

                    <i class="bi bi-bell-fill"></i>

                </div>

            </div>

        </div>


        <!-- EXPIRED -->

        <div class="summary-card expired-card">

            <div class="summary-top">

                <div>

                    <div class="summary-label">
                        Expired
                    </div>

                    <h2>
                        <?= number_format($expiredCount) ?>
                    </h2>

                    <div class="summary-note">
                        Renewal required
                    </div>

                </div>


                <div class="summary-icon">

                    <i class="bi bi-x-circle-fill"></i>

                </div>

            </div>

        </div>


        <!-- TODAY -->

        <div class="summary-card today-card">

            <div class="summary-top">

                <div>

                    <div class="summary-label">
                        Expires Today
                    </div>

                    <h2>
                        <?= number_format($todayCount) ?>
                    </h2>

                    <div class="summary-note">
                        Immediate attention
                    </div>

                </div>


                <div class="summary-icon">

                    <i class="bi bi-alarm-fill"></i>

                </div>

            </div>

        </div>


        <!-- UPCOMING -->

        <div class="summary-card upcoming-card">

            <div class="summary-top">

                <div>

                    <div class="summary-label">
                        Upcoming
                    </div>

                    <h2>
                        <?= number_format($upcomingCount) ?>
                    </h2>

                    <div class="summary-note">
                        Within next 30 days
                    </div>

                </div>


                <div class="summary-icon">

                    <i class="bi bi-calendar-check-fill"></i>

                </div>

            </div>

        </div>


    </div>


    <!-- =====================================================
         CONTENT CARD
    ====================================================== -->

    <div class="content-card">


        <!-- HEADER -->

        <div class="card-header-modern">


            <div class="card-title-box">


                <div class="card-title-icon">

                    <i class="bi bi-globe2"></i>

                </div>


                <div>

                    <h3>
                        Renewal Alerts
                    </h3>

                    <p>
                        Domains requiring attention
                    </p>

                </div>


            </div>


            <!-- SEARCH -->

            <div class="search-box">

                <i class="bi bi-search"></i>

                <input
                    type="text"
                    id="domainSearch"
                    placeholder="Search domain, client or mobile...">

            </div>


        </div>


        <!-- =================================================
             TABLE
        ================================================== -->

        <?php if (count($domains) > 0): ?>


            <div class="table-wrap">


                <table
                    class="modern-table"
                    id="domainTable">


                    <thead>

                        <tr>

                            <th>
                                ID
                            </th>

                            <th>
                                Domain
                            </th>

                            <th>
                                Client
                            </th>

                            <th>
                                Mobile
                            </th>

                            <th>
                                Expiry
                            </th>

                            <th>
                                Days
                            </th>

                            <th>
                                Status
                            </th>

                            <th>
                                Reminder
                            </th>

                        </tr>

                    </thead>


                    <tbody>


                    <?php foreach ($domains as $row): ?>


                        <?php

                        $days =
                            (int)$row['days_left'];


                        /*
                         * Determine row class
                         */

                        if ($days < 0) {

                            $rowClass =
                                'expired-row';

                            $daysClass =
                                'days-expired';

                            $statusClass =
                                'status-expired';

                            $statusText =
                                'Expired';

                        } elseif ($days === 0) {

                            $rowClass =
                                'today-row';

                            $daysClass =
                                'days-today';

                            $statusClass =
                                'status-today';

                            $statusText =
                                'Expires Today';

                        } else {

                            $rowClass =
                                'upcoming-row';

                            $daysClass =
                                'days-upcoming';

                            $statusClass =
                                'status-upcoming';

                            $statusText =
                                'Renew Soon';
                        }


                        /*
                         * Clean mobile number
                         */

                        $mobileRaw =
                            (string)(
                                $row['client_mobile']
                                ?? ''
                            );

                        $mobileDigits =
                            preg_replace(
                                '/[^0-9]/',
                                '',
                                $mobileRaw
                            );


                        /*
                         * WhatsApp number
                         *
                         * If number already has 91,
                         * don't add 91 again.
                         */

                        if (
                            strlen($mobileDigits) === 10
                        ) {

                            $waNumber =
                                '91' .
                                $mobileDigits;

                        } else {

                            $waNumber =
                                $mobileDigits;
                        }


                        /*
                         * WhatsApp message
                         */

                        if ($days < 0) {

                            $msg =
                                "Dear " .
                                ($row['client_name'] ?: 'Customer') .
                                ", your domain " .
                                $row['domain_name'] .
                                " expired on " .
                                $row['expiry_date'] .
                                ". Please contact CAPAPP IT SOLUTION for renewal. Thank you.";

                        } elseif ($days === 0) {

                            $msg =
                                "Dear " .
                                ($row['client_name'] ?: 'Customer') .
                                ", your domain " .
                                $row['domain_name'] .
                                " expires today (" .
                                $row['expiry_date'] .
                                "). Please renew it soon to avoid interruption. - CAPAPP IT SOLUTION";

                        } else {

                            $msg =
                                "Dear " .
                                ($row['client_name'] ?: 'Customer') .
                                ", your domain " .
                                $row['domain_name'] .
                                " is due to expire on " .
                                $row['expiry_date'] .
                                " (" .
                                $days .
                                " days left). Please renew it soon. - CAPAPP IT SOLUTION";
                        }


                        $waUrl =
                            "https://wa.me/" .
                            $waNumber .
                            "?text=" .
                            urlencode($msg);

                        ?>


                        <tr
                            class="<?= e($rowClass) ?>"
                            data-search="<?= e(
                                strtolower(
                                    ($row['domain_name'] ?? '') .
                                    ' ' .
                                    ($row['client_name'] ?? '') .
                                    ' ' .
                                    ($row['client_mobile'] ?? '')
                                )
                            ) ?>">


                            <!-- ID -->

                            <td>

                                <span class="id-badge">

                                    #<?= e($row['id']) ?>

                                </span>

                            </td>


                            <!-- DOMAIN -->

                            <td>

                                <div class="domain-info">


                                    <div class="domain-icon">

                                        <i class="bi bi-globe2"></i>

                                    </div>


                                    <div>

                                        <div class="domain-name">

                                            <?= e(
                                                $row['domain_name']
                                            ) ?>

                                        </div>

                                        <span class="domain-label">
                                            Domain
                                        </span>

                                    </div>


                                </div>

                            </td>


                            <!-- CLIENT -->

                            <td>

                                <div class="client-info">


                                    <div class="client-avatar">

                                        <i class="bi bi-person-fill"></i>

                                    </div>


                                    <div class="client-name">

                                        <?= e(
                                            $row['client_name']
                                            ?: '-'
                                        ) ?>

                                    </div>


                                </div>

                            </td>


                            <!-- MOBILE -->

                            <td>

                                <?php if ($mobileDigits !== ''): ?>

                                    <a
                                        href="tel:<?= e($mobileDigits) ?>"
                                        class="mobile-link">

                                        <i class="bi bi-telephone-fill"></i>

                                        <?= e(
                                            $row['client_mobile']
                                        ) ?>

                                    </a>

                                <?php else: ?>

                                    <span class="text-muted">
                                        No mobile
                                    </span>

                                <?php endif; ?>

                            </td>


                            <!-- EXPIRY -->

                            <td>

                                <span class="expiry-date">

                                    <i class="bi bi-calendar3"></i>

                                    <?= e(
                                        date(
                                            'd M Y',
                                            strtotime(
                                                $row['expiry_date']
                                            )
                                        )
                                    ) ?>

                                </span>

                            </td>


                            <!-- DAYS -->

                            <td>

                                <?php if ($days < 0): ?>

                                    <span
                                        class="days-box <?= e($daysClass) ?>">

                                        <i class="bi bi-arrow-down-circle"></i>

                                        <?= abs($days) ?> days ago

                                    </span>

                                <?php elseif ($days === 0): ?>

                                    <span
                                        class="days-box <?= e($daysClass) ?>">

                                        <i class="bi bi-alarm"></i>

                                        Today

                                    </span>

                                <?php else: ?>

                                    <span
                                        class="days-box <?= e($daysClass) ?>">

                                        <i class="bi bi-clock"></i>

                                        <?= $days ?> days left

                                    </span>

                                <?php endif; ?>

                            </td>


                            <!-- STATUS -->

                            <td>

                                <span
                                    class="status-badge <?= e($statusClass) ?>">

                                    <?php if ($days < 0): ?>

                                        <i class="bi bi-x-circle-fill"></i>

                                    <?php elseif ($days === 0): ?>

                                        <i class="bi bi-exclamation-circle-fill"></i>

                                    <?php else: ?>

                                        <i class="bi bi-clock-fill"></i>

                                    <?php endif; ?>


                                    <?= e($statusText) ?>

                                </span>

                            </td>


                            <!-- WHATSAPP -->

                            <td>

                                <?php if ($mobileDigits !== ''): ?>

                                    <a
                                        href="<?= e($waUrl) ?>"
                                        target="_blank"
                                        rel="noopener noreferrer"
                                        class="whatsapp-btn">

                                        <i class="bi bi-whatsapp"></i>

                                        Send Reminder

                                    </a>

                                <?php else: ?>

                                    <span
                                        class="text-muted small">

                                        No number

                                    </span>

                                <?php endif; ?>

                            </td>


                        </tr>


                    <?php endforeach; ?>


                    </tbody>

                </table>


                <!-- NO SEARCH RESULT -->

                <div
                    id="noSearchResult"
                    class="empty-state"
                    style="display:none;">

                    <div class="empty-icon">

                        <i class="bi bi-search"></i>

                    </div>

                    <h4>
                        No Matching Domain
                    </h4>

                    <p>
                        Try searching with another domain, client or mobile number.
                    </p>

                </div>


            </div>


        <?php else: ?>


            <!-- EMPTY -->

            <div class="empty-state">

                <div class="empty-icon">

                    <i class="bi bi-check-circle-fill"></i>

                </div>

                <h4>
                    No Renewal Alerts
                </h4>

                <p>
                    There are no expired or upcoming domains within the next 30 days.
                </p>

            </div>


        <?php endif; ?>


        <!-- FOOTER -->

        <div class="card-footer-modern">


            <div class="footer-status">

                <span class="live-dot"></span>

                Renewal monitoring active

            </div>


            <div>

                Showing
                <strong>
                    <?= number_format($totalDomains) ?>
                </strong>
                domain alerts

            </div>


        </div>


    </div>


</div>


<!-- =========================================================
     JAVASCRIPT
========================================================= -->

<script>

/* ============================================================
   SIDEBAR TOGGLE
============================================================ */

function toggleMenu(){

    const sidebar =
        document.getElementById('sidebar');

    const overlay =
        document.getElementById('sidebarOverlay');

    sidebar.classList.toggle('active');

    overlay.classList.toggle('active');
}


function closeMenu(){

    const sidebar =
        document.getElementById('sidebar');

    const overlay =
        document.getElementById('sidebarOverlay');

    sidebar.classList.remove('active');

    overlay.classList.remove('active');
}


/* ============================================================
   SEARCH
============================================================ */

const searchInput =
    document.getElementById('domainSearch');

const table =
    document.getElementById('domainTable');

const noSearchResult =
    document.getElementById('noSearchResult');


if(searchInput && table){

    searchInput.addEventListener(
        'input',
        function(){

            const keyword =
                this.value
                    .toLowerCase()
                    .trim();

            const rows =
                table.querySelectorAll(
                    'tbody tr'
                );

            let visibleCount = 0;


            rows.forEach(function(row){

                const text =
                    row.getAttribute(
                        'data-search'
                    ) || '';


                if(
                    text.includes(keyword)
                ){

                    row.style.display = '';

                    visibleCount++;

                }else{

                    row.style.display = 'none';

                }

            });


            if(noSearchResult){

                if(
                    keyword !== '' &&
                    visibleCount === 0
                ){

                    noSearchResult.style.display =
                        'block';

                    table.style.display =
                        'none';

                }else{

                    noSearchResult.style.display =
                        'none';

                    table.style.display =
                        '';
                }

            }

        }
    );

}


/* ============================================================
   CLOSE SIDEBAR AFTER LINK CLICK
============================================================ */

document
    .querySelectorAll('.sidebar a')
    .forEach(function(link){

        link.addEventListener(
            'click',
            function(){

                if(window.innerWidth <= 991){

                    closeMenu();

                }

            }
        );

    });


/* ============================================================
   ESC KEY
============================================================ */

document.addEventListener(
    'keydown',
    function(event){

        if(event.key === 'Escape'){

            closeMenu();

        }

    }
);

</script>


</body>
</html>