<?php

namespace App\Controllers;

use App\Models\RewarderTaxModel;

class Taxes extends AdminBaseController
{
    public $title = 'Taxes';
    public $menu = 'settings';
    public $submenu = 'taxes';

    public function index()
    {
        $this->permissionCheck('taxes_list');
        $rows = (new RewarderTaxModel())->orderBy('id', 'desc')->findAll();
        return view('admin/taxes/list', compact('rows'));
    }

    public function add()
    {
        $this->permissionCheck('taxes_add');
        return view('admin/taxes/add');
    }

    public function save()
    {
        $this->permissionCheck('taxes_add');
        postAllowed();
        (new RewarderTaxModel())->create([
            'name' => post('name'),
            'rate_type' => post('rate_type'),
            'rate' => post('rate'),
            'status' => (int) post('status'),
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ]);
        return redirect()->to('taxes')->with('notifySuccess', 'Tax created successfully');
    }

    public function edit($id)
    {
        $this->permissionCheck('taxes_edit');
        $row = (new RewarderTaxModel())->getById($id);
        return view('admin/taxes/edit', compact('row'));
    }

    public function update($id)
    {
        $this->permissionCheck('taxes_edit');
        postAllowed();
        (new RewarderTaxModel())->update($id, [
            'name' => post('name'),
            'rate_type' => post('rate_type'),
            'rate' => post('rate'),
            'status' => (int) post('status'),
            'updated_at' => date('Y-m-d H:i:s'),
        ]);
        return redirect()->to('taxes')->with('notifySuccess', 'Tax updated successfully');
    }

    public function delete($id)
    {
        $this->permissionCheck('taxes_delete');
        (new RewarderTaxModel())->delete($id);
        return redirect()->to('taxes')->with('notifySuccess', 'Tax deleted successfully');
    }
}

