<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;

use App\history;
use App\elink;
use App\form;
use App\implink;
use App\notice;
use App\slider;
use App\gallery;
use App\admission;
use App\routine;
use App\result;
use App\contact;
use App\schedule;
use App\speech;
//for BD date
use Jenssegers\Date\Date;


class AdminController extends Controller
{
    
   
    //public  view  part
    
    public function indexview()
    {
        Date::setLocale('bd');
        $date =  Date::now()->format('l j F Y '); 
        $sliders = slider::all();
        
        $history = DB::table('historys')->first();
        $video = DB::table('videos')->first();
        $contact = DB::table('contacts')->first();
        $schedule = DB::table('schedule')->first();
        $notices = notice::all();
        $forms = form::all();
        $elinks = elink::all();
        $implinks =  implink::all();
        $galleries =  DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $speechs = DB::table('speechs')->limit(2)->get();
        return view('home')->with(['date'=>$date,'galleries'=>$galleries,'slider'=>$sliders,'contact'=>$contact,'schedule'=>$schedule,'history'=>$history,'speechs'=>$speechs,'video'=>$video,'notices'=> $notices,'forms'=>$forms,'elinks'=>$elinks,'implinks'=>$implinks]);
    }
    
    
       public function history()
    {
        Date::setLocale('bd');
        $date =  Date::now()->format('l j F Y '); 
        $forms = form::all();
        $elinks = elink::all();
        $implinks =  implink::all();
        $notices = notice::all();
        $history = DB::table('historys')->first();
        $schedule = DB::table('schedule')->first();
        $galleries =  DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $contact = DB::table('contacts')->first();
        return view('history')->with(['schedule'=>$schedule,'galleries'=>$galleries,'date'=>$date,'contact'=>$contact,'history'=>$history,'notices'=> $notices,'forms'=>$forms,'elinks'=>$elinks,'implinks'=>$implinks]);
    }
    
    public function notice()
    {
        Date::setLocale('bd');
        $date =  Date::now()->format('l j F Y '); 
        $forms = form::all();
        $elinks = elink::all();
        $implinks =  implink::all();
        $notices = notice::all();
        $schedule = DB::table('schedule')->first();
        $galleries =  DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $history = DB::table('historys')->first();
        $contact = DB::table('contacts')->first();
        return view('notice')->with(['schedule'=>$schedule,'galleries'=>$galleries,'date'=>$date,'contact'=>$contact,'history'=>$history,'notices'=> $notices,'forms'=>$forms,'elinks'=>$elinks,'implinks'=>$implinks]);
    }
      public function single_notice($id)
    {
        Date::setLocale('bd');
        $date =  Date::now()->format('l j F Y '); 
        $forms = form::all();
        $elinks = elink::all();
        $implinks =  implink::all();
        $single_notice = DB::table('notices')->where('id', '=', $id)->get();  
        $schedule = DB::table('schedule')->first();
        $notices = notice::all();
        $galleries =  DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $history = DB::table('historys')->first();
        $contact = DB::table('contacts')->first();

        return view('single-notice')->with(['schedule'=>$schedule,'galleries'=>$galleries,'date'=>$date,'contact'=>$contact,'history'=>$history,'notices'=> $notices,'single_notice'=>$single_notice, 'forms'=>$forms,'elinks'=>$elinks,'implinks'=>$implinks]);
    }
    
    
     
    public function importantlinks()
    {
        Date::setLocale('bd');
        $date =  Date::now()->format('l j F Y '); 
        $forms = form::all();
        $elinks = elink::all();
        $implinks =  implink::all();
        $notices = notice::all();
        $schedule = DB::table('schedule')->first();
        $galleries =  DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $history = DB::table('historys')->first();
        $contact = DB::table('contacts')->first();
        return view('important-links')->with(['schedule'=>$schedule,'galleries'=>$galleries,'date'=>$date,'contact'=>$contact,'history'=>$history,'notices'=> $notices,'forms'=>$forms,'elinks'=>$elinks,'implinks'=>$implinks]);

    }
      public function eshebalinks()
    {
        Date::setLocale('bd');
        $date =  Date::now()->format('l j F Y '); 
        $forms = form::all();
        $elinks = elink::all();
        $implinks =  implink::all();
        $notices = notice::all();
        $schedule = DB::table('schedule')->first();
        $galleries =  DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $history = DB::table('historys')->first();
        $contact = DB::table('contacts')->first();
        return view('esheba-links')->with(['schedule'=>$schedule,'galleries'=>$galleries,'date'=>$date,'contact'=>$contact,'history'=>$history,'notices'=> $notices,'forms'=>$forms,'elinks'=>$elinks,'implinks'=>$implinks]);

    }
    
