<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Activities;
use App\Models\Feedback;
use App\Models\ResultSummary;
use DB;
use Log;

class ActivitiesController extends Controller {

    public function getActivities($id) {
        try {
            $activities = Feedback::Where('parent_id',$id)->get();
            return response()->json([ 'activities' => $activities], 200);
        } catch ( \Exception $e ) {
            Log::error( $e );
            return response()->json( ['message' => $e->getmessage()], 500 );
        }
    }

    
    public function getMarks($id) {
        try {
            $summary = ResultSummary::Where('student_id',$id)->get();
            return response()->json([ 'summary' => $summary], 200);
        } catch ( \Exception $e ) {
            Log::error( $e );
            return response()->json( ['message' => $e->getmessage()], 500 );
        }
    }

    public function create( Request $request ) {
        try {
                DB::beginTransaction();

                $activities = new Feedback();
                $activities->name  = $request->name;
                $activities->parent_id = $request->parent_id;
                $activities->feedback = $request->feedback;
                $activities->subject = $request->subject;
                $activities->date = $request->date;
                $activities->time = $request->time;
                $activities->save();

                DB::commit();
                return response()->json([ 'activities' => $activities, 'message' => 'saved' ], 200 );
            } catch (\Exception $e) {
                DB::rollBack();
                Log::error($e);
                return response()->json([ 'message' => $e->getmessage() ], 500);
            }
    }
}
