<?php

namespace App\Controllers\View;
use App\Controllers\BaseController;
use App\Models\SettingsModel;

class SettingsController extends BaseController
{
    protected $settingsModel;

    public function __construct()
    {
        $this->settingsModel = new SettingsModel();
    }

    public function index()
    {
        // Retrieve current settings from the database
        $settings = $this->settingsModel->getSettings();
        $timezones = timezone_identifiers_list();
        
        // Pass the settings and timezones to the view
        return view('administrator/settings', ['settings' => $settings, 'timezones' => $timezones]);
    }

    public function update()
    {
        // Handle the form submission to update settings
        $data = [
            'site_name' => $this->request->getPost('site_name'),
            'administrator_email' => strtolower($this->request->getPost('administrator_email')),
            'timezone' => $this->request->getPost('timezone'),
        ];
        $this->settingsModel->updateSettings($data);
        return redirect()->to('/administrator/settings');
    }
}
?>