<?php

namespace App\Http\Controllers;

use App\Student;
use App\Batch;
use App\User;
use App\Message;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use DataTables;

class StudentController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth', ['except' => ['list', 'checkStudentData', 'patchUpdate', 'patchUpdatePassword', 'verifyLogin']]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    // public function index()
    // {
    //     //
    // }

    public function index(Request $request, $id)
    {
        $data = Batch::where('batches.id', $id)
            ->leftJoin('courses', 'batches.courseid', '=', 'courses.id')
            ->select('batches.id', 'courses.name', 'currentsem', 'batch')->first();
        $coursename = $data->name;
        $batch = $data->batch;
        return view('student.index', compact('id', 'coursename', 'batch'));
    }


    public function showstudents($id, $status)
    {


        if ($status == "active") {
            $data = Student::latest()->where('batchid', $id)->get();
        } else {
            $data = Student::onlyTrashed()->where('batchid', $id)->get();
        }

        return Datatables::of($data)
            ->addIndexColumn()
            ->addColumn('edit', function ($row) {

                $btn = '<a href="/student/' . $row->id . '/edit" class="edit btn btn-primary btn-sm">Edit</a>';
                if ($row->deleted_at != null) {
                    $btn = "";
                }
                return $btn;
            })
            ->addColumn('delete', function ($row) {
                if (is_null($row->deleted_at)) {
                    $btn = '<a href="javascript:void(0)" data-id="' . $row->id . '" class="deleteData btn btn-danger btn-sm">Delete</a>';
                } else {
                    $btn = '<a href="javascript:void(0)" data-id="' . $row->id . '" class="restoreData btn btn-success btn-sm">Restore</a>';
                }
                return $btn;
            })
            ->rawColumns(['edit', 'delete'])
            ->make(true);
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($id)
    {
        $data = Batch::where('batches.id', $id)
            ->leftJoin('courses', 'batches.courseid', '=', 'courses.id')
            ->select('batches.id', 'courses.name', 'currentsem', 'batch')->first();
        $coursename = $data->name;
        $batch = $data->batch;
        return view('student.create', compact('id', 'coursename', 'batch'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = Student::Create($request->all());
        if ($data) {
            $user = User::create([
                'name' => $data->name,
                'email' => $data->email,
                'password' => Hash::make('12345678'),
                'usertype' => 3,
            ]);
            $data->update(array('userid' => $user->id));
            return true;
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Student  $student
     * @return \Illuminate\Http\Response
     */
    public function show(Student $student)
    {
        return $student;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Student  $student
     * @return \Illuminate\Http\Response
     */
    public function edit(Student $student)
    {
        $data = Batch::where('batches.id', $student->batchid)
            ->leftJoin('courses', 'batches.courseid', '=', 'courses.id')
            ->select('batches.id', 'courses.name', 'currentsem', 'batch')->first();
        $id = $student->batchid;
        $coursename = $data->name;
        $batch = $data->batch;
        return view('student.edit', compact('student', 'id', 'coursename', 'batch'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Student  $student
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Student $student)
    {
        if ($student->fill($request->all())->save()) {
            return true;
        }
    }

    /**
     * restore the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Student  $student
     * @return \Illuminate\Http\Response
     */
    public function restore(Request $request)
    {
        Student::withTrashed()
            ->where('id', $request->id)
            ->restore();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Student  $student
     * @return \Illuminate\Http\Response
     */
    public function destroy(Student $student)
    {
        $student->deleted_by = Auth::id();
        $student->save();
        if ($student->delete()) {
            return true;
        }
    }

    public function list()
    {
        return Student::latest()->get();
    }

    public function checkStudentData(Request $request)
    {
        $data = Student::where('mobile', $request->mobile)->first();
        if ($data) {
            if ($data->online == "0") {
                $OTP = "1234";
                $data->update(['OTP' => $OTP]);
            }
        }
        return  compact('data');
    }

    public function patchUpdate(Request $request, $id)
    {
        $student = Student::findOrFail($id);
        if ($student->update($request->all())) {
            return true;
        }
    }


    public function patchUpdatePassword(Request $request, $id, $userid)
    {
        $data = User::find($userid);
        $password = Hash::make($request->password);
        if ($data->update(['password' => $password])) {

            $student = Student::find($id);
            if (!is_null($student)) {
                $online = "1";
                $student->update(['online' => $online]);
            }

            return compact('data');
        }
    }

    public function verifyLogin(Request $request)
    {
        $data = Student::leftJoin('users', 'userid', '=', 'users.id')
            ->join('batches', 'batches.id', '=', 'students.batchid')
            ->join('courses', 'courses.id', '=', 'batches.courseid')
            ->where([['mobile', $request->mobile]])
            ->select('batches.id as batchid', 'courses.id as courseid', 'rollno', 'mobile', 'users.password as password','courses.name as coursename', 'students.name', 'students.id as studentid', 'users.id as userid', 'students.email', 'currentsem', 'batch')->first();
            
            error_log("Data password".$data->password);
        if (isset($data->password)) {
            error_log("student Password");
            if (Hash::check($request->password, $data->password)) {
                return compact('data');
            }
        }

        return false;
    }

    public function messagestore(Request $request)
    {


        $message = new Message($request->input());

        $message->save();

        // $request->attachment->store('uploads');
    }

    // public function verifyOTP(Request $request)
    // {
    //     $result=array();
    //     $result["verify"]="fail";
    //     $data = Student::leftJoin('users', 'userid', '=', 'users.id')
    //     ->where([['mobile', $request->mobile]])->first();
    //     if(isset($data->OTP))
    //     {
    //         if($request->OTP == $data->OTP)
    //         {
    //             $result["verify"]="success";
    //         }
    //     }

    //     return json_encode($result); 
    // }

}
