<?php

namespace App\Controllers\Panel\App\Install;

use App\Controllers\Panel\BC;
use CodeIgniter\API\ResponseTrait;

class Main extends BC
{
    use ResponseTrait;
    public function index()
    {
        try {
            options('logo');
            return redirect()->to(dashboard_url());
        } catch (\Throwable $th) {
            return redirect()->to('panel/app/install/step/1');
        }
    }

    public function lc()
    {
        if (!file_exists(WRITEPATH . "cache/lct")) {
            file_put_contents(WRITEPATH . "cache/lct", time());
        }
        $l = options('licensekey');
        if ($l) {
            $valid = checkLicense($l);
            if ($valid['state'] == 2) return $this->respond("OK");
            if ($valid['state'] == 1) {
                try {

                    $data = [
                        [
                            'option_name' => 'npsn',
                            'option_value' => $valid['NPSN']
                        ],
                        [
                            'option_name' => 'nama_sekolah',
                            'option_value' => $valid['NamaSekolah']
                        ],
                        [
                            'option_name' => 'eat',
                            'option_value' => $valid['expired_at']
                        ],
                    ];
                    save_options($data);
                } catch (\Throwable $th) {
                }
                return $this->respond("OK");
            }
            if ($valid['state'] == 0) {
                try {

                    $data = [
                        [
                            'option_name' => 'npsn',
                            'option_value' => null
                        ],
                        [
                            'option_name' => 'nama_sekolah',
                            'option_value' => null
                        ],
                        [
                            'option_name' => 'licensekey',
                            'option_value' => null
                        ],
                    ];
                    save_options($data);
                } catch (\Throwable $th) {
                }
                return $this->respond("OK");
            }
            file_put_contents(WRITEPATH . "cache/lct", time());
        }
        return $this->respond("OK");
    }
}
