<?php

namespace App\Nova\Resources\Ecommerce\Product\Category\Filters;

use Laravel\Nova\Filters\Filter;
use Laravel\Nova\Http\Requests\NovaRequest;

class Level extends Filter
{
    /**
     * The filter's component.
     *
     * @var string
     */
    public $component = 'select-filter';

    /**
     * Apply the filter to the given query.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  mixed  $value
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function apply(NovaRequest $request, $query, $value)
    {
        if ($value == 1) {
            return $query->where('parent_id', null);
        }

        if ($value == 2) {
            $query->whereNotNull('parent_id');
        }

        // if ($value == 3) {
        //     $query->isLeaf();
        // }

        return $query;
    }

    /**
     * Get the filter's available options.
     *
     * @return array
     */
    public function options(NovaRequest $request)
    {
        return [
            1 => 1,
            2 => 2,
            // 3 => 3
        ];
    }
}
