<?php
/**
 * modules/roles/add_roles.php
 */

// 1. STRICT SECURITY KICK-OUT
$p = $GLOBALS['my_privs'] ?? ['can_view' => 0, 'can_add' => 0, 'can_edit' => 0, 'can_del' => 0];
if (in_array(1, $_SESSION['all_role_ids'] ?? [])) {
    $p = ['can_view' => 1, 'can_add' => 1, 'can_edit' => 1, 'can_del' => 1];
}

if ($p['can_add'] == 0) {
    header("Location: index.php?s=" . encrypt_url('mod=roles&page=list_roles') . "&status=access_denied");
    exit;
}

// 2. FORM POST HANDLER (PRG Pattern)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $role_name   = $db->escape($_POST['role_name']);
    $description = $db->escape($_POST['description']);
    $status      = $db->escape($_POST['status']);
    $created_by  = (int)$_SESSION['user_id'];

    // Prevent duplicate role names
    $check_sql = "SELECT ROLE_ID FROM TBL_ROLES WHERE ROLE_NAME = '$role_name'";
    if (mysqli_num_rows($db->query($check_sql)) > 0) {
        header("Location: index.php?s=" . encrypt_url('mod=roles&page=add_roles') . "&status=duplicate");
        exit;
    }

    $sql = "INSERT INTO TBL_ROLES (ROLE_NAME, DESCRIPTION, STATUS, CREATED_BY) 
            VALUES ('$role_name', '$description', '$status', $created_by)";
    
    if ($db->query($sql)) {
        header("Location: index.php?s=" . encrypt_url('mod=roles&page=list_roles') . "&status=added");
        exit;
    }
}
?>

<section class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1 class="font-weight-bold text-primary"><i class="fas fa-plus-circle mr-2"></i>Create New Role</h1>
            </div>
            <div class="col-sm-6 text-right">
                <a href="index.php?s=<?php echo encrypt_url('mod=roles&page=list_roles'); ?>" class="btn btn-default shadow-sm">
                    <i class="fas fa-arrow-left mr-1"></i> Back to Roles
                </a>
            </div>
        </div>
    </div>
</section>

<section class="content">
    <div class="container-fluid">
        <form action="" method="POST">
            <div class="card card-outline card-primary shadow-sm">
                <div class="card-header bg-white">
                    <h3 class="card-title font-weight-bold text-primary">Role Specifications</h3>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4 form-group">
                            <label class="text-secondary">Role Title <span class="text-danger">*</span></label>
                            <input type="text" name="role_name" class="form-control" placeholder="e.g. Weighbridge Operator" required>
                        </div>
                        <div class="col-md-6 form-group">
                            <label class="text-secondary">Description</label>
                            <input type="text" name="description" class="form-control" placeholder="Briefly describe the responsibilities...">
                        </div>
                        <div class="col-md-2 form-group">
                            <label class="text-secondary">Status</label>
                            <select name="status" class="form-control">
                                <option value="ACTIVE">ACTIVE</option>
                                <option value="INACTIVE">INACTIVE</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="card-footer bg-white text-right">
                    <button type="submit" class="btn btn-primary px-5 font-weight-bold shadow-sm">
                        <i class="fas fa-save mr-2"></i> SAVE ROLE
                    </button>
                </div>
            </div>
        </form>
    </div>
</section>