<?php

namespace App\Datatables\Admin\Shop;

use App\Datatables\ParentDataTable as DataTable;
use App\Models\Shop\Offer;
use App\Repositories\Shop\Offers;
use Yajra\DataTables\Html\Column;

class OffersDataTable extends DataTable
{
    public $model_name = 'offers';

    public $sortedColumn = 1;

    public function query(Offer $model)
    {
        $model = $model->with(['article.article_nature', 'variation', 'tariff'])->select($model->getTable().'.*');
        $model = self::filterByArticleNature($model);
        $model = self::filterByPackage($model);

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

    public static function filterByArticleNature($model, $articleNatureId = false)
    {
        $articleNatureId = $articleNatureId ? $articleNatureId : self::isFilteredByField('article_nature_id');

        return $articleNatureId ? $model->byArticleNature($articleNatureId) : $model;
    }

    public static function filterByPackage($model, $packageId = false)
    {
        $packageId = $packageId ? $packageId : self::isFilteredByField('package_id');

        return $packageId ? $model->byPackage($packageId) : $model;
    }

    public function modifier($datatables)
    {
        $datatables
            ->editColumn('thumb', function (Offer $offer) {
                return '<img src="'.Offers::getThumbSrc($offer).'">';
            })
            ->editColumn('status_id', function (Offer $offer) {
                return view('components.form.toggle', [
                    'name' => 'status_id',
                    'value' => $offer->status_id,
                    'on' => __('active'),
                    'off' => __('inactive'),
                    'meta' => 'data-id='.$offer->id,
                    'size' => 'xs',
                ]);
            })
            ->editColumn('stock_delayed', function (Offer $offer) {
                return $offer->stock_delayed.' - '.$offer->delay_type;
            })
            ->rawColumns(['active', 'thumb', 'action']);

        return parent::modifier($datatables);
    }

    protected function getColumns()
    {
        return [
            Column::make('status_id')->title('')->width(40)->searchable(false),
            Column::make('article.article_nature.name')->title('Nature')->defaultContent('')->searchable(false),
            Column::make('thumb')->title('')->width(30)->searchable(false)->addClass('text-center'),
            Column::make('article.name')->title('Article')->defaultContent(''),
            Column::make('variation.name')->title('Déclinaison')->defaultContent('')->searchable(false),
            Column::make('weight')->title('Poids')->searchable(false)->addClass('text-right'),
            Column::make('tariff.name')->title('Tarif')->defaultContent('')->searchable(false),
            Column::make('stock_current')->title('Appro im')->searchable(false)->addClass('text-right'),
            Column::make('stock_delayed')->title('Appro délai')->searchable(false)->addClass('text-right'),
            Column::make('stock_ondemand')->title('Dmde')->searchable(false)->addClass('text-right'),
            $this->makeColumnButtons(),
        ];
    }
}
