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

<!-- Page Header -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h3 class="mb-1">Edit Portal</h3>
        <p class="text-muted mb-0">Update portal configuration</p>
    </div>
    <a href="<?= base_url('superadmin/portals') ?>" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left me-2"></i>Back to Portals
    </a>
</div>

<!-- Edit Form -->
<div class="row">
    <div class="col-lg-8">
        <div class="table-card">
            <div class="card-header">
                <h5><i class="bi bi-pencil me-2"></i>Portal Information</h5>
            </div>
            <div class="card-body">
                <?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; ?>
                
                <?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; ?>
                
                <form method="POST" action="<?= base_url('superadmin/portals/edit/' . $portal->id) ?>">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Portal Name <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" name="portal_name" required value="<?= htmlspecialchars($portal->name) ?>">
                        </div>
                        
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Portal Code</label>
                            <input type="text" class="form-control" value="<?= htmlspecialchars($portal->code) ?>" readonly disabled>
                            <small class="text-muted">Portal code cannot be changed</small>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Portal URL</label>
                        <input type="url" class="form-control" name="portal_url" value="<?= htmlspecialchars($portal->url ?? '') ?>">
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Logo URL</label>
                        <input type="url" class="form-control" name="logo_url" value="<?= htmlspecialchars($portal->logo_url ?? '') ?>">
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Primary Color</label>
                            <div class="input-group">
                                <input type="color" class="form-control form-control-color" name="primary_color" value="<?= htmlspecialchars($portal->primary_color ?? '#007bff') ?>">
                                <input type="text" class="form-control" value="<?= htmlspecialchars($portal->primary_color ?? '#007bff') ?>" readonly>
                            </div>
                        </div>
                        
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Status</label>
                            <select class="form-select" name="status">
                                <option value="active" <?= ($portal->status ?? 'active') == 'active' ? 'selected' : '' ?>>Active</option>
                                <option value="inactive" <?= ($portal->status ?? '') == 'inactive' ? 'selected' : '' ?>>Inactive</option>
                                <option value="maintenance" <?= ($portal->status ?? '') == 'maintenance' ? 'selected' : '' ?>>Maintenance</option>
                            </select>
                        </div>
                    </div>
                    
                    <hr class="my-4">
                    
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-sa-primary">
                            <i class="bi bi-check-circle me-2"></i>Update Portal
                        </button>
                        <a href="<?= base_url('superadmin/portals') ?>" class="btn btn-outline-secondary">
                            Cancel
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <div class="col-lg-4">
        <div class="table-card mb-4">
            <div class="card-header">
                <h5><i class="bi bi-info-circle me-2"></i>Portal Details</h5>
            </div>
            <div class="card-body">
                <div class="d-flex justify-content-between py-2 border-bottom">
                    <span class="text-muted">Portal ID</span>
                    <strong><?= $portal->id ?></strong>
                </div>
                <div class="d-flex justify-content-between py-2 border-bottom">
                    <span class="text-muted">Portal Code</span>
                    <strong><?= htmlspecialchars($portal->code) ?></strong>
                </div>
                <div class="d-flex justify-content-between py-2 border-bottom">
                    <span class="text-muted">Status</span>
                    <span class="badge bg-<?= $portal->status == 'active' ? 'success' : ($portal->status == 'maintenance' ? 'warning' : 'danger') ?>"><?= ucfirst($portal->status) ?></span>
                </div>
                <div class="d-flex justify-content-between py-2">
                    <span class="text-muted">Created</span>
                    <strong><?= date('M j, Y', strtotime($portal->created_at)) ?></strong>
                </div>
            </div>
        </div>
        
        <div class="table-card">
            <div class="card-header bg-danger text-white">
                <h5 class="text-white mb-0"><i class="bi bi-exclamation-triangle me-2"></i>Danger Zone</h5>
            </div>
            <div class="card-body">
                <p class="text-muted small">Deleting a portal will remove all its configuration. User data and campaigns will remain in the database.</p>
                <button class="btn btn-outline-danger btn-sm w-100" onclick="deletePortal(<?= $portal->id ?>)">
                    <i class="bi bi-trash me-2"></i>Delete Portal
                </button>
            </div>
        </div>
    </div>
</div>

<script>
function deletePortal(id) {
    if(confirm('Are you sure you want to delete this portal? This action cannot be undone.')) {
        window.location.href = '<?= base_url('superadmin/portals/delete/') ?>' + id;
    }
}
</script>

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