@extends('admin.layouts.app')

@section('title', 'إدارة النقاط')

@section('content')
<div class="container-fluid">
    <!-- Header -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center">
                <h1 class="h3 mb-0 text-gray-800">
                    <i class="fas fa-coins text-warning"></i>
                    إدارة النقاط
                </h1>
                <div class="btn-group">
                    <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#adjustPointsModal">
                        <i class="fas fa-plus"></i> تعديل نقاط
                    </button>
                    <a href="{{ route('admin.points.dashboard') }}" class="btn btn-outline-secondary">
                        <i class="fas fa-arrow-right"></i> العودة للوحة التحكم
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- إحصائيات سريعة -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card border-left-primary shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
                                إجمالي المستخدمين
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                {{ $users->total() }}
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-users fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- جدول المستخدمين -->
    <div class="card shadow">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">قائمة المستخدمين ونقاطهم</h6>
        </div>
        <div class="card-body">
            @if($users->count() > 0)
                <div class="table-responsive">
                    <table class="table table-bordered table-hover">
                        <thead class="thead-light">
                            <tr>
                                <th>#</th>
                                <th>المستخدم</th>
                                <th>البريد الإلكتروني</th>
                                <th>إجمالي النقاط</th>
                                <th>النقاط المتاحة</th>
                                <th>النقاط المستخدمة</th>
                                <th>آخر نشاط</th>
                                <th>الإجراءات</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($users as $index => $user)
                                <tr>
                                    <td>{{ $users->firstItem() + $index }}</td>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <div class="mr-2">
                                                <div class="bg-primary rounded-circle d-flex align-items-center justify-content-center" style="width: 40px; height: 40px;">
                                                    <span class="text-white font-weight-bold">{{ substr($user->name, 0, 1) }}</span>
                                                </div>
                                            </div>
                                            <div>
                                                <div class="font-weight-bold">{{ $user->name }}</div>
                                                <small class="text-muted">ID: {{ $user->id }}</small>
                                            </div>
                                        </div>
                                    </td>
                                    <td>{{ $user->email }}</td>
                                    <td>
                                        <span class="badge badge-primary badge-lg">{{ number_format($user->points->points ?? 0) }}</span>
                                    </td>
                                    <td>
                                        <span class="badge badge-success badge-lg">{{ number_format($user->points->available_points ?? 0) }}</span>
                                    </td>
                                    <td>
                                        <span class="badge badge-warning badge-lg">{{ number_format($user->points->used_points ?? 0) }}</span>
                                    </td>
                                    <td>
                                        <small class="text-muted">
                                            {{ $user->points->updated_at ? $user->points->updated_at->diffForHumans() : 'لم يتم تسجيل نشاط' }}
                                        </small>
                                    </td>
                                    <td>
                                        <div class="btn-group btn-group-sm">
                                            <button class="btn btn-outline-primary" onclick="adjustPoints({{ $user->id }}, '{{ $user->name }}', {{ $user->points->points ?? 0 }})">
                                                <i class="fas fa-edit"></i>
                                            </button>
                                            <button class="btn btn-outline-info" onclick="viewTransactions({{ $user->id }}, '{{ $user->name }}')">
                                                <i class="fas fa-history"></i>
                                            </button>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                <div class="d-flex justify-content-center">
                    {{ $users->links() }}
                </div>
            @else
                <div class="text-center py-5">
                    <i class="fas fa-users fa-3x text-muted mb-3"></i>
                    <h5 class="text-muted">لا توجد مستخدمين</h5>
                </div>
            @endif
        </div>
    </div>
</div>

<!-- Modal تعديل النقاط -->
<div class="modal fade" id="adjustPointsModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('admin.points.adjust') }}" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title">تعديل النقاط</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="user_id" class="form-label">المستخدم</label>
                        <select class="form-select" id="user_id" name="user_id" required>
                            <option value="">اختر المستخدم</option>
                            @foreach($users as $user)
                                <option value="{{ $user->id }}" data-points="{{ $user->points->points ?? 0 }}">
                                    {{ $user->name }} ({{ $user->points->points ?? 0 }} نقطة)
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="mb-3">
                        <label for="type" class="form-label">نوع التعديل</label>
                        <select class="form-select" id="type" name="type" required>
                            <option value="add">إضافة نقاط</option>
                            <option value="subtract">خصم نقاط</option>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label for="points" class="form-label">عدد النقاط</label>
                        <input type="number" class="form-control" id="points" name="points" min="1" required>
                    </div>

                    <div class="mb-3">
                        <label for="description" class="form-label">الوصف</label>
                        <textarea class="form-control" id="description" name="description" rows="3" required></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إلغاء</button>
                    <button type="submit" class="btn btn-primary">تطبيق التعديل</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Modal معاملات المستخدم -->
<div class="modal fade" id="transactionsModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">معاملات المستخدم</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div id="transactionsContent">
                    <div class="text-center">
                        <div class="spinner-border" role="status">
                            <span class="visually-hidden">جاري التحميل...</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@endsection

@push('scripts')
<script>
function adjustPoints(userId, userName, currentPoints) {
    document.getElementById('user_id').value = userId;
    document.getElementById('description').value = `تعديل نقاط ${userName}`;
    new bootstrap.Modal(document.getElementById('adjustPointsModal')).show();
}

function viewTransactions(userId, userName) {
    document.querySelector('#transactionsModal .modal-title').textContent = `معاملات ${userName}`;
    
    // جلب المعاملات عبر AJAX
    fetch(`/admin/points/transactions/${userId}`)
        .then(response => response.text())
        .then(html => {
            document.getElementById('transactionsContent').innerHTML = html;
            new bootstrap.Modal(document.getElementById('transactionsModal')).show();
        })
        .catch(error => {
            document.getElementById('transactionsContent').innerHTML = '<div class="alert alert-danger">خطأ في تحميل المعاملات</div>';
            new bootstrap.Modal(document.getElementById('transactionsModal')).show();
        });
}
</script>
@endpush
