<?php

namespace App\Http\Controllers\dashboard;

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

class DashboardController extends Controller
{
	public function __construct()
    {
       $this->middleware('auth'); 
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
		$totalfinancebills=DB::table('financebills')
						->count();
		$financebillsspassed=DB::table('financebilldetails')
						->where('Status',1)
						->count();
		$financebillsrejected=DB::table('financebilldetails')
						->where('Status',2)
						->count();
		$financebillsspending=DB::table('financebilldetails')
						->where('Status',0)
						->count();
		$totalproposals=DB::table('proposals')
						->count();
		$proposalspassed=DB::table('proposals')
						->where('Status',1)
						->count();
		$proposalsrejected=DB::table('proposals')
						->where('Status',2)
						->count();
		$proposalspending=DB::table('proposals')
						->where('Status',0)
						->count();
		$suggestedammendments=DB::table('suggestedamendments')
								->count();
		$totalusers=DB::table('users')->count();
		$this->data['totalfinancebills']=$totalfinancebills;
		$this->data['financebillsspassed']=$financebillsspassed;
		$this->data['financebillsrejected']=$financebillsrejected;
		$this->data['financebillsspending']=$financebillsspending;
		$this->data['totalproposals']=$totalproposals;
		$this->data['totalusers']=$totalusers;
		return view('dashboard.index',$this->data);
    }
	
	public function changepassword()
    {
        $user=DB::table('users')
					->where('id',auth()->user()->id)
					->first();
		$this->data['user']=$user;
		return view('users.user.changepassword',$this->data);
    }
	
	public function updatepassword(Request $request)
    {
        $request->validate([
            'current_password' => 'required',
            'new_password' => 'required_with:new_confirm_password|min:8|different:current_password',
            'new_confirm_password' => 'same:new_password',
        ]);
		
		$user = User::findOrFail(auth()->user()->id);

		if (Hash::check($request->current_password, $user->password)) { 
			$user->fill([
				'password' => Hash::make($request->new_password)
				])->save();
				
			return back()->with('success','Password was changed successfully');
		}
		else{
			return back()->with('error','Current password does not match with your password');
		}

    }

    /**
     * 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');
    }
	
}
