<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Rules\GoogleRecaptcha;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
use MenuHelper;
class HomeController extends Controller
{

    public function searchPrice(Request $request){
        //dd('shakil');
            //dd($request->all());
    }
    public function index() {
        $paramn='error';
        $response = Http::post(config('app.backendapi').'module/module-list', [
            "data"=>[
                "storeId" => config('app.storeId'),
                "slug" => "home-3"
            ]
			
        ]);
		//dd($response);exit;
        $details = $response->json();
		
		
        $data = '';
        if(isset($details['errorNode']['errorCode']) && $details['errorNode']['errorCode'] == 0){
            if(count($details['data']['details']) > 0){
                $data = $details['data']['details'][0];
                $paramn = str_replace(' ','-', strtolower($details['data']['pageName']));
            }
        }
        
        // if($data!=''){
        //     if(view()->exists('home'.$paramn)==true){
        //         return view('welcome'.$paramn)->with('data',$data)->with('page',$paramn);
        //     } else {
        //         return view('home.common')->with('data',$data)->with('page',$paramn);
        //     }
        // } else {
        //     return view('welcome'.$paramn)->with('data',$data)->with('page',$paramn);
        // }

        if($data!=''){
            if(view()->exists('home/'.$paramn)==true){
                return view('home/'.$paramn)->with('data',$data)->with('page',$paramn);
            } else {
                return view('home/common')->with('data',$data)->with('page',$paramn);
            }
        } else {
            return view('home/'.$paramn)->with('data',$data)->with('page',$paramn);
        }



    }
    public function allMenu(Request $request){
        $data['success'] = false;
        $data['topMenu'] = '';
        $data['sideMenu'] = '';
        $data['footerMenu'] = '';
        $response = Http::post(config('app.backendapi').'menu/menu-list', [
            "data"=>[
                "storeId" => config('app.storeId')
            ]
        ]);
        $menu = $response->json();
        if($menu['errorNode']['errorCode'] == 0){
            $data['success'] = true;
            $data['topMenu'] = MenuHelper::HeaderMenuRecursive($menu['data']['headerMenu'],'-top');
            $data['sideMenu'] = MenuHelper::HeaderMenuSideRecursive($menu['data']['headerMenu'],'-side');
            $data['footerMenu'] = MenuHelper::HeaderMenuFooterRecursive($menu['data']['footerMenu'],'-footer');
        }
        return response()->json($data);
    }
    public function commonPage($param) {
        $paramn='error';
        $response = Http::post(config('app.backendapi').'module/module-list', [
            "data"=>[
                "storeId" => config('app.storeId'),
                "slug"=>$param
            ]
        ]);
        $details = $response->json();
        $data = '';
        if(isset($details['errorNode']['errorCode']) && $details['errorNode']['errorCode'] == 0){
            if(count($details['data']['details']) > 0){
                $data = $details['data']['details'][0];
                $paramn = str_replace(' ','-', strtolower($details['data']['pageName']));
            }
        }
        
        if($data!=''){
            if(view()->exists('home/'.$paramn)==true){
                return view('home/'.$paramn)->with('data',$data)->with('page',$paramn);
            } else {
                return view('home/common')->with('data',$data)->with('page',$paramn);
            }
        } else {
            return view('home/'.$paramn)->with('data',$data)->with('page',$paramn);
        }
    }
    public function business($param) {
        $paramn='error';
        $response = Http::post(config('app.backendapi').'module/module-details', [
            "data"=>[
                "storeId" => config('app.storeId'),
                "slug"=>$param
            ]
        ]);
        $details = $response->json();
        $data = '';
        if(isset($details['errorNode']['errorCode']) && $details['errorNode']['errorCode'] == 0){
            if(count($details['data']['details']) > 0){
                $data = $details['data']['details'][0];
                $paramn = 'business';
            }
        }
        if($data!=''){
            if(view()->exists('home/'.$paramn)==true){
                return view('home/'.$paramn)->with('data',$data)->with('page',$paramn);
            } else {
                return view('home/common')->with('data',$data)->with('page',$paramn);
            }
        } else {
            return view('home/'.$paramn)->with('data',$data)->with('page',$paramn);
        }
    }

