<?php

namespace App\Controllers;

class Token extends BaseController
{
    private $token;

    public function __construct()
    {
        $this->token = env('APP_TOKEN');
    }

    /**
     * Halaman Token
     */
    public function index()
    {
        $data = [
            'menu' => $this->request->getGet('menu')
        ];

        return view('token', $data);
    }

    /**
     * Cek Token
     */
    public function cek()
    {
        $inputToken = $this->request->getPost('token');
        $menu       = $this->request->getGet('menu');

        if ($inputToken == $this->token) {

            session()->set('akses_input', true);

            // Redirect sesuai menu
            switch ($menu) {

                case 'penghargaan':
                    return redirect()->to('/InputPenghargaan');

                case 'pelanggaran':
                default:
                    return redirect()->to('/InputPelanggaran');
            }
        }

        return redirect()->back()->with('error', 'Token yang Anda masukkan salah.');
    }

    /**
     * Logout Token
     */
    public function logout()
    {
        session()->destroy();

        return redirect()->to('/');
    }
}
