<?php

namespace app\controllers;

use yii;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use app\models\AccountForm;

class DeauthenticationController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ]
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'index' => ['post'],
                ],
            ]
        ];
    }

    public function actionIndex()
    {
        // Инициализация
        yii::$app->response->format = Response::FORMAT_JSON;

        // Удаление сессии
        yii::$app->session['status'] = 'inactive';
        // yii::$app->session->close();

        // Выход из аккаунта
        yii::$app->user->logout();


        // Инициализация
        $model = new AccountForm(yii::$app->request->post('AccountForm') ?? yii::$app->request->get('AccountForm') ?? null);

        // Ответа
        return [
            'menu' => $this->renderPartial('/account/panel/deauthenticated', compact('model')),
            'main' => $this->renderPartial('/index'),
            'redirect' => '/',
            '_csrf' => yii::$app->request->getCsrfToken()
        ];
    }
}
