<?php
namespace wdigital\cms\inspinia\widgets;

use wdigital\cms\inspinia\assets\SlickAsset;
use Yii;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\helpers\Html;

/**
 * Class Menu
 * Theme menu widget.
 */
class SiteTree extends Widget
{
    /**
     * @inheritdoc
     */
    public function renderTree($children,$active_section_id, $expanded, $tree, $seperator = '') {
        $tree .= '<ul>';
        foreach($children as $child){
            $tree .= '<li id="'.$child->id.'"';
            if($child->type == 'html'){
                $tree .= 'data-href="'.Url::to(['/site-tree/html/edit', 'id' => $child->id]).'"';
            }else if($child->type == 'redirect'){
                $tree .= 'data-href="'.Url::to(['/site-tree/default/edit?id='.$child->id]).'"';
            }else if($child->type == 'gdpr'){
                $tree .= 'data-href="'.Url::to(['/site-tree/gdpr/edit', 'id' => $child->id]).'"';
            }
            $tree .= 'data-parent="'.$child->parent_id.'"><span class="pull-left">'.$child->name.'</span>';
            if(!empty($child->children)){
                $site_tree = new self();
                $tree .= $site_tree->renderTree($child->children, '',$child->id,$active_section_id,$expanded,$tree,$child);
            }
            $tree .='<div class="controls pull-right">';
            $tree .='<a class="redirect inline-block js-tooltip-button"';
            $tree .= 'title="'.Yii::t('section', 'Section settings').'" data-placement="top"';
            $tree .= 'href="'.Url::to(['edit', 'id'=>$child->id]).'">';
            $tree .= '<i class="fa fa-wrench fa-fw"></i>';
            $tree .= '</a>';
            $tree .='<a href="/site-tree/'.$child->id.'"></a>';
            $tree .= '<a href="#" class="dragHandle ui-sortable-handle js-tooltip-button" title="" data-placement="top">';
            $tree .= '<i class="fa fa-arrows fa-fw"></i>';
            $tree .= '</a>';
            $tree .= '<a href="'.Url::to(['delete', 'id'=>$child->id]).'"';
            $tree .= 'class="js-delete-button js-tooltip-button"';
            $tree .= 'data-title="'.Yii::t('section', 'Are you sure you?').'"';
            $tree .= 'data-placement="top"';
            $tree .= 'data-text="' .Yii::t('section', 'Are you sure you want to delete section ').Html::encode($child->name).Yii::t('section', ' and it\'s children? ').'">';
            $tree .= '<i class="fa fa-trash fa-fw"></i>';
            $tree .= '</a>';
            $tree .= '</div>';
            $tree .='</li>';
        }
        $tree .= '</ul>';
        return $tree;
    }

    public function renderTreePermissions($children,$active_section_id, $expanded, $tree, $seperator = '') {
        $tree .= '<ul>';
        foreach($children as $child){
            $tree .= '<li>';
            if($seperator != ''){
                $tree.= $seperator;
            }
            $tree.= $child->name;
            if(!empty($child->children)){
                $tree .= self::renderTree($child->children, '',$child->id,$active_section_id,$expanded,$tree,$child);
            }
            $tree .='</li>';
        }
        $tree .= '</ul>';
        return $tree;
    }
}
