<?php

namespace App\Http\Controllers\Hotel;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Models\Hotel\LodgingPayments;
use App\Models\Hotel\ExtraPayments;
use App\Models\Hotel\ConsumptionPayments;

use App\Http\Controllers\Hotel\ReservationController;

class ReservationPaymentsController extends Controller
{
  //* Constructor
    public function __construct() {
        $this->middleware(['auth']);
    }

  //* Basic CRUD
    public function getLodgingPayments($reservation_id) {
        return LodgingPayments::where('reservation_id',$reservation_id)->get();
    }

    public function getExtraPayments($reservation_id) {
        return ExtraPayments::where('reservation_id',$reservation_id)->get();
    }

    public function getConsumptionPayments($reservation_id) {
        return ConsumptionPayments::where('reservation_id',$reservation_id)->get();
    }

    // Store Functions //
    public function storeLodgingPayments(Request $request) {
        $form_data = array(
            'reservation_id' => $request->lodging_payment_reservation,
            'payment_date' => $request->lodging_payment_date,
            'payment_concept' => $request->lodging_payment_concept,
            'payment_price' => $request->lodging_payment_amount,
            'payment_method' => $request->lodging_payment_method
        );
        LodgingPayments::create($form_data);

        (new ReservationController)->updateHistory($request->lodging_payment_reservation,'Update', $request->lodging_payment_receptionist);

        return response()->json(['success' => 'Pago Registrado' ]);
    }

    public function storeExtraPayments(Request $request) {
        $form_data = array(
            'reservation_id' => $request->extra_payment_reservation,
            'payment_date' => $request->extra_payment_date,
            'payment_concept' => $request->extra_payment_concept,
            'payment_price' => $request->extra_payment_amount,
            'payment_method' => $request->extra_payment_method
        );
        ExtraPayments::create($form_data);

        (new ReservationController)->updateHistory($request->extra_payment_reservation,'Update', $request->extra_payment_receptionist);

        return response()->json(['success' => 'Pago Registrado' ]);
    }

    public function storeConsumptionPayments(Request $request) {
        $form_data = array(
            'reservation_id' => $request->consumption_payment_reservation,
            'payment_date' => $request->consumption_payment_date,
            'payment_concept' => $request->consumption_payment_concept,
            'payment_price' => $request->consumption_payment_amount,
            'payment_method' => $request->consumption_payment_method
        );
        ConsumptionPayments::create($form_data);

        (new ReservationController)->updateHistory($request->consumption_payment_reservation,'Update', $request->consumption_payment_receptionist);

        return response()->json(['success' => 'Pago Registrado' ]);
    }

    // Edit Functions //
    public function editLodgingPayments($id) {
        if(request()->ajax()){
            $data = LodgingPayments::findOrFail($id);
            return response()->json(['data' => $data]);
        }

        return view('errors.404');
    }

    public function editExtraPayments($id) {
        if(request()->ajax()){
            $data = ExtraPayments::findOrFail($id);
            return response()->json(['data' => $data]);
        }

        return view('errors.404');
    }

    public function editConsumptionPayments($id) {
        if(request()->ajax()){
            $data = ConsumptionPayments::findOrFail($id);
            return response()->json(['data' => $data]);
        }

        return view('errors.404');
    }

    // Update Functions //
    public function updateLodgingPayments(Request $request) {
        $form_data = array(
            'reservation_id' => $request->lodging_payment_reservation,
            'payment_date' => $request->lodging_payment_date,
            'payment_concept' => $request->lodging_payment_concept,
            'payment_price' => $request->lodging_payment_amount,
            'payment_method' => $request->lodging_payment_method
        );
        LodgingPayments::whereId($request->lodging_payment_id)->update($form_data);

        (new ReservationController)->updateHistory($request->lodging_payment_reservation,'Update', auth()->user()->name);

        return response()->json(['success' => 'Pago Actualizado' ]);
    }

    public function updateExtraPayments(Request $request) {
        $form_data = array(
            'reservation_id' => $request->extra_payment_reservation,
            'payment_date' => $request->extra_payment_date,
            'payment_concept' => $request->extra_payment_concept,
            'payment_price' => $request->extra_payment_amount,
            'payment_method' => $request->extra_payment_method
        );
        ExtraPayments::whereId($request->extra_payment_id)->update($form_data);

        (new ReservationController)->updateHistory($request->extra_payment_reservation,'Update', auth()->user()->name);

        return response()->json(['success' => 'Pago Actualizado' ]);
    }

    public function updateConsumptionPayments(Request $request) {
        $form_data = array(
            'reservation_id' => $request->consumption_payment_reservation,
            'payment_date' => $request->consumption_payment_date,
            'payment_concept' => $request->consumption_payment_concept,
            'payment_price' => $request->consumption_payment_amount,
            'payment_method' => $request->consumption_payment_method
        );
        ConsumptionPayments::whereId($request->consumption_payment_id)->update($form_data);

        (new ReservationController)->updateHistory($request->consumption_payment_reservation,'Update', auth()->user()->name);

        return response()->json(['success' => 'Pago Actualizado' ]);
    }

    // Delete Functions //
    public function removeLodgingPayments($id) {
        $data = LodgingPayments::findOrFail($id);
        $data->status = 'Eliminado';
        $data->save();

        (new ReservationController)->updateHistory($data->reservation->id,'Update',auth()->user()->name);

        return $data->reservation->id;
    }

    public function removeExtraPayments($id) {
        $data = ExtraPayments::findOrFail($id);
        $data->status = 'Eliminado';
        $data->save();

        (new ReservationController)->updateHistory($data->reservation->id,'Update',auth()->user()->name);

        return $data->reservation->id;
    }

    public function removeConsumptionPayments($id) {
        $data = ConsumptionPayments::findOrFail($id);
        $data->status = 'Eliminado';
        $data->save();

        (new ReservationController)->updateHistory($data->reservation->id,'Update',auth()->user()->name);

        return $data->reservation->id;
    }

}