     public function importantforms()
    {
        Date::setLocale('bd');
        $date =  Date::now()->format('l j F Y '); 
        $forms = form::all();
        $elinks = elink::all();
        $implinks =  implink::all();
        $notices = notice::all();
        $schedule = DB::table('schedule')->first();
        $galleries =  DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $history = DB::table('historys')->first();
        $contact = DB::table('contacts')->first();
        return view('important-forms')->with(['schedule'=>$schedule,'galleries'=>$galleries,'date'=>$date,'contact'=>$contact,'history'=>$history,'notices'=> $notices,'forms'=>$forms,'elinks'=>$elinks,'implinks'=>$implinks]);

    }
    
    
public function admission_info()
    {
        Date::setLocale('bd');
        $date =  Date::now()->format('l j F Y '); 
        $forms = form::all();
        $elinks = elink::all();
        $implinks =  implink::all();
        $notices = notice::all();
        $schedule = DB::table('schedule')->first();
        $galleries =  DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $admission = DB::table('admission_infos')->first();
        $contact = DB::table('contacts')->first();
        //$request->session()->flash('success', 'Admission Information  Added successfully!');
        return view('admission')->with(['schedule'=>$schedule,'galleries'=>$galleries,'contact'=>$contact,'date'=>$date,'admission'=>$admission,'notices'=> $notices,'forms'=>$forms,'elinks'=>$elinks,'implinks'=>$implinks]);

    }
    public function gallery_view()
    {
        Date::setLocale('bd');
        $date =  Date::now()->format('l j F Y '); 
        $forms = form::all();
        $elinks = elink::all();
        $implinks =  implink::all();
        $notices = notice::all();
        $galleries = gallery::all();
         $schedule = DB::table('schedule')->first();
        $contact = DB::table('contacts')->first();   
        return view('gallery')->with(['schedule'=>$schedule,'galleries'=>$galleries,'contact'=>$contact,'date'=>$date,'galleries'=>$galleries,'notices'=> $notices,'forms'=>$forms,'elinks'=>$elinks,'implinks'=>$implinks]);
    }
    
     public function routine_show()
    {
        Date::setLocale('bd');
        $date =  Date::now()->format('l j F Y '); 
        $forms = form::all();
        $elinks = elink::all();
        $implinks =  implink::all();
        $notices = notice::all();
         $schedule = DB::table('schedule')->first();
        $galleries =  DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $routines = routine::all();
        $contact = DB::table('contacts')->first();
        return view('routine')->with(['schedule'=>$schedule,'routines'=>$routines,'contact'=>$contact,'galleries'=>$galleries,'date'=>$date,'galleries'=>$galleries,'notices'=> $notices,'forms'=>$forms,'elinks'=>$elinks,'implinks'=>$implinks]);
    }
     public function result_show()
    {
        Date::setLocale('bd');
        $date =  Date::now()->format('l j F Y '); 
        $forms = form::all();
        $elinks = elink::all();
        $implinks =  implink::all();
        $notices = notice::all();
         $schedule = DB::table('schedule')->first();
        $galleries =  DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $results = result::all();
        $contact = DB::table('contacts')->first();
        return view('result')->with(['schedule'=>$schedule,'results'=>$results,'contact'=>$contact,'galleries'=>$galleries,'date'=>$date,'galleries'=>$galleries,'notices'=> $notices,'forms'=>$forms,'elinks'=>$elinks,'implinks'=>$implinks]);
    }
    
    
  public function contact()
    {
        Date::setLocale('bd');
        $date =  Date::now()->format('l j F Y '); 
        $forms = form::all();
        $elinks = elink::all();
        $implinks =  implink::all();
        $notices = notice::all();
         $schedule = DB::table('schedule')->first();
        $galleries =  DB::table('galleries')->inRandomOrder()->limit(4)->get();
        $contact = DB::table('contacts')->first();
        return view('contact')->with(['schedule'=>$schedule,'galleries'=>$galleries,'contact'=>$contact,'date'=>$date,'notices'=> $notices,'forms'=>$forms,'elinks'=>$elinks,'implinks'=>$implinks]);
    }   
    
  
//public  view  part end ADMIN  PART START
    
