<?php

namespace App\Datatables\Admin\Shop;

use App\Datatables\ParentDataTable as DataTable;
use App\Models\Shop\PriceList;
use App\Repositories\Shop\PriceLists;
use Yajra\DataTables\Html\Column;

class PriceListsDataTable extends DataTable
{
    public $model_name = 'price_lists';

    public function __construct()
    {
        $this->url = route('Admin.Shop.PriceLists.index');
    }

    public function query(PriceList $model)
    {
        $model = $model->with(['sale_channel', 'price_list_values']);
        $model = self::filterByTariff($model);

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

    public static function filterByTariff($model, $tariff_id = false)
    {
        $tariff_id = $tariff_id ? $tariff_id : self::isFilteredByField('tariff_id');

        return $tariff_id ? $model->byTariff($tariff_id) : $model;
    }

    public function modifier($datatables)
    {
        $datatables
            ->editColumn('status', function (PriceList $price_list) {
                return PriceLists::getStatus($price_list['status_id']);
            })
            ->editColumn('tariff_id', function (PriceList $price_list) {
                return view('Admin.Shop.PriceLists.partials.table-prices', ['prices' => $price_list['price_list_values']]);
            })
            ->rawColumns(['tariff_id', 'action']);

        return parent::modifier($datatables);
    }

    protected function getColumns()
    {
        return [
            Column::make('status_id')->data('status')->title('etat'),
            Column::make('name')->title('Nom'),
            Column::make('sale_channel.name')->title('Canal de vente'),
            Column::make('tariff_id')->title('Liste de prix'),
            $this->makeColumnButtons(),
        ];
    }
}
