<?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';
?>

<div class="container-fluid px-4">
    <!-- Breadcrumb -->
    <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">Import Contacts</li>
        </ol>
    </nav>

    <div class="row">
        <!-- Import Form -->
        <div class="col-lg-8">
            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="bi bi-upload me-2"></i>Import Contacts from Excel</h5>
                </div>
                <div class="card-body">

                    <?php if (isset($_SESSION['import_errors']) && !empty($_SESSION['import_errors'])): ?>
                        <div class="alert alert-warning alert-dismissible fade show" role="alert">
                            <strong>Import Errors:</strong>
                            <ul class="mb-0 mt-2">
                                <?php foreach (array_slice($_SESSION['import_errors'], 0, 10) as $error): ?>
                                    <li><?= htmlspecialchars($error) ?></li>
                                <?php endforeach; ?>
                                <?php if (count($_SESSION['import_errors']) > 10): ?>
                                    <li><em>... and <?= count($_SESSION['import_errors']) - 10 ?> more errors</em></li>
                                <?php endif; ?>
                            </ul>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                        <?php unset($_SESSION['import_errors']); ?>
                    <?php endif; ?>

                    <form action="<?php echo URL_PATH; ?>/admin/excel/process-import" method="POST" enctype="multipart/form-data" id="importForm">
                        <div class="mb-3">
                            <label for="user_id" class="form-label">Select User <span class="text-danger">*</span></label>
                            <select class="form-select" id="user_id" name="user_id" required>
                                <option value="">-- Select User --</option>
                                <?php foreach ($users as $user): ?>
                                    <option value="<?= $user['id'] ?>"><?= htmlspecialchars($user['username']) ?> (<?= htmlspecialchars($user['email']) ?>)</option>
                                <?php endforeach; ?>
                            </select>
                            <div class="form-text">Contacts will be assigned to this user</div>
                        </div>

                        <div class="mb-3">
                            <label for="excel_file" class="form-label">Excel File <span class="text-danger">*</span></label>
                            <input type="file" class="form-control" id="excel_file" name="excel_file" accept=".xlsx,.xls,.csv" required>
                            <div class="form-text">Supported formats: .xlsx, .xls, .csv (Max 10MB)</div>
                        </div>

                        <div class="d-grid gap-2">
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-upload me-2"></i>Import Contacts
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Instructions -->
        <div class="col-lg-4">
            <div class="card shadow-sm mb-3">
                <div class="card-header bg-info text-white">
                    <h6 class="mb-0"><i class="bi bi-info-circle me-2"></i>Instructions</h6>
                </div>
                <div class="card-body">
                    <h6>Excel File Format:</h6>
                    <p class="small mb-2">Your Excel file must have these columns:</p>
                    <ul class="small">
                        <li><strong>full_name</strong> (required)</li>
                        <li><strong>contact_number</strong> (required, 10 digits)</li>
                        <li><strong>date_of_birth</strong> (required, YYYY-MM-DD)</li>
                        <li><strong>email</strong> (optional)</li>
                        <li><strong>note</strong> (optional)</li>
                    </ul>
                    
                    <hr>
                    
                    <h6>Date Format:</h6>
                    <p class="small mb-2">Accepted formats:</p>
                    <ul class="small">
                        <li>YYYY-MM-DD (e.g., 1990-01-15)</li>
                        <li>DD-MM-YYYY (e.g., 15-01-1990)</li>
                        <li>DD/MM/YYYY (e.g., 15/01/1990)</li>
                    </ul>
                    
                    <hr>
                    
                    <h6>Important Notes:</h6>
                    <ul class="small mb-0">
                        <li>First row must contain column headers</li>
                        <li>Phone numbers must be exactly 10 digits</li>
                        <li>Duplicate phone numbers will be skipped</li>
                        <li>Invalid rows will be reported</li>
                    </ul>
                </div>
            </div>

            <div class="card shadow-sm">
                <div class="card-header bg-success text-white">
                    <h6 class="mb-0"><i class="bi bi-download me-2"></i>Sample Template</h6>
                </div>
                <div class="card-body">
                    <p class="small">Download a sample Excel template with the correct format and example data.</p>
                    <a href="<?php echo URL_PATH; ?>/admin/excel/download-template" class="btn btn-success btn-sm w-100">
                        <i class="bi bi-download me-2"></i>Download Template
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

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