<?php

namespace App\Datatables\Admin\Shop;

use App\Datatables\ParentDataTable as DataTable;
use App\Models\Shop\ArticleNature;
use App\Repositories\Core\Medias;
use App\Repositories\Shop\ArticleNatures;
use Yajra\DataTables\Html\Column;

class ArticleNaturesDataTable extends DataTable
{
    public $model_name = 'article_natures';

    public $sortedColumn = 1;

    public function query(ArticleNature $model)
    {
        $model = $model::withCount('Articles');

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

    public function modifier($datatables)
    {
        $datatables
            ->editColumn('icon', function (ArticleNature $nature) {
                $logo = Medias::getImage($nature, 'normal', 'icon');

                return $logo ? "<img src='{$logo}'>" : '';
            })
            ->editColumn('product_type', function (ArticleNature $nature) {
                return ArticleNatures::getProductTypeName($nature->product_type);
            })
            ->rawColumns(['icon', 'action']);

        return parent::modifier($datatables);
    }

    protected function getColumns()
    {
        return [
            Column::make('icon')->title('Icone')->width(40)->orderable(false),
            Column::make('product_type')->title('Famille de produit')->width(140),
            Column::make('name')->title('Nom'),
            Column::make('articles_count')->title('Nb Art.')->addClass('text-right')->searchable(false)->width(60),
            $this->makeColumnButtons(),
        ];
    }
}
