<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\PagoModel;

class Pagos extends BaseController
{
    public function index()
    {
        $model = new PagoModel();
        $q = $this->request->getGet('q');

        $builder = $model->select('pago.*, usuario.nombres as cliente_nombres, usuario.apellidos as cliente_apellidos, reserva.total as reserva_total')
                         ->join('reserva', 'reserva.id_reserva = pago.id_reserva')
                         ->join('usuario', 'usuario.id_usuario = reserva.id_usuario');

        if (!empty($q)) {
            $builder->groupStart()
                    ->like('usuario.nombres', $q)
                    ->orLike('usuario.apellidos', $q)
                    ->orLike('pago.referencia', $q)
                    ->orLike('pago.metodo', $q)
                    ->groupEnd();
        }

        $pagos = $builder->paginate(10, 'default');

        return view('admin/pagos/index', [
            'pagos' => $pagos,
            'pager' => $model->pager,
            'q'     => $q
        ]);
    }
}
