<?php $isEdit = $user !== null; ?>
<div class="panel" style="max-width:640px;">
<form method="POST" action="<?= Router::url($isEdit ? 'admin/users/update' : 'admin/users/store') ?>">
    <?= $csrf ?>
    <?php if ($isEdit): ?><input type="hidden" name="id" value="<?= $user['id'] ?>"><?php endif; ?>

    <div class="form-group">
        <label>Full Name</label>
        <input type="text" name="full_name" class="form-control" value="<?= e($user['full_name'] ?? '') ?>" required>
    </div>

    <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
        <div class="form-group">
            <label>Username</label>
            <input type="text" name="username" class="form-control" value="<?= e($user['username'] ?? '') ?>" <?= $isEdit ? 'readonly' : 'required' ?>>
        </div>
        <div class="form-group">
            <label>Password <?= $isEdit ? '(leave blank to keep unchanged)' : '' ?></label>
            <input type="password" name="password" class="form-control" <?= $isEdit ? '' : 'required' ?>>
        </div>
    </div>

    <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
        <div class="form-group">
            <label>Email</label>
            <input type="email" name="email" class="form-control" value="<?= e($user['email'] ?? '') ?>">
        </div>
        <div class="form-group">
            <label>Phone</label>
            <input type="text" name="phone" class="form-control" value="<?= e($user['phone'] ?? '') ?>">
        </div>
    </div>

    <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
        <div class="form-group">
            <label>Role</label>
            <select name="role_id" class="form-control" <?= ($isEdit && $user['is_protected']) ? 'disabled' : '' ?>>
                <?php foreach ($roles as $r): ?>
                <option value="<?= $r['id'] ?>" <?= (isset($user['role_id']) && $user['role_id'] == $r['id']) ? 'selected' : '' ?>><?= e($r['role_name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="form-group">
            <label>NIC Number</label>
            <input type="text" name="nic_number" class="form-control" value="<?= e($user['nic_number'] ?? '') ?>">
        </div>
    </div>

    <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:16px;">
        <div class="form-group">
            <label>Wage Type</label>
            <select name="wage_type" class="form-control">
                <?php foreach (['MONTHLY','DAILY','PIECE_RATE'] as $wt): ?>
                <option value="<?= $wt ?>" <?= (($user['wage_type'] ?? 'MONTHLY') === $wt) ? 'selected' : '' ?>><?= $wt ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="form-group">
            <label>Basic Salary (Monthly)</label>
            <input type="number" step="0.01" name="basic_salary" class="form-control" value="<?= e((string)($user['basic_salary'] ?? 0)) ?>">
        </div>
        <div class="form-group">
            <label>Daily Wage</label>
            <input type="number" step="0.01" name="daily_wage" class="form-control" value="<?= e((string)($user['daily_wage'] ?? 0)) ?>">
        </div>
    </div>

    <?php if ($isEdit && !$user['is_protected']): ?>
    <div class="form-group">
        <label><input type="checkbox" name="is_active" value="1" <?= $user['is_active'] ? 'checked' : '' ?>> Active (can log in)</label>
    </div>
    <?php endif; ?>

    <button type="submit" class="btn btn-primary"><i class="fa-solid fa-floppy-disk"></i> <?= $isEdit ? 'Save Changes' : 'Create User' ?></button>
    <a href="<?= Router::url('admin/users') ?>" class="btn btn-secondary"><i class="fa-solid fa-xmark"></i> Cancel</a>
</form>
</div>
