<?php

namespace App\Http\Controllers;

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

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

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

        if ($request->ajax()) {
            if ($request->status == "active") {
                $status="active";
                $data = Batch::orderBy('batches.created_at', 'desc')
                    ->leftJoin('courses', 'batches.courseid', '=', 'courses.id')
                    ->select('batches.id', 'courses.name', 'currentsem', 'batch', 'batches.deleted_at')->get();
            } else {
                $data = Batch::onlyTrashed()
                    ->leftJoin('courses', 'batches.courseid', '=', 'courses.id')
                    ->select('batches.id', 'courses.name', 'currentsem', 'batch', 'batches.deleted_at')->get();
            }
            return Datatables::of($data)
                ->addIndexColumn()
                ->addColumn('students', function ($row) {

                    $btn = '<a href="/student/'. $row->id .'" class="edit btn btn-success btn-sm">Students</a>';
                    if ($row->deleted_at != null) {
                        $btn="";
                    }
                    return $btn;
                })
                ->addColumn('edit', function ($row) {

                    $btn = '<a href="/batch/' . $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-primary btn-sm">Restore</a>';
                    }
                    return $btn;
                })
                ->rawColumns(['students','tutorial','edit', 'delete'])
                ->make(true);
        }

        return view('batch.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $courses = Course::all(['id', 'name']);
        return view('batch.create', compact('courses'));
    }

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

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Batch  $batch
     * @return \Illuminate\Http\Response
     */
    public function edit(Batch $batch)
    {
        $courses = Course::all(['id', 'name', 'semester']);
        return view('batch.edit', compact('batch', 'courses'));
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Batch  $batch
     * @return \Illuminate\Http\Response
     */
    public function showbatches($id)
    {
        $batches = Batch::where('courseid', $id)->get();
        return $batches;
    }

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Batch  $batch
     * @return \Illuminate\Http\Response
     */
    public function destroy(Batch $batch)
    {
        $batch->deleted_by = Auth::id();;
        $batch->save();
        if ($batch->delete()) {
            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);
        Batch::withTrashed()
            ->where('id', $request->id)
            ->restore();
    }

    public function allFacultyBatches($facultyid)
    {
        $data=Batch::join('teacher_subject_allocations','batches.id','=','teacher_subject_allocations.batchid')
        ->join('courses','courses.id','=','batches.courseid')
        ->where('teacher_subject_allocations.facultyid','=',$facultyid)
        ->select('batches.id as batchid','batch','courses.id as courseid','courses.name as coursename','currentsem')
        ->groupby('batches.id')
        ->get();

        return compact('data');
    }


   
}
