<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\SettingModel;

class ServiceFee extends BaseController
{
    public function index()
    {
        $settings = model(SettingModel::class);
        $serviceFee  = $settings->getValue('service_fee') ?? '0';
        $deliveryFee = $settings->getValue('delivery_fee') ?? '0';
        $discordWebhook = $settings->getValue('discord_webhook_url') ?? '';
        $content = view('admin/service_fee', [
            'service_fee'       => $serviceFee,
            'delivery_fee'      => $deliveryFee,
            'discord_webhook_url' => $discordWebhook,
        ]);
        return view('admin/layout', ['title' => 'Service Fee', 'content' => $content]);
    }

    public function save()
    {
        $settings = model(SettingModel::class);
        $fee = $this->request->getPost('service_fee');
        if ($fee !== null && is_numeric($fee)) {
            $settings->setValue('service_fee', (string) $fee);
        }
        $deliveryFee = $this->request->getPost('delivery_fee');
        if ($deliveryFee !== null && is_numeric($deliveryFee)) {
            $settings->setValue('delivery_fee', (string) $deliveryFee);
        }
        $webhook = $this->request->getPost('discord_webhook_url');
        if ($webhook !== null) {
            $settings->setValue('discord_webhook_url', trim((string) $webhook));
        }
        return redirect()->to(base_url('admin/service-fee'))->with('message', 'Settings saved.');
    }
}
