<?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\Payment;
use App\Models\Student;

class PaymentController extends Controller
{
  function returnToLogin()
  {
    return redirect()
      ->route('login')
      ->with('success', 'sorry but the session already timeout');
  }

  function errHandler($th)
  {
    $ERR_CANNOT_GET_EMAIL = 'Attempt to read property "email" on null';

    if ($ERR_CANNOT_GET_EMAIL == $th->getMessage()) {
      return $this->returnToLogin();
    } else {
      throw dd($th);
    }
  }

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

  public function index(Request $request)
  {
    Session::forget('status');

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

  public function destroy(Payment $payment)
  {
    $payment->delete();

    return redirect()
      ->route('payments.index')
      ->with('success', 'payment has been deleted successfully');
  }

  public function dtGetPayments()
  {
    try {
      $payments = Payment::with(['student'])->select();

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

  public function show(Payment $payment, Request $request)
  {
    try {
      return view('payments.show', compact('payment', 'request'));
    } catch (\Exception $e) {
      dd($e);
      return 'show payment failed';
    }
  }

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

  public function update(Request $request, Payment $payment)
  {
    try {
      $validatedData = $request->validate([
        'date' => 'required',
        'student_id' => 'required',
        'subject' => 'required',
        'amount' => 'required',
        'remarks' => '',
      ]);

      // $payment->fill($request->post())->save();

      $payment = Payment::find($payment->id);
      $payment->date = $validatedData['date'];
      $payment->student_id = $validatedData['student_id'];
      $payment->subject = $validatedData['subject'];
      $payment->amount = $validatedData['amount'];
      $payment->remarks = $validatedData['remarks'];

      $payment->save();

      Session::forget('status');

      return redirect()
        ->route('payments.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('payments.edit', compact('payment', 'request'));
    }
  }

  public function store(Request $request)
  {
    try {
      $validatedData = $request->validate([
        'from' => 'required',
        'amount' => 'required',
      ]);

      DB::beginTransaction();

      $payment = new Payment();
      $payment->from = $validatedData['from'];
      $payment->subject = $validatedData['subject'];
      $payment->amount = $validatedData['amount'];
      $payment->remarks = $validatedData['remarks'];
      $payment->save();

      Session::forget('status');

      DB::commit();

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

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

      return view('adminShowPayments', compact('request'));
    }
  }
}
