<?php
$role = isset($role) ? $role : null;
$is_edit = !empty($role);
$page_title = isset($page_title) ? $page_title : ($is_edit ? 'Edit Role' : 'Add Role');
$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>
        </div>
        <div class="col-sm-4 text-sm-right">
          <a href="<?php echo base_url('index.php/roles'); ?>" 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:640px">
        <div class="card-body">
          <form method="post" action="<?php echo htmlspecialchars($form_action); ?>">
            <div class="form-group">
              <label>Role name <span class="text-danger">*</span></label>
              <input type="text" name="name" class="form-control" required
                     value="<?php echo htmlspecialchars($is_edit ? $role->name : ''); ?>">
            </div>
            <div class="form-group">
              <label>Slug</label>
              <input type="text" name="slug" class="form-control"
                     placeholder="auto-generated from name if blank"
                     value="<?php echo htmlspecialchars($is_edit ? $role->slug : ''); ?>"
                     <?php echo ($is_edit && !empty($role->is_superadmin)) ? 'readonly' : ''; ?>>
            </div>
            <div class="form-group">
              <label>Description</label>
              <textarea name="description" class="form-control" rows="2"><?php echo htmlspecialchars($is_edit ? $role->description : ''); ?></textarea>
            </div>
            <div class="form-group">
              <label>Status</label>
              <?php $st = $is_edit ? (int) $role->status : 1; ?>
              <select name="status" class="form-control" <?php echo ($is_edit && !empty($role->is_superadmin)) ? 'disabled' : ''; ?>>
                <option value="1" <?php echo $st ? 'selected' : ''; ?>>Active</option>
                <option value="0" <?php echo !$st ? 'selected' : ''; ?>>Inactive</option>
              </select>
              <?php if ($is_edit && !empty($role->is_superadmin)) { ?>
                <input type="hidden" name="status" value="1">
              <?php } ?>
            </div>
            <button type="submit" class="btn btn-primary"><?php echo $is_edit ? 'Save changes' : 'Create role'; ?></button>
            <a href="<?php echo base_url('index.php/roles'); ?>" class="btn btn-link">Cancel</a>
          </form>
        </div>
      </div>
    </div>
  </section>
</div>