    public function contactUsSave(Request $request){
        $response = Http::post(config('app.backendapi').'student/contactUsSave', [
            "data"=>[
                "storeId" => config('app.storeId'),
                "name" => $request->fname,
                "email" => $request->email,
                "contactNo" => $request->phone,
                "url" => $request->url,
                "date" => $request->date,
                "message" => $request->message,
                'g-recaptcha-response' => ['required', new GoogleRecaptcha]
                ,['g-recaptcha-response.required' => 'The recaptcha field is required.']
            ]
        ]);
        $details = $response->json();
        $data = '';
        $err = '';
        if(isset($details['errorNode']['errorCode']) && $details['errorNode']['errorCode'] == 0){
            $data = $details['data']['message'];
        } else {
            $err = $details['data']['message'];
        }

        if($data!=''){
            Session::flash('message', $data); 
            return back()->withInput();
        } else {
            Session::flash('message', $err); 
            return back()->withInput();
        }
    }
    public function enquiryForm(Request $request){
        $response = Http::post(config('app.backendapi').'student/contactUsSave', [
            "data"=>[
                "storeId" => config('app.storeId'),
                "name" => $request->name,
                "email" => $request->email,
                "contactNo" => $request->mobile,
                "message" => "Enquiry: ".$request->enquiry."\nEnquiry For: ".$request->enquiryfor,
            ]
        ]);
        $details = $response->json();
        $data = '';
        $err = '';
        if(isset($details['errorNode']['errorCode']) && $details['errorNode']['errorCode'] == 0){
            $data = $details['data']['message'];
        } else {
            $err = $details['data']['message'];
        }
        if($data!=''){
            Session::flash('message', $data); 
            return back()->withInput();
        } else {
            Session::flash('message', $err); 
            return back()->withInput();
        }
    }
    public function currentOpenings(Request $request){
        $response = Http::post(config('app.backendapi').'student/contactUsSave', [
            "data"=>[
                "storeId" => config('app.storeId'),
                "name" => $request->name,
                "email" => $request->email,
                "contactNo" => $request->mobile,
                "message" => "Address: ".$request->address."\nApplying For: ".$request->applyingfor."\nExpected CTC: ".$request->expectedctc,
                "file" => base64_encode($request->file('file'))
            ]
        ]);
        $details = $response->json();
        $data = '';
        $err = '';
        if(isset($details['errorNode']['errorCode']) && $details['errorNode']['errorCode'] == 0){
            $data = $details['data']['message'];
        } else {
            $err = $details['data']['message'];
        }
        if($data!=''){
            Session::flash('message', $data); 
            return back()->withInput();
        } else {
            Session::flash('message', $err); 
            return back()->withInput();
        }
    }
	public function noticeDetails($param) {
        $paramn = 'error';
        $response = Http::post(config('app.backendapi') . 'module/subsection-details', [
            "data" => [
                "storeId" => config('app.storeId'),
                "slug" => $param
            ]
        ]);
        $details = $response->json();
		//dd($details);exit;
        $data = '';
        if (isset($details['errorNode']['errorCode']) && $details['errorNode']['errorCode'] == 0) {
            $data = $details['data']['details'];
            $paramn = 'noticedetails';
        }
        
        $title = 'Notice Details';
        return view('home/' . $paramn, compact('title'))->with('data', $data);
    }
    public function newsletter(Request $request){
        $response = Http::post(config('app.backendapi').'iudyog/subscribe',[
            "data"=>[
                "storeId" => config('app.storeId'),
                "email" => $request->email,
            ]
        ]);
        $details = $response->json();
        $data = '';
        $err = '';
        if(isset($details['errorNode']['errorCode']) && $details['errorNode']['errorCode'] == 0){
            $data = $details['data']['message'];
        } else {
            $err = $details['data']['message'];
        }
        if($data!=''){
            Session::flash('message', $data); 
            return back()->withInput();
        } else {
            Session::flash('message', $err); 
            return back()->withInput();
        }
    }
    public function Error() {
        return view('home/error');
    }
}
