<?php

namespace App\Http\Controllers\Environment;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

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

use App\Helpers\Environment\SystemModulesHelper;

class PaymentMethodController extends Controller
{
  //* Constructor
    public function __construct() {
        $this->middleware(['auth', 'role:administrador']);
    }

  //* Basic CRUD
    public function index() {
      //- Table Data
        if(request()->ajax()){
            return datatables()->of(PaymentMethod::latest()->get())
                ->addColumn('created_at', function($data){
                    return date("d-m-Y _ H:i:s", strtotime($data->created_at));
                })
                ->addColumn('updated_at', function($data){
                    return date("d-m-Y _ H:i:s", strtotime($data->updated_at));
                })
                ->addColumn('action', function($data){
                    $extra_attribute = ($data->id === 3 && SystemModulesHelper::getModuleStatus('audits')) ? 'disabled' : ''; // Disable Actions for "Cash" Method

                    $button = '<button type="button" name="edit" id="'.$data->id.'" class="edit btn btn-primary btn-ms"><i class="fa fa-edit"></i></button>';
                    $button .= '&nbsp;&nbsp;';
                    $button .= '<button type="button" '.$extra_attribute.' name="delete" id="'.$data->id.'" class="delete btn btn-danger btn-ms"><i class="fa fa-trash"></i></button>';
                    return $button;
                })
                ->rawColumns(['action'])
                ->make(true);
        }

      //- View Data
        $paymentMethodLength = PaymentMethod::all()->count();
        $paymentMethod = PaymentMethod::all();

        return view('environment.payment-methods.index', compact(['paymentMethod','paymentMethodLength']));
    }

    public function store(Request $request) {
        $form_data = array(
            'method_name' => $request->titulo
        );
        PaymentMethod::Create($form_data);

        return response()->json(['success' => 'Registrado con éxito']);
    }

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

        return view('errors.404');
    }

    public function update(Request $request) {
        $old_data = PaymentMethod::findOrFail($request->hidden_id);

        //? Update Record
            $form_data = array(
                'method_name' => $request->titulo
            );
            PaymentMethod::whereId($request->hidden_id)->update($form_data);

        //? Update Payments
            $lp = LodgingPayments::where('payment_method', $old_data->method_name)->get();
            $ep = ExtraPayments::where('payment_method', $old_data->method_name)->get();
            $cp = ConsumptionPayments::where('payment_method', $old_data->method_name)->get();

            foreach ($lp as $payment) {
                $payment->payment_method = $request->titulo;
                $payment->save();
            }
            foreach ($ep as $payment) {
                $payment->payment_method = $request->titulo;
                $payment->save();
            }
            foreach ($cp as $payment) {
                $payment->payment_method = $request->titulo;
                $payment->save();
            }

        return response()->json(['success', 'Actualizado con éxito']);
    }

    public function destroy($id) {
        $data = PaymentMethod::findOrFail($id);
        $data->delete();
    }
}
