<?php

namespace App\Http\Controllers\Admin;

use Auth;
use App\User;
use App\HomeSections;
use App\Category;
use App\Album;
use App\Artists;
use App\Songs;
use App\Playlist;

use App\Http\Requests;
use Illuminate\Http\Request;
use Session;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Str; 

class HomeSectionsController extends MainAdminController
{
	public function __construct()
    {
		 $this->middleware('auth');
          
    }

    public function list()
    { 
 
        if(Auth::User()->usertype!="Admin" AND Auth::User()->usertype!="Sub_Admin")
        {

            \Session::flash('flash_message', trans('words.access_denied'));

            return redirect('dashboard');
            
        }

        $list = HomeSections::orderBy('id','DESC')->paginate(10);

        $page_title=trans('words.home_sections');
         
        return view('admin.pages.home_sections.list',compact('page_title','list'));
    }

    public function add()    
    {     
          if(Auth::User()->usertype!="Admin" AND Auth::User()->usertype!="Sub_Admin")
            {

                \Session::flash('flash_message', trans('words.access_denied'));

                return redirect('dashboard');
                
            }  

          $page_title=trans('words.add_section');

          $category_list = Category::orderBy('id')->get();
          $artist_list = Artists::orderBy('id')->get();
          $album_list = Album::orderBy('id')->get();
          $songs_list = Songs::orderBy('id')->get();
          $playlist_list = Playlist::orderBy('id')->get();
         
          return view('admin.pages.home_sections.addedit',compact('page_title','category_list','artist_list','album_list','songs_list','playlist_list'));
        
    }

    public function edit($page_id)    
    {     
           if(Auth::User()->usertype!="Admin" AND Auth::User()->usertype!="Sub_Admin")
            {

                \Session::flash('flash_message', trans('words.access_denied'));

                return redirect('dashboard');
                
            }  

          $page_title=trans('words.edit_Section');

          $info = HomeSections::findOrFail($page_id);

          $category_list = Category::orderBy('id')->get();
          $artist_list = Artists::orderBy('id')->get();
          $album_list = Album::orderBy('id')->get();
          $songs_list = Songs::orderBy('id')->get();
          $playlist_list = Playlist::orderBy('id')->get();
        
          return view('admin.pages.home_sections.addedit',compact('page_title','info','category_list','artist_list','album_list','songs_list','playlist_list'));
        
    }

    public function addnew(Request $request)
    {  
       
       $data =  \Request::except(array('_token')) ;
        
        if(!empty($inputs['id'])){
                
                $rule=array(
                'section_name' => 'required'
                  );
        }
        else
        {
            $rule=array(
                'section_name' => 'required',                            
                 );
        }
        
        
        $validator = \Validator::make($data,$rule);
 
        if ($validator->fails())
        {
                return redirect()->back()->withErrors($validator->messages());
        } 
        $inputs = $request->all();
        
        if(!empty($inputs['id'])){
           
            $data_obj = HomeSections::findOrFail($inputs['id']);

        }else{

            $data_obj = new HomeSections;

        }
         
 
         $data_obj->section_name = addslashes($inputs['section_name']);
         $data_obj->section_type = $inputs['section_type'];        
         
         if($inputs['section_type']=="category")
         {
            $data_obj->post_ids = implode(',', $inputs['category_ids']);
         }
         else if($inputs['section_type']=="artist")
         {
            $data_obj->post_ids = implode(',', $inputs['artist_ids']);
         }
         else if($inputs['section_type']=="album")
         {
            $data_obj->post_ids = implode(',', $inputs['album_ids']);
         }
         else if($inputs['section_type']=="song")
         {
            $data_obj->post_ids = implode(',', $inputs['song_ids']);
         }
         else if($inputs['section_type']=="playlist")
         {
            $data_obj->post_ids = implode(',', $inputs['playlist_ids']);
         }
         else
         {
            $data_obj->post_ids = null;
         }
         
         $data_obj->status = $inputs['status']; 
         
         $data_obj->save();
         
        
        if(!empty($inputs['id'])){

            \Session::flash('flash_message', trans('words.successfully_updated'));

            return \Redirect::back();
        }else{

            \Session::flash('flash_message', trans('words.added'));

            return \Redirect::back();

        }               
        
        
    }

    public function delete($post_id)
    {
        if(Auth::User()->usertype=="Admin" OR Auth::User()->usertype=="Sub_Admin")
        {
            
             $data_obj = HomeSections::findOrFail($post_id);
             $data_obj->delete();

             \Session::flash('flash_message', trans('words.deleted'));
             return redirect()->back();
             
            
        }
        else
        {
            \Session::flash('flash_message', trans('words.access_denied'));
            return redirect('admin/dashboard');            
        
        }
    }
 
}
