<?php

use App\Models\User;
use Illuminate\Support\Facades\Route;
// use PdfReport;
use SamuelTerra22\ReportGenerator\ReportMedia\PdfReport;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('generate', function () {
    // Retrieve any filters
    // $fromDate = $request->input('from_date');
    // $toDate = $request->input('to_date');
    // $sortBy = $request->input('sort_by');

    // Report title
    $title = 'Registered User Report';

    // For displaying filters description on header
    $meta = [];

    // Do some querying..
    $queryBuilder = User::select([
        'id',
        'name',
        'email',
        'created_at'
    ]);

    // Set Column to be displayed
    $columns = [
        'Name' => 'name',
        'Registered At' => 'created_at',
        // if no column_name specified, this will automatically seach for snake_case of column name (will be registered_at) column from query result
        'Email' => 'email',
        'Status' => function ($result) { // You can do if statement or any action do you want inside this closure
        return ($result->id > 100000) ? 'In-active' : 'Active';
    }
];

    /*
        Generate Report with flexibility to manipulate column class even manipulate column value (using Carbon, etc).

        - of()         : Init the title, meta (filters description to show), query, column (to be shown)
        - editColumn() : To Change column class or manipulate its data for displaying to report
        - editColumns(): Mass edit column
        - showTotal()  : Used to sum all value on specified column on the last table (except using groupBy method). 'point' is a type for displaying total with a thousand separator
        - groupBy()    : Show total of value on specific group. Used with showTotal() enabled.
        - limit()      : Limit record to be showed
        - make()       : Will producing DomPDF / SnappyPdf instance so you could do any other DomPDF / snappyPdf method such as stream() or download()
    */
return PdfReport::of($title, $meta, $queryBuilder, $columns)
->editColumn('Registered At', [
    'displayAs' => function ($result) {
        return $result->created_at->format('d M Y');
    }
])
->limit(20)
->stream(); // or download('filename here..') to download pdf

});
