<?php
include('header.php');

$result = mysqli_query($conn, "SELECT * FROM delhi_birth WHERE userid='" . $_SESSION['phone'] . "' ORDER BY id DESC");
$total_certificates = mysqli_num_rows($result);

// Get overall statistics
$stats_query = mysqli_query($conn, "SELECT COUNT(*) as total FROM delhi_birth");
$global_total = ($stats_query && mysqli_num_rows($stats_query) > 0) ? mysqli_fetch_assoc($stats_query)['total'] : 0;

// Get user's recent activities 
$recent_query = mysqli_query($conn, "SELECT * FROM delhi_birth WHERE userid='" . $_SESSION['phone'] . "' ORDER BY created_at DESC LIMIT 5");
?>

<!-- Stats Banner -->
<div style="background: linear-gradient(135deg, #1e40af 0%, #1e3a8a 100%); color: white; padding: 25px 0; margin-bottom: 25px;">
    <div class="container">
        <div class="row text-center">
            <div class="col-md-3 col-6">
                <div style="font-size: 32px; font-weight: 800;"><?php echo number_format($global_total); ?>+</div>
                <div style="font-size: 14px; opacity: 0.9;">Total Certificates</div>
            </div>
            <div class="col-md-3 col-6">
                <div style="font-size: 32px; font-weight: 800;"><?php echo $total_certificates; ?></div>
                <div style="font-size: 14px; opacity: 0.9;">Your Certificates</div>
            </div>
            <div class="col-md-3 col-6">
                <div style="font-size: 32px; font-weight: 800;">24/7</div>
                <div style="font-size: 14px; opacity: 0.9;">Online Service</div>
            </div>
            <div class="col-md-3 col-6">
                <div style="font-size: 32px; font-weight: 800;">100%</div>
                <div style="font-size: 14px; opacity: 0.9;">Secure & Verified</div>
            </div>
        </div>
    </div>
</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.4/css/dataTables.bootstrap4.min.css">

