<?php

/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace home\widgets;

use yii\helpers\Url;
use common\assets\ArtDialogAsset;

/**
 * Alert widget renders a message from session flash. All flash messages are displayed
 * in the sequence they were assigned using setFlash. You can set message as following:
 *
 * ```php
 * \Yii::$app->getSession()->setFlash('error', 'This is the message');
 * \Yii::$app->getSession()->setFlash('success', 'This is the message');
 * \Yii::$app->getSession()->setFlash('info', 'This is the message');
 * ```
 *
 * Multiple messages could be set as follows:
 *
 * ```php
 * \Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']);
 * ```
 *
 * @author Kartik Visweswaran <kartikv2@gmail.com>
 * @author Alexander Makarov <sam@rmcreative.ru>
 */
class Alert extends \yii\base\Widget {

	/**
	 * @var array the alert types configuration for the flash messages.
	 * This array is setup as $key => $value, where:
	 * - $key is the name of the session flash variable
	 * - $value is the bootstrap alert type (i.e. danger, success, info, warning)
	 */
	public $types = [
		'error' => 'alert-danger',
		'success' => 'alert-success',
		'danger' => 'alert-danger',
		'info' => 'alert-info',
		'warning' => 'alert-warning'
	];

    /**
     * @var array|string 要跳转的地址
     */
    public $to;

    public $from;

    /**
     * @var int 跳转秒数
     */
    public $closeTime = 4000;

    /**
     * @var int 跳转秒数
     */
    public $jumpTime = 3000;

	public function init() {
		parent::init();

		$session = \Yii::$app->getSession();
		$flashes = $session->getAllFlashes();
        $msg = '';

		foreach ($flashes as $type => $data) {
			if (isset($this->types[$type])) {
				$data = (array) $data;
				foreach ($data as $d) {
                    $msg .= $d;
				}
				$session->removeFlash($type);
			}
		}

		$view = $this->getView();

        $js = '';
        if (isset($flashes['success'])) {
            $js = <<<JS
                    art.dialog({
                        title: '成功提示',
                        time: 1.5,
                        icon: 'succeed',
                        content: '{$msg}',
                        close: function() {
                            var opener = art.dialog.opener;
                            var mainDialog = opener.art.dialog.list['main-dialog'];
                            if (mainDialog != undefined) {
                                mainDialog.close();
                            }
                            return true;
                        }
                    });
JS;
        }
        elseif (isset($flashes['error'])) {
            $js = <<<JS
                    art.dialog({
                        title: '失败提示',
                        time: 1.5,
                        icon: 'error',
                        content: '{$msg}'
                    });
JS;
        }

        if (!empty($this->from) && isset($flashes['success'])) {
            $js .= <<<JS
                    setTimeout(function () {
                        location.href = '{$this->from}';
                    }, {$this->jumpTime});
JS;
        }

        $view->registerJs($js, \yii\web\View::POS_END);


    }

}
