<?php

namespace App\Http\Controllers\FCControllers;

use Illuminate\Http\Request;
use App\Models\fcmodels\Organization;
use App\Models\fcmodels\Organizationshortcode;

use Auth;
use App\Models\fcmodels\AuditLog;
use App\Models\fcmodels\Wallet;
class OrganizationshortcodeContloller extends Controller
{
   
 
    public function index()
    {
      
           exit();
            $Organizationshortcodes = Organizationshortcode::All();
            return view('organizationshortcode.index',compact('Organizationshortcodes'));
    }


    //get groups
    public function getOrganization()
    {
        if(Auth::guard('organization')->check())
        {
            $id = Auth::guard('organization')->user()->organization_id;
        }
        else if(Auth::guard('web')->check())
        {
            $id = Auth::guard('web')->user()->organization_id;
        }
            $groups = Organization::All();
            return $groups;
    }

    //store
    public function storeOrganizationshortcode(Request $request)
    {
        
         if(Auth::guard('organization')->check())
        {
            $id = Auth::guard('organization')->user()->organization_id;
        }
        else if(Auth::guard('web')->check())
        {
            $id = Auth::guard('web')->user()->organization_id;
        //return $id;
        
        }  
        $request->validate([
        
            'group_name' => 'required',
    
                    
                ]);
            

        
        $data = new Organizationshortcode();
        $data->shortcode = $request->name;
        $data->description = $request->description;
        $data->status = 1;
        $data->organization_id = $id;


         //return $data;
        $data->save ();
        //log
        $adminLog = new AuditLog();
        $adminLog->audit_module = "Organization";
        $adminLog->audit_activity = "Organization user ".$request->username ." added by Admin ID# " .$id;
       
        $adminLog->user_category = "Admin";
        $adminLog->user_id = $id;
        ;
        $adminLog->save();
        
        return response()->json($data);
    

}



/**
     * updateGroup the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function updateGroup(Request $request)
    {

      //  try
        { 
            request()->validate([
                'group_name'=> 'required',
                'description' => 'required',
           
            ]);
          //  return $id;
        //current customer details
        $id=$request->group_id;
        $customerdets = Group::where('group_id',$id)->get();
        //updateGroup
        $data = Group::findOrFail($id);
       
        $data->group_name = $request->get('group_name');
        $data->description = $request->get('description');
        $data->status = 1;
        // log data
        $customerLog = new AuditLog();
        $customerLog->audit_module = "Customer";
        $customerLog->audit_activity = "Customer details ";
        
        $customerLog->user_category = "Admin";
        $customerLog->user_id = 1;
        
        $customerLog->save();
        $data->save();

          return redirect()->back()
            ->with('success','Group Edited Succesfully.'); 
            
        }
      //  catch (Exception $exception)
       // {
          //  return back()->withInput()
            //    ->with(['failure' => 'Unexpected error occurred while trying to process your request. Please try Again.']);
      //  }
    }


 //delete
 public function deleteGroup(Request $request,$id)
  {

    //find user details to delete
    $admin = Group::where('group_id',$id)->get();
    //set delete
    $deleteDepartment = Group::find($id);
    $deleteDepartment->delete();
    //set logging
   
    $adminLog = new AuditLog();
    $adminLog->audit_module = "Organization";
    $adminLog->audit_activity = "Organization of details: ".$admin." deleted by Admin ID#" .$id;
    
    $adminLog->user_category = "organization";
    $adminLog->user_id = $id; 
    $adminLog->save();
}
}
