<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ReportingManagerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $managers=DB::table('managers')->get();
        $businessunits=DB::table('branches')->get();
        $departments = DB::table('departments')->get();
        $jobtitles = DB::table('job_titles')->get();
        return view('Configurations.ReportingmangerConfig.index',compact(['managers','businessunits','departments','jobtitles']));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'department'=>'required',
            'business_unit'=>'required',
            'name'=>'required',
            
        ]);
        DB::table('managers')
        ->insertGetId([
            'department' => $request['department'],
            'business_unit' => $request['business_unit'],
            'job_title_code'=> $request['jobtitle_code'],
            'name' => $request['name'],
            'created_at' => DB::raw('NOW()'),
        ]);

    return redirect('/managers')->with('Success','Reporting Manager added successfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $manager = DB::table('managers')->where('id', $id)->first();
        $jobtitles = DB::table('job_titles')->get();
        $branches=DB::table('branches')->get();
        $departments=DB::table('departments')->get();
        $selectedjobtitle= DB::table('job_titles')->select('job_title_code')->where('id',$id)->get();
        $selectedbranch= DB::table('branches')->select('business_unit')->where('id',$id)->get();
        $selecteddepartment= DB::table('departments')->select('department_name')->where('id',$id)->get();
        return view('Configurations.ReportingmangerConfig.edit',compact(['manager','jobtitles','branches','departments','selectedjobtitle','selectedbranch','selecteddepartment']));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'department'=>'required',
            'business_unit'=>'required',
            'name'=>'required',
            
        ]);       

        DB::table('managers')
            ->where('id', $id)
            ->update([
                'department' => $request['department'],
                'business_unit' => $request['business_unit'],
                'job_title_code'=> $request['jobtitle_code'],
                'name' => $request['name'],
                ]);
        return redirect('/managers')->with('success', 'Reporting Manager has been updated Successfully');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if(DB::table('managers')->where('id',$id)->exists()){
            DB::table('managers')->where('id', $id)->delete();
            return redirect('/managers')->with('success', 'Reporting Manager has been deleted Successfully');
        }else{
            return redirect('/managers')->with('error', 'Reporting Manager does not exist');
 
        }
       
    }
}
