<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use App\Models\ProductDescription;
use Exception;
use App\Models\Rate;
use App\Models\Visitor;
use App\Models\UserDescription;
use App\Models\User;
use Illuminate\View\View;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;

class RateController extends Controller
{

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

    /**
     * Display a listing of the rates.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('rates.head');
        $request= $request->toArray();
        $rates = [1,2,3,4,5];
        return view($this->viewDirectory.'index',compact('title','rates'));
    }

    /**
     * return data of the rates.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $query = Rate::where('user_id','!=',null)->latest();

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

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

        if ($request->user) {
            $user_ids = User::where('name', 'LIKE', '%'.$request->user.'%')->pluck('id')->toArray();
            if(count($user_ids) == 0)
            {
                $user_ids =['0'];
            }
            $visitor_ids = Visitor::where('name', 'LIKE', '%'.$request->user.'%')->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).")");
//            $query->where('name', 'LIKE', '%'.$request->user.'%');
        }
        if ($request->rate) {
            $query->where('rate', $request->rate);
        }
        if ($request->product) {
            $ids = ProductDescription::where('name', 'LIKE', '%'.$request->product.'%')->pluck('product_id')->toArray();
            if(count($ids) == 0)
            {
                $ids =['0'];
            }
            $query->whereRaw("product_id IN (".implode(',', $ids).")");
        }

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

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


    public function edit(Rate $rate) :View
    {
        $head= __('rates.edit');

        return view( $this->viewDirectory.'form', compact('rate', 'head') );
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param City $rate
     * @return RedirectResponse|Redirector
     */
    public function update(Request $request, Rate $rate) :RedirectResponse
    {

        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }



    /**
     * Remove the specified resource from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $rate = Rate::withTrashed()->find($id);
        if ($rate) {
            if ($rate->deleted_at) {
                $rate->forceDelete();
            } else {
                $rate->delete();
            }
            return response()->json(['message'=> __('dashboard.deletedDone')]);
        } else{
            return response()->json(['message'=> __('dashboard.noResult')], 400);
        }
    }

    /**
     * Remove several pages by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $ids= $request->ids;
        if ($request->force) {
            Rate::onlyTrashed()->whereIn('id', $ids)->forceDelete();
        } else {
            Rate::whereIn('id', $ids)->delete();
        }

        return response()->json(['message'=> __('dashboard.deletedDone')]);
    }

    /**
     * Restore the specified category from storage
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $rate = Rate::withTrashed()->find($id);

        if ($rate){
            $rate->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
        return response()->json(['message'=> __('dashboard.noResult')], 400);
    }

    /**
     * Restore several pages by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $rate = Rate::whereIn('id', $request->ids)->onlyTrashed();
        if ($rate){
            $rate->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
        return response()->json(['message'=> __('dashboard.saveDone')]);
    }



}