<style>
.delhi-list-card {
    max-width: 1200px;
    margin: 25px auto;
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.1);
    overflow: hidden;
}
.delhi-list-header {
    background: linear-gradient(135deg, #00008B 0%, #1a3a8a 100%);
    color: #fff;
    padding: 25px 35px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.delhi-list-header h4 { margin: 0; font-weight: 700; font-size: 22px; }
.delhi-list-body { padding: 25px 35px; }
.btn-add {
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    color: #fff;
    padding: 12px 25px;
    border-radius: 12px;
    font-weight: 600;
    text-decoration: none;
    font-size: 14px;
    transition: all 0.3s ease;
    box-shadow: 0 4px 12px rgba(16,185,129,0.3);
}
.btn-add:hover { 
    color: #fff; 
    text-decoration: none; 
    transform: translateY(-2px); 
    box-shadow: 0 6px 20px rgba(16,185,129,0.4);
}
.btn-pdf {
    background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
    color: #fff;
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 600;
    text-decoration: none;
    transition: all 0.3s ease;
    margin-right: 5px;
    box-shadow: 0 2px 8px rgba(239,68,68,0.3);
}
.btn-pdf:hover { 
    color: #fff; 
    text-decoration: none; 
    transform: translateY(-1px); 
    box-shadow: 0 4px 15px rgba(239,68,68,0.4);
}
.btn-view {
    background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
    color: #fff;
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 600;
    text-decoration: none;
    transition: all 0.3s ease;
    margin-right: 5px;
    box-shadow: 0 2px 8px rgba(59,130,246,0.3);
}
.btn-view:hover { 
    color: #fff; 
    text-decoration: none; 
    transform: translateY(-1px); 
    box-shadow: 0 4px 15px rgba(59,130,246,0.4);
}
.btn-verify {
    background: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%);
    color: #fff;
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 600;
    text-decoration: none;
    transition: all 0.3s ease;
    box-shadow: 0 2px 8px rgba(139,92,246,0.3);
}
.btn-verify:hover { 
    color: #fff; 
    text-decoration: none; 
    transform: translateY(-1px); 
    box-shadow: 0 4px 15px rgba(139,92,246,0.4);
}
table.dataTable thead th {
    background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
    font-size: 13px;
    font-weight: 700;
    color: #1e293b;
    text-transform: uppercase;
    border-bottom: 2px solid #e2e8f0;
}
table.dataTable tbody td { 
    font-size: 13px; 
    vertical-align: middle; 
    padding: 12px 8px;
}
.status-badge {
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
}
.status-approved { 
    background: #d1fae5; 
    color: #065f46; 
}
.certificate-card {
    background: #ffffff;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 15px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
    transition: all 0.3s ease;
}
.certificate-card:hover {
    box-shadow: 0 4px 20px rgba(0,0,0,0.1);
    transform: translateY(-2px);
}
.quick-actions {
    background: #f8fafc;
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 25px;
}
.service-feature {
    text-align: center;
    padding: 20px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
    margin-bottom: 20px;
}
.service-feature i {
    font-size: 36px;
    color: #3b82f6;
    margin-bottom: 15px;
}
.service-feature h6 {
    font-weight: 700;
    color: #1e293b;
    margin-bottom: 10px;
}
.service-feature p {
    color: #64748b;
    font-size: 14px;
    margin: 0;
}
</style>

<div class="delhi-list-card">
    <div class="delhi-list-header">
        <div>
            <h4><i class="fas fa-certificate"></i> Birth Certificate Portal</h4>
            <small style="opacity:0.9;">Municipal Corporation of Delhi - Digital Certificate Management</small>
        </div>
        <a href="dehli_birth_apply" class="btn-add"><i class="fas fa-plus-circle"></i> New Application</a>
    </div>
    <div class="delhi-list-body">
        <!-- Quick Services -->
        <?php if ($total_certificates == 0): ?>
        <div class="quick-actions">
            <h6 style="color: #1e3a8a; margin-bottom: 20px; text-align: center;"><i class="fas fa-rocket"></i> Get Started with Delhi Birth Certificate</h6>
            <div class="row">
                <div class="col-md-3">
                    <div class="service-feature">
                        <i class="fas fa-edit"></i>
                        <h6>Apply Online</h6>
                        <p>Submit application from anywhere, anytime</p>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="service-feature">
                        <i class="fas fa-clock"></i>
                        <h6>Instant Process</h6>
                        <p>Get certificate immediately after payment</p>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="service-feature">
                        <i class="fas fa-download"></i>
                        <h6>Download PDF</h6>
                        <p>Download digitally signed certificate</p>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="service-feature">
                        <i class="fas fa-shield-alt"></i>
                        <h6>Verify Online</h6>
                        <p>QR code verification available</p>
                    </div>
                </div>
            </div>
        </div>
        <?php endif; ?>
        <?php if ($result && mysqli_num_rows($result) > 0): ?>
        <div class="table-responsive">
            <table id="delhiBirthTable" class="table table-bordered table-striped" style="width:100%">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Registration No</th>
                        <th>Name</th>
                        <th>Father Name</th>
                        <th>DOB</th>
                        <th>Issue Date</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $i = 1; while ($row = mysqli_fetch_assoc($result)): ?>
                    <tr>
                        <td><strong><?php echo $i++; ?></strong></td>
                        <td>
                            <code style="background: #e0e7ff; color: #1e40af; padding: 3px 8px; border-radius: 6px; font-size: 12px;">
                                <?php echo $row['registration_no']; ?>
                            </code>
                        </td>
                        <td><strong><?php echo $row['name']; ?></strong></td>
                        <td><?php echo $row['father_name']; ?></td>
                        <td><?php echo date('d-M-Y', strtotime($row['date_of_birth'])); ?></td>
                        <td><?php echo date('d-M-Y', strtotime($row['date_of_issue'])); ?></td>
                        <td>
                            <span class="status-badge status-approved">
                                <i class="fas fa-check-circle"></i> Approved
                            </span>
                        </td>
                        <td>
                            <a href="dehli_birth_view?reg_no=<?php echo urlencode($row['registration_no']); ?>" class="btn-view" target="_blank" title="View Certificate">
                                <i class="fas fa-eye"></i> View
                            </a>
                            <a href="dehli_birth_pdf?reg_no=<?php echo urlencode($row['registration_no']); ?>" class="btn-pdf" target="_blank" title="Download PDF">
                                <i class="fas fa-file-pdf"></i> Download
                            </a>
                            <a href="../verify_certificate.php?reg=<?php echo urlencode($row['registration_no']); ?>" class="btn-verify" target="_blank" title="Verify Certificate">
                                <i class="fas fa-shield-alt"></i> Verify
                            </a>
                        </td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
        <?php else: ?>
        <div style="text-align:center; padding:60px 20px; color:#64748b;">
            <div style="font-size: 80px; color: #cbd5e1; margin-bottom: 30px;"><i class="fas fa-certificate"></i></div>
            <h5 style="color: #374151; margin-bottom: 15px;">No Birth Certificates Yet</h5>
            <p style="font-size:16px; margin-bottom: 30px; max-width: 400px; margin-left: auto; margin-right: auto;">Apply for your first Delhi Birth Certificate and get instant digital certificate with QR code verification.</p>
            <div style="margin-bottom: 30px;">
                <a href="dehli_birth_apply" class="btn-add" style="font-size: 16px; padding: 15px 30px;">
                    <i class="fas fa-plus"></i> Apply for Birth Certificate
                </a>
            </div>
            <div class="row" style="margin-top: 40px;">
                <div class="col-md-4">
                    <div style="color: #3b82f6; font-size: 24px; margin-bottom: 10px;"><i class="fas fa-bolt"></i></div>
                    <h6 style="color: #374151;">Instant Process</h6>
                    <p style="font-size: 14px;">Get your certificate immediately after payment</p>
                </div>
                <div class="col-md-4">
                    <div style="color: #10b981; font-size: 24px; margin-bottom: 10px;"><i class="fas fa-shield-alt"></i></div>
                    <h6 style="color: #374151;">100% Secure</h6>
                    <p style="font-size: 14px;">Government verified digital certificates</p>
                </div>
                <div class="col-md-4">
                    <div style="color: #f59e0b; font-size: 24px; margin-bottom: 10px;"><i class="fas fa-qrcode"></i></div>
                    <h6 style="color: #374151;">QR Verification</h6>
                    <p style="font-size: 14px;">Verify authenticity with QR code scan</p>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

<script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.4/js/dataTables.bootstrap4.min.js"></script>
<script>
$(document).ready(function() {
    $('#delhiBirthTable').DataTable({
        "order": [[0, "desc"]],
        "pageLength": 25,
        "language": { "search": "Search:", "lengthMenu": "Show _MENU_ entries" }
    });
});
</script>

<script src="plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
<script src="dist/js/adminlte.js"></script>
</body>
</html>