    public function index()
    {
        return view('admin.dashboard');
    }
    
    public function school_history()
    {
        
        $history = DB::table('historys')->first();
        return view('admin.history')->with(['history'=>$history]);
    }
    
    public function updatehistory(Request $request)
        {
        $data = $request->history;
        DB::table('historys')->where('id', 1)->update(['history' => $data]);
        $history = DB::table('historys')->first();
        $request->session()->flash('success', 'School History  Updated successfully!');
        return redirect('admin/history')->with(['history'=>$history]);
    }
    
    
    public function speech()
    { 
        $speech = DB::table('speechs')->first();
        return view('admin.speech')->with(['speech'=>$speech]);
    }
    
    public function updatespeech(Request $request)
    {    
        $data = $request->speech;
        DB::table('speechs')->where('id', 1)->update(['speech' => $data]);
    }
    
     //for  video
    public function videos()
    {
        $video = DB::table('videos')->first();
        
        return view('admin.video')->with(['video'=>$video]);
    }
    

    public function update_video(Request $request)
    {  
        $data = $request->url;
        DB::table('videos')->where('id', 1)->update(['url' => $data]);
         $request->session()->flash('success', 'Video Url  Updated successfully!');
        return redirect('admin/videos');
    }

   public function schedule()
    {
        $schedule = DB::table('schedule')->first();
        return view('admin.schedule')->with(['schedule'=>$schedule]);
    }
     public function update_schedule(Request $request)
    {  
        $saturday = $request->saturday;
        $sunday= $request->sunday;
        $monday = $request->monday;
        $tuesday = $request->tuesday;
        $wednesday = $request->wednesday;
        $thursday = $request->thursday;
        $friday = $request->friday;
        
        DB::table('schedule')->where('id', 1)->update(['saturday' => $saturday,'sunday' => $sunday,'monday' => $monday,'tuesday' => $tuesday,'wednesday' => $wednesday,'thursday' => $thursday,'friday' => $friday]);
        $request->session()->flash('success', 'Schedule  Updated successfully!');
        return redirect('admin/schedule');
    }
    
    
    public function admisssioninfo()
    { 
       
        $description = DB::table('admission_infos')->first();
        
        return view('admin.admissioninfo')->with(['description'=>$description]);
    }
    
    public function updateadmisssioninfo(Request $request)
    {
        $data = $request->description;
        DB::table('admission_infos')->where('id', 1)->update(['description' => $data]);
        
         $description = DB::table('admission_infos')->first();
           $request->session()->flash('success', 'Admission Information Updated successfully!');
       
         return redirect('admin/admissioninfo');
        
    }
    
    
    
    //elinks  code
    public function elinks()
    {
        return view('admin.elink');
    }
    
    public function create_elink(Request $request)
    {
        $title = $request->title;
        $url = $request->url;
        $elinks = new elink();
        $elinks->title = $title;
        $elinks->url = $url;
        $elinks->save();
         $request->session()->flash('success', 'New Elink Added successfully!');
        return redirect('admin/elinks');        
    }
    public function elinklist()
    {
        $elinks = elink::all();     
        return view('admin.elinkslist',['elinks'=> $elinks]);
    }
    
      //implinks  code
    public function implinks()
    {
        return view('admin.implink');
    }
    
    public function create_implink(Request $request)
    {    
        $title = $request->title;
        $url = $request->url;
        $implinks = new implink();
        $implinks->title = $title;
        $implinks->url = $url;
        $implinks->save();
           $request->session()->flash('success', 'New Link Added successfully!');
        return redirect('admin/implinks');
    }
    public function implinklist()
    {
        $implinks = implink::all();     
        return view('admin.implinkslist',['implinks'=> $implinks]);
    }
    
    
    //forms code
    
