<?php

namespace App\Http\Controllers\Web;

use App\Helpers\ImageHelper;
use App\Http\Controllers\Controller;
use App\Page;
use App\PageDescription;

use App\Http\Requests\ContactRequest;
use App\Http\Requests\RequestsRequest;

use Illuminate\Http\Request;
use \Illuminate\Support\Str;
use Illuminate\View\View;
use App\About;
use App\Branch;
use App\Contact;
use App\Result;
use App\Review;
use App\Type;
use App\User;
use App\Service;
use App\Requests;
use App\Offer;
use App\Video;
use App\DoctorExperience;
use App\DoctorImage;
use App\DoctorLanguage;


class PageController extends Controller
{
    private $viewDirectory = 'web.pages.';
    private $view = 'web.pages.';
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        // $this->middleware('auth')->except('lang');
    }

    /**
     * Show the Pages.
     *
     * @return \View
     */
     public function about(){
         $about = About::where('id','1')->first();
          return view($this->view.'about',compact('about'));
     }
     
     public function offers(){
         $offers = Offer::orderBy('id','DESC')->paginate(6);
          return view($this->view.'offers',compact('offers'));
     }
     public function privacy(){
        
          return view($this->view.'privacy');
     }
     public function videos(){
         $videos = Video::orderBy('id','DESC')->paginate(6);
          return view($this->view.'videos',compact('videos'));
     }
     
     
     
     public function contact(){
         $branches = Branch::cursor();
          return view($this->view.'contact',compact('branches'));
     }
     public function results(){
         $results = Result::cursor();
          return view($this->view.'results',compact('results'));
     }
     public function team(){
         $doctors = User::where('role_id','4')->get();
          return view($this->view.'team',compact('doctors'));
     }
     public function contacts(ContactRequest $request){
         Contact::create($request->all());
          return redirect()->back()->with('success','تم تسجيل طلبك بنجاح');
     }
     public function reviews(){
         $reviews = Review::cursor();
          return view($this->view.'reviews',compact('reviews'));
     }
     public function requests(){
         $services = Service::cursor();
         $branches = Branch::cursor();
          return view($this->view.'requests',compact('services','branches'));
     }
      public function request(RequestsRequest $request){
          Requests::create($request->all());
          return redirect()->back()->with('success','تم تسجيل طلبك بنجاح');
      }
     public function services(){
         $types = Type::cursor();
          return view($this->view.'services',compact('types'));
     }
     public function doctor($id){
         $doctor = User::find($id);
         $experiences = DoctorExperience::where('user_id',$id)->get();
         $images = DoctorImage::where('user_id',$id)->get();
         $langs = DoctorLanguage::where('user_id',$id)->get();
          return view($this->view.'doctor',compact('doctor','langs','experiences','images'));
     }
    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'));
    }

    /**
     * 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') );
    }
}
