<?php

namespace common\widgets;

use yii\base\Widget;
use yii\helpers\Html;

class CommentWidget extends Widget
{

    public $comments;
    public $reload;

    private function html ($d, $lvl)
    {
        echo '
        <li class="comment" level="' . $lvl . '">
        <div class="comment_body" comment_id="'. $d['id'] .'">
            <div class="comment_info">
                <span class="comment_username">' . $d['username'] . '</span>&nbsp;
                <span class="comment_created_time">' . date('F j Y h:i', strtotime($d['created_at'])) . '</span>
                <div class="dropdown">
                    <button class="btn btn-default btn-xs dropdown-toggle" type="button" id="" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                        Manage
                        <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
                        <li><a href="#">Edit</a></li>
                        <li><a href="#">Hide</a></li>
                        <li><a href="#">Delete</a></li>
                    </ul>
                </div>
            </div>
            <div class="message">
                <p>' . $d['content'] . '</p>
            </div>
            <button type="button" class="btn btn-default btn-xs btn-reply">Reply</button>
        </div>
        </li>';
    }

    private function display(array $comments, $level = 0)
    {
        foreach ($comments as $info)
        {
            echo '<ul>';

            $this->html($info, $level + 1);

            if (!empty($info['childs']))
            {
                $this->display($info['childs'], $level + 1);
            }

            echo '</ul>';
        }

        if (isset($this->reload) && $this->reload == false)
        {

        }

    }

    private function field()
    {
        echo '<div class="container new_comment">
                <div class="form-group">
                    <label for="comment">Comment:</label>
                    <textarea class="form-control" rows="5" id="comment"></textarea>
                    <button type="button" class="btn btn-info btn-md btn-send">Send</button>
                </div>
              </div>';
    }

    public function init()
    {

    }
    public function run()
    {
        echo '<div class="container" id="comment_wrapper">';
        $this->display($this->comments);
        echo '</div>';
        $this->field();
    }
}
