<?php

namespace App\Http\Controllers;

use App\Batch;
use App\Course;
use App\Subject;
use App\BatchWiseSubject;
use Illuminate\Http\Request;

class BatchWiseSubjectController 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()
    {
        $courses = Course::all(['id', 'name', 'semester']);
        $batches = Batch::all(['id', 'batch', 'courseid']);
        $subjects = Subject::all(['id', 'name']);
        return view('batchwisesubject.index', compact('courses', 'batches', 'subjects'));
    }

    /**
     * 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)
    {
        $subjects = $request->input('subjectid');
        $batchid = $request->input('batchid');
        $data = BatchWiseSubject::where('batchid', $batchid);
        $data->delete();
        foreach ($subjects as $subjectid) {
            $input = $request->except(['courseid']);
            $input["subjectid"] = $subjectid;
            BatchWiseSubject::Create($input);
        }
        return true;
    }

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

    /**
     * Display the specified resource.
     *
     * @param  \App\BatchWiseSubject  $batchWiseSubject
     * @return \Illuminate\Http\Response
     */
    public function showsubjects($batchid, $semester)
    {
        $subjects = Subject::latest()->get()->toArray();
        $selections = BatchWiseSubject::where('batchid', $batchid)->Where('semester', $semester)->get(['subjectid'])->toArray();
        $selection = array();
        foreach ($subjects as $subject) {
            $selflag = 0;
            foreach ($selections as $selectiondata) {
                if ($selectiondata["subjectid"] == $subject["id"]) {
                    $selflag = 1;
                    break;
                }
            }
            $subject["selection"] = $selflag;
            $selection[] = $subject;
        }
        return $selection;
    }

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

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

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