<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

use Yajra\DataTables\Facades\DataTables;

use App\Models\Attendance;
use App\Models\Student;
use App\Models\Subject;

class attendanceController extends Controller
{
  /**
   * Display a listing of the resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function index(Request $request)
  {
    Session::forget('status');

    try {
      return view('attendances.index', compact('request'));
    } catch (\Exception $e) {
      $debug = config('app.debug');
      if ($debug) {
        dd($e);
      } else {
        return 'sorry but list attendance failed';
      }
    }
  }

  public function getAttendancesJson()
  {
    try {
      $arr = [];
      $attendances = Attendance::orderBy('id', 'asc')->paginate(100);

      foreach ($attendances as $row) {
        array_push($arr, [
          'check_time' => $row->check_time,
          'student_email' => $row->student_email,
          'subject_id' => $row->subject_id,
        ]);
      }

      return response()->json($arr);
    } catch (\Exception $e) {
      $debug = config('app.debug');
      if ($debug) {
        dd($e);
      } else {
        return 'sorry but list attendance failed';
      }
    }
  }

  public function show(Attendance $attendance, Request $request)
  {
    try {
      return view('attendances.show', compact('attendance', 'request'));
    } catch (\Exception $e) {
      $debug = config('app.debug');
      if ($debug) {
        dd($e);
      } else {
        return 'sorry but show attendance failed';
      }
    }
  }

  public function edit(Attendance $attendance, Request $request)
  {
    try {
      return view('attendances.edit', compact('attendance', 'request'));
    } catch (\Exception $e) {
      $debug = config('app.debug');
      if ($debug) {
        dd($e);
      } else {
        return 'sorry but edit attendance failed';
      }
    }
  }

  public function update(Request $request, Attendance $attendance)
  {
    try {
      $validatedData = $request->validate([
        'student_id' => 'required',
        'subject_id' => 'required',
        'remarks' => '',
      ]);

      $attendance->fill($request->post())->save();

      return view('attendances.index', compact('request'));
    } catch (\Exception $e) {
      $debug = config('app.debug');
      if ($debug) {
        dd($e);
      } else {
        return 'sorry but list attendance failed';
      }
    }
  }

  public function destroy(Attendance $attendance)
  {
    try {
      $attendance->delete();
      return redirect()
        ->route('attendances.index')
        ->with('success', 'attendance has been deleted successfully');
    } catch (\Exception $e) {
      $debug = config('app.debug');
      if ($debug) {
        dd($e);
      } else {
        $ERR_MSG = 'delete classroom failed';
        return $ERR_MSG;
      }
    }
  }

  public function dtGetAttendances()
  {
    try {
      $attendances = Attendance::with(['subject', 'student'])->select();

      return DataTables::eloquent($attendances)->toJson();
    } catch (\Exception $e) {
      $debug = config('app.debug');
      if ($debug) {
        dd($e);
      } else {
        return 'sorry but list attendance failed';
      }
    }
  }

  public function create(Request $request)
  {
    try {
      return view('attendances.create', compact('request'));
    } catch (\Exception $e) {
      dd($e);
      return 'create attendances failed';
    }
  }

  public function store(Request $request)
  {
    try {
      $validatedData = $request->validate([
        'check_time' => 'required',
        'student_id' => 'required',
        'subject_id' => 'required',
        'remarks' => '',
      ]);

      $attendance = new Attendance();
      $attendance->check_time = $validatedData['check_time'];
      $attendance->student_id = $validatedData['student_id'];
      $attendance->subject_id = $validatedData['subject_id'];
      $attendance->remarks = $validatedData['remarks'];
      $attendance->save();

      return view('attendances.index', compact('request'));
    } catch (\Exception $e) {
      dd($e);
      return 'create attendances failed';
    }
  }
}
