<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Mail\SendIdCardLink;
use App\Models\IdRequest;
use App\Models\ManagerUser;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class IdCardRequestController extends Controller
{
    public function __construct()
    {
        $this->middleware(['permission:view_id_card_request'])->only(['id_card_requests']);
        $this->middleware(['permission:accept_id_card_request'])->only(['id_card_requests.accept']);
        $this->middleware(['permission:reject_id_card_request'])->only(['id_card_requests.reject']);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.id_card_requests.index', ['id_card_requests' => IdRequest::all()]);
    }

    public function accept(Request $request)
    {
        $idRequest = IdRequest::whereId($request->id_card_request_id)->whereUserId($request->user_id)->first();
        $idRequest->update(['status' => 'accepted']);

        $details = [
            'title' => 'ID Card Request' . ' ' . $idRequest->user->the_id . ' ' . $idRequest->user->name_en . ' ' . '(Accepted)',
            'body' => 'ID Card Request' . ' ' . $idRequest->user->the_id . ' ' . $idRequest->user->name_en . "<br />" . 'Dear ' . $idRequest->user->name_en . ' , Kindly note that your ID card request ' . $idRequest->user->name_en . ' ' . $idRequest->created_at->format('d-m-y H:i:s') . ' ' .  route('business_card', $request->user_id) . ' has been accepted and will be deliver to you  in 4 working days . '. "<br /> <br /> <br />" . 'Human resources team'
        ];

        $managers = ManagerUser::whereUserId($idRequest->user->id)->get();

        if($managers !== null){
            foreach ($managers as $manager) {
                Mail::to(User::whereId($manager->manager_id)->first()->email)->send(new SendIdCardLink($details));
            }
        }

        Mail::to($idRequest->user->email)->send(new SendIdCardLink($details));
        Mail::to('ahmedhamzawy3@gmail.com')->send(new SendIdCardLink($details));
        //Mail::to('bolaayad1@gmail.com')->send(new SendIdCardLink($details));


        $details = [
            'title' => 'ID Card Request' . ' ' . $idRequest->user->the_id . ' ' . $idRequest->user->name_en . ' ' . '(Accepted New Request)',
            'body' => 'ID Card Request' . ' ' . $idRequest->user->the_id . ' ' . $idRequest->user->name_en . "<br />" . 'Dear all , Kindly find the ID card Request ' . $idRequest->user->name_en . ' ' . $idRequest->created_at->format('d-m-y H:i:s') . ' ' .  route('business_card', $request->user_id) . ' has been accepted delivery time 1 working day . '. "<br /> <br /> <br />" . 'EAST human recourses team' . "<br />" . 'HR [ Mobile number]' . "<br /> <br />" . 'Notes: this is sending email only, if you need support or more details kindly call the HR above number'
        ];


        Mail::to('east.abcidp@gmail.com')->send(new SendIdCardLink($details));
        Mail::to('ahmedhamzawy3@gmail.com')->send(new SendIdCardLink($details));

        activity()->log('قام '.auth()->user()->name.'بالموافقه على الاى دى كارد رقم '.$request->id_card_request_id);

        toastr()->success(trans('dashboard.accepted'));

        return redirect()->back();
    }

    public function reject(Request $request)
    {
        $idRequest = IdRequest::whereId($request->id_card_request_id)->whereUserId($request->user_id)->first();
        $idRequest->update(['status' => 'rejected']);

        $details = [
            'title' => 'ID Card Request' . ' ' . $idRequest->user->the_id . ' ' . $idRequest->user->name_en . ' ' . '(Rejected)',
            'body' => 'ID Card Request' . ' ' . $idRequest->user->the_id . ' ' . $idRequest->user->name_en . "<br />" . 'Dear ' . $idRequest->user->name_en . ' , Kindly note that your ID card request ' . $idRequest->user->name_en . ' ' . $idRequest->created_at->format('d-m-y H:i:s') . ' ' .  route('business_card', $request->user_id) . ' has been rejected , please check the notes below'
        ];

        $managers = ManagerUser::whereUserId($idRequest->user->id)->get();

        if($managers !== null){
            foreach ($managers as $manager) {
                Mail::to(User::whereId($manager->manager_id)->first()->email)->send(new SendIdCardLink($details));
            }
        }

        Mail::to($idRequest->user->email)->send(new SendIdCardLink($details));
        Mail::to('ahmedhamzawy3@gmail.com')->send(new SendIdCardLink($details));
        //Mail::to('bolaayad1@gmail.com')->send(new SendBusinessCardLink($details));

        activity()->log('قام '.auth()->user()->name.'برفض الاى دى كارد رقم '.$request->id_card_request_id);

        toastr()->error(trans('dashboard.rejected'));

        return redirect()->back();
    }
}
