<?php

namespace App\Http\Controllers\Admin;

use Auth;
use App\User;
use App\Playlist;
use App\Songs;

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

class PlaylistController 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');
            
        }

        if(isset($_GET['s']))
        {
            $keyword = $_GET['s'];  
            $list = Playlist::where("playlist_name", "LIKE","%$keyword%")->orderBy('playlist_name')->paginate(12);

            $list->appends(\Request::only('s'))->links();
        }         
        else
        {

            $list = Playlist::orderBy('id','DESC')->paginate(12);
        }
        
        $page_title=trans('words.playlist_text');
         
        return view('admin.pages.playlist.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_playlist');

          $songs_list = Songs::orderBy('id')->get();
         
          return view('admin.pages.playlist.addedit',compact('page_title','songs_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_playlist');

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

          $songs_list = Songs::orderBy('id')->get();
        
          return view('admin.pages.playlist.addedit',compact('page_title','info','songs_list'));
        
    }

    public function addnew(Request $request)
    {  
       
       $data =  \Request::except(array('_token')) ;
        
        if(!empty($inputs['id'])){
                
                $rule=array(
                'playlist_name' => 'required'
                  );
        }
        else
        {
            $rule=array(
                'playlist_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 = Playlist::findOrFail($inputs['id']);

        }else{

            $data_obj = new Playlist;

        }
         
 
         $data_obj->playlist_name = addslashes($inputs['playlist_name']);
         $data_obj->playlist_image = $inputs['playlist_image'];        
         $data_obj->songs_ids = implode(',', $inputs['songs_ids']);
         $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 = Playlist::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');            
        
        }
    }
 
}
