<?php

namespace App\Http\Controllers\nn_site;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\nn_menu_item;
use App\Models\nn_menu_item_lang;
use Validator;

class SiteContactPage extends SiteController
{
    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
        $menuItem = nn_menu_item::where(['slug' => 'contact'])->firstOrFail();

        $data['item'] = nn_menu_item_lang::where(['nn_menu_item_id' => $menuItem->id, 'lang' => getCurrentLocale()])->firstOrFail();

        return view('nn_site.pages.contact', $data);
    }

    /**
     * Send.
     *
     * @return \Illuminate\Http\Response
     */
    public function send(Request $request)
    {
        $name =     $request->name;
        $email =    $request->email;
        $subject =  'from user: '.$name;
        $message =  $name.': <br>'.$request->message;
        $to =       'contact@orien.ge';
        $headers =  'From: '.$email."\r\n" .
                    'Reply-To: '.$email."\r\n".
                    'Content-Type: text/html; charset=UTF-8';

        // validation
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required|email',
            'message' => 'required'
        ]);

        if($validator->fails()){
            return redirect(getCurrentLocale() . '/contact-us')->withErrors($validator->errors())->withInput();
        }else{
            if(mail($to, $subject, $message, $headers)){
                return redirect(getCurrentLocale() . '/contact-us')->with('status', 'success');
            }
        }
    }
}