<?php
require_once APP_PATH . '/views/layouts/header.php';
require_once APP_PATH . '/views/layouts/sidebar.php';
require_once APP_PATH . '/views/layouts/navbar.php';

$contactModel = new Contact();
$totalContacts = $contactModel->count();
?>

<div class="container-fluid px-4">
    <nav aria-label="breadcrumb" class="mt-4">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="<?php echo URL_PATH; ?>/admin/dashboard">Dashboard</a></li>
            <li class="breadcrumb-item active">Export Contacts</li>
        </ol>
    </nav>

    <!-- Page Header -->
    <div class="d-flex align-items-center justify-content-between mb-4">
        <div>
            <h4 class="mb-1"><i class="bi bi-file-earmark-excel text-success me-2"></i>Export Contacts</h4>
            <p class="text-muted mb-0 small">Download contacts as Excel — export all at once or per individual user</p>
        </div>
        <!-- Export All -->
        <form method="POST" action="<?php echo URL_PATH; ?>/admin/excel/process-export">
            <input type="hidden" name="user_id" value="">
            <button type="submit" class="btn btn-success">
                <i class="bi bi-download me-2"></i>Export All (<?= $totalContacts ?> contacts)
            </button>
        </form>
    </div>

    <?php if ($totalContacts === 0): ?>
        <div class="alert alert-info">
            <i class="bi bi-info-circle me-2"></i>
            No contacts available to export. Please add contacts first.
        </div>
    <?php else: ?>

    <!-- Per-User Export Cards -->
    <div class="row g-3">
        <?php foreach ($users as $user):
            $count  = $contactModel->count($user['id']);
            $active = $contactModel->countActive($user['id']);
            $initials = strtoupper(substr($user['username'], 0, 2));
            $colors = ['#667eea','#48bb78','#ed8936','#e53e3e','#38b2ac','#9f7aea','#f6ad55'];
            $color  = $colors[$user['id'] % count($colors)];
        ?>
        <div class="col-xl-3 col-lg-4 col-md-6">
            <div class="card shadow-sm h-100 border-0" style="border-top: 4px solid <?= $color ?> !important;">
                <div class="card-body">
                    <!-- User Info -->
                    <div class="d-flex align-items-center mb-3">
                        <div class="rounded-circle d-flex align-items-center justify-content-center me-3 flex-shrink-0"
                             style="width:46px;height:46px;background:<?= $color ?>20;color:<?= $color ?>;font-weight:700;font-size:1rem;">
                            <?= $initials ?>
                        </div>
                        <div class="overflow-hidden">
                            <h6 class="mb-0 text-truncate"><?= htmlspecialchars($user['username']) ?></h6>
                            <small class="text-muted text-truncate d-block"><?= htmlspecialchars($user['email']) ?></small>
                        </div>
                    </div>

                    <!-- Stats -->
                    <div class="d-flex gap-3 mb-3">
                        <div class="text-center">
                            <div class="fw-bold fs-5" style="color:<?= $color ?>"><?= $count ?></div>
                            <div class="text-muted" style="font-size:0.72rem;">Total</div>
                        </div>
                        <div class="text-center">
                            <div class="fw-bold fs-5 text-success"><?= $active ?></div>
                            <div class="text-muted" style="font-size:0.72rem;">Active</div>
                        </div>
                        <div class="text-center">
                            <div class="fw-bold fs-5 text-warning"><?= $count - $active ?></div>
                            <div class="text-muted" style="font-size:0.72rem;">Inactive</div>
                        </div>
                    </div>

                    <!-- Export Button -->
                    <?php if ($count > 0): ?>
                        <form method="POST" action="<?php echo URL_PATH; ?>/admin/excel/process-export">
                            <input type="hidden" name="user_id" value="<?= $user['id'] ?>">
                            <button type="submit" class="btn btn-sm w-100 text-white" style="background:<?= $color ?>">
                                <i class="bi bi-file-earmark-excel me-1"></i>Export <?= $count ?> Contacts
                            </button>
                        </form>
                    <?php else: ?>
                        <button class="btn btn-sm btn-outline-secondary w-100" disabled>
                            <i class="bi bi-slash-circle me-1"></i>No Contacts
                        </button>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>

    <!-- Summary Row -->
    <div class="row g-3 mt-2">
        <div class="col-12">
            <div class="card shadow-sm border-0 bg-light">
                <div class="card-body py-3">
                    <div class="d-flex flex-wrap gap-4 align-items-center">
                        <div><span class="text-muted small">Total Users:</span> <strong><?= count($users) ?></strong></div>
                        <div><span class="text-muted small">Total Contacts:</span> <strong><?= $totalContacts ?></strong></div>
                        <div><span class="text-muted small">Active Contacts:</span> <strong class="text-success"><?= $contactModel->countActive() ?></strong></div>
                        <div class="ms-auto">
                            <small class="text-muted"><i class="bi bi-info-circle me-1"></i>Files over 1000 contacts are packaged as ZIP</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php endif; ?>
</div>

<?php require_once APP_PATH . '/views/layouts/footer.php'; ?>
