{% if best_choices is not iterable or best_choices is empty %}
    {% set best_choices = [0] %}
{% endif %}

<h3>
    {{__('Poll results', 'Votes of the poll')}} {% if hidden %}<i>({{__('PollInfo', 'Results are hidden')}})</i>{% endif %}
    {% if accessGranted %}
        <a href="" data-toggle="modal" data-target="#hint_modal"><i class="glyphicon glyphicon-info-sign"></i></a><!-- TODO Add accessibility -->
    {% endif %}
</h3>


{% include 'part/scroll_left_right.twig' %}


<div id="tableContainer" class="tableContainer">
    <form action="{% if admin %}{{ path('vote_poll_admin', {'admin_poll_id': poll.adminId}) }}{% else %}{{ path('vote_poll', {'poll_id': poll.id}) }}{% endif %}" method="POST" id="poll_form">
        <input type="hidden" name="control" value="{{ choices_hash }}"/>
        <table class="results">
            <caption class="sr-only">{{__('Poll results', 'Votes of the poll')}} {{poll.title|raw}}</caption>
            <thead>
            {% if admin and not expired %}
                <tr class="hidden-print">
                    <th role="presentation"></th>
                    {% for id, choice in poll.choices %}
                        <td headers="C{{ id }}">
                            <a href="{{ poll_url(poll.adminId, true, 'delete_column', choice.name)}}"
                               data-remove-confirmation="{{__('adminstuds', 'Confirm removal of the column.')}}"
                               class="btn btn-link btn-sm remove-column" title="{{__('adminstuds', 'Remove the column')}} {{ choice.name|raw}}">
                                <i class="glyphicon glyphicon-remove text-danger"></i><span class="sr-only">{{__('Generic', 'Remove')}}</span>
                            </a>
                            </td>
                    {% endfor %}
                    <td>
                        <a href="{{poll_url(poll.adminId, true, 'add_column')}}"
                           class="btn btn-link btn-sm" title="{{__('adminstuds', 'Add a column')}}">
                            <i class="glyphicon glyphicon-plus text-success"></i><span class="sr-only">{{__('Poll results', 'Add a column')}}</span>
                        </a>
                    </td>
                </tr>
            {% endif %}
            <tr>
                <th role="presentation"></th>
                {% for id, choice in poll.choices %}
                    <th class="bg-info" id="C{{ id }}" title="{{ choice.name }}">{{ choice.name | markdown | raw }}</th>
                {% endfor %}
                <th></th>
            </tr>
            </thead>
            <tbody>
            {% for vote in votes %}
                {# Edited line #}

                {% if editingVoteId == vote.uniqId and not expired %}

                <tr class="hidden-print">
                    <td class="bg-info btn-edit">
                        <div class="input-group input-group-sm" id="edit">
                            <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                            <input type="hidden" name="edited_vote" value="{{ vote.uniqId }}"/>
                            <input type="text" id="name" name="name" value="{{ vote.name }}" class="form-control" title="{{ __('Generic', 'Your name') }}" placeholder="{{__('Generic', 'Your name')}}" />
                        </div>
                    </td>

                    {% set id = 0 %}
                    {% for choice in poll.choices %}
                        {% set choice = vote.choices[id] %}

                        <td class="bg-info" headers="C{{ id }}">
                            <ul class="list-unstyled choice">
                                <li class="yes">
                                    <input type="radio" id="y-choice-{{ id }}" name="choices[{{ id }}]" value="2" {% if choice == '2' %}checked {% endif %}/>
                                    <label class="btn btn-default btn-xs" for="y-choice-{{ id }}" title="{{__('Poll results', 'Vote yes for')|raw}} {{ poll.choices[id].title|raw}}">
                                        <i class="glyphicon glyphicon-ok"></i><span class="sr-only">{{__('Generic', 'Yes')}}</span>
                                    </label>
                                </li>
                                <li class="ifneedbe">
                                    <input type="radio" id="i-choice-{{ id }}" name="choices[{{ id }}]" value="1" {% if choice == '1' %}checked {% endif %}/>
                                    <label class="btn btn-default btn-xs" for="i-choice-{{ id }}" title="{{__('Poll results', 'Vote ifneedbe for')|raw}} {{ poll.choices[id].title|raw}}">
(<i class="glyphicon glyphicon-ok"></i>)<span class="sr-only">{{__('Generic', 'Ifneedbe')}}</span>
                                    </label>
                                </li>
                                <li class="no">
                                    <input type="radio" id="n-choice-{{ id }}" name="choices[{{ id }}]" value="0" {% if choice == '0' %}checked {% endif %}/>
                                    <label class="btn btn-default btn-xs" for="n-choice-{{ id }}" title="{{__('Poll results', 'Vote no for')|raw}} {{ poll.choices[id].title|raw}}">
                                        <i class="glyphicon glyphicon-ban-circle"></i><span class="sr-only">{{__('Generic', 'No')}}</span>
                                    </label>
                                </li>
                                <li class="hide">
                                    <input type="radio" id="n-choice-{{ id }}" name="choices[{{ id }}]" value=" " {% if choice != '2' and choice != '1' and choice != '0' %}checked {% endif %}/>
                                </li>
                            </ul>
                        </td>

                        {% set id = id + 1 %}
                    {% endfor %}

                    <td class="btn-edit"><button type="submit" class="btn btn-success btn-xs" name="save" value="{{ vote.id }}" title="{{__('Poll results', 'Save the choices')}} {{ vote.name }}">{{__('Generic', 'Save')}}</button></td>
                </tr>
                {% elseif not hidden %} {# Voted line #}
                <tr>

                    <th class="bg-info">{{ vote.name|raw }}
					{% if active and not expired and accessGranted and
                        (
                        poll.editable == constant('Framadate\Editable::EDITABLE_BY_ALL')
                        or admin
                        or (poll.editable == constant('Framadate\Editable::EDITABLE_BY_OWN') and editedVoteUniqueId == vote.uniqId)
                        ) and
                         poll.choices > 4 %}
					<span class="edit-username-left">
						<a href="{% if admin %}{{ poll_url(poll.admin_id, vote.uniqId, true) }}{% else %}{{ poll_url(poll.id, vote.uniqId) }}{% endif %}" class="btn btn-default btn-sm" title="{{__('Poll results', 'Edit the line: %s', vote.name)|raw}}">
                    	<i class="glyphicon glyphicon-pencil"></i><span class="sr-only">{{__('Generic', 'Edit')}}</span>
                   	 	</a>
					</span>
					{% endif %}
					</th>

                    {% set id = 0 %}
                    {% for choice in poll.choices %}
                        {% set  choice = vote.choices[id] %}

                        {% if choice == '2' %}
                            <td class="bg-success text-success" headers="C{{ id }}">
                                <i class="glyphicon glyphicon-ok"></i>
                                <span class="sr-only">{{ __('Generic', 'Yes') }}</span>
                            </td>
                        {% elseif choice == '1' %}
                            <td class="bg-warning text-warning" headers="C{{ id }}">
                                (<i class="glyphicon glyphicon-ok"></i>)
                                <span class="sr-only">{{__('Generic', 'Ifneedbe')}}</span>
                            </td>
                        {% elseif choice == '0' %}
                            <td class="bg-danger text-danger" headers="C{{ id }}">
                                <i class="glyphicon glyphicon-ban-circle"></i>
                                <span class="sr-only">{{__('Generic', 'No')}}</span>
                            </td>
                        {% else %}
                            <td class="bg-info" headers="C{$id}"><span class="sr-only">{{ __('Generic', 'Unknown')}}</span></td>
                        {% endif %}

                        {% set id = id + 1 %}
                    {% endfor %}

                    {% if active and not expired and accessGranted and
                        (
                         poll.editable == constant('Framadate\\Entity\\Editable::EDITABLE_BY_ALL')
                         or admin
                         or (poll.editable == constant('Framadate\\Entity\\Editable::EDITABLE_BY_OWN') and editedVoteUniqueId == vote.uniqId)
                        )
                    %}

                        <td class="hidden-print">
                            <a href="{% if admin %}{{ poll_url(poll.admin_id, vote.uniqId, true) }}{% else %}{{ poll_url(poll.id, vote.uniqId) }}{% endif %}" class="btn btn-default btn-sm" title="{{__('Poll results', 'Edit the line: %s', vote.name)|raw}}">
                                <i class="glyphicon glyphicon-pencil"></i><span class="sr-only">{{__('Generic', 'Edit')}}</span>
                            </a>
                            {% if admin %}
                                <a href="{poll_url id=$poll->id vote_id=$vote->uniqId}" class="btn btn-default btn-sm clipboard-url" data-toggle="popover" data-trigger="manual" title="{__('Poll results', 'Link to edit this particular line')}" data-content="{__('Poll results', 'Link to edit this particular line has been copied!')}">
                                    <span class="btn-link glyphicon glyphicon-link"></span>
                                </a>
                                <a href="{{ poll_url(poll.adminId, true, 'delete_vote', vote.id) }}"
                                   class="btn btn-default btn-sm"
                                   title="{{__('Poll results', 'Remove the line:')}} {{ vote.name|raw}}">
                                    <i class="glyphicon glyphicon-remove text-danger"></i><span class="sr-only">{{__('Generic', 'Remove')}}</span>
                                </a>
                            {% endif %}
                        </td>
                    {% else %}
                        <td></td>
                    {% endif %}
                </tr>
                {% endif %}
            {% endfor %}

            {# Line to add a new vote #}

            {% if active and editingVoteId == 0 and not expired and accessGranted %}
                <tr id="vote-form" class="hidden-print">
                    <td class="bg-info btn-edit">
                        <div class="input-group input-group-sm">
                            <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                            <input type="text" id="name" name="name" class="form-control" title="{__('Generic', 'Your name')}" placeholder="{{__('Generic', 'Your name')}}" />
                        </div>
                    </td>
                    {% for id, choice in poll.choices %}
                        <td class="bg-info" headers="C{{ id }}">
                            <ul class="list-unstyled choice">
                                {% if poll.ValueMax is null or poll.ValueMax is same as(0) or best_choices.y | first < poll.ValueMax %}
                               	 	<li class="yes">
                                    	<input type="radio" id="y-choice-{{ id }}" name="choices[{{ id }}]" value="2" />
                                    	<label class="btn btn-default btn-xs" for="y-choice-{{ id }}" title="{{__('Poll results', 'Vote yes for')|raw}} {{choice.name | raw}}">
                                       		<i class="glyphicon glyphicon-ok"></i><span class="sr-only">{{__('Generic', 'Yes')}}</span>
                                    	</label>
                                	</li>
                               		 <li class="ifneedbe">
                                  	  <input type="radio" id="i-choice-{{ id }}" name="choices[{{ id }}]" value="1" />
                                  	  <label class="btn btn-default btn-xs" for="i-choice-{{ id }}" title="{{__('Poll results', 'Vote ifneedbe for')|raw}} {{ choice.name|raw}}">
                                        (<i class="glyphicon glyphicon-ok"></i>)<span class="sr-only">{{__('Generic', 'Ifneedbe')}}</span>
                                    	</label>
                               	 	</li>
								{% endif %}
                                <li class="no">
                                    <input type="radio" id="n-choice-{{ id }}" name="choices[{{ id }}]" value="0" />
                                    <label class="btn btn-default btn-xs startunchecked" for="n-choice-{{ id }}" title="{{__('Poll results', 'Vote no for')|raw}} {{ choice.name|raw}}">
                                        <i class="glyphicon glyphicon-ban-circle"></i><span class="sr-only">{{__('Generic', 'No')}}</span>
                                    </label>
                                </li>
                                <li class="hide">
                                  <input type="radio" id="n-choice-{{ id }}" name="choices[{{ id }}]" value=" " checked/>
                                </li>
                            </ul>
                        </td>

                    {% endfor %}
                    <td><button type="submit" class="btn btn-success btn-md" name="save" title="{{__('Poll results', 'Save the choices')}}">{{__('Generic', 'Save')}}</button></td>
                </tr>
            {% endif %}

            {% if not hidden %}
                {# Line displaying best moments #}
                {% set count_bests = 0 %}
                {% set max = max(best_choices['y']) %}
                {% if max > 0 %}
                    <tr id="addition">
                        <td>{{__('Poll results', 'Addition')}}<br/>{{ votes|length }} {% if (votes|length) == 1 %}{{__('Poll results', 'polled user')}}{% else %}{{__('Poll results', 'polled users')}}{% endif %}</td>
                        {% for i, best_choice in best_choices['y'] %}
                            {% if max == best_choice %}
                                {% set count_bests = count_bests +1 %}
                                <td><i class="glyphicon glyphicon-star text-info"></i><span class="yes-count">{{ best_choice | raw }}</span>
                                    {% if best_choices['inb'][i] > 0 %}
                                        <br/>
                                        <span class="small text-muted">(+
                                            <span class="inb-count">{{ best_choices['inb'][i] | raw }}</span>)
                                        </span>
                                    {% endif %}
                                </td>
                            {% elseif best_choice > 0 %}
                                <td>
                                    <span class="yes-count">{{ best_choice | raw }}</span>
                                    {% if best_choices['inb'][i] > 0 %}
                                        <br/>
                                        <span class="small text-muted">(+
                                            <span class="inb-count">{{ best_choices['inb'][i]|raw }}</span>)
                                        </span>
                                    {% endif %}
                                </td>
                            {% elseif best_choices['inb'][i] > 0 %}
                                <td>
                                    <br/>
                                    <span class="small text-muted">(+
                                        <span class="inb-count">{{ best_choices['inb'][i]|raw }}</span>)
                                    </span>
                                </td>
                            {% else %}
                                <td></td>
                            {% endif %}
                        {% endfor %}
                    </tr>
                {% endif %}
            {% endif %}
            </tbody>
        </table>
    </form>
</div>

{% if not hidden and max > 0 %}
    <div class="row" aria-hidden="true">
        <div class="col-xs-12">
            <p class="text-center" id="showChart">
                <button class="btn btn-lg btn-default">
                    <span class="fa fa-fw fa-bar-chart"></span> {{__('Poll results', 'Display the chart of the results')}}
                </button>
            </p>
        </div>
    </div>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#showChart').on('click', function() {
                $('#showChart')
                        .after("<h3>{{__('Poll results', 'Chart')}}</h3><canvas id=\"Chart\"></canvas>")
                        .remove();

                var resIfneedbe = [];
                var resYes = [];

                $('#addition').find('td').each(function () {
                    var inbCountText = $(this).find('.inb-count').text();
                    if(inbCountText != '' && inbCountText != undefined) {
                        resIfneedbe.push($(this).find('.inb-count').html())
                    } else {
                        resIfneedbe.push(0);
                    }

                    var yesCountText = $(this).find('.yes-count').text();
                    if(yesCountText != '' && yesCountText != undefined) {
                        resYes.push($(this).find('.yes-count').html())
                    } else {
                        resYes.push(0);
                    }
                });
                var cols = [
                {% for id, choice in poll.choices %}
                    $('<div/>').html('{{ choice.name | markdown | raw }}').text(),
                {% endfor %}
                ];

                resIfneedbe.shift();
                resYes.shift();

                var barChartData = {
                    labels : cols,
                    datasets : [
                    {
                        label: "{{__('Generic', 'Ifneedbe')}}",
                        fillColor : "rgba(255,207,79,0.8)",
                        highlightFill: "rgba(255,207,79,1)",
                        barShowStroke : false,
                        data : resIfneedbe
                    },
                    {
                        label: "{{__('Generic', 'Yes')}}",
                        fillColor : "rgba(103,120,53,0.8)",
                        highlightFill : "rgba(103,120,53,1)",
                        barShowStroke : false,
                        data : resYes
                    }
                    ]
                };

                var ctx = document.getElementById("Chart").getContext("2d");
                window.myBar = new Chart(ctx).StackedBar(barChartData, {
                    responsive : true
                });
                return false;
            });
        });
    </script>

