<?php

namespace App\Http\Controllers\Web;

use App\Helpers\ImageHelper;
use App\Http\Controllers\Controller;
use App\Page;
use App\Method;
use App\Branch;
use App\Reason;
use App\Vision;
use App\Mission;
use App\Mail;
use App\Contact;
use App\Reservation;
use App\Service;
use App\PageDescription;
use Illuminate\Http\Request;
use App\Http\Requests\Web\SubscribeRequest;
use App\Http\Requests\Web\ContactRequest;
use App\Http\Requests\Web\BookRequest;
use \Illuminate\Support\Str;
use Illuminate\View\View;

class PageController extends Controller
{
    private $viewDirectory = 'web.pages.';
    /**
     * Create a new controller instance.
     *
     * @return void
     */
     
     public function subscribe(SubscribeRequest $request){
         Mail::create($request->all());
         return json_encode('done');
     }
     
     public function contacting(ContactRequest $request){
         Contact::create($request->all());
         return json_encode('done');
     }
     public function book(BookRequest $request){
         $reserve = Reservation::find($request->date);
         $request->merge(['type'=>'reserved']);
         unset($request['date']);
         $reserve->update($request->all());
         return json_encode('done');
     }
     
     
     
    public function __construct()
    {
        // $this->middleware('auth')->except('lang');
    }

    /**
     * Show the Pages.
     *
     * @return \View
     */
    public function index(): View
    {
        $query= Page::latest()
            ->join('page_descriptions AS pd', 'pages.id', 'pd.page_id')
            ->where('status', 1)
            ->where('language_id', currentLanguage()->id)
            ->select(['pd.*', 'pages.*'])->get();

        $pages= [];
        if($query){
            $pages= $query->toArray();
        }
        foreach ($pages as $key => $value) {
            $pages[$key]['image']= ImageHelper::get($value['image']);
            $pages[$key]['intro']= Str::words(strip_tags($value['description']), 10, '...');
            $pages[$key]['route']= route('page_details', ['page'=> $value['page_id'], 'slug'=> str_slug($value['slug'])] );
        }
        // dd($pages);
        $settings= settings();
        $title= $settings['website_name'];
        $keywords= $settings['keywords'];
        $meta_description= $settings['meta_description'];

        return view($this->viewDirectory.'index', compact( 'pages', 'title', 'keywords', 'meta_description'));
    }
    
    public function contactus(){
        $methods  = Method::cursor();
        $branches = Branch::cursor();
        return view($this->viewDirectory.'contactus', compact( 'methods','branches'));
    }
    public function booking(){
        $reasons  = Reason::cursor();
        return view($this->viewDirectory.'booking', compact( 'reasons'));
    }
    public function aboutus(){
        $about  = Page::first();
        $visions = Vision::where('page_id',$about->id)->where('language_id',currentLanguage()['id'])->get();
        $missions = Mission::where('page_id',$about->id)->where('language_id',currentLanguage()['id'])->get();
        return view($this->viewDirectory.'aboutus', compact( 'about','visions','missions'));
    }
    
    public function method(){
        $methods  = Method::cursor();
        return view($this->viewDirectory.'methods', compact( 'methods'));
    }
    public function service(){
        $services  = Service::cursor();
        return view($this->viewDirectory.'services', compact( 'services'));
    }
    
    /**
     * Show the Page details.
     * @param Page
     * @return \View
     */
    public function details(Page $page, Request $request) :View
    {
        $image= ImageHelper::get($page->image);
        $description = PageDescription::where('page_id', $page->id)
        ->where('language_id', currentLanguage()->id)
        ->first();
        // dd($description->toArray(), $image);

        $title= $description->title;
        $keywords= $description->keywords;
        $meta_description= $description->meta_description;

        return view( $this->viewDirectory.'details', compact('page', 'description', 'image', 'title', 'keywords', 'meta_description') );
    }
}
