<?php namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\LoanProductModel;

class Products extends BaseController
{
    public function index()
    {
        $model = new LoanProductModel();
        $products = $model->findAll();
        echo view('layouts/admin_header', ['title'=>'Loan Products']);
        echo view('admin/products', compact('products'));
        echo view('layouts/admin_footer');
    }

    public function save()
    {
        $model = new LoanProductModel();
        $data = [
            'name'=>$this->request->getPost('name'),
            'slug'=>$this->request->getPost('slug'),
            'base_interest'=>$this->request->getPost('base_interest'),
            'processing_fee_pct'=>$this->request->getPost('processing_fee_pct'),
            'late_fee'=>$this->request->getPost('late_fee') ?: 0,
            'min_amount'=>$this->request->getPost('min_amount'),
            'max_amount'=>$this->request->getPost('max_amount'),
        ];
        if ($this->request->getPost('id')) {
            $model->update($this->request->getPost('id'), $data);
        } else {
            $model->insert($data);
        }
        return redirect()->to('/admin/products')->with('success','Product saved.');
    }
}
