<?php

namespace App\Http\Controllers;

use App\Batch;
use App\Course;
use App\Subject;
use App\Faculty;
use App\Tutorial;
use App\Message;
use App\TeacherSubjectAllocation;
use Illuminate\Http\Request;
use DataTables;

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

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $courses = Course::all(['id', 'name', 'semester']);
        $batches = Batch::all(['id', 'batch', 'courseid']);
        $subjects = Subject::all(['id', 'name']);
        $faculties = Faculty::all(['id', 'name']);
        return view('teachersubjectallocation.index', compact('courses', 'batches', 'subjects', 'faculties'));
    }


    public function indexMyBatches()
    {
        $userid = auth()->user()->id;

        $faculty = Faculty::join('users', 'users.id', '=', 'faculties.userid')
            ->where('faculties.userid', '=', $userid)
            ->select('faculties.id')->first();

        $facultyid = $faculty->id;
        // error_log("faculty".$faculty);

        $courses = Batch::join('teacher_subject_allocations', 'teacher_subject_allocations.batchid', '=', 'batches.id')
            ->join('courses', 'courses.id', '=', 'batches.courseid')
            ->where('teacher_subject_allocations.facultyid', '=', $faculty->id)
            ->select('courses.id', 'courses.name')->get();

        return view('mybatches.index', compact('courses', 'facultyid'));
    }

    public function showSubjectsMyBatches($facultyid, Request $request)
    {
        $subject = Subject::join('teacher_subject_allocations', 'teacher_subject_allocations.subjectid', '=', 'subjects.id')
            ->where('teacher_subject_allocations.facultyid', '=', $facultyid)
            ->where('teacher_subject_allocations.semester', '=', $request->semester)
            ->where('teacher_subject_allocations.batchid', '=', $request->batchid)
            ->select('subjects.id', 'subjects.name')->get();

        return $subject;
    }

    public function showTutorialsMyBatches($batchid, $semester, $facultyid, $subjectid)
    {
        $data = Tutorial::where('batchid', $batchid)
            ->Where('semester', $semester)
            ->Where('facultyid', $facultyid)
            ->Where('subjectid', $subjectid)
            ->get();



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

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


        return view('teachersubjectallocation.showtutorials');

        // return $result;
    }

    public function indexMyMessages()
    {
        $userid = auth()->user()->id;

        $faculty = Faculty::join('users', 'users.id', '=', 'faculties.userid')
            ->where('faculties.userid', '=', $userid)
            ->select('faculties.id')->first();

        $facultyid = $faculty->id;
        // error_log("faculty".$faculty);

        $courses = Batch::join('teacher_subject_allocations', 'teacher_subject_allocations.batchid', '=', 'batches.id')
            ->join('courses', 'courses.id', '=', 'batches.courseid')
            ->where('teacher_subject_allocations.facultyid', '=', $faculty->id)
            ->select('courses.id', 'courses.name')->get();

        return view('mymessages.index', compact('courses', 'facultyid'));
    }

    public function showMessagesMyBatches($batchid, $semester, $facultyid, $subjectid)
    {
        $data = Message::where('batchid', $batchid)
            ->Where('semester', $semester)
            ->Where('facultyid', $facultyid)
            ->Where('subjectid', $subjectid)
            ->get();

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

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


        return view('teachersubjectallocation.showmessages');

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        if (TeacherSubjectAllocation::Create($request->all())) {
            return true;
        }

        return true;
    }

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

    /**
     * Display the specified resource.
     *
     * @param  \App\TeacherSubjectAllocation  $teachersubjectallocation
     * @return \Illuminate\Http\Response
     */
    public function showallocation($batchid, $semester)
    {
        $data = TeacherSubjectAllocation::join('subjects', 'subjects.id', '=', 'teacher_subject_allocations.subjectid')->join('faculties', 'faculties.id', '=', 'teacher_subject_allocations.facultyid')->where('batchid', $batchid)->Where('semester', $semester)->get(['subjects.name as subjectname', 'faculties.name as facultyname', 'teacher_subject_allocations.id as id']);

        return Datatables::of($data)
            ->addIndexColumn()
            ->addColumn('delete', function ($row) {
                $btn = '<a href="javascript:void(0)" data-id="' . $row->id . '" class="deleteData btn btn-danger btn-sm">Delete</a>';
                return $btn;
            })
            ->rawColumns(['delete'])
            ->make(true);


        return view('teachersubjectallocation.showallocation');

        // return $result;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\TeacherSubjectAllocation  $teacherSubjectAllocation
     * @return \Illuminate\Http\Response
     */
    public function edit(TeacherSubjectAllocation $teacherSubjectAllocation)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\TeacherSubjectAllocation  $teacherSubjectAllocation
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, TeacherSubjectAllocation $teacherSubjectAllocation)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\TeacherSubjectAllocation  $teacherSubjectAllocation
     * @return \Illuminate\Http\Response
     */
    public function destroy(TeacherSubjectAllocation $teacherSubjectAllocation)
    {
        //
    }

    public function allFacultyBatchwiseSubjects($facultyid, $batchid, $semester)
    {
        $data = Subject::join('teacher_subject_allocations', 'subjects.id', '=', 'teacher_subject_allocations.subjectid')
            ->where('teacher_subject_allocations.facultyid', '=', $facultyid)
            ->where('teacher_subject_allocations.batchid', '=', $batchid)
            ->where('teacher_subject_allocations.semester', '=', $semester)
            ->select('subjects.id as subjectid', 'subjects.name as subjectname')->get();

        return compact('data');
    }
}
