{% extends "advbase.html" %}
{% block advcontent -%}
<div class="pure-form pure-form-aligned">
    <fieldset>
        <legend><h3>{{ _("Deliver now") }}</h3></legend>
        <p><small>{{ _("Deliver selected books now.") }}</small></p>
        <div class="box-list">
            {% if (booksnum == 0 and not books) or (booksnum == 1 and books[0].builtin == False and books[0].feeds.count() == 0) %}
                <div class="box">{{ _("Do not have any subscribed book.") }}</div>
            {% endif %}
            {% if booksnum == 1 and books[0].builtin == False -%}
                {% for item in books[0].feeds %}
                <label for="{{ item.key().id() }}" class="pure-checkbox box">
                    <input id="{{ item.key().id() }}" bookid="{{ books[0].key().id() }}" type="checkbox" checked="1" onclick="UpdateDeliverFeedLink();" />
                    <span>{{item.title}}</span>
                </label>
                {% endfor %}
                {% if books[0].feeds.count() > 0 %}
                <div class="cornerControls" style="padding:.5em 0;text-align:center;">
                    <a href="#" class="actionButton act" onclick="SelectAll();UpdateDeliverFeedLink();return false;">{{ _("Select all") }}</a>
                    <a href="#" class="actionButton act" onclick="SelectNone();UpdateDeliverFeedLink();return false;">{{ _("Select none") }}</a>
                </div>
                {% endif %}
            {%- elif booksnum > 0 -%}
                {% for item in books %}
                <label for="{{ item.key().id() }}" class="pure-checkbox box">
                    <input id="{{ item.key().id() }}" type="checkbox" checked="1" onclick="UpdateDeliverLink();" />
                    <span>{{ item.title }}</span>
                </label>
                {% endfor %}
                <div class="cornerControls" style="padding:.5em 0;text-align:center;">
                    <a href="#" class="actionButton act" onclick="SelectAll();UpdateDeliverLink();return false;">{{ _("Select all") }}</a>
                    <a href="#" class="actionButton act" onclick="SelectNone();UpdateDeliverLink();return false;">{{ _("Select none") }}</a>
                </div>
            {%- endif %}
        </div>
        {% if booksnum > 0 and ((books[0].builtin == True and books) or ((books[0].builtin == False and books[0].feeds.count() >= 1) or booksnum >= 2)) %}
        <div style="text-align:center;">
            <a href="/deliver?u={{nickname}}" value="{{ _('Deliver') }}" id="deliverButton" class="pure-button pure-button-primary pure-input-rounded">{{ _('Deliver') }}</a>
        </div>
        {% endif %}
    </fieldset>
</div>
{% endblock -%}

{% block javascriptinhead %}
<script type="text/javascript">
    //Update link of deliver button for feeds (custom rss)
    function UpdateDeliverFeedLink() {
        var feedsId = [];
        var inputs = document.getElementsByTagName("input");
        var bookId = "";
        var deliverButton = document.getElementById("deliverButton");
        var inputsNum = inputs.length;
        for (var i = 0; i < inputsNum; i++) {
            if (bookId == "") {
                bookId = inputs[i].attributes["bookid"].value;
            }
            if ((inputs[i].type == "checkbox") && inputs[i].checked) {
                feedsId.push(inputs[i].attributes["id"].value);
            }
        }
        var feedsNum = feedsId.length;
        if ((feedsNum > 0) && (feedsNum != inputsNum)) {
            deliverButton.href = "/deliver?u={{nickname}}&id=" + bookId + "&feedsId=" + feedsId.join("|");
        } else {
            deliverButton.href = "/deliver?u={{nickname}}&id=" + bookId;
        }
    }

    //Update link of deliver button for books
    function UpdateDeliverLink() {
        var booksId = [];
        var inputs = document.getElementsByTagName("input");
        var deliverButton = document.getElementById("deliverButton");
        var inputsNum = inputs.length;
        for (var i = 0; i < inputsNum; i++) {
            if ((inputs[i].type == "checkbox") && inputs[i].checked) {
                booksId.push(inputs[i].attributes["id"].value);
            }
        }
        var booksNum = booksId.length;
        if ((booksNum > 0) && (booksNum != inputsNum)) {
            deliverButton.href = "/deliver?u={{nickname}}&id=" + booksId.join("|");
        } else {
            deliverButton.href = "/deliver?u={{nickname}}";
        }
    };

    function SelectAll() {
        var inputs = document.getElementsByTagName("input");
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].type == "checkbox") {
                inputs[i].checked = true;
            }
        }
    };

    function SelectNone() {
        var inputs = document.getElementsByTagName("input");
        var deliverButton = document.getElementById("deliverButton");
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].type == "checkbox") {
                inputs[i].checked = false;
            }
        }
    };
</script>
{% endblock %}
