<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\StudentDetails;
use Auth;
use Mail;
use PDF;
use File;
use Excel;
use DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class StudentController extends Controller
{
    public function view()
    {
        $student = User::where('user_type', 'student')->latest()->get();
        if(count($student) > 0)
        {
            return response()->json(['result' => true, 'student' => $student]);
        }
        else
        {
            return response()->json(['result' => false, 'message' => 'No Teacher Found']);
        }
    }
    
    public function store(Request $request)
    {
        
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required|email|unique:users',
        ]);
        
        if($validator->fails()){
            return response()->json(['result' => false, 'message' => $validator->errors()]);    
        }
        
        $user                = new User();
        $user->name          = $request->name;
        $user->email         = $request->email;
        $user->phone         = $request->phone;
        $user->user_type     = "student";
        $user->status        = $request->status;
        $user->password      = Hash::make($request->password);
        $user->save();
        
        if ($user) {
            // print_r($user['id']);exit;
            $student_details                = new StudentDetails();
            $student_details->user_id       = $user['id'];
            $student_details->father_name   = $request->father_name;
            $student_details->mother_name   = $request->mother_name;
            $student_details->date_of_birth = $request->date_of_birth;
            $student_details->student_id    = $request->student_id;
            $student_details->gender        = $request->gender;
            $student_details->save();
            
            $data['student'] = $user;
            
            return response()->json(['result' => true, 'student' => $data]);
        }
        else{
            return response()->json(['result' => false, 'message' => 'Somthing Wrong']);
        }
    }
    
    public function update(Request $request)
    {
        
        $validator = Validator::make($request->all(), [
            'name' => 'required',
        ]);
     
        
        if($validator->fails()){
            return response()->json(['result' => false, 'message' => $validator->errors()]);    
        }
        
        $user                = User::find($request->user_id);
        $user->name          = $request->name;
        $user->email         = $request->email;
        $user->phone         = $request->phone;
        $user->user_type     = "student";
        $user->status        = $request->status;
        $user->password      = Hash::make($request->password);
        $user->save();
        
        if ($user) {
            
            $details_check = StudentDetails::where('user_id', $request->user_id)->first();
            
            $student_details                = StudentDetails::find($details_check->id);
            $student_details->user_id       = $user['id'];
            $student_details->father_name   = $request->father_name;
            $student_details->mother_name   = $request->mother_name;
            $student_details->date_of_birth = $request->date_of_birth;
            $student_details->student_id    = $request->student_id;
            $student_details->gender        = $request->gender;
            $student_details->save();
            
            $data['student'] = $user;
            
            return response()->json(['result' => true, 'data' => $data]);
        }
        else{
            return response()->json(['result' => false, 'message' => 'Somthing Wrong']);
        }
    }
    
    public function delete(Request $request)
    {
        $user_details = User::where('id', $request->user_id)->first();
        if($user_details)
        {
            // dd($user_details);
            User::findOrFail($request->user_id)->delete();
            return response()->json(['result' => true, 'message' => 'Student Deleted Successfully']);
        }
        
        return response()->json(['result' => false, 'message' => 'Student not Found']);

    }
    
    public function edit(Request $request)
    {
        $user_details = User::where('id', $request->user_id)->first();
        if($user_details)
        {
            $data['student'] = $user_details;
            $data['student_details'] = $user_details->student_details;
            return response()->json(['result' => true, 'data' => $data]);
        }
        
        return response()->json(['result' => false, 'message' => 'Student not Found']);
    }
    
    public function TeacherView()
    {
        $student = User::where('user_type', 'student')->latest()->get();
        if(count($student) > 0)
        {
            return response()->json(['result' => true, 'student' => $student]);
        }
        else
        {
            return response()->json(['result' => false, 'message' => 'No Teacher Found']);
        }
    }
    
    public function TeacherStore(Request $request)
    {
        
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required|email|unique:users',
        ]);
        
        if($validator->fails()){
            return response()->json(['result' => false, 'message' => $validator->errors()]);    
        }
        
        $user                = new User();
        $user->name          = $request->name;
        $user->email         = $request->email;
        $user->phone         = $request->phone;
        $user->user_type     = "student";
        $user->status        = $request->status;
        $user->password      = Hash::make($request->password);
        $user->save();
        
        if ($user) {
            // print_r($user['id']);exit;
            $student_details                = new StudentDetails();
            $student_details->user_id       = $user['id'];
            $student_details->father_name   = $request->father_name;
            $student_details->mother_name   = $request->mother_name;
            $student_details->date_of_birth = $request->date_of_birth;
            $student_details->student_id    = $request->student_id;
            $student_details->gender        = $request->gender;
            $student_details->save();
            
            $data['student'] = $user;
            
            return response()->json(['result' => true, 'student' => $data]);
        }
        else{
            return response()->json(['result' => false, 'message' => 'Somthing Wrong']);
        }
    }
    
    public function TeacherUpdate(Request $request)
    {
        
        $validator = Validator::make($request->all(), [
            'name' => 'required',
        ]);
     
        
        if($validator->fails()){
            return response()->json(['result' => false, 'message' => $validator->errors()]);    
        }
        
        $user                = User::find($request->user_id);
        $user->name          = $request->name;
        $user->email         = $request->email;
        $user->phone         = $request->phone;
        $user->user_type     = "student";
        $user->status        = $request->status;
        $user->password      = Hash::make($request->password);
        $user->save();
        
        if ($user) {
            
            $details_check = StudentDetails::where('user_id', $request->user_id)->first();
            
            $student_details                = StudentDetails::find($details_check->id);
            $student_details->user_id       = $user['id'];
            $student_details->father_name   = $request->father_name;
            $student_details->mother_name   = $request->mother_name;
            $student_details->date_of_birth = $request->date_of_birth;
            $student_details->student_id    = $request->student_id;
            $student_details->gender        = $request->gender;
            $student_details->save();
            
            $data['student'] = $user;
            
            return response()->json(['result' => true, 'data' => $data]);
        }
        else{
            return response()->json(['result' => false, 'message' => 'Somthing Wrong']);
        }
    }
    
    public function TeacherDelete(Request $request)
    {
        $user_details = User::where('id', $request->user_id)->first();
        if($user_details)
        {
            // dd($user_details);
            User::findOrFail($request->user_id)->delete();
            return response()->json(['result' => true, 'message' => 'Student Deleted Successfully']);
        }
        
        return response()->json(['result' => false, 'message' => 'Student not Found']);

    }
    
    public function TeacherEdit(Request $request)
    {
        $user_details = User::where('id', $request->user_id)->first();
        if($user_details)
        {
            $data['student'] = $user_details;
            $data['student_details'] = $user_details->student_details;
            return response()->json(['result' => true, 'data' => $data]);
        }
        
        return response()->json(['result' => false, 'message' => 'Student not Found']);
    }
    
}
