<?php

namespace App\Http\Controllers\Hotel;

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

use App\Models\Hotel\ConsumptionCharges;
use App\Models\Hotel\ExtraCharges;

use App\Http\Controllers\Hotel\ReservationController;

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

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

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

    // Store Functions //
    public function storeExtraCharges(Request $request) {
        $form_data = array(
            'reservation_id' => $request->extra_charge_reservation,
            'extras_date' => $request->extra_charge_date,
            'extras_concept' => $request->extra_charge_concept,
            'extras_price' => $request->extra_charge_amount,
        );
        ExtraCharges::create($form_data);

        (new ReservationController)->updateHistory($request->extra_charge_reservation,'Update', $request->extra_charges_receptionist);

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

    public function storeConsumptionCharges(Request $request) {
        $form_data = array(
            'reservation_id' => $request->consumption_charge_reservation,
            'consumption_date' => $request->consumption_charge_date,
            'consumption_concept' => $request->consumption_charge_concept,
            'consumption_price' => $request->consumption_charge_amount,
        );
        ConsumptionCharges::create($form_data);

        (new ReservationController)->updateHistory($request->consumption_charge_reservation,'Update', $request->consumption_charges_receptionist);

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

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

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

        return view('errors.404');
    }

    // Update Functions //
    public function updateExtraCharges(Request $request) {
        $form_data = array(
            'reservation_id' => $request->extra_charge_reservation,
            'extras_date' => $request->extra_charge_date,
            'extras_concept' => $request->extra_charge_concept,
            'extras_price' => $request->extra_charge_amount,
        );
        ExtraCharges::whereId($request->extra_charge_id)->update($form_data);

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

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

    public function updateConsumptionCharges(Request $request) {
        $form_data = array(
            'reservation_id' => $request->consumption_charge_reservation,
            'consumption_date' => $request->consumption_charge_date,
            'consumption_concept' => $request->consumption_charge_concept,
            'consumption_price' => $request->consumption_charge_amount,
        );
        ConsumptionCharges::whereId($request->consumption_charge_id)->update($form_data);

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

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

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

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

        return $data->reservation->id;
    }

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

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

        return $data->reservation->id;
    }

}
