<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use App\Models\Notifications;
use Maatwebsite\Excel\Facades\Excel;
use Auth, DB, Mail, Validator, File, DataTables, Exception;

class NotificationController extends Controller{
    /** construct */
        public function __construct(){
            $this->middleware('permission:notifications-create', ['only' => ['create']]);
            /*$this->middleware('permission:notifications-edit', ['only' => ['edit']]);
            $this->middleware('permission:notifications-view', ['only' => ['view']]);
            $this->middleware('permission:notifications-delete', ['only' => ['delete']]);*/
        }
    /** construct */

    /** index */
        public function index(Request $request){
            if($request->ajax()){
                $a_id = auth()->id();
                $data = Notifications::where(['created_by' => $a_id])->get();
                
                
                return Datatables::of($data)
                        ->addIndexColumn()
                        ->addColumn('action', function($data){
                            $return = '<div class="btn-group">';

                            // if(auth()->user()->can('notifications-view')){
                                /*$return .= '<a href="'.route('notifications.view', ['id' => base64_encode($data->id)]).'" class="btn btn-default btn-xs">
                                                <i class="fa fa-eye"></i>
                                            </a> &nbsp;';*/
                            // }   
                            
                            // if(auth()->user()->can('notifications-edit')){
                               /* $return .= '<a href="'.route('notifications.edit', ['id' => base64_encode($data->id)]).'" class="btn btn-default btn-xs">
                                                <i class="fa fa-pencil"></i>
                                            </a> &nbsp;';*/
                            // }   

                            // if (auth()->user()->can('notifications-delete')) {
                                $return .= '<a href="javascript:;" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">
                                                    <i class="fa fa-bars"></i>
                                                </a> &nbsp;
                                                <ul class="dropdown-menu">
                                                    <li><a class="dropdown-item" href="javascript:;" onclick="change_status(this);" data-status="active" data-id="' . base64_encode($data->id) . '">Active</a></li>
                                                    <li><a class="dropdown-item" href="javascript:;" onclick="change_status(this);" data-status="inactive" data-id="' . base64_encode($data->id) . '">Inactive</a></li>
                                                    <li><a class="dropdown-item" href="javascript:;" onclick="change_status(this);" data-status="deleted" data-id="' . base64_encode($data->id) . '">Deleted</a></li>
                                                </ul>';
                            // }

                            $return .= '</div>';

                            return $return;
                        })

                        ->editColumn('notification_link', function ($data) {
                            //return '<a href="'.$data->notification_link.'">'.$data->notification_link.'</a>';

                            return '<a href="'.$data->notification_link.'" class="btn btn-default btn-xs">
                                                <i class="fa fa-eye"></i>
                                            </a> &nbsp;';
                        })
                        ->editColumn('created_at', function ($data) {

                            return ''.$data->created_at->format("d-m-Y").'';
                        })
                        ->editColumn('status', function ($data) {
                            if ($data->status == 'active') {
                                return '<span class="badge badge-pill badge-success">Active</span>';
                            } else if ($data->status == 'inactive') {
                                return '<span class="badge badge-pill badge-warning">Inactive</span>';
                            } else if ($data->status == 'deleted') {
                                return '<span class="badge badge-pill badge-danger">Deleted</span>';
                            }
                        })


                        ->rawColumns(['notification_link' ,'percentage' ,'status'])
                        ->make(true);
            }
            return view('notifications.index');
        }
    /** index */

    
    /** create */
       /* public function create(Request $request){
            
            return view('notifications.create');
        }*/
    /** create */

    /** insert */
       /* public function insert(Request $request){
            if($request->ajax()) { return true; }

            if(!empty($request->name)){
                $crud['name'] = $request->name;
            }else{
                $crud['name'] = '';
            }

            $crud = [
                'name' => $request->name,
                'status' => 'active',
                'created_at' => date('Y-m-d H:i:s'),
                'created_by' => auth()->user()->id,
                
            ];

            $last_id = Notifications::insertGetId($crud);

            if ($last_id)
            return redirect()->route('notifications')->with('success', 'Record inserted successfully');
            else
                return redirect()->back()->with('error', 'Failed to insert record')->withInput();
            
            }*/
    /** insert */

    /** view */
        /*public function view(Request $request){
            $id = base64_decode($request->id);
            $data = Carvarient::where(['id' => $id])->first();
            
            // return view('notifications.view')->with(['data'=>$data]);

            $Carmodel = Carmodel::where(['status' => 'active'])->get();
            $Colortone = Colortone::where(['status' => 'active'])->get();
            $Fuletype = Fuletype::where(['status' => 'active'])->get();
            $Transmission = Transmission::where(['status' => 'active'])->get();
            $Interiorcolor = Interiorcolor::where(['status' => 'active'])->get();
            $Exteriorcolor = Exteriorcolor::where(['status' => 'active'])->get();
            return view('notifications.view')->with(['data'=>$data,'Carmodel' => $Carmodel ,'Colortone' => $Colortone,'Fuletype'=>$Fuletype,'Transmission' => $Transmission,'Interiorcolor' => $Interiorcolor,'Exteriorcolor' => $Exteriorcolor]);
        }*/
    /** view */
    
    /** edit */
       /* public function edit(Request $request){
            $id = base64_decode($request->id);
            $data = Notifications::where(['id' => $id])->first();
           
            return view('notifications.edit')->with(['data'=>$data]);
        }*/
    /** edit */

    /** update */
       /* public function update(Request $request){
            if($request->ajax()){ return true; }
            $id = $request->id;
            
            if(!empty($request->name)){
                $crud['name'] = $request->name;
            }else{
                $crud['name'] = '';
            }

            $crud = [
                'name' => $request->name,
                'updated_at' => date('Y-m-d H:i:s'),
                'updated_by' => auth()->user()->id
            ];

            DB::enableQueryLog();
            $update = Notifications::where(['id' => $id])->update($crud);
            

            if ($update)
            return redirect()->route('notifications')->with('success', 'Record updated successfully');
            else
                return redirect()->back()->with('error', 'Failed to updated record')->withInput();
            
            }*/
        
    /** update */

    /** change-status */
        public function change_status(Request $request){
            if (!$request->ajax()) { exit('No direct script access allowed'); }
            $id = base64_decode($request->id);
            
            $data = Notifications::where(['id' => $id])->first();

            if (!empty($data)) {
                $update = Notifications::where(['id' => $id])->update(['status' => $request->status, 'updated_at' => date('Y-m-d H:i:s'), 'updated_by' => auth('sanctum')->user()->id]);
                if ($update) {
                    return response()->json(['code' => 200]);
                } else {
                    return response()->json(['code' => 201]);
                }
            } else {
                return response()->json(['code' => 201]);
            }
           
        }
    /** change-status */
        public function top_notify(){
             $a_id = auth()->id();

             $data = DB::table('notifications')->where(['created_by' => $a_id])->orderBy('id','desc')->limit(5)->get();

             $notifications_data['notification_view'] = 1; 
             DB::table('notifications')->where(['created_by' => $a_id])->update($notifications_data);

             $notification_count = DB::table('notifications')->select('id')->where(['notification_view' => 0,'created_by' => $a_id])->count();

              return response()->json(['data' => $data,'notification_count' => $notification_count]);
        }

        public function notification_count_get(){
            $a_id = auth()->id();

            $notification_count = DB::table('notifications')->select('id')->where(['notification_view' => 0,'created_by' => $a_id])->count();

             return response()->json(['notification_count' => $notification_count]);
        }

}
