<?php

namespace App\Http\Controllers\Admin;

use App\Models\Mail;

use Illuminate\View\View;

use App\Exports\MailExport;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;

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)
    {
        $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();
        }

        if($request->export)
        {
            $mails= $query->cursor();
            return Excel::download(new MailExport($mails), 'Mails.xlsx');
        }
        
        $mails= $query->paginate(100);

        return view($this->viewDirectory.'grid',compact('mails'));
    }
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $mail = Mail::withTrashed()->find($id);
        if ($mail) {
            if ($mail->deleted_at) {
                $mail->forceDelete();
            } else {
                $mail->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) {
            Mail::onlyTrashed()->whereIn('id', $ids)->forceDelete();
        } else {
            Mail::whereIn('id', $ids)->delete();
        }

        return response()->json(['message'=> __('dashboard.deletedDone')]);
    }
    public function restore(Request $request, int $id) :JsonResponse
    {
        $mail = Mail::withTrashed()->find($id);

        if ($mail){
            $mail->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
    {
        $mail = Mail::whereIn('id', $request->ids)->onlyTrashed();
        if ($mail){
            $mail->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
        return response()->json(['message'=> __('dashboard.saveDone')]);
    }
}
