<?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">Add New User</h3>
        <p class="text-muted mb-0">Create a new user for any portal</p>
    </div>
    <a href="<?= base_url('superadmin/users') ?>" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left me-2"></i>Back to Users
    </a>
</div>

<!-- Flash Messages -->
<?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(validation_errors()): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
    <i class="bi bi-exclamation-circle me-2"></i><?= validation_errors() ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<!-- Create User Form -->
<div class="table-card">
    <div class="card-header">
        <h5><i class="bi bi-person-plus me-2"></i>User Details</h5>
    </div>
    <div class="card-body">
        <form method="post" action="<?= base_url('superadmin/users/create') ?>">
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Username <span class="text-danger">*</span></label>
                    <input type="text" name="username" class="form-control" value="<?= set_value('username') ?>" required>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Email <span class="text-danger">*</span></label>
                    <input type="email" name="email" class="form-control" value="<?= set_value('email') ?>" required>
                </div>
            </div>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Password <span class="text-danger">*</span></label>
                    <input type="password" name="password" class="form-control" minlength="6" required>
                    <small class="text-muted">Minimum 6 characters</small>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Portal <span class="text-danger">*</span></label>
                    <select name="portal_id" class="form-select" required>
                        <option value="">Select Portal</option>
                        <?php foreach($portals as $portal): ?>
                        <option value="<?= $portal->code ?>" <?= set_select('portal_id', $portal->code) ?>><?= htmlspecialchars($portal->name) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Role <span class="text-danger">*</span></label>
                    <select name="role" class="form-select" required>
                        <option value="user" <?= set_select('role', 'user', TRUE) ?>>User</option>
                        <option value="admin" <?= set_select('role', 'admin') ?>>Admin</option>
                    </select>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Status <span class="text-danger">*</span></label>
                    <select name="status" class="form-select" required>
                        <option value="active" <?= set_select('status', 'active', TRUE) ?>>Active</option>
                        <option value="inactive" <?= set_select('status', 'inactive') ?>>Inactive</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>Create User
                </button>
                <a href="<?= base_url('superadmin/users') ?>" class="btn btn-outline-secondary">Cancel</a>
            </div>
        </form>
    </div>
</div>

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