<?php
namespace App\Http\Controllers\Admin;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Config;
use App\Http\Controllers\Controller;

class AdminsController extends Controller
{
    public function index(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
         if($request->ajax()){
             return datatables()->of(User::select('id', 'name', 'email', 'status', 'phone_number', 'created_at')->where('user_type','1'))
            ->addColumn('actions', function($row){
                return '<span class="action-links"><a class="uk-text-primary uk-edit" href="'.url('admin/admins/'.$row->id.'/edit').'" uk-tooltip="Edit Admin"><i class="fa-solid fa-pen-to-square"></i></a>  <a data-id="'.$row->id.'" class="uk-text-danger delete-admin" href="javascript:void(0)" uk-tooltip="Delete Admin"><i class="fa-solid fa-trash"></i></a></span>';
            })
            ->addColumn('status', function($row){
                if($row->status == '1'){ 
                    $status = '<a href="javascript:void(0)" data-id="'.$row->id.'" class="uk-label uk-label-success active">Active</a>';
                }else{
                    $status = '<a href="javascript:void(0)" data-id="'.$row->id.'" class="uk-label uk-label-danger inactive">Inactive</a>';
                }
                return $status;
            })
            ->rawColumns(['actions','status'])->toJson();
        }
        else{
            $this->bladeContent['title']  = 'Admins'; 
            $this->bladeContent['active'] = 'admins';
            return view('admin.admins.index',$this->bladeContent);
        }
    }
    
    public function create(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $this->bladeContent['title']  = 'Add Admin';
        $this->bladeContent['active'] = 'admins';
        return view ('admin.admins.create', $this->bladeContent);
    }
    
    public function store(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        
        $admin               = new User();
        $admin->name         = request('name');
        $admin->email        = request('email');
        $admin->phone_number = request('phone_number');
        $admin->password     = (request('password') ? Hash::make(request('password')) : '');
        $admin->user_type    = 1;
        $admin->save();
        return redirect('admin/admins')->with('success','Admin successfully created.');
    }

    public function edit($id)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $admin = User::select('id', 'name', 'email', 'phone_number')->where('id', $id)->first();
        $this->bladeContent['title']  = 'Edit Admin';
        $this->bladeContent['active'] = 'admins';
        $this->bladeContent['data']   =  $admin;
        return view('admin.admins.edit', $this->bladeContent);
    }

    public function update(Request $request,$id)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        
        $admin               = User::select('id', 'name', 'email', 'phone_number')->where('id', $id)->first();
        $admin->name         = request('name');
        $admin->email        = request('email');
        $admin->phone_number = request('phone_number');
        if($request->get('password')){
            $admin->password   = Hash::make(request('password'));  
        }
        $admin->save();
        return redirect('admin/admins')->with('success','Admin successfully updated.');
    }

    public function destroy($id)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        User::select('id')->where('id',$id)->delete();
        return response()->json([
            'success' => 1,
            'message' => 'Data successfully deleted.'
        ]);
    } 
    
    public function status(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $id = request('id');
        if($request->type == 'active'){
            $value = '1';
        }else{
            $value = '0';
        }
       User::select('id')->where('id', $id)->update(array('status' => $value));
        return response()->json([
        'success' => 1,
        'message' => 'status changed.'
      ]);
    } 
}