   public function forms()
    {
        return view('admin.form');
    }
    public function add_form(Request $request)
    {
        $title = $request->title;
        $image = $request->file('file');
		
		$name = time().rand(1, 100000).'.'.$image->getClientOriginalExtension();
            $image->move('formfiles',$name);
			
			
       // $name = time().'.'.$image->getClientOriginalExtension();
       // $destinationPath = public_path('/formfiles');
       // $image->move($destinationPath, $name);
        $forms =  new form(); 
        $forms->title = $title;
        $forms->file = $name;
        $forms->save();
        $request->session()->flash('success', ' New Form  added successfully!');
        return redirect('admin/forms');

    }
      public function formlist()
    {
        
        $forms = form::all();
        return view('admin.formslist',['forms'=> $forms]);
    }
   
    
 
 
 //Notices code
  
 
    public function routines()
    {
        return view('admin.routine');
    }
      public function create_routine(Request $request)
    {  
        $routines =  new routine(); 
        $class = $request->class;
        $image = $request->file('file');
        if($image!=''){
            //$name = time().'.'.$image->getClientOriginalExtension();
            //$destinationPath = public_path('/routinefiles');
            //$image->move($destinationPath, $name);
            $name = time().rand(1, 100000).'.'.$image->getClientOriginalExtension();
            $image->move('routinefiles',$name);
			
			$routines->file = $name;
        }
        $routines->class = $class;
        $routines->save();
        $request->session()->flash('success', 'New Routine  added successfully!');
        return redirect('admin/routines');
    } 
	
	public function add_result()
    {
        return view('admin.result');
    }
	  public function create_result(Request $request)
    {  
        $results =  new result(); 
        $class = $request->class;
        $image = $request->file('file');
        if($image!=''){
			$name = time().rand(1, 100000).'.'.$image->getClientOriginalExtension();
            $image->move('resultfiles',$name);
			
            //$name = time().'.'.$image->getClientOriginalExtension();
            //$destinationPath = public_path('/resultfiles');
            //$image->move($destinationPath, $name);
            $results->file = $name;
        }
        $results->class = $class;
        $results->save();
        $request->session()->flash('success', ' Results  added successfully!');
        return redirect('admin/results');
    } 
	 public function resultlist()
    {
        $results = result::all();
        return view('admin.resultlist',['results'=> $results]);
    }
   
   
	
    public function notices()
    {
        return view('admin.notice');
    }
    public function create_notice(Request $request)
    {  
        $notices =  new notice(); 
        $title = $request->title;
        $description = $request->description;
        if(strlen($description)<1){
            $description = '';
        }
        $image = $request->file('file');
        
        if($image!=''){
			$name = time().rand(1, 100000).'.'.$image->getClientOriginalExtension();
            $image->move('noticefiles',$name);
			
			
            //$name = time().'.'.$image->getClientOriginalExtension();
            //$destinationPath = public_path('/noticefiles');
            
			//$image->move($destinationPath, $name);
            $notices->file = $name;
        }
        
        $notices->title = $title;
        $notices->description = $description;
        $notices->save();
        $request->session()->flash('success', ' Notice  added successfully!');
        return redirect('admin/notices');
    }   
    
    
    public function noticelist()
    {
        $notices = notice::all(); 
        return view('admin.noticelist',['notices'=> $notices]);
    }
      public function edit_notice($id)
    {
        $notice_byid = DB::table('notices')->where('id', '=', $id)->get(); 
        return view('admin.edit-notice',['notice_byid'=> $notice_byid]);
        
    }
    
      public function edit_slider($id)
    {
        $sliderby_ID = DB::table('sliders')->where('id', '=', $id)->get(); 
        return view('admin.edit-slider',['sliderby_ID'=> $sliderby_ID]);
        
    }
     public function update_slider(Request $request)
    {  
        $slider =  slider::find($request->id); 
          $image = $request->file('file');
        if($image!=''){
            $slider->caption_title = $request->caption_title;
            $slider->caption_description = $request->caption_description;
			$name = time().rand(1, 100000).'.'.$image->getClientOriginalExtension();
            $image->move('sliderphoto',$name);
            $slider->image = $name;
            $slider->save();  
        }else{
            $slider->caption_title = $request->caption_title;
            $slider->caption_description = $request->caption_description;
          
            $slider->save();
        }

        $request->session()->flash('success', ' Slider has Updated successfully!');
        return redirect('admin/sliderlist');
    }      
    
    
    
    
     public function update_notice(Request $request)
    {  
        $notice =  notice::find($request->id); 
          $image = $request->file('file');
        if($image!=''){
            $notice->title = $request->title;
            $notice->description = $request->description;
            

			$name = time().rand(1, 100000).'.'.$image->getClientOriginalExtension();
            $image->move('noticefiles',$name);
            $notice->file = $name;
            $notice->save();  
        }else{
            $notice->title = $request->title;
            $notice->description = $request->description;
          
            $notice->save();
        }

        $request->session()->flash('success', ' Notice has Updated successfully!');
        return redirect('admin/notices');
    }      
    public function delete_notice($id)
    {
        $notice = notice::find($id);
        $notice->delete();
        //session()->flash('warning', 'Notice  has deleted successfully!');
        return redirect('admin/noticelist');
        
    }
    
