<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\OrderRequest;
use App\Models\Order;
use App\Models\OrderAddress;
use App\Models\UserDescription;
use App\Models\OrderPayment;
use App\Models\OrderProduct;
use App\Models\User;
use App\Models\Visitor;
use App\Models\OrderShipping;
use App\Services\Notification\NotificationService;
use Illuminate\View\View;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/orders';
    /** View folder */
    private $viewDirectory= 'admin.orders.';

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('orders.head');
        $request= $request->toArray();
        return view($this->viewDirectory.'index',compact('title'));
    }

    /**
     * return data of the pages.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $query = Order::latest();

        if ($request->date_from) {
            $query->whereDate('orders.created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('orders.created_at', '<=', $request->date_to);
        }

        if ($request->id) {
            $query->where('id', $request->id);
        }
        
        if ($request->name) {
            $user_ids = User::where('name', 'LIKE', '%'.$request->name.'%')->pluck('id')->toArray();
            if(count($user_ids) == 0)
            {
                $user_ids =['0'];
            }
            $visitor_ids = Visitor::where('name', 'LIKE', '%'.$request->name.'%')->pluck('id')->toArray();
            if(count($visitor_ids) == 0)
            {
                $visitor_ids =['0'];
            }
            $ids = array_merge($visitor_ids,$user_ids);
            $company_ids = UserDescription::where('name', 'LIKE', '%'.$request->name.'%')->pluck('user_id')->toArray();
            if(count($company_ids) == 0)
            {
                $company_ids =['0'];
            }
            $ids = array_merge($company_ids,$user_ids);
            $query->whereRaw("user_id IN (".implode(',', $ids).")");
        }
        
        if ($request->phone) {
            $user_ids = User::where('phone', $request->phone)->pluck('id')->toArray();
            if(count($user_ids) == 0)
            {
                $user_ids =['0'];
            }
            $visitor_ids = Visitor::where('phone', $request->phone)->pluck('id')->toArray();
            if(count($visitor_ids) == 0)
            {
                $visitor_ids =['0'];
            }
            $ids = array_merge($visitor_ids,$user_ids);
            $query->whereRaw("user_id IN (".implode(',', $ids).")");
        }

        if ($request->role) {
            
            if($request->role == 'visitor')
            {
                $query->where('user_status','visitor');
            }
            if($request->role == 'user')
            {
                $ids = User::where('role', '!=','company')->pluck('id')->toArray();
                $query->where('user_status','user')->whereRaw("user_id IN (".implode(',', $ids).")");
            }
            if($request->role == 'company')
            {
                $ids = User::where('role', 'company')->pluck('id')->toArray();
                $query->where('user_status','user')->whereRaw("user_id IN (".implode(',', $ids).")");
            }
            
        }
        
        if ($request->type) {
            $query->where('status', $request->type);
        }

        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        $orders= $query->paginate(100);

        return view($this->viewDirectory.'grid',compact('orders'));
    }

    public function edit(Order $order)
    {
        $head= __('orders.edit');
        $statuses = ['progress','preparing','shipping','recieved'];
        $action= route('orders.update', $order->id);
        $products = OrderProduct::where('order_id',$order->id)->cursor();
        $address = OrderAddress::where('order_id',$order->id)->first();
        $payment = OrderPayment::where('order_id',$order->id)->first();
        $shipping = OrderShipping::where('order_id',$order->id)->first();
        return view($this->viewDirectory.'form', get_defined_vars());
    }
    public function update(OrderRequest $request, Order  $order)
    {
        $order->update(['status'=>$request->status]);
        if($order->user_status != 'visitor')
        {
            (new NotificationService)->sendNotification($request->status,$order->user_id,$order->id);
        }
        if($request->status == 'recieved')
        {
            $payment = OrderPayment::where('order_id',$order->id)->first();
            if($payment)
            {
                $payment->update(['user_status_paid'=>'paid']);
            }
        }
        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }
}
