<?php

namespace App\Http\Controllers\Admin;

use App\Module;
use Exception;
use App\Product;
use App\User;
use App\ProductPhoto;
use App\Models;
use App\Exports\ProductExport;
use Maatwebsite\Excel\Facades\Excel;
use App\City;
use App\Follow;
use App\Log;
use App\Country;
use App\ProductAttribute;
use App\Writer;
use App\Category;
use Illuminate\View\View;
use App\ProductDescription;
use App\ProductElement;
use App\ProductTag;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use App\Http\Controllers\Controller;
use App\Http\Requests\ViewRequest;
use Illuminate\Http\RedirectResponse;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class LogController extends Controller
{

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

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return View
     */
    
    


    public function index(Request $request) :View
    {
        $title= __('logs.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 = Log::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->admin) {
            $ids = User::where('name', 'LIKE', '%'.$request->admin.'%')->pluck('id');
            $query->whereIn('id',$ids);
        }
        if ($request->type) {
            $query->where('type', 'LIKE', '%'.$request->type.'%');
        }
        if ($request->action) {
            $query->where('action', 'LIKE', '%'.$request->action.'%');
        }
        

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

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

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