<?php

namespace App\Datatables\Botanic;

use App\Datatables\ParentDataTable as DataTable;
use App\Models\Botanic\Variety;
use App\Repositories\Botanic\Varieties;
use App\Repositories\Shop\Tags;
use Yajra\DataTables\Html\Column;

class VarietiesDataTable extends DataTable
{
    public $model_name = 'varieties';

    public function query(Variety $model)
    {
        $model = $model::joinRelationship('Specie')->select('botanic_varieties.*', 'botanic_species.name as specie_name')->with(['image', 'Specie', 'tags'])->withCount(['Articles', 'tags', 'images']);

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

    public function modifier($datatables)
    {
        $datatables
            ->editColumn('thumb', function (Variety $variety) {
                return Varieties::getThumb($variety->image, false);
            })
            ->editColumn('tags2', function (Variety $variety) {
                $html = '';
                foreach ($variety->tags as $tag) {
                    $html .= Tags::getTagHtml($tag);
                }

                return $html;
            })
            ->rawColumns(['thumb', 'tags2', 'action']);

        return parent::modifier($datatables);
    }

    protected function getColumns()
    {
        return [
            Column::make('Specie.name')->data('specie_name')->title('Espèce'),
            Column::make('thumb')->title('')->searchable(false)->orderable(false)->width(40)->class('text-center'),
            Column::make('name')->title('Nom'),
            Column::make('tags2')->title('Tags')->searchable(false)->orderable(false),
            Column::make('articles_count')->title('#Art')->class('text-right')->searchable(false),
            Column::make('tags_count')->title('#Tag')->class('text-right')->searchable(false),
            Column::make('images_count')->title('#Pho')->class('text-right')->searchable(false),
            $this->makeColumnButtons(),
        ];
    }
}