{% endif %}



{% if not hidden %}
    {# Best votes listing #}
    {% set max = max(best_choices['y']) %}
    {% if max > 0 %}
        <div class="row">
        {% if count_bests == 1 %}
            <div class="col-sm-12">
                <h3>{{ __('Poll results', 'Best choice')}}</h3>
            </div>
            <div class="col-sm-6 col-sm-offset-3 alert alert-info">
                <p>
                    <i class="glyphicon glyphicon-star text-info"></i>
                    {{__('Poll results', 'The best choice at this time is:')}}
                </p>
        {% elseif count_bests > 1 %}
            <div class="col-sm-12">
                <h3>{{__('Poll results', 'Best choices')}}</h3>
            </div>
            <div class="col-sm-6 col-sm-offset-3 alert alert-info">
                <p>
                    <i class="glyphicon glyphicon-star text-info"></i>
                    {{__('Poll results', 'The bests choices at this time are:')}}
                </p>
        {% endif %}


        {% set i = 0 %}
        <ul class="list-unstyled">
            {% for choice in poll.choices %}
                {% if best_choices['y'][i] == max %}
                    <li>{{ choice.name | markdown | raw }}</li>
                {% endif %}
                {% set i = i + 1 %}
            {% endfor %}
        </ul>
        <p>{% spaceless %}
            {{__('Generic', 'with')}}
            <b>{{ max|raw }}</b>
                {% if max == 1 %}{{__('Generic', 'vote')}}{% else %}{{__('Generic', 'votes')}}{% endif %}.
        {% endspaceless %}</p>
        </div>
            </div>
    {% endif %}
{% endif %}
