<?php

namespace App\Http\Controllers\Admin;

use App\Module;
use Exception;
use App\Mail;

use Illuminate\View\View;

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

use Illuminate\Http\RedirectResponse;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class MailController extends Controller
{

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

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('mails.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 = Mail::latest();

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

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

        if ($request->email) {
            $query->where('email', 'LIKE', '%'.$request->email.'%');
        }

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

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

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

}
