<?php $this->load->view('superadmin/layouts/header'); ?>

<!-- Page Header -->
<div class="mb-4">
    <h3 class="mb-1">Settings</h3>
    <p class="text-muted mb-0">Manage your account and super admin settings</p>
</div>

<!-- Flash Messages -->
<?php if($this->session->flashdata('success')): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
    <i class="bi bi-check-circle me-2"></i><?= $this->session->flashdata('success') ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<?php if($this->session->flashdata('error')): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
    <i class="bi bi-exclamation-circle me-2"></i><?= $this->session->flashdata('error') ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<div class="row">
    <!-- Sidebar Navigation -->
    <div class="col-lg-3 mb-4">
        <div class="table-card">
            <div class="card-body p-0">
                <nav class="nav flex-column">
                    <a class="nav-link active" href="#profile" data-bs-toggle="tab">
                        <i class="bi bi-person me-2"></i>Profile
                    </a>
                    <a class="nav-link" href="#password" data-bs-toggle="tab">
                        <i class="bi bi-key me-2"></i>Change Password
                    </a>
                    <a class="nav-link" href="#admins" data-bs-toggle="tab">
                        <i class="bi bi-people me-2"></i>Manage Admins
                    </a>
                    <a class="nav-link" href="#activity" data-bs-toggle="tab">
                        <i class="bi bi-clock-history me-2"></i>Activity Log
                    </a>
                </nav>
            </div>
        </div>
    </div>
    
    <!-- Content Area -->
    <div class="col-lg-9">
        <div class="tab-content">
            <!-- Profile Tab -->
            <div class="tab-pane fade show active" id="profile">
                <div class="table-card">
                    <div class="card-header">
                        <h5><i class="bi bi-person me-2"></i>Profile Settings</h5>
                    </div>
                    <div class="card-body">
                        <form method="POST" action="<?= base_url('superadmin/settings/update_profile') ?>">
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">Full Name</label>
                                    <input type="text" class="form-control" name="full_name" value="<?= htmlspecialchars($admin['full_name'] ?? '') ?>" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">Username</label>
                                    <input type="text" class="form-control" value="<?= htmlspecialchars($admin['username'] ?? '') ?>" disabled>
                                    <small class="text-muted">Username cannot be changed</small>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">Email</label>
                                <input type="email" class="form-control" name="email" value="<?= htmlspecialchars($admin['email'] ?? '') ?>" required>
                            </div>
                            
                            <button type="submit" class="btn btn-sa-primary">
                                <i class="bi bi-check-circle me-2"></i>Update Profile
                            </button>
                        </form>
                    </div>
                </div>
            </div>
            
            <!-- Password Tab -->
            <div class="tab-pane fade" id="password">
                <div class="table-card">
                    <div class="card-header">
                        <h5><i class="bi bi-key me-2"></i>Change Password</h5>
                    </div>
                    <div class="card-body">
                        <form method="POST" action="<?= base_url('superadmin/settings/update_password') ?>">
                            <div class="mb-3">
                                <label class="form-label">Current Password</label>
                                <input type="password" class="form-control" name="current_password" required>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">New Password</label>
                                <input type="password" class="form-control" name="new_password" required minlength="8">
                                <small class="text-muted">Minimum 8 characters</small>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">Confirm New Password</label>
                                <input type="password" class="form-control" name="confirm_password" required>
                            </div>
                            
                            <button type="submit" class="btn btn-sa-primary">
                                <i class="bi bi-check-circle me-2"></i>Update Password
                            </button>
                        </form>
                    </div>
                </div>
            </div>
            
            <!-- Manage Admins Tab -->
            <div class="tab-pane fade" id="admins">
                <div class="table-card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5><i class="bi bi-people me-2"></i>Super Admins</h5>
                        <button class="btn btn-sm btn-sa-primary" data-bs-toggle="modal" data-bs-target="#addAdminModal">
                            <i class="bi bi-plus-circle me-1"></i>Add Admin
                        </button>
                    </div>
                    <div class="card-body">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Name</th>
                                    <th>Username</th>
                                    <th>Email</th>
                                    <th>Status</th>
                                    <th>Last Login</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if(!empty($admins)): ?>
                                <?php foreach($admins as $a): ?>
                                <tr>
                                    <td><strong><?= htmlspecialchars($a['full_name']) ?></strong></td>
                                    <td><?= htmlspecialchars($a['username']) ?></td>
                                    <td><?= htmlspecialchars($a['email']) ?></td>
                                    <td>
                                        <span class="portal-status <?= $a['status'] ?>">
                                            <?= ucfirst($a['status']) ?>
                                        </span>
                                    </td>
                                    <td>
                                        <?php if(!empty($a['last_login'])): ?>
                                        <?= date('M j, Y g:i A', strtotime($a['last_login'])) ?>
                                        <?php else: ?>
                                        <span class="text-muted">Never</span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <?php if($a['id'] != $this->session->userdata('super_admin_id')): ?>
                                        <button class="btn btn-sm btn-outline-danger" onclick="deleteAdmin(<?= $a['id'] ?>)">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                        <?php else: ?>
                                        <span class="badge bg-primary">You</span>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                                <?php else: ?>
                                <tr>
                                    <td colspan="6" class="text-center py-4 text-muted">No admins found</td>
                                </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            
            <!-- Activity Log Tab -->
            <div class="tab-pane fade" id="activity">
                <div class="table-card">
                    <div class="card-header">
                        <h5><i class="bi bi-clock-history me-2"></i>Recent Activity</h5>
                    </div>
                    <div class="card-body">
                        <?php if(!empty($activity_logs)): ?>
                        <?php foreach($activity_logs as $log): ?>
                        <div class="activity-item">
                            <div class="activity-icon bg-light text-primary">
                                <i class="bi bi-<?= $log['action_type'] == 'login' ? 'box-arrow-in-right' : 'gear' ?>"></i>
                            </div>
                            <div class="activity-content">
                                <h6><?= htmlspecialchars($log['description']) ?></h6>
                                <small><?= date('M j, Y g:i A', strtotime($log['created_at'])) ?></small>
                                <?php if(!empty($log['ip_address'])): ?>
                                <small class="text-muted ms-2">IP: <?= htmlspecialchars($log['ip_address']) ?></small>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php endforeach; ?>
                        <?php else: ?>
                        <div class="text-center py-4 text-muted">
                            <i class="bi bi-clock-history fs-1 d-block mb-2"></i>
                            No activity logs
                        </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Add Admin Modal -->
<div class="modal fade" id="addAdminModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="bi bi-person-plus me-2"></i>Add Super Admin</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="<?= base_url('superadmin/settings/add_admin') ?>">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Full Name</label>
                        <input type="text" class="form-control" name="full_name" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Username</label>
                        <input type="text" class="form-control" name="username" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Email</label>
                        <input type="email" class="form-control" name="email" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Password</label>
                        <input type="password" class="form-control" name="password" required minlength="8">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-sa-primary">Add Admin</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function deleteAdmin(id) {
    if(confirm('Are you sure you want to delete this admin?')) {
        window.location.href = '<?= base_url('superadmin/settings/delete_admin/') ?>' + id;
    }
}
</script>

<?php $this->load->view('superadmin/layouts/footer'); ?>
