<?php

namespace App\Http\Controllers\Admin;

use Auth;
use App\User;
use App\Album;
use App\Artists;
use App\Category;
use App\Songs;
use App\Favourite;
use App\PostRatings;
use App\PostViewsDownload;
use App\Reports; 

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

class SongsController 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 = Songs::where("song_title", "LIKE","%$keyword%")->orderBy('song_title')->paginate(12);

            $list->appends(\Request::only('s'))->links();
        }
        else if(isset($_GET['cat_id']) OR isset($_GET['artist_id']) OR isset($_GET['album_id']))
        {
           
            $cat_id = isset($_GET['cat_id'])?$_GET['cat_id']:0;  
            $artist_id = isset($_GET['artist_id'])?$_GET['artist_id']:0;  
            $album_id = isset($_GET['album_id'])?$_GET['album_id']:0;  
            
            $list = Songs::when($cat_id, function ($q) use ($cat_id) {
                return $q->where('cat_id',$cat_id);
            })             
            ->when($artist_id, function ($q) use ($artist_id) {
                return $q->whereRaw("find_in_set('$artist_id',artist_ids)");
            })
            ->when($album_id, function ($q) use ($album_id) {
                return $q->where('album_id',$album_id);
            })             
            ->orderBy('id','DESC')->paginate(12);
              
            $list->appends(request()->input())->links();
        }
        else
        {    

            $list = Songs::orderBy('id','DESC')->paginate(12);
        }

        $page_title=trans('words.songs_text');
         
        return view('admin.pages.songs.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_song');

          $cat_list = Category::orderBy('id')->get();
          $artist_list = Artists::orderBy('id')->get();
          $album_list = Album::orderBy('id')->get();

         
          return view('admin.pages.songs.addedit',compact('page_title','cat_list','artist_list','album_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_song');

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

          $cat_list = Category::orderBy('id')->get();
          $artist_list = Artists::orderBy('id')->get();
          $album_list = Album::orderBy('id')->get();
        
          return view('admin.pages.songs.addedit',compact('page_title','info','cat_list','artist_list','album_list'));
        
    }

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

        }else{

            $data_obj = new Songs;

        }
         
         $artist_ids= isset($inputs['artist_ids'])?implode(',', $inputs['artist_ids']):'';
 
         $data_obj->song_title = addslashes($inputs['song_title']);
         $data_obj->cat_id = $inputs['category'];
         $data_obj->artist_ids = $artist_ids;
         $data_obj->album_id = $inputs['album_id'];

         $data_obj->song_type = $inputs['song_type'];
         
         if($inputs['song_type']=="server_url")
         {
            $data_obj->song_url = $inputs['song_url_server'];
         }
         else
         {
            $data_obj->song_url = $inputs['song_url_local'];
         }

         $data_obj->song_image = $inputs['song_image'];

         $data_obj->song_info = addslashes($inputs['song_info']); 
         $data_obj->song_lyrics = addslashes($inputs['song_lyrics']); 
         $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")
        {
             
             $view_obj = PostViewsDownload::where('post_id',$post_id)->delete();
             $fav_obj = Favourite::where('post_id',$post_id)->delete();
             $rate_obj = PostRatings::where('post_id',$post_id)->delete();
             $rep_obj = Reports::where('post_id',$post_id)->delete();

             $data_obj = Songs::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');            
        
        }
    }
 
}
