<?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\ClassRoom;

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

    try {
      return view('classrooms.index', compact('request'));
    } catch (\Exception $e) {
      dd($e);
      return 'list classroom failed';
    }
  }

  /**
   * Show the form for creating a new resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function create(Request $request)
  {
    try {
      return view('classrooms.create', compact('request'));
    } catch (\Exception $e) {
      dd($e);
      return 'create classrooms failed';
    }
  }

  /**
   * Store a newly created resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @return \Illuminate\Http\Response
   */
  public function store(Request $request)
  {
    try {
      $validatedData = $request->validate([
        'classRoomTitle' => 'required',
        'location' => 'required',
        'capacity' => 'required',
      ]);

      // ClassRoom::create($request->post());

      // Subject::create($request->post());
      $class_room = new ClassRoom();
      $class_room->classRoomTitle = $validatedData['classRoomTitle'];
      $class_room->location = $validatedData['location'];
      $class_room->capacity = $validatedData['capacity'];
      $class_room->save();

      Session::forget('status');

      return redirect()
        ->route('classrooms.index')
        ->with('success', 'ClassRoom has been created successfully.');
    } catch (\Exception $e) {
      // dd($e);
      DB::rollback();

      $message = $e->getMessage();
      $request->session()->put('status', $message);

      return view('classrooms.create', compact('request'));
      // return 'save classrooms failed';
    }
  }

  /**
   * Display the specified resource.
   *
   * @param  \App\ClassRoom  $classroom
   * @return \Illuminate\Http\Response
   */
  public function show(ClassRoom $classroom, Request $request)
  {
    try {
      return view('classrooms.show', compact('classroom', 'request'));
    } catch (\Exception $e) {
      dd($e);
      return 'show classroom failed';
    }
  }

  /**
   * Show the form for editing the specified resource.
   *
   * @param  \App\ClassRoom  $classroom
   * @return \Illuminate\Http\Response
   */
  public function edit(ClassRoom $classroom, Request $request)
  {
    try {
      return view('classrooms.edit', compact('classroom', 'request'));
    } catch (\Exception $e) {
      dd($e);
      return 'edit classroom failed';
    }
  }

  /**
   * Update the specified resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \App\ClassRoom  $classroom
   * @return \Illuminate\Http\Response
   */
  public function update(Request $request, ClassRoom $classroom)
  {
    try {
      $validatedData = $request->validate([
        'classRoomTitle' => 'required',
        'location' => 'required',
        'capacity' => 'required',
      ]);

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

      Classroom::where('id', $classroom->id)->update([
        'classRoomTitle' => $validatedData['classRoomTitle'],
        'location' => $validatedData['location'],
        'capacity' => $validatedData['capacity'],
      ]);

      Session::forget('status');

      return redirect()
        ->route('classrooms.index')
        ->with('success', 'ClassRoom Has Been updated successfully');
    } catch (\Exception $e) {
      DB::rollback();
      // dd($e);

      $message = $e->getMessage();
      $request->session()->put('status', $message);

      return view('classrooms.edit', compact('classroom', 'request'));
      // return 'update classroom failed';
    }
  }

  /**
   * Remove the specified resource from storage.
   *
   * @param  \App\ClassRoom  $classroom
   * @return \Illuminate\Http\Response
   */
  public function destroy(ClassRoom $classroom)
  {
    try {
      $classroom->delete();
      return redirect()
        ->route('classrooms.index')
        ->with('success', 'ClassRoom has been deleted successfully');
    } catch (\Exception $e) {
      dd($e);
      return 'delete classroom failed';
    }
  }

  public function getClassRoomsJson(Request $request)
  {
    try {
      $arr = [];

      if (is_null($request->search) == true) {
        $search = $request->search;
        $classrooms = ClassRoom::where('deleted', '=', 'false')
          ->orderBy('id', 'asc')
          ->paginate(100);

        foreach ($classrooms as $row) {
          array_push($arr, [
            // "name" => $row->name,
            'id' => $row->id,
            'classRoomId' => $row->classRoomId,
            'classRoomTitle' => $row->classRoomTitle,
            'location' => $row->location,
            'capacity' => $row->capacity,
          ]);
        }

        return response()->json($arr);
      } else {
        $search = $request->search;
        $classrooms = ClassRoom::where('deleted', '=', 'false')
          ->where('name', 'like', "%{$search}%")
          ->get();

        foreach ($classrooms as $row) {
          array_push($arr, [
            // "name" => $row->name,
            'id' => $row->id,
            'classRoomId' => $row->classRoomId,
            'classRoomTitle' => $row->classRoomTitle,
            'location' => $row->location,
            'capacity' => $row->capacity,
          ]);
        }
      }

      return response()->json($arr);
    } catch (\Exception $e) {
      dd($e);
      return 'getClassRoomsJson failed';
    }
  }

  public function dtGetClassrooms()
  {
    $classrooms = ClassRoom::select([
      'id',
      'classRoomTitle',
      'location',
      'capacity',
    ])->where('deleted', '=', 'false');

    return DataTables::eloquent($classrooms)->toJson();
  }

  public function SoftDelete(Request $request, string $id)
  {
    // to prevent the delete alter the schedules assigned

    try {
      Classroom::where('id', $id)->update([
        'deleted' => 'true',
      ]);

      return redirect()
        ->route('classrooms.index')
        ->with('success', 'ClassRoom Has Been updated successfully');
    } catch (\Exception $e) {
      return redirect()
        ->route('classrooms.index')
        ->with('failed', 'failed during delete classrooms');
    }
  }
}
