<?= $this->extend('admin/layout/main') ?>
<?= $this->section('content') ?>

<h3 class="mb-3"><?= $user ? lang('App.edit') : lang('App.add_new') ?> - <?= lang('App.users') ?></h3>

<?php if (session()->getFlashdata('errors')): ?>
    <div class="alert alert-danger">
        <ul class="mb-0">
        <?php foreach (session()->getFlashdata('errors') as $err): ?>
            <li><?= esc($err) ?></li>
        <?php endforeach; ?>
        </ul>
    </div>
<?php endif; ?>

<div class="card p-4" style="max-width:550px;">
<form method="post" action="<?= $user ? '/admin/users/' . $user['id'] : '/admin/users' ?>">
    <?= csrf_field() ?>

    <div class="mb-3">
        <label class="form-label"><?= lang('App.name') ?></label>
        <input type="text" name="name" class="form-control" value="<?= esc($user['name'] ?? '') ?>" required>
    </div>

    <div class="mb-3">
        <label class="form-label"><?= lang('App.email') ?></label>
        <input type="email" name="email" class="form-control" value="<?= esc($user['email'] ?? '') ?>" required>
    </div>

    <div class="mb-3">
        <label class="form-label"><?= lang('App.password') ?> <?= $user ? '(اتركها فارغة لعدم التغيير)' : '' ?></label>
        <input type="password" name="password" class="form-control" <?= $user ? '' : 'required' ?>>
    </div>

    <div class="mb-3">
        <label class="form-label">لغة الواجهة</label>
        <select name="preferred_locale" class="form-select">
            <option value="ar" <?= ($user['preferred_locale'] ?? 'ar') === 'ar' ? 'selected' : '' ?>>العربية</option>
            <option value="en" <?= ($user['preferred_locale'] ?? '') === 'en' ? 'selected' : '' ?>>English</option>
        </select>
    </div>

    <div class="mb-3">
        <label class="form-label">تاريخ انتهاء الصلاحية (اختياري)</label>
        <input type="date" name="expires_at" class="form-control" value="<?= esc($user['expires_at'] ?? '') ?>">
        <div class="form-text">اتركه فارغاً لحساب دائم بدون انتهاء صلاحية. لو محدد، الحساب هيتقفل تلقائياً بعد هذا التاريخ إلى ما يتجدد.</div>
    </div>

    <?php if (session()->get('is_super_admin')): ?>
        <div class="mb-3">
            <label class="form-label"><?= lang('App.companies') ?></label>
            <select name="company_id" class="form-select">
                <option value="">— بدون شركة (سوبر أدمن) —</option>
                <?php foreach ($companies as $c): ?>
                    <option value="<?= $c['id'] ?>" <?= ($user['company_id'] ?? null) == $c['id'] ? 'selected' : '' ?>><?= esc($c['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="form-check mb-3">
            <input type="checkbox" name="is_super_admin" class="form-check-input" value="1" <?= ($user['is_super_admin'] ?? 0) ? 'checked' : '' ?>>
            <label class="form-check-label">سوبر أدمن (صلاحية كاملة على كل الشركات)</label>
        </div>
    <?php endif; ?>

    <button class="btn btn-dark"><?= lang('App.save') ?></button>
    <a href="/admin/users" class="btn btn-light"><?= lang('App.cancel') ?></a>
</form>
</div>
<?= $this->endSection() ?>