     //gallery code
    
    public function gallerys()
    {
        return view('admin.gallery');
    }
    public function create_gallery(Request $request)
    { 
    $gallery_name  = $request->gallery_name;
    $images=array();
    
    if($files=$request->file('file')){
        foreach($files as $file){
            $name = time().rand(1, 100000).'.'.$file->getClientOriginalExtension();
            $file->move('galleryphoto',$name);
			
            $images[]=$name;
            $gallery =  new gallery(); 
            $gallery->gallery_name = $gallery_name;
            $gallery->image = $name;
            $gallery->save();
            return redirect('admin/gallerys');
        }
      }
    }
    
    
    public function gallerylist()
    {
          $gallerys = gallery::all();
          return view('admin.gallerylist',['gallerys'=> $gallerys]);
    }
    
   //slider code
    
   public function sliders()
    {
        return view('admin.slider');
    }
    public function create_slider(Request $request)
    {
        $caption_title = $request->caption_title;
        $caption_description = $request->caption_description;
        $image = $request->file('image');
		
		$name = time().rand(1, 100000).'.'.$image->getClientOriginalExtension();
            $image->move('sliderphoto',$name);
			
        //$name = time().'.'.$image->getClientOriginalExtension();
        //$destinationPath = public_path('/sliderphoto');
        //$image->move($destinationPath, $name);
        $sliders =  new slider(); 
        $sliders->caption_title = $caption_title;
        $sliders->caption_description = $caption_description;
        $sliders->image = $name;
        $sliders->save();
        $request->session()->flash('success', ' New Slider Added successfully!');
        return redirect('admin/sliders');

    }
    public function sliderlist()
    {
        $sliders = slider::all();     
        return view('admin.sliderlist',['sliders'=> $sliders]);
    }
    //contact  info
    
     
     public function contact_info()
    {  
        $contact = DB::table('contacts')->first();
        return view('admin.contact')->with(['contact'=>$contact]);
    }
    
      public function update_contact(Request $request)
    {
        $phone = $request->phone;
        $email = $request->email;
        $address = $request->address;
        $facebook = $request->facebook;
        $twitter = $request->twitter;
        $ytube = $request->ytube;
        DB::table('contacts')->where('id', 1)->update(['phone' => $phone,'email' => $email,'address' => $address,'facebook' => $facebook,'twitter' => $twitter,'ytube' => $ytube,]);
        $contact = DB::table('contacts')->first();
        $request->session()->flash('success', ' Contact  Information Updated successfully!');
        return redirect('admin/contact')->with(['contact'=>$contact]);
         
    }
    
  
     public function delete_gallery($id)
    {
        $gallery = gallery::find($id);
        $gallery->delete();
        return redirect('admin/gallerylist');
    }
      public function delete_slider($id)
    {
        $slider = slider::find($id);
        $slider->delete();
        return redirect('admin/sliderlist');
    }
      public function delete_form($id)
    {
        $form = form::find($id);
        $form->delete();
        return redirect('admin/formlist');
    }
      public function delete_result($id)
    {
        $result = result::find($id);
        $result->delete();
        return redirect('admin/resultlist');
    }
     public function delete_implink($id)
    {
        $implink = implink::find($id);
        $implink->delete();
        return redirect('admin/implinklist');
    }
     public function delete_elink($id)
    {
        $elink = elink::find($id);
        $elink->delete();
        return redirect('admin/elinklist');
    }
    
    
    
    
    
    
    
    
    
    
    
    
}