<?php

namespace App\Http\Controllers;

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

class ReliefController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $relief = DB::table('relief_config')->get();
        $list_items = DB::table('list_items')->get();

        return view('Relief.relief', compact(['relief', 'list_items']));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'relief-name' => 'required|min:3|max:30'
        ]);

        DB::table('relief_config')
            ->insertGetId([
                'relief_name' => $request['relief-name'],
                'created_by' => 1,
                'created_at' => DB::raw('NOW()')
            ]);

        return redirect('/relief');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $relief = DB::table('relief_config')
            ->where('id', $id)
            ->first();
        $id = $relief->id;

        $groups_in_relief = DB::table('payroll_item_groups')
            ->where([
                ['payroll_item_id', $id],
                ['payroll_item', 'relief']
                ])
            ->get();

        #Get the details of the employees in the group(s)
        $employee_groups= [];
        foreach($groups_in_relief as $group){

            $users = $group->employees;
            $users = explode(',', $users);

            $count = 0;
            foreach($users as $user){
                $employee_details = DB::table('main_users')
                    ->where('id', $user)
                    ->first();

                $employee_groups[$group->id][$count] = strtoupper($employee_details->last_name)." ".$employee_details->first_name;
                $count++;
            }
        }

        return view('Relief.show', compact(['relief', 'groups_in_relief', 'employee_groups']));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $relief = DB::table('relief_config')->where('id', $id)->first();
        $list_items = DB::table('list_items')->get();

        return view('Relief.edit', compact(['relief', 'list_items']));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'relief-name' => 'required|min:3|max:30'
        ]);

        DB::table('relief_config')
            ->where('id', $id)
            ->update([
                'relief_name' => $request['relief-name'],
                'modified_by' => 1,
                'updated_at' => DB::raw('NOW()')
            ]);

        return redirect('/relief')->with('messages', ['success', 'Relief has been updated.']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        if ($request->ajax()){
            DB::table('relief_config')->where('id', $request->reliefId)->delete();

            return response()->json(array('status' => 1));
        }else{
            return response()->json(array('status' => 0));
        }
    }

    /**
     * Add groups to a relief
     */

    public function add_employees($id){
        $departments = DB::table('departments')->get();
        return view('Relief.employees', compact(['departments']));
    }

    /**
     * @return \Illuminate\Http\Response
     * Edit a group of employees in an allowance
     */

    public function edit_group($id){
        $departments = DB::table('departments')->get();

        #Get the group details
        $group = DB::table('payroll_item_groups')
            ->where('id', $id)
            ->first();

        #Get the employees in the group
        $employees = $group->employees;
        $employees = explode(',', $employees);

        for ($i=0;$i<count($employees);$i++){
            $employee_details = DB::table('main_users')
                ->where('id', $employees[$i])
                ->first();

            $employee_names[] = ['id' => $employee_details->id,
                'name' => strtoupper($employee_details->last_name)." ".$employee_details->first_name];
        }

        return view('Relief.edit_group', compact(['departments', 'group', 'employee_names']));
    }
}
