<?php

namespace App\Datatables\Botanic;

use App\Datatables\ParentDataTable as DataTable;
use App\Models\Botanic\Genre;
use Yajra\DataTables\Html\Column;

class GenresDataTable extends DataTable
{
    public $model_name = 'genres';

    public function query(Genre $model)
    {
        $model = $model::with('family')->withCount('species')->withCount('varieties');

        return $this->buildQuery($model);
    }

    public function modifier($datatables)
    {
        $datatables
            ->editColumn('family_name', function (Genre $genre) {
                return $genre->family ? $genre->family->name : '';
            })
            ->rawColumns(['genre_name', 'action']);

        return parent::modifier($datatables);
    }

    protected function getColumns()
    {
        return [
            Column::make('name')->title('Nom'),
            Column::make('alias'),
            Column::make('latin'),
            Column::make('family.name')->data('family_name')->title('Famille'),
            Column::make('species_count')->title('Nb Espèces')->searchable(false)->addClass('text-right'),
            Column::make('varieties_count')->title('Nb Variétés')->searchable(false)->addClass('text-right'),
            $this->makeColumnButtons(),
        ];
    }
}
