<?php

namespace App\Http\Controllers\Admin;

use App\Schedule;
use App\Shift;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Models\AdditionalShift;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class AdditionalShiftController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::all();

        return view('admin.additionalShift.index')->with([
            'users'=> $users
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function view(Request $request)
    {
        // dd($request->user_id);
        $users = User::all();
        $selectedUser = User::find($request->user_id);
        $additionalShifts = $selectedUser->additionalShift;
        $futureAdditionalShift = array();

        foreach ($additionalShifts as $additionalShift){
            if($additionalShift->date >= Carbon::now()){
                array_push($futureAdditionalShift, $additionalShift);
            }
        }

        return view('admin.additionalShift.view')->with([
            'users'=> $users,
            'selectedUser'=> $selectedUser,
            'futureAdditionalShift' => $futureAdditionalShift
        ]);
    }

    public function refresh($id) {

        $users = User::all();
        $selectedUser = User::find($id);
        $additionalShifts = $selectedUser->additionalShift;
        $futureAdditionalShift = array();

        foreach ($additionalShifts as $additionalShift){
            if($additionalShift->date >= Carbon::now()){
                array_push($futureAdditionalShift, $additionalShift);
            }
        }

        return view('admin.additionalShift.view')->with([
            'users'=> $users,
            'selectedUser'=> $selectedUser,
            'futureAdditionalShift' => $futureAdditionalShift
        ]);
    }

    /**
     * Get the list of Shift in a given date
    **/
    public function shiftList(Request $request)
    {
        $dateSelected = $request->shiftDate;
        $userSelected = $request->userSelected;
        $shiftArray = array();

        $user = User::find($userSelected);
        $schedule = Schedule::where([['schedules.start_date', '<=', $dateSelected], ['schedules.end_date', '>=', $dateSelected]])->firstOrFail();
        $shiftSchedules = Shift::where('shifts.schedule_id', '=', $schedule->id)->get();
        foreach ($user->specialties as $specialty){
            foreach ($shiftSchedules as $shiftSchedule){
                if($specialty->id == $shiftSchedule->specialty_id){
                    array_push($shiftArray, $shiftSchedule);
                }
            }
        }

        return response()->json(['listShift'=> $shiftArray]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $dataValidated = $request->validate([
            'shift_date' => ['required', 'date'],
            'shift_id' => ['required', 'numeric'],
            'start_time_shift' => ['required'],
            'end_time_shift' => ['required']
        ]);

        $startTime = Carbon::parse($dataValidated['start_time_shift'])->format('H:i:s');
        $endTime = Carbon::parse($dataValidated['end_time_shift'])->format('H:i:s');

        AdditionalShift::create([
            'user_id' => $request->user_id,
            'shift_id' => $dataValidated['shift_id'],
            'date' => $dataValidated['shift_date'],
            'start_time' => $startTime,
            'end_time' => $endTime,
        ]);

        return redirect('/admin/additionalShift/'. $request->user_id . '/manage')->with('success', 'Additional Shift Added');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete(Request $request)
    {
        AdditionalShift::destroy($request->additional_shift_id);
        return redirect('/admin/additionalShift/'. $request->user_id . '/manage')->with('success', 'Additional Shift Deleted');
    }
}
