<?php

namespace App\Http\Controllers\Admin;

use Auth;
use App\User;
use App\Category;
use App\Album;
use App\Artists;
use App\Songs;
use App\Playlist;
use App\Slider;
use App\HomeSections;
use App\SubscriptionPlan;
use App\AppAds;
use App\PaymentGateway;
use App\Suggestion;
use App\Reports;
use App\Favourite;
use App\PostRatings; 
use App\Pages;
use App\PostViewsDownload;

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

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

    public function ajax_status(Request $request)
    {  
       
       //$data =  \Request::except(array('_token'));

        $inputs = $request->all(); 
        //dd($inputs);exit;

        if(is_array($inputs['id']))
        {
            $post_ids=$inputs['id'];
        }
        else
        {
            $post_id=$inputs['id'];
            $value=$inputs['value'];
        }
  
        $action_for=$inputs['action_for'];
        
        if($action_for=="cat_status")
        {

            $data_obj = Category::findOrFail($post_id);        
     
            if($value=="true")
            {
                $data_obj->status = 1; 
            }
            else
            {
                $data_obj->status = 0; 
            }        
             
            $data_obj->save();             
            $response['status'] = 1;
            
        }
        else if($action_for=="album_status")
        {

            $data_obj = Album::findOrFail($post_id);        
     
            if($value=="true")
            {
                $data_obj->status = 1; 
            }
            else
            {
                $data_obj->status = 0; 
            }        
             
            $data_obj->save();             
            $response['status'] = 1;
            
        }
        else if($action_for=="playlist_status")
        {

            $data_obj = Playlist::findOrFail($post_id);        
     
            if($value=="true")
            {
                $data_obj->status = 1; 
            }
            else
            {
                $data_obj->status = 0; 
            }        
             
            $data_obj->save();             
            $response['status'] = 1;
            
        }
        else if($action_for=="songs_status")
        {

            $data_obj = Songs::findOrFail($post_id);        
     
            if($value=="true")
            {
                $data_obj->status = 1; 
            }
            else
            {
                $data_obj->status = 0; 
            }        
             
            $data_obj->save();             
            $response['status'] = 1;
            
        }
        else if($action_for=="song_enable_multiple")
        {

            foreach($post_ids as $pid)
            { 
                $data_obj = Songs::findOrFail($pid);        
     
                $data_obj->status = 1;   
                
                $data_obj->save();  

            }
             
            $response['status'] = 1;
            
        }
        else if($action_for=="song_disable_multiple")
        {

            foreach($post_ids as $pid)
            { 
                $data_obj = Songs::findOrFail($pid);        
     
                $data_obj->status = 0;   
                
                $data_obj->save();  

            }
             
            $response['status'] = 1;
            
        }
        else if($action_for=="slider_status")
        {

            $data_obj = Slider::findOrFail($post_id);        
     
            if($value=="true")
            {
                $data_obj->status = 1; 
            }
            else
            {
                $data_obj->status = 0; 
            }        
             
            $data_obj->save();             
            $response['status'] = 1;
            
        }
        else if($action_for=="home_sec_status")
        {

            $data_obj = HomeSections::findOrFail($post_id);        
     
            if($value=="true")
            {
                $data_obj->status = 1; 
 
            }
            else
            {
                $data_obj->status = 0; 
            }        
             
            $data_obj->save();             
            $response['status'] = 1;
            
        }        
        else if($action_for=="ads_status")
        {

            $data_obj = AppAds::findOrFail($post_id);        
     
            if($value=="true")
            {
                $data_obj->status = 1; 

                //Other Ads Disable
                AppAds::where('id','!=', $post_id)->update(['status' => 0]);
            }
            else
            {
                $data_obj->status = 0; 
            }        
             
            $data_obj->save();             
            $response['status'] = 1;
            
        }     
        else if($action_for=="payment_status")
        {

            $data_obj = PaymentGateway::findOrFail($post_id);        
     
            if($value=="true")
            {
                $data_obj->status = 1; 
 
            }
            else
            {
                $data_obj->status = 0; 
            }        
             
            $data_obj->save();             
            $response['status'] = 1;
            
        }
        else
        {
            $response['status'] = 0;
        }     

        echo json_encode($response);
        exit;   
    }

    public function ajax_delete(Request $request)
    {  
        
        $inputs = $request->all(); 
        //dd($inputs);exit;

        if(!isset($inputs['id']))
        {
            $response['status'] = 0;           
              
            echo json_encode($response);
            exit;

        }
        
        if(is_array($inputs['id']))
        {
            $post_ids=$inputs['id'];
        }
        else
        {
            $post_id=$inputs['id'];
        }

        //echo $post_id;exit;
         
        //$post_id=$inputs['id'];
        $action_for=$inputs['action_for'];
        
        if($action_for=="cat_delete")
        {
            $data_obj = Category::findOrFail($post_id);
            $data_obj->delete(); 
             
            $response['status'] = 1;            
        }
        else if($action_for=="artist_delete")
        {
            $data_obj = Artists::findOrFail($post_id);
            $data_obj->delete(); 
             
            $response['status'] = 1;            
        }
        else if($action_for=="album_delete")
        {
            $data_obj = Album::findOrFail($post_id);
            $data_obj->delete(); 
             
            $response['status'] = 1;            
        }
        else if($action_for=="playlist_delete")
        {
            $data_obj = Playlist::findOrFail($post_id);
            $data_obj->delete(); 
             
            $response['status'] = 1;            
        }
        else if($action_for=="song_delete")
        {
            $fav_obj = PostViewsDownload::where('post_id',$post_id)->delete();
            $rate_obj = Favourite::where('post_id',$post_id)->delete();
            $rep_obj = PostRatings::where('post_id',$post_id)->delete();
            $view_obj = Reports::where('post_id',$post_id)->delete(); 

            $data_obj = Songs::findOrFail($post_id);
            $data_obj->delete(); 
             
            $response['status'] = 1;            
        }
        else if($action_for=="song_delete_selected")
        {
             
            foreach($post_ids as $pid){

                $fav_obj = PostViewsDownload::where('post_id',$pid)->delete();
                $rate_obj = Favourite::where('post_id',$pid)->delete();
                $rep_obj = PostRatings::where('post_id',$pid)->delete();
                $view_obj = Reports::where('post_id',$pid)->delete(); 

                $data_obj = Songs::findOrFail($pid);
                $data_obj->delete();

            } 
             
            $response['status'] = 1;            
        }
        else if($action_for=="slider_delete")
        {
            $data_obj = Slider::findOrFail($post_id);
            $data_obj->delete(); 
             
            $response['status'] = 1;            
        }
        else if($action_for=="home_sec_delete")
        {
            $data_obj = HomeSections::findOrFail($post_id);
            $data_obj->delete(); 
             
            $response['status'] = 1;            
        }
        else if($action_for=="user_delete")
        {
            if($post_id==1)
            { 
                $response['status'] = 0;
            }
            else
            { 
                $fav_obj = Favourite::where('user_id',$post_id)->delete();
                $rate_obj = PostRatings::where('user_id',$post_id)->delete();
                $sugg_obj = Suggestion::where('user_id',$post_id)->delete();
                $rep_obj = Reports::where('user_id',$post_id)->delete();
                 
                $data_obj = User::findOrFail($post_id);
                $data_obj->delete(); 
             
                $response['status'] = 1;     
            }
                   
        }         
        else if($action_for=="plan_delete")
        {
            $data_obj = SubscriptionPlan::findOrFail($post_id);
            $data_obj->delete(); 
             
            $response['status'] = 1;            
        }
        else if($action_for=="suggestion_delete")
        {
            $data_obj = Suggestion::findOrFail($post_id);
            $data_obj->delete(); 
             
            $response['status'] = 1;            
        }
        else if($action_for=="reports_delete")
        {
            $data_obj = Reports::findOrFail($post_id);
            $data_obj->delete(); 
             
            $response['status'] = 1;            
        }        
        else if($action_for=="page_delete")
        {
            $data_obj = Pages::findOrFail($post_id);
            $data_obj->delete(); 
             
            $response['status'] = 1;            
        }        
        else
        {
            $response['status'] = 0;            
        }     

        echo json_encode($response);
        exit;    
             
    }
     
}
