@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-exchange-alt text-secondary"></i>
                    إدارة المعاملات
                </h1>
                <a href="{{ route('admin.points.dashboard') }}" class="btn btn-outline-secondary">
                    <i class="fas fa-arrow-right"></i> العودة للوحة التحكم
                </a>
            </div>
        </div>
    </div>

    <!-- فلاتر -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card shadow">
                <div class="card-body">
                    <form method="GET" class="row g-3">
                        <div class="col-md-2">
                            <label for="type" class="form-label">النوع</label>
                            <select class="form-select" id="type" name="type">
                                <option value="">جميع الأنواع</option>
                                <option value="earned" {{ request('type') == 'earned' ? 'selected' : '' }}>كسب</option>
                                <option value="used" {{ request('type') == 'used' ? 'selected' : '' }}>استخدام</option>
                                <option value="admin_adjustment" {{ request('type') == 'admin_adjustment' ? 'selected' : '' }}>تعديل إداري</option>
                                <option value="refund" {{ request('type') == 'refund' ? 'selected' : '' }}>استرداد</option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label for="user_id" class="form-label">المستخدم</label>
                            <select class="form-select" id="user_id" name="user_id">
                                <option value="">جميع المستخدمين</option>
                                @foreach($transactions->pluck('user')->unique() as $user)
                                    <option value="{{ $user->id }}" {{ request('user_id') == $user->id ? 'selected' : '' }}>
                                        {{ $user->name }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label for="date_from" class="form-label">من تاريخ</label>
                            <input type="date" class="form-control" id="date_from" name="date_from" value="{{ request('date_from') }}">
                        </div>
                        <div class="col-md-2">
                            <label for="date_to" class="form-label">إلى تاريخ</label>
                            <input type="date" class="form-control" id="date_to" name="date_to" value="{{ request('date_to') }}">
                        </div>
                        <div class="col-md-2">
                            <label for="min_points" class="form-label">الحد الأدنى للنقاط</label>
                            <input type="number" class="form-control" id="min_points" name="min_points" value="{{ request('min_points') }}" min="0">
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">&nbsp;</label>
                            <div class="d-grid">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fas fa-search"></i> بحث
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- إحصائيات سريعة -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card border-left-success 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-success text-uppercase mb-1">
                                نقاط مكتسبة
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                {{ number_format($transactions->where('type', 'earned')->sum('points')) }}
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-arrow-up fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-danger 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-danger text-uppercase mb-1">
                                نقاط مستخدمة
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                {{ number_format(abs($transactions->where('type', 'used')->sum('points'))) }}
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-arrow-down fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-info 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-info text-uppercase mb-1">
                                إجمالي المعاملات
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                {{ $transactions->total() }}
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-exchange-alt fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-warning 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-warning text-uppercase mb-1">
                                تعديلات إدارية
                            </div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                {{ $transactions->where('type', 'admin_adjustment')->count() }}
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-user-cog 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($transactions->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($transactions as $index => $transaction)
                                <tr>
                                    <td>{{ $transactions->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: 35px; height: 35px;">
                                                    <span class="text-white font-weight-bold">{{ substr($transaction->user->name, 0, 1) }}</span>
                                                </div>
                                            </div>
                                            <div>
                                                <div class="font-weight-bold">{{ $transaction->user->name }}</div>
                                                <small class="text-muted">{{ $transaction->user->email }}</small>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        @switch($transaction->type)
                                            @case('earned')
                                                <span class="badge badge-success">كسب</span>
                                                @break
                                            @case('used')
                                                <span class="badge badge-danger">استخدام</span>
                                                @break
                                            @case('admin_adjustment')
                                                <span class="badge badge-warning">تعديل إداري</span>
                                                @break
                                            @case('refund')
                                                <span class="badge badge-info">استرداد</span>
                                                @break
                                            @default
                                                <span class="badge badge-secondary">{{ $transaction->type }}</span>
                                        @endswitch
                                    </td>
                                    <td>
                                        <span class="font-weight-bold {{ $transaction->type === 'earned' || $transaction->type === 'refund' ? 'text-success' : 'text-danger' }}">
                                            {{ $transaction->type === 'earned' || $transaction->type === 'refund' ? '+' : '' }}{{ $transaction->points }}
                                        </span>
                                    </td>
                                    <td>
                                        <div class="text-truncate" style="max-width: 200px;" title="{{ $transaction->description }}">
                                            {{ $transaction->description }}
                                        </div>
                                    </td>
                                    <td>
                                        @if($transaction->metadata)
                                            <button class="btn btn-sm btn-outline-info" onclick="viewMetadata({{ $transaction->id }})">
                                                <i class="fas fa-info-circle"></i> عرض
                                            </button>
                                        @else
                                            <span class="text-muted">-</span>
                                        @endif
                                    </td>
                                    <td>
                                        <small class="text-muted">{{ $transaction->created_at->format('Y-m-d H:i:s') }}</small>
                                    </td>
                                    <td>
                                        <div class="btn-group btn-group-sm">
                                            <button class="btn btn-outline-info" onclick="viewTransactionDetails({{ $transaction->id }})">
                                                <i class="fas fa-eye"></i>
                                            </button>
                                            @if($transaction->type === 'admin_adjustment')
                                                <button class="btn btn-outline-warning" onclick="editTransaction({{ $transaction->id }})">
                                                    <i class="fas fa-edit"></i>
                                                </button>
                                            @endif
                                        </div>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>

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

<!-- Modal تفاصيل المعاملة -->
<div class="modal fade" id="detailsModal" 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" id="transactionDetails">
                <div class="text-center">
                    <div class="spinner-border" role="status">
                        <span class="visually-hidden">جاري التحميل...</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal البيانات الإضافية -->
<div class="modal fade" id="metadataModal" tabindex="-1">
    <div class="modal-dialog">
        <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" id="metadataContent">
                <div class="text-center">
                    <div class="spinner-border" role="status">
                        <span class="visually-hidden">جاري التحميل...</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@endsection

@push('scripts')
<script>
function viewTransactionDetails(transactionId) {
    fetch(`/admin/points/transactions/${transactionId}`)
        .then(response => response.text())
        .then(html => {
            document.getElementById('transactionDetails').innerHTML = html;
            new bootstrap.Modal(document.getElementById('detailsModal')).show();
        })
        .catch(error => {
            document.getElementById('transactionDetails').innerHTML = '<div class="alert alert-danger">خطأ في تحميل التفاصيل</div>';
            new bootstrap.Modal(document.getElementById('detailsModal')).show();
        });
}

function viewMetadata(transactionId) {
    fetch(`/admin/points/transactions/${transactionId}/metadata`)
        .then(response => response.text())
        .then(html => {
            document.getElementById('metadataContent').innerHTML = html;
            new bootstrap.Modal(document.getElementById('metadataModal')).show();
        })
        .catch(error => {
            document.getElementById('metadataContent').innerHTML = '<div class="alert alert-danger">خطأ في تحميل البيانات</div>';
            new bootstrap.Modal(document.getElementById('metadataModal')).show();
        });
}

function editTransaction(transactionId) {
    // يمكن إضافة وظيفة تعديل المعاملة هنا
    alert('وظيفة تعديل المعاملة قيد التطوير');
}
</script>
@endpush
