<?php

namespace App\Controllers\Superadmin;

use App\Controllers\BaseController;
use App\Models\CompanySettingModel;
use App\Models\ActivityLogModel;

class Setting extends BaseController
{
    protected $settingModel;

    protected $activityModel;

    public function __construct()
    {
        $this->settingModel = new CompanySettingModel();

        $this->activityModel = new ActivityLogModel();
    }

    /*
    |--------------------------------------------------------------------------
    | Pengaturan Sistem
    |--------------------------------------------------------------------------
    */

    public function index()
    {
        $setting = $this->settingModel->first();

        return view(
            'superadmin/setting/index',
            [
                'title' => 'Pengaturan Sistem',
                'setting' => $setting
            ]
        );
    }

   public function save() 
    {
        $setting = $this->settingModel->first();
        
        $data = [
            'company_name'  => $this->request->getPost('company_name'),
            'owner_name'    => $this->request->getPost('company_owner'), // Di sini diperbaiki
            'email'         => $this->request->getPost('email'),
            'phone'         => $this->request->getPost('phone'),
            'address'       => $this->request->getPost('address'),
            // Tambahkan kolom lainnya sesuai kebutuhan
        ];

        if ($setting) {
            $this->settingModel->update($setting['id_setting'], $data);
        } else {
            $this->settingModel->insert($data);
        }

        $this->activityModel->logActivity(session()->get('id_user'), 'Mengubah Pengaturan Sistem');
        
        return redirect()->back()->with('success', 'Pengaturan berhasil disimpan.');
    }

}