<?php


namespace App\Filters;


  use Illuminate\Http\Request;

class DischargeTaskFilters
{
    protected $request;
    protected $queryBuilder;
    protected $filters = [
        'owner_id', 'created_at_start', 'created_at_end', 'numbers', 'status', 'type', 'income_remark','facilitator_id','created_range'
    ];
    protected $array_filter;
    protected $params = [];

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->params = $request->all();
        $this->array_filter = array_filter($this->request->only($this->filters));
    }

    public function apply($builder)
    {
        $this->queryBuilder = $builder;
        $this->beforeApply();
        foreach ($this->array_filter as $filter => $value) {
            if (method_exists($this, $filter)) {
                $this->$filter($value, $this->queryBuilder);
            }
        }
        return $this->queryBuilder;
    }

    private function beforeApply(){
        if(isset($this->params['data'])){
            $ids = explode(',',$this->params['data']);
            $this->id($ids);
        }
    }

    private function isSearchLike($str)
    {
        if (substr($str, 0, 1) == "%" || substr($str, strlen($str) - 1, 1) == "%") {
            return true;
        }
        return false;
    }

    private function searchWay($query, $param, $column)
    {
        if ($this->isSearchLike($param)) {
            $query->where($column, 'like', $param);
        } else {
            $query->whereIn($column, array_filter(preg_split('/[,， ]+/is', $param)));
        }
        return $query;
    }

    public function owner_id($owner_id)
    {
        $this->queryBuilder->where('discharge_tasks.owner_id',$owner_id);
    }

    public function created_at_start($created_at_start)
    {
        $this->queryBuilder->where('discharge_tasks.created_at','>=',$created_at_start);
    }
    public function created_at_end($created_at_end)
    {
        $this->queryBuilder->where('discharge_tasks.created_at','<=',$created_at_end);
    }

    public function numbers($numbers)
    {
        $this->searchWay($this->queryBuilder,$numbers,'discharge_tasks.numbers');
    }

    public function status($status)
    {
        $this->queryBuilder->where('discharge_tasks.status',$status);
    }

    public function type($type)
    {
        $this->queryBuilder->where('discharge_tasks.type',$type);
    }

    public function income_remark($income_remark)
    {
        $this->queryBuilder->where('discharge_tasks.income_remark','like',$income_remark."%");
    }

    public function id($ids)
    {
        $this->queryBuilder->whereIn('discharge_tasks.id',$ids);
    }

    public function facilitator_id($facilitator_id)
    {
        $this->searchWay($this->queryBuilder,$facilitator_id,'discharge_tasks.facilitator_id');
    }

    public function created_range($created_range)
    {
        $this->queryBuilder->where('discharge_tasks.created_at','like',$created_range.'%');
    }

}
