<?php

namespace App\Http\Controllers\Web;

use App\VisitorMessage;
use Illuminate\View\View;
use Illuminate\Http\JsonResponse;
use App\Jobs\SendEmailToAdminJob;
use App\Http\Controllers\Controller;
use App\Http\Requests\Web\VisitorMessageRequest;

class VisitorMessageController extends Controller
{
    private $view = 'web.contact_us.';
    /**
     * Show the Contact US details.
     * @return View
     */
    public function index()
    {
        $title = __('visitorMessages.head');
        return view( $this->view.'index', get_defined_vars());
    }

    /**
     * Store a Message and send it to the admin in storage.
     *
     * @param VisitorMessageRequest $request
     * @return JsonResponse
     */
    public function store(VisitorMessageRequest $request) :JsonResponse
    {
        $email = $request->subject == 'sales' ? settings('sales_email') : settings('complaints_email');
        try{
            $data = [
                'user_name' => $request->name,
                'user_email' => $request->email,
                'subject' => $request->subject,
                'message' => $request->message,
                'email' => $email,
            ];
            SendEmailToAdminJob::dispatch($data, 'web.contact_us.email');
        } catch (\Exception $exception){}
        VisitorMessage::create($request->all());
        return response()->json(['message' => __('visitorMessages.message_success')]);
    }
}
