<?php

/**
 * Workspace User Editing form view.
 *
 * Variables expected:
 *   $user    — array — Workspace user details.
 *   $errors  — array — Field validation errors.
 */

/** @var array $user */
/** @var array $errors */

?>

<div class="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8">

    <!-- Title Section -->
    <div class="mb-6">
        <div class="flex items-center gap-1.5 text-xs font-semibold text-slate-500 mb-1.5">
            <a href="<?= url('/app/couple/users') ?>" class="hover:text-[#D4A28C] transition-colors">Workspace Users</a>
            <span class="text-slate-300">/</span>
            <span class="text-slate-400">Edit User</span>
        </div>
        <h1 class="text-2xl font-black tracking-tight text-slate-800 flex items-center gap-2.5">
            <span class="page-header-icon">
                <i class="fa-solid fa-user-pen"></i>
            </span>
            Edit Workspace User
        </h1>
        <p class="text-sm text-slate-500 mt-1.5 ml-0.5">Update details, assign roles, or manage access for <strong class="text-slate-700"><?= escape_html($user['username']) ?></strong>.</p>
    </div>

    <!-- Error alerts -->
    <?php if (!empty($errors)): ?>
        <div class="p-4 bg-rose-50 border border-rose-150 text-rose-800 rounded-2xl text-sm shadow-sm mb-4">
            <div class="font-bold mb-2 flex items-center gap-2">
                <i class="fa-solid fa-circle-xmark text-rose-500"></i> Please fix the following:
            </div>
            <ul class="list-disc pl-5 text-xs space-y-1">
                <?php foreach ($errors as $field => $err): ?>
                    <li><strong><?= escape_html(ucwords(str_replace('_', ' ', $field))) ?>:</strong> <?= escape_html($err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <!-- Form Card -->
    <div class="card p-0 overflow-hidden">
        <form action="<?= url('/app/couple/users/edit/' . (int) $user['id']) ?>" method="POST" class="p-6 space-y-6">
            <?= csrf_field() ?>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <!-- Display Name -->
                <div class="form-group">
                    <label for="display_name" class="form-label">Display Name</label>
                    <input 
                        type="text" 
                        id="display_name" 
                        name="display_name" 
                        value="<?= escape_html($user['display_name']) ?>" 
                        placeholder="e.g. John Doe"
                        class="form-input"
                        required
                    >
                </div>

                <!-- Username -->
                <div class="form-group">
                    <label for="username" class="form-label">Username</label>
                    <input 
                        type="text" 
                        id="username" 
                        name="username" 
                        value="<?= escape_html($user['username']) ?>" 
                        placeholder="e.g. johndoe"
                        class="form-input"
                        required
                    >
                </div>

                <!-- Password (optional reset) -->
                <div class="form-group">
                    <label for="password" class="form-label">Reset Password</label>
                    <input 
                        type="password" 
                        id="password" 
                        name="password" 
                        placeholder="Leave blank to keep current password"
                        class="form-input"
                    >
                </div>



                <!-- Status Selection -->
                <div class="form-group">
                    <label for="is_active" class="form-label">Status</label>
                    <select 
                        id="is_active" 
                        name="is_active" 
                        class="form-select"
                    >
                        <option value="1" <?= (int) $user['is_active'] === 1 ? 'selected' : '' ?>>Active</option>
                        <option value="0" <?= (int) $user['is_active'] === 0 ? 'selected' : '' ?>>Deactivated</option>
                    </select>
                </div>
            </div>

            <!-- Action buttons -->
            <div class="pt-6 border-t border-slate-100 flex justify-end gap-3">
                <a href="<?= url('/app/couple/users') ?>" class="btn btn-secondary btn-md">
                    <i class="fa-solid fa-xmark text-[10px] mr-1"></i> Cancel
                </a>
                <button type="submit" class="btn btn-primary btn-md">
                    <i class="fa-solid fa-floppy-disk text-[10px] mr-1"></i> Save Changes
                </button>
            </div>
        </form>
    </div>

</div>
