<?php
    namespace frontend\widgets;
    
    use yii\base\Widget;
    use yii\helpers\Html;
    use frontend\assets\ArtboxModalAsset;
    
    class ArtboxModalWidget extends Widget
    {
        
        public $modalTagOptions = [
            'class' => '',
        ];
    
        public $headerTagOptions = [
            'class' => '',
        ];
    
        public $titleTagOptions = [
            'class' => '',
        ];
        
        public $headerText = '';
        
        public $bodyTagOptions = [
            'class' => 'modal-body',
        ];
        
        
        
        public function init()
        {
            parent::init();
    
            $this->modalTagOptions[ 'class' ] = ($this->modalTagOptions[ 'class' ]??"") . " forms_ ";
            $this->headerTagOptions[ 'class' ] = ($this->headerTagOptions[ 'class' ]??"") . " modal-header";
            $this->titleTagOptions[ 'class' ] = ($this->titleTagOptions[ 'class' ]??"") . " modal-title";
            $this->bodyTagOptions[ 'class' ] = ($this->bodyTagOptions[ 'class' ]??"") . " modal-body";
    
            ob_start();
        }
        
        public function run()
        {
            
            $content = ob_get_clean();
            
            $closeButton = Html::button(
                "×",
                [
                    'class' => 'close',
                ]
            );
            $modalHeader = Html::tag(
                'div',
                $closeButton .
                Html::tag(
                    'h3',
                    $this->headerText,
                    $this->titleTagOptions
                ),
                $this->headerTagOptions
            );
            
            $modalBody = Html::tag(
                'div',
                $content,
                $this->bodyTagOptions
            );
            
            $formWrapper = Html::tag(
                "div",
                $modalHeader . "\n" . $modalBody,
                [
                    'class' => 'forms_wr_'
                ]
            );
            
            $result = Html::tag(
                "div",
                $formWrapper . "<div class='overlay-new'></div>",
                $this->modalTagOptions
            );
            
            $this->AssetRegister();
            
            return $result;
            
        }
        
        private function AssetRegister(){
            
            ArtboxModalAsset::register($this->getView());
            
        }
    
    }