<?php

namespace App\Http\Controllers;

use App\Models\HomeWork;
use App\Models\User;
use Auth;
use Illuminate\Http\Request;

class HomeWorkController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function create()
    {
        $students = User::where('user_type', 'student')->get();
        return view('teacher.home-work.add', compact('students'));
    }

    public function view()
    {
        $homework = HomeWork::latest()->get();
        return view('teacher.home-work.view', compact('homework'));
    }

    public function edit($id)
    {
        $students = User::where('user_type', 'student')->get();
        $homework = HomeWork::where('id', $id)->first();
        return view('teacher.home-work.edit', compact('homework', 'students'));
    }

    public function delete($id)
    {

        HomeWork::findOrFail($id)->delete();
        return redirect()->back()->with('error', 'Deleted Successfully');

    }

    public function store(Request $request)
    {
        // dd($request->all());

        $request->validate([
            'homework_title'   => 'required|max:255'
        ]);

        $students = $request->student;

        if($students)
        {
            foreach ($students as $key => $value) {
                
                $homework                       = new HomeWork();
                $homework->student_id           = $value;
                $homework->teacher_id           = Auth::user()->id;
                $homework->homework_title       = $request->homework_title ;
                $homework->homework_last_date   = $request->homework_last_date;
                $homework->homework_description = $request->homework_description;
                $homework->status               = 0;
                $homework->save();

            }

            return redirect()->route('teacher.home.work.view')->with('success', 'Home Work Created Successfully');
        }
        else{
            return redirect()->back()->with('error', 'Something Wrong');
        }

    }

    public function update(Request $request)
    {
        // dd($request->all());

        $request->validate([
            'homework_title'   => 'required|max:255'
        ]);

        $homework                       = HomeWork::find($request->homework_id);
        $homework->student_id           = $request->student;
        $homework->teacher_id           = Auth::user()->id;
        $homework->homework_title       = $request->homework_title ;
        $homework->homework_last_date   = $request->homework_last_date;
        $homework->homework_description = $request->homework_description;
        $homework->status               = 0;
        
        if($homework->save())
        {
            return redirect()->route('teacher.home.work.view')->with('warning', 'Home Work Details Updated Successfully');
        }
        else{
            return redirect()->back()->with('error', 'Something Wrong');
        }

    }

    public function details($id)
    {
        $homework = HomeWork::where('id', $id)->first();
        return view('teacher.home-work.details', compact('homework'));
    }
}
