<?php
$user = isset($user) ? $user : null;
$roles = isset($roles) ? $roles : array();
$is_edit = !empty($user);
$page_title = isset($page_title) ? $page_title : ($is_edit ? 'Edit User' : 'Add User');
$form_action = isset($form_action) ? $form_action : '#';
?>
<div class="content-wrapper">
  <section class="content-header">
    <div class="container-fluid">
      <div class="row mb-2">
        <div class="col-sm-8">
          <h1><?php echo htmlspecialchars($page_title); ?></h1>
          <p class="report-lead">Assign a role to control which reports this user can view or export.</p>
        </div>
        <div class="col-sm-4 text-sm-right">
          <a href="<?php echo base_url('index.php/users'); ?>" class="btn btn-outline-secondary btn-sm">&larr; Back</a>
        </div>
      </div>
    </div>
  </section>
  <section class="content">
    <div class="container-fluid">
      <?php if ($this->session->flashdata('error')) { ?>
        <div class="alert alert-danger"><?php echo htmlspecialchars($this->session->flashdata('error')); ?></div>
      <?php } ?>
      <div class="report-card card" style="max-width:720px">
        <div class="card-header">
          <h3 class="card-title"><?php echo $is_edit ? 'Update account' : 'New account'; ?></h3>
        </div>
        <div class="card-body">
          <form method="post" action="<?php echo htmlspecialchars($form_action); ?>" autocomplete="off">
            <div class="form-group">
              <label>Full name <span class="text-danger">*</span></label>
              <input type="text" name="name" class="form-control" required
                     value="<?php echo htmlspecialchars($is_edit ? $user->name : ''); ?>">
            </div>
            <div class="form-row">
              <div class="form-group col-md-6">
                <label>Username <span class="text-danger">*</span></label>
                <input type="text" name="username" class="form-control" required
                       value="<?php echo htmlspecialchars($is_edit ? $user->username : ''); ?>">
              </div>
              <div class="form-group col-md-6">
                <label>Password <?php echo $is_edit ? '' : '<span class="text-danger">*</span>'; ?></label>
                <input type="password" name="password" class="form-control" <?php echo $is_edit ? '' : 'required'; ?>
                       placeholder="<?php echo $is_edit ? 'Leave blank to keep current' : ''; ?>" autocomplete="new-password">
              </div>
            </div>
            <div class="form-row">
              <div class="form-group col-md-6">
                <label>Email</label>
                <input type="email" name="email" class="form-control"
                       value="<?php echo htmlspecialchars($is_edit && isset($user->email) ? $user->email : ''); ?>">
              </div>
              <div class="form-group col-md-6">
                <label>Display / org name</label>
                <input type="text" name="university_name" class="form-control"
                       value="<?php echo htmlspecialchars($is_edit ? $user->university_name : ''); ?>">
              </div>
            </div>
            <div class="form-row">
              <div class="form-group col-md-6">
                <label>Role <span class="text-danger">*</span></label>
                <select name="role_id" class="form-control" required>
                  <option value="">— Select role —</option>
                  <?php foreach ($roles as $role) {
                    $sel = ($is_edit && (int) $user->role_id === (int) $role->id) ? 'selected' : '';
                  ?>
                    <option value="<?php echo (int) $role->id; ?>" <?php echo $sel; ?>>
                      <?php echo htmlspecialchars($role->name); ?>
                    </option>
                  <?php } ?>
                </select>
              </div>
              <div class="form-group col-md-6">
                <label>Status</label>
                <select name="status" class="form-control">
                  <?php
                  $st = $is_edit && isset($user->status) ? (int) $user->status : 1;
                  ?>
                  <option value="1" <?php echo $st ? 'selected' : ''; ?>>Active</option>
                  <option value="0" <?php echo !$st ? 'selected' : ''; ?>>Disabled</option>
                </select>
              </div>
            </div>
            <div class="pt-2">
              <button type="submit" class="btn btn-primary"><?php echo $is_edit ? 'Save changes' : 'Create user'; ?></button>
              <a href="<?php echo base_url('index.php/users'); ?>" class="btn btn-link">Cancel</a>
            </div>
          </form>
        </div>
      </div>
    </div>
  </section>
</div>
