<?php

namespace App\Http\Controllers;

use App\Course;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use DataTables;

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

    /**
     * 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 = Course::latest()->get();
            } else {
                $data = Course::onlyTrashed()->get();
            }

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

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

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if (Course::Create($request->all())) {
            return true;
        }
    }

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



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

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

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

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

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

    public function validateData(Request $request)
    {
        $data = Course::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);
    }
}
