<?php

namespace App\Http\Controllers\Firebase;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Kreait\Firebase\Factory;
use Kreait\Firebase\Database;
use Yajra\DataTables\Facades\DataTables;


class ScoreExamenController extends Controller
{

    public function index2()
    {
        $firestore = app('firebase.firestore');
        $collection = $firestore->collection('scoreexamens');  
        $documents = $collection->documents();

        $scores = [];
        foreach ($documents as $document) {
            $scores[] = $document->data();
        }

        return response()->json($scores);
    }

    public function index(Request $request)
    {
        $firestore = app('firebase.firestore');
        $collection = $firestore->collection('scoreexamens');  
        $documents = $collection->documents();

        $scores = [];
        foreach ($documents as $document) {
            $data = $document->data();
            $data['id'] = $document->id(); // Ajout de l'ID Firestore
            $scores[] = $data;
        }

        if ($request->ajax()) {
            return DataTables::of($scores)
                ->addIndexColumn()
                ->addColumn('action', function ($row) {
                    return '<a href="javascript:void(0)" data-id="' . $row['id'] . '" class="edit btn btn-primary btn-sm editScore">Voire</a>';
                })
                ->rawColumns(['action'])
                ->make(true);
        }

        return view('firebase.examens.index'); 
    }
}
