<?php include APP_PATH . '/views/layouts/header.php'; ?>

<div class="container-fluid">
    <div class="page-header">
        <h1>Edit Contact</h1>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="<?php echo URL_PATH; ?>/admin/dashboard">Dashboard</a></li>
                <li class="breadcrumb-item"><a href="<?php echo URL_PATH; ?>/admin/contacts">Contacts</a></li>
                <li class="breadcrumb-item active">Edit</li>
            </ol>
        </nav>
    </div>
    
    <div class="row">
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header">
                    <i class="bi bi-pencil"></i> Edit Contact Information
                </div>
                <div class="card-body">
                    <?php if (isset($errorMessage)): ?>
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            <i class="bi bi-exclamation-triangle"></i>
                            <?php echo $errorMessage; ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <form method="POST" action="<?php echo URL_PATH; ?>/admin/contacts/edit/<?php echo $contact->id; ?>" class="needs-validation" novalidate>
                        <div class="mb-3">
                            <label for="user_id" class="form-label">Assign to User *</label>
                            <select class="form-select" id="user_id" name="user_id" required>
                                <option value="">Select User</option>
                                <?php foreach ($users as $user): ?>
                                    <option value="<?php echo $user['id']; ?>" <?php echo (isset($_POST['user_id']) ? $_POST['user_id'] : $contact->user_id) == $user['id'] ? 'selected' : ''; ?>>
                                        <?php echo htmlspecialchars($user['username']); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                            <div class="invalid-feedback">Please select a user</div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="full_name" class="form-label">Full Name *</label>
                                <input type="text" class="form-control" id="full_name" name="full_name" 
                                       value="<?php echo htmlspecialchars($_POST['full_name'] ?? $contact->full_name); ?>" required>
                                <div class="invalid-feedback">Please enter full name</div>
                            </div>
                            
                            <div class="col-md-6 mb-3">
                                <label for="contact_number" class="form-label">Contact Number *</label>
                                <input type="text" class="form-control phone-input" id="contact_number" name="contact_number" 
                                       value="<?php echo htmlspecialchars($_POST['contact_number'] ?? $contact->contact_number); ?>" 
                                       maxlength="10" pattern="[0-9]{10}" required>
                                <small class="text-muted">Exactly 10 digits</small>
                                <div class="invalid-feedback">Please enter 10 digit phone number</div>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="email" class="form-label">Email</label>
                                <input type="email" class="form-control" id="email" name="email" 
                                       value="<?php echo htmlspecialchars($_POST['email'] ?? $contact->email); ?>">
                            </div>
                            
                            <div class="col-md-6 mb-3">
                                <label for="designation" class="form-label">Designation</label>
                                <input type="text" class="form-control" id="designation" name="designation" 
                                       value="<?php echo htmlspecialchars($_POST['designation'] ?? $contact->designation); ?>">
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="village" class="form-label">Village</label>
                                <input type="text" class="form-control" id="village" name="village" 
                                       value="<?php echo htmlspecialchars($_POST['village'] ?? $contact->village); ?>">
                            </div>
                            
                            <div class="col-md-6 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" 
                                       value="<?php echo htmlspecialchars($_POST['date_of_birth'] ?? $contact->date_of_birth); ?>" required>
                                <div class="invalid-feedback">Please select date of birth</div>
                            </div>
                        </div>
                        
                        <div class="mb-3">
                            <label for="note" class="form-label">Note</label>
                            <textarea class="form-control" id="note" name="note" rows="3"><?php echo htmlspecialchars($_POST['note'] ?? $contact->note); ?></textarea>
                        </div>
                        
                        <div class="mb-3">
                            <label for="status" class="form-label">Status *</label>
                            <select class="form-select" id="status" name="status" required>
                                <option value="active" <?php echo (isset($_POST['status']) ? $_POST['status'] : $contact->status) === 'active' ? 'selected' : ''; ?>>Active</option>
                                <option value="inactive" <?php echo (isset($_POST['status']) ? $_POST['status'] : $contact->status) === 'inactive' ? 'selected' : ''; ?>>Inactive</option>
                            </select>
                        </div>
                        
                        <hr>
                        
                        <div class="d-flex justify-content-between">
                            <a href="<?php echo URL_PATH; ?>/admin/contacts" class="btn btn-secondary">
                                <i class="bi bi-arrow-left"></i> Cancel
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-save"></i> Update Contact
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        
        <div class="col-lg-4">
            <div class="card">
                <div class="card-header">
                    <i class="bi bi-info-circle"></i> Contact Details
                </div>
                <div class="card-body">
                    <p><strong>Contact ID:</strong> <?php echo $contact->id; ?></p>
                    <p><strong>Created:</strong> <?php echo formatDateTime($contact->created_at); ?></p>
                    <p><strong>Last Updated:</strong> <?php echo formatDateTime($contact->updated_at); ?></p>
                </div>
            </div>
        </div>
    </div>
</div>

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