<?php

namespace App\Http\Controllers;

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

class SubjectController 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 = Subject::latest()->get();
            } else {
                $data = Subject::onlyTrashed()->get();
            }

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

                    $btn = '<a href="/subject/' . $row->id . '/edit" class="edit btn btn-primary btn-sm">Edit</a>';

                    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('subject.index');
    }

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

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

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

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

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

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

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

    public function list(Request $request)
    {
        return Subject::orderBy('name', 'asc')
            ->leftJoin('teacher_subject_allocations', 'teacher_subject_allocations.subjectid', '=', 'subjects.id')
            ->leftJoin('batches', 'teacher_subject_allocations.batchid', '=', 'batches.id')
            ->leftJoin('courses', 'courses.id', '=', 'batches.courseid')
            ->leftJoin('faculties', 'faculties.id', '=', 'teacher_subject_allocations.facultyid')
            ->select('subjects.id', 'subjects.name', 'batches.id as batchid', 'courses.id as courseid', 'courses.name as coursename', 'currentsem', 'faculties.name as facultyname', 'faculties.id as facultyid')
            ->where([['teacher_subject_allocations.batchid', $request->batchid]])
            ->get();
    }

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