<?php


namespace App\Filters;



use App\Procurement;
use App\ProcurementDeliverie;
use App\Supplier;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class ProcurementCheckSheetFilters
{
    protected $request;
    protected $queryBuilder;
    protected $procurementDeliveryQuery;
    protected $procurementQuery;
    protected $supplierQuery;
    protected $array_filter;
    protected $filters = [
        'id','status','signed_at','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),function($item){
            return $item == null ? false : true;
        });
        $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 getProcurementDeliveryQuery():Builder
    {
        if(!$this->procurementDeliveryQuery)
            $this->procurementDeliveryQuery = ProcurementDeliverie::query()->selectRaw('id');
        return $this->procurementDeliveryQuery;
    }
    private function getProcurementQuery():Builder
    {
        if(!$this->procurementQuery)
            $this->procurementQuery = Procurement::query()->selectRaw('id');
        return $this->procurementQuery;
    }
    private function getSupplierQuery():Builder
    {
        if(!$this->supplierQuery)
            $this->supplierQuery = Supplier::query()->selectRaw('id');
        return $this->supplierQuery;
    }
    public function id($id)
    {
        $this->queryBuilder->whereIn('id',$id);
    }
    public function status($status)
    {
        $this->queryBuilder->where('status',$status);
    }

    public function supplier_id($supplier_id)
    {
        $this->searchWay($this->getSupplierQuery(),$supplier_id,'id');
    }
    public function signed_at($signed_at)
    {
//        $this->getProcurementDeliveryQuery()->where('signed_at','>=',"{$signed_at} 00:00:00")->where('signed_at','<=',"{$signed_at} 23:59:59");
        $this->getProcurementDeliveryQuery()->where('signed_at','like',$signed_at.'%');
    }

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

    public function afterApply()
    {

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

        if($this->procurementQuery)
            $this->getProcurementDeliveryQuery()->whereIn('procurement_id',$this->procurementQuery);

        if($this->procurementDeliveryQuery)
            $this->queryBuilder->whereIn('procurement_delivery_id',$this->procurementDeliveryQuery);


    }
}
