<?php include __DIR__ . '/../header.php'; ?>

<?php
$isEdit  = !empty($member);
$title   = $isEdit ? __('team.edit_member', 'تعديل عضو') : __('team.add_member', 'إضافة عضو جديد');
$action  = $isEdit ? '/admin/team/update/' . (int)$member['id'] : '/admin/team/store';
$current = $_SESSION['admin_role'] ?? 'owner';
?>

<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="col-lg-6">

            <div class="d-flex align-items-center gap-3 mb-4">
                <a href="/admin/team" class="btn btn-outline-secondary btn-sm">
                    <i class="bi bi-arrow-right"></i>
                </a>
                <h1 class="h3 mb-0"><?= $title ?></h1>
            </div>

            <div class="card shadow-sm">
                <div class="card-body">
                    <form method="POST" action="<?= $action ?>">
                        <?= \Core\System\Csrf::field() ?>

                        <div class="mb-3">
                            <label class="form-label fw-semibold">
                                <?= __('team.username', 'اسم المستخدم') ?> *
                            </label>
                            <input type="text" name="username" class="form-control"
                                   value="<?= htmlspecialchars($member['username'] ?? '') ?>"
                                   <?= $isEdit ? 'readonly' : 'required' ?>>
                            <?php if ($isEdit): ?>
                                <div class="text-muted small mt-1"><?= __('team.username_readonly', 'لا يمكن تغيير اسم المستخدم') ?></div>
                            <?php endif; ?>
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-semibold">
                                <?= __('team.password', 'كلمة المرور') ?>
                                <?= $isEdit ? '' : '*' ?>
                            </label>
                            <input type="password" name="password" class="form-control"
                                   <?= $isEdit ? '' : 'required' ?>>
                            <?php if ($isEdit): ?>
                                <div class="text-muted small mt-1"><?= __('team.password_hint', 'اتركه فارغاً للإبقاء على كلمة المرور الحالية') ?></div>
                            <?php endif; ?>
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-semibold"><?= __('team.email', 'البريد الإلكتروني') ?></label>
                            <input type="email" name="email" class="form-control"
                                   value="<?= htmlspecialchars($member['email'] ?? '') ?>">
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-semibold"><?= __('team.role', 'الدور') ?> *</label>
                            <select name="role" class="form-select" required
                                <?= ($member['role'] ?? '') === 'owner' ? 'disabled' : '' ?>>
                                <?php foreach ($roles as $key => $label_key): ?>
                                    <?php if ($key === 'owner') continue; ?>
                                    <option value="<?= $key ?>"
                                        <?= ($member['role'] ?? 'confirmer') === $key ? 'selected' : '' ?>>
                                        <?= \Core\App\Models\TeamMemberModel::roleLabel($key) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                            <!-- وصف الأدوار -->
                            <div class="mt-2 p-3 bg-light rounded small">
                                <div id="role_desc_manager" class="role-desc text-muted">
                                    <i class="bi bi-info-circle me-1"></i>
                                    <?= __('team.role_desc_manager', 'صلاحيات كاملة ما عدا إعدادات النظام الحساسة') ?>
                                </div>
                                <div id="role_desc_confirmer" class="role-desc text-muted">
                                    <i class="bi bi-info-circle me-1"></i>
                                    <?= __('team.role_desc_confirmer', 'يرى فقط الطلبات المعينة له ويستطيع تأكيدها أو رفضها') ?>
                                </div>
                                <div id="role_desc_shipping" class="role-desc text-muted">
                                    <i class="bi bi-info-circle me-1"></i>
                                    <?= __('team.role_desc_shipping', 'يدير الشحنات والطلبات المؤكدة فقط') ?>
                                </div>
                                <div id="role_desc_accountant" class="role-desc text-muted">
                                    <i class="bi bi-info-circle me-1"></i>
                                    <?= __('team.role_desc_accountant', 'يرى الإحصائيات والتقارير المالية فقط') ?>
                                </div>
                            </div>
                        </div>

                        <?php if ($isEdit): ?>
                        <div class="mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" name="is_active"
                                       id="is_active" value="1"
                                       <?= ($member['is_active'] ?? 1) ? 'checked' : '' ?>
                                       <?= ($member['role'] ?? '') === 'owner' ? 'disabled' : '' ?>>
                                <label class="form-check-label fw-semibold" for="is_active">
                                    <?= __('team.active', 'نشط') ?>
                                </label>
                            </div>
                        </div>
                        <?php endif; ?>

                        <div class="d-flex gap-2 mt-4">
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-save me-1"></i>
                                <?= __('actions.save', 'حفظ') ?>
                            </button>
                            <a href="/admin/team" class="btn btn-outline-secondary">
                                <?= __('actions.cancel', 'إلغاء') ?>
                            </a>
                        </div>

                    </form>
                </div>
            </div>

        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    var roleSelect = document.querySelector('select[name="role"]');
    var descs = document.querySelectorAll('.role-desc');

    function updateDesc() {
        descs.forEach(function(d) { d.style.display = 'none'; });
        var desc = document.getElementById('role_desc_' + roleSelect.value);
        if (desc) desc.style.display = 'block';
    }

    if (roleSelect) {
        roleSelect.addEventListener('change', updateDesc);
        updateDesc();
    }
});
</script>

<?php include __DIR__ . '/../footer.php'; ?>