<?php

namespace App\Controllers\Client;

use App\Controllers\BaseController;
use App\Models\InvoiceItemModel;
use App\Models\InvoiceModel;

class InvoiceController extends BaseController
{
    public function index()
    {
        $model = new InvoiceModel();
        $invoices = $model->where('client_id', session()->get('client_id'))
            ->orderBy('due_date', 'DESC')->paginate(15);

        return view('client/invoices/index', ['invoices' => $invoices, 'pager' => $model->pager]);
    }

    public function show($id)
    {
        $invoice = (new InvoiceModel())->where('client_id', session()->get('client_id'))->find($id);
        if (! $invoice) return redirect()->to('/client/invoices');

        $items = (new InvoiceItemModel())->where('invoice_id', $id)->findAll();

        return view('client/invoices/show', compact('invoice', 'items'));
    }
}
