<?php 
$pageTitle = 'Add Contact';
$activePage = 'add-contact';
include __DIR__ . '/_header.php'; 
?>

<header class="pwa-header">
    <div class="container-fluid">
        <div class="d-flex align-items-center justify-content-between">
            <a href="<?php echo URL_PATH; ?>/home" class="btn-icon">
                <i class="bi bi-arrow-left"></i>
            </a>
            <h1 class="header-title mb-0">Add Contact</h1>
            <a href="<?php echo URL_PATH; ?>/logout" class="btn-icon" title="Logout">
                <i class="bi bi-box-arrow-right"></i>
            </a>
        </div>
    </div>
</header>

<main class="pwa-content">
    <div class="container-fluid">
        <form method="POST" action="<?php echo URL_PATH; ?>/add-contact" class="needs-validation" novalidate>
            <div class="card mb-3">
                <div class="card-body">
                    <div class="mb-3">
                        <label for="full_name" class="form-label">Full Name *</label>
                        <input type="text" class="form-control" id="full_name" name="full_name" required>
                        <div class="invalid-feedback">Please enter a name</div>
                    </div>

                    <div class="mb-3">
                        <label for="phone_number" class="form-label">Phone Number *</label>
                        <input type="tel" class="form-control" id="phone_number" name="phone_number" 
                            required
                               placeholder="10 digit number">
                        <div class="invalid-feedback">Please enter a valid 10-digit phone number</div>
                    </div>

                    <div class="mb-3">
                        <label for="designation" class="form-label">Designation</label>
                        <input type="text" class="form-control" id="designation" name="designation" 
                               placeholder="Job title or designation">
                    </div>

                    <div class="mb-3">
                        <label for="village" class="form-label">Village</label>
                        <input type="text" class="form-control" id="village" name="village" 
                               placeholder="Village or location">
                    </div>

                    <div class="mb-3">
                        <label for="notes" class="form-label">
                            <i class="bi bi-sticky me-1"></i>
                            Notes
                        </label>
                        <textarea class="form-control" id="notes" name="notes" rows="3" 
                                  placeholder="Add any notes or reminders about this contact..."></textarea>
                        <small class="text-muted">Optional: Add relationship, preferences, or any other details</small>
                    </div>

                    <div class="mb-3">
                        <label for="date_of_birth" class="form-label">Date of Birth *</label>
                        <input type="date" class="form-control" id="date_of_birth" name="date_of_birth" required>
                        <div class="invalid-feedback">Please select a date of birth</div>
                    </div>

                    <button type="submit" class="btn btn-primary w-100">
                        <i class="bi bi-person-plus me-2"></i>
                        Add Contact
                    </button>
                </div>
            </div>
        </form>
    </div>
</main>

<?php 
$additionalJS = <<<'JS'
<script>
    // Form validation
    (function() {
        'use strict';
        const forms = document.querySelectorAll('.needs-validation');
        Array.from(forms).forEach(form => {
            form.addEventListener('submit', event => {
                if (!form.checkValidity()) {
                    event.preventDefault();
                    event.stopPropagation();
                }
                form.classList.add('was-validated');
            }, false);
        });
    })();

    document.getElementById('phone_number').addEventListener('input', function() {
        let digits = this.value.replace(/\D/g, '');
    
        // If number is longer than 10 digits (like +91XXXXXXXXXX)
        if (digits.length > 10) {
            digits = digits.slice(-10); // take last 10 digits
        }
    
        this.value = digits;
    });

</script>
JS;

include __DIR__ . '/_footer.php'; 
?>
