<?php

namespace App\Http\Controllers\activitylogs;

use App\Http\Controllers\Controller;
use App\Models\User;
use Spatie\Permission\Models\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ActivityLogsController extends Controller
{
	public function __construct()
    {
		$this->middleware('auth');
        $this->middleware('permission:view-subcounties', ['only' => ['index']]);
        $this->middleware('permission:create-subcounties', ['only' => ['create','store']]);
        $this->middleware('permission:update-subcounties', ['only' => ['update','edit']]);
        $this->middleware('permission:delete-subcounties', ['only' => ['destroy']]);
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
		return view('activitylogs.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => [
                'required',
                'string',
                //'unique:organizations,name'
            ]
        ]);
		
		//check if organization name exists
		$exists=Organization::where('name',$request->name)->first();
		if($exists){
			return back()->withErrors(['name'=>'Organization name already exists!'],'addOrganization')
						->withInput();//->with('error','Organization name already exists')
		}

        Organization::create([
            'name' => $request->name,
			'user_id' => auth()->user()->id
        ]);

        return back()->with('success','Organization Created Successfully');
    }

    /**
     * Display the specified resource.
     */
    public function show(organization $organization)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(organization $organization)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Organization $organization)
    {
        $request->validate([
            'edit_name' => [
                'required',
                'string',
                'unique:organizations,name,'. $organization->id
            ]
        ]);

        $organization->update([
            'name' => $request->edit_name,
			'admin' => $request->admin
        ]);

        return back()->with('success','Organization Updated Successfully');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(organization $organization)
    {
        //
    }
	
	public function activate($organizationId)
    {
        $organization = Organization::findOrFail($organizationId);
		$organization->status = 1;
		$organization->save();
		
		$enableusers=User::where('org_id',$organizationId)
						->update(['status'=>1]);

        return back()->with('success','Organization Activated Successfully');
    }
	
	public function deactivate($organizationId)
    {
        $organization = Organization::findOrFail($organizationId);
		$organization->status = 0;
		$organization->save();
		
		$disableusers=User::where('org_id',$organizationId)
						->update(['status'=>0]);

        return back()->with('success','Organization Deactivated Successfully');
    }
	
	public function updateorganization(Request $request)
    {
        $request->validate([
            'edit_name' => [
             'required',
                'string',
				//'unique:organizations,name,'. $request->organizationid
            ]
        ]);
		
		//check if organization name exists
		$exists=Organization::where('name',$request->edit_name)->first();
		if($exists){
			return back()->withErrors(['edit_name'=>'Organization name already exists!'],'editOrganization')
						->withInput();
		}
		
		$organization=Organization::find($request->organizationid);

        $organization->update([
            'name' => $request->edit_name,
			'admin' => $request->admin
        ]);

        return back()->with('success','Organization Updated Successfully');
    }
	
}
