<?php


namespace App\Filters;


use App\Customer;
use App\Material;
use App\Owner;
use App\OwnerMaterial;
use App\Supplier;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class ProcurementFilters
{
    protected $request;
    protected $queryBuilder;
    protected $ownerMaterialQuery;
    protected $materialQuery;
    protected $ownerQuery;
    protected $supplierQuery;
    protected $customerQuery;
    protected $array_filter;
    protected $filters = [
        'id','owner_id','material_id','company_name','created_at_start','created_at_end','supplier_id'
    ];
    protected $params = [];

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

    public function apply($builder)
    {
        $this->queryBuilder = $builder;
        $filters = array_filter($this->request->only($this->filters));
        $this->beforeApply();

        foreach ($filters as $filter => $value) {
            if (method_exists($this, $filter)) {
                $this->$filter($value, $this->queryBuilder);
            }
        }
        $this->afterApply();
        return $this->queryBuilder;
    }

    private function isSearchLike($str):bool
    {
        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;
    }

    private function getMaterialQuery():Builder
    {
        if(!$this->materialQuery)
            $this->materialQuery = Material::query()->selectRaw('id');
        return $this->materialQuery;
    }
    private function getOwnerMaterialQuery():Builder
    {
        if(!$this->ownerMaterialQuery)
            $this->ownerMaterialQuery = OwnerMaterial::query()->selectRaw('id');
        return $this->ownerMaterialQuery;
    }
    private function getOwnerQuery():Builder
    {
        if(!$this->ownerQuery)
            $this->ownerQuery = Owner::query()->selectRaw('id');
        return $this->ownerQuery;
    }
    private function getSupplierQuery():Builder
    {
        if(!$this->supplierQuery)
            $this->supplierQuery = Supplier::query()->selectRaw('id');
        return $this->supplierQuery;
    }
    private function getCustomerQuery():Builder
    {
        if(!$this->customerQuery)
            $this->customerQuery = Customer::query()->selectRaw('id');
        return $this->customerQuery;
    }

    public function created_at_start($created_at)
    {
        $this->queryBuilder->where('created_at','>=',"{$created_at} 00:00:00");
    }

    public function created_at_end($created_at)
    {
        $this->queryBuilder->where('created_at','<=',"{$created_at} 23:59:59");
    }

    public function company_name($company_name)
    {
        $this->getCustomerQuery()->where('company_name','like',$company_name);
    }
    public function id($id)
    {
        $this->queryBuilder->whereIn('id',$id);
    }
    public function owner_id($owner_id)
    {
        $this->searchWay($this->getOwnerQuery(),$owner_id,'owner_id');
    }
    public function supplier_id($supplier_id)
    {
        $this->searchWay($this->getSupplierQuery(),$supplier_id,'id');
    }

    public function material_id($material_id)
    {
        $this->searchWay($this->getMaterialQuery(),$material_id,'id');
    }

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

    public function afterApply()
    {
        if($this->customerQuery)
            $this->getOwnerQuery()->whereIn('customer_id',$this->customerQuery);

        if($this->ownerQuery)
            $this->getOwnerMaterialQuery()->whereIn('owner_id',$this->ownerQuery);

        if($this->materialQuery)
            $this->getOwnerMaterialQuery()->whereIn('material_id',$this->materialQuery);

        if($this->ownerMaterialQuery)
            $this->queryBuilder->whereIn('owner_material_id',$this->ownerMaterialQuery);

        if($this->supplierQuery)
            $this->queryBuilder->whereIn('supplier_id',$this->supplierQuery);

    }
}
