<?php

namespace App\Datatables\Admin\Shop;

use App\Datatables\ParentDataTable as DataTable;
use App\Models\Shop\Tariff;
use App\Repositories\Shop\Tariffs;
use Yajra\DataTables\Html\Column;

class TariffsDataTable extends DataTable
{
    public $model_name = 'tariffs';

    public function query(Tariff $model)
    {
        $model = $model->with(['sale_channels'])->withCount(['price_lists', 'offers']);

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

    public function modifier($datatables)
    {
        $datatables
            ->editColumn('status', function (Tariff $tariff) {
                return Tariffs::getStatusBadge($tariff['status_id']);
            })
            ->editColumn('sale_channels2', function (Tariff $tariff) {
                $html = '';
                foreach ($tariff->sale_channels as $sale_channel) {
                    $html .= $sale_channel->code.', ';
                }

                return $html;
            })
            ->rawColumns(['status', 'sale_channels2', 'action']);

        return parent::modifier($datatables);
    }

    protected function getColumns()
    {
        return [
            Column::make('status_id')->data('status')->title('status')->searchable(false)->orderable(false),
            Column::make('name')->title('Nom'),
            Column::make('sale_channels2')->title('Canaux de vente')->searchable(false)->orderable(false),
            Column::make('code')->title('Code'),
            Column::make('ref')->title('Référence'),
            Column::make('price_lists_count')->title('#Lst prix')->searchable(false)->orderable(false)->class('text-right'),
            Column::make('offers_count')->title('#Offres')->searchable(false)->orderable(false)->class('text-right'),
            $this->makeColumnButtons(),
        ];
    }
}
