<?php


namespace App\Filters;


use App\Facilitator;
use App\Traits\ModelSearchWay;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class FacilitatorExternalFilters
{

    use ModelSearchWay;

    protected $request;
    protected $queryBuilder;
    protected $filters = [
        'created_at_start', 'created_at_end', 'facilitator_id', 'range_data', 'id'
    ];
    protected $array_filter;
    protected $params = [];

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->params = $request->all();
        $request['range_data'] = $request['data'];
        unset($request['data']);
        $this->array_filter = array_filter($this->request->only($this->filters));
    }

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

    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 range_data($range_data)
    {
        $rangeData = explode(',', $range_data);
        $params = [];
        foreach ($rangeData as $rangeDatum) {
            $data = explode(';', $rangeDatum);
            $data = $this->getSearchArr($data);
            if (empty($params[$data['id']])) {
                $params[$data['id']] = [];
            }
            $params[$data['id']][] = $data['months'];
        }
        $arr = [];
        foreach ($params as $key => $param) {
            $facilitator_id = $key;
            $rangeData = '';
            foreach ($param as $item) {
                $rangeData .= $item . '|';
            }
            $rangeData = rtrim($rangeData, '|');
            $arr[$facilitator_id] = $rangeData;
        }

        $this->queryBuilder->where(function ($query) use ($arr) {
            foreach ($arr as $key => $item) {
                $query->orWhere(function ($query) use ($key, $item) {
                    /** @var Builder $query */
                    $query->where('discharge_tasks.facilitator_id', $key)->where('discharge_tasks.created_at', 'regexp', '(' . $item . ')');
                });
            }
        });
    }

    private function getSearchArr($data): array
    {
        $param = [];
        foreach ($data as $datum) {
            $arr = explode(':', $datum);
            $param[$arr[0]] = $arr[1];
        }
        return $param;
    }

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

    public function id($id)
    {
        $facilitator =  Facilitator::query()->fromQuery('select id from facilitators where MD5(id) = ?',[$id]);
        $this->queryBuilder->whereIn('discharge_tasks.facilitator_id',$facilitator);
    }

}
