<?php


namespace App\Filters;

use App\Order;
use App\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use phpDocumentor\Reflection\Types\Boolean;

class OrderPackageFilters
{
    protected $request;
    protected $queryBuilder;
    protected $filters = ['logistic_number', 'status', 'received_at_start',
        'received_at_end', 'is_weighed', 'logistic_id', 'owner_id', 'sent_at_start', 'sent_at_end', 'is_exception', 'exception_type', 'default_date','has_transfer_status'];

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

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


    private function logistic_number($logistic_number)
    {
        if (strpos($logistic_number, ',') || strpos($logistic_number, '，') || strpos($logistic_number, ' ')) {
            $arr = array_filter(preg_split('/[,， ]+/is', $logistic_number));
            $this->queryBuilder->whereIn('logistic_number',$arr);
        } else {
            $this->queryBuilder->where('logistic_number','like',$logistic_number."%");
        }
    }

    private function exception_type($exception_type)
    {
        $this->queryBuilder->where('exception_type', $exception_type);
    }
    private function has_transfer_status($has_transfer_status)
    {
        if ($has_transfer_status=='是') {
            $this->queryBuilder->whereNotNull('transfer_status');
        } elseif ($has_transfer_status=='否') {
            $this->queryBuilder->whereNull('transfer_status');
        }
    }

    private function status($status)
    {
        $this->queryBuilder->where('status', $status);
    }

    private function received_at_start($received_at_start)
    {
        $this->queryBuilder->where('received_at', '>=', $received_at_start);
    }

    private function received_at_end($received_at_end)
    {
        $this->queryBuilder->where('received_at', '<=', $received_at_end);
    }

    private function is_weighed($is_weighed)
    {
        if ($is_weighed == 'true') {
            $this->queryBuilder->whereNotNull('weighed_at');
        } else {
            $this->queryBuilder->whereNull('weighed_at');
        }
    }

    private function logistic_id($logistic_id)
    {
        $logistic_ids = array_filter(preg_split('/[,， ]+/is', $logistic_id));
        $this->queryBuilder->whereIn('order_id', function ($query) use ($logistic_ids) {
            $query->from('orders')->select('id')->whereIn('logistic_id', $logistic_ids);
        });
    }

    private function owner_id($owner_id)
    {
        $owner_ids = array_filter(preg_split('/[,， ]+/is', $owner_id));
        $this->queryBuilder->whereIn('owner_id', $owner_ids);
    }

    private function sent_at_start($sent_at_start)
    {
        $this->queryBuilder->where('sent_at', '>=', Carbon::parse($sent_at_start)->startOfDay());
    }

    private function sent_at_end($sent_at_end)
    {
        $this->queryBuilder->where('sent_at', '<=', Carbon::parse($sent_at_end)->endOfDay());
    }

    private function is_exception($is_exception)
    {
        $this->queryBuilder->where('exception', $is_exception);
    }

    public function default_date()
    {
        $this->queryBuilder->where('sent_at', '>=', now()->startOfDay()->subDays(15))->where('sent_at', '<', now()->startOfDay()->addDay());
    }
}
