<?php

namespace App\Http\Controllers;

use App\Faculty;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use DataTables;

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

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

    public function index(Request $request)
    {

        if ($request->ajax()) {
            if ($request->status == "active") {
                $data = Faculty::latest()->get();
            } else {
                $data = Faculty::onlyTrashed()->get();
            }

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

                    $btn = '<a href="/faculty/' . $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);
        }

        return view('faculty.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('faculty.create');
    }

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

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Faculty  $faculty
     * @return \Illuminate\Http\Response
     */
    public function edit(Faculty $faculty)
    {
        return view('faculty.edit', compact('faculty'));
    }

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

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

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

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

    public function validateData(Request $request)
    {
        $data = Faculty::where('name', '=', $request->name)
        ->where('id', '!=', $request->id)
        ->first();
        
        $result=array();
        $result["status"]="success";
        if(isset($data->id))
        {
            $result["status"]="fail";
        }
        return json_encode($result);
    }

    public function validateEmail(Request $request)
    {
        $data = User::where('email', '=', $request->email)
        ->where('id', '!=', $request->id)
        ->first();
        
        $result=array();
        $result["status"]="success";
        if(isset($data->id))
        {
            $result["status"]="fail";
        }
        return json_encode($result);
    }

    public function verifyLogin(Request $request)
    {
        $data = Faculty::leftJoin('users', 'userid', '=', 'users.id')
            ->where([['mobile', $request->mobile]])
            ->select('mobile','users.id as userid','faculties.email','faculties.id as facultyid','users.password as password','faculties.name as name')->first();
        if (isset($data->password)) {
            error_log("faculty Password");
            if (Hash::check($request->password, $data->password)) {
                return compact('data');
            }
        }
        
        return false;
    }

    public function patchUpdatePassword(Request $request,$id,$userid)
    {
        $data = User::find($userid);
        $password = Hash::make($request->password);
        if ($data->update(['password' => $password])) {
            
            $faculty = Faculty::find($id);
            if(!is_null($faculty))
            {
                $online="1";
                $faculty->update(['online' => $online]);
            }
          
            return compact('data');
        }

    }

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