<?php

namespace App\Http\Controllers;

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

class StudentHomeWorkController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function student_index()
    {
        return view('student');
    }

    public function StudentProfile()
    {
        $user = User::where('id', Auth::user()->id)->first();
        
        return view('student.profile', compact('user'));

    }

    public function StudentProfileUpload(Request $request, $id)
    {
        // dd($request->all());

        $user               = User::find($id);
        if ($request->password) {
            $request->validate([
                'name' => 'required|max:255',
                'password' => 'required|confirmed'
            ]);

            $password           = Hash::make($request->password);
            $user->password     = $password;
        }
        else
        {
            $request->validate([
                'name' => 'required|max:255'
            ]);
        }

        if ($request->hasFile('profile_picture')) {
            $image = $request->file('profile_picture');
            $filename = time().'.'.$image->extension();
            $destinationPath = public_path('/profile');
            // dd($destinationPath);
            $image->move($destinationPath, $filename);
            $user->profile_picture           = $filename;
        }

        $user->name           = $request->name;
        $user->phone          = $request->phone;
        $user->gender         = $request->gender;
        if ($user->save()) {
            return redirect()->route('student.profile')->with('warning', 'Profile Edited Successfully');
        }
        else{
            return redirect()->back()->with('error', 'Something Wrong');
        }
    }

    public function view()
    {
        $homework = HomeWork::where('student_id', Auth::user()->id)->latest()->get();
        return view('student.home-work.view', compact('homework'));
    }

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

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

        $homework_update = HomeWork::find($request->homework_id);
        $homework_update->student_description = $request->user_comment;
        $homework_update->student_confirm_date  = date('Y-md H:i:s');
        $homework_update->status = 1;

        if($homework_update->save())
        {
            return redirect()->route('student.home.work.view')->with('warning', 'Home Work Details Updated.');
        }
        else
        {
            return redirect()->back()->with('error', 'Something Wrong');
        }

    }

    public function MarkDetails()
    {
        $mark_details = Mark::where('student_id', Auth::user()->id)->latest()->get();
        return view('student.mark_details', compact('mark_details'));
    }

}
