{% extends "layout_default.phtml" %}
{% import "macro_functions.phtml" as mf %}
{% block meta_title %}{% trans 'Staff' %}{% endblock %}
{% set active_menu = 'system' %}

{% block breadcrumbs %}
<ul>
    <li class="firstB"><a href="{{ '/'|alink }}">{% trans 'Home' %}</a></li>
    <li><a href="{{ 'system'|alink }}">{% trans 'Settings' %}</a></li>
    <li class="lastB">{% trans 'Staff' %}</li>
</ul>
{% endblock %}

{% block content %}
<div class="widget simpleTabs">

    <ul class="tabs">
        <li><a href="#tab-staff">{% trans 'Staff' %}</a></li>
        <li><a href="#tab-settings">{% trans 'Settings' %}</a></li>
        <li><a href="#tab-new">{% trans 'New staff member' %}</a></li>
        <li><a href="#tab-groups">{% trans 'Manage groups' %}</a></li>
        <li><a href="#tab-new-group">{% trans 'New group' %}</a></li>
    </ul>

    <div class="tabs_container">
        <div class="fix"></div>
        <div class="tab_content nopadding" id="tab-staff">
            
            {{ mf.table_search }}
            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td colspan="2">{% trans 'Name' %}</td>
                        <td>{% trans 'Email' %}</td>
                        <td>{% trans 'Group' %}</td>
                        <td>{% trans 'Status' %}</td>
                        <td style="width: 5%">&nbsp;</td>
                    </tr>
                </thead>
                
                <tbody>
                {% set members = admin.staff_get_list({"per_page":100, "page":request.page}|merge(request)) %}
                {% for i, member in members.list %}
                <tr>
                    <td style="width: 5%"><a href="{{ 'staff/manage'|alink }}/{{ member.id }}"><img src="{{ member.email|gravatar }}?size=25" alt="{{ member.id }}" /></a></td>
                    <td>{{ member.name }}</td>
                    <td>{{ member.email }}</td>
                    <td><a href="{{ '/staff/group'|alink }}/{{member.group.id}}">{{member.group.name}}</a></td>
                    <td>{{ mf.status_name(member.status) }}</td>
                    <td style="width: 13%" class="actions">
                        <a class="bb-button btn14" href="{{ '/staff/manage'|alink }}/{{member.id}}"><img src="images/icons/dark/pencil.png" alt=""></a>
                        <a class="bb-button btn14 bb-rm-tr api-link" data-api-confirm="Are you sure?" data-api-redirect="{{ 'staff'|alink }}" href="{{ 'api/admin/staff/delete'|link({'id' : member.id}) }}"><img src="images/icons/dark/trash.png" alt=""></a>
                    </td>
                </tr>
                </tbody>

                {% else %}
                <tbody>
                    <tr>
                        <td colspan="5">
                            {% trans 'The list is empty' %}
                        </td>
                    </tr>
                </tbody>
                {% endfor %}
            </table>

        </div>
        
        <div class="fix"></div>
        <div class="tab_content nopadding" id="tab-settings">
            {% set params = admin.extension_config_get({"ext":"mod_staff"}) %}
            <form method="post" action="{{ 'api/admin/extension/config_save'|link }}" class="mainForm api-form" data-api-msg="{% trans 'Configuration updated' %}">

                <fieldset>
                    <div class="rowElem noborder">
                        <label>{% trans 'Check login IP' %}</label>
                        <div class="formRight">
                            <input type="radio" name="check_ip" value="1" {% if params.check_ip == "1" %}checked="checked"{% endif %}/><label>{% trans 'Yes' %}</label>
                            <input type="radio" name="check_ip" value="0" {% if not params.check_ip %}checked="checked"{% endif %}/><label>{% trans 'No' %}</label>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div id="check_ip" {% if not params.check_ip %}style="display:none;"{% endif %}>
                        <div class="rowElem">
                            <label class="topLabel">{% trans 'Allowed IPs. One per line' %}</label>
                            <div class="formBottom">
                                <textarea name="allowed_ips" cols="5" rows="5" placeholder="{{ admin.system_env({"ip":1}) }}" />{{ params.allowed_ips }}</textarea>
                            </div>
                            <div class="fix"></div>
                        </div>
                    </div>

                    <input type="submit" value="{% trans 'Update' %}" class="greyishBtn submitForm" />
                    <input type="hidden" name="ext" value="mod_staff" />
                </fieldset>
            </form>

        </div>
        
        <div class="fix"></div>
        <div class="tab_content nopadding" id="tab-new">
            <form method="post" action="{{ 'api/admin/staff/create'|link }}" class="mainForm api-form" data-api-jsonp="onAfterStaffCreate">
                <fieldset>
                    <div class="rowElem noborder">
                        <label>{% trans 'Group' %}</label>
                        <div class="formRight">
                            {{ mf.selectbox('admin_group_id', admin.staff_group_get_pairs, '', 1) }}
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Name' %}</label>
                        <div class="formRight">
                            <input type="text" name="name" value="" required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Email' %}</label>
                        <div class="formRight">
                            <input type="email" name="email" value="{{ request.email }}" required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Password' %}</label>
                        <div class="formRight">
                            <input type="password" name="password" required="required" />
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Signature' %}</label>
                        <div class="formRight">
                            <textarea name="signature" cols="5" rows="2"></textarea>
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Status' %}:</label>
                        <div class="formRight">
                            <input type="radio" name="status" value="inactive" checked="checked"/><label>{% trans 'Inactive' %}</label>
                            <input type="radio" name="status" value="active"/><label>{% trans 'Active' %}</label>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <input type="submit" value="{% trans 'Create' %}" class="greyishBtn submitForm" />
                </fieldset>
            </form>
        </div>
        
        <div class="fix"></div>
        <div class="tab_content nopadding" id="tab-groups">

            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td>{% trans 'Name' %}</td>
                        <td style="width: 13%">&nbsp;</td>
                    </tr>
                </thead>
                <tbody>
                {% set groups = admin.staff_group_get_list({"per_page":100, "page":request.page}) %}
                {% for i, group in groups.list %}

                <tr>
                    <td>{{ group.name }}</td>
                    <td class="actions">
                        <a class="bb-button btn14" href="{{ '/staff/group'|alink }}/{{group.id}}"><img src="images/icons/dark/pencil.png" alt=""></a>
                        <a class="bb-button btn14 bb-rm-tr api-link" data-api-confirm="Are you sure?" data-api-redirect="{{ 'staff'|alink }}" href="{{ 'api/admin/staff/group_delete'|link({'id' : group.id}) }}"><img src="images/icons/dark/trash.png" alt=""></a>
                    </td>
                </tr>
                </tbody>

                {% else %}
                <tbody>
                    <tr>
                        <td colspan="5">
                            {% trans 'The list is empty' %}
                        </td>
                    </tr>
                </tbody>
                {% endfor %}
            </table>

        </div>

        <div class="fix"></div>
        <div class="tab_content nopadding" id="tab-new-group">
            <form method="post" action="{{ 'api/admin/staff/group_create'|link }}" class="mainForm save api-form" data-api-redirect="{{ 'extension/settings/staff'|alink }}">
                <fieldset>
                    <div class="rowElem noborder">
                        <label>{% trans 'Name' %}</label>
                        <div class="formRight">
                            <input type="text" name="name" value="" required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>
                    <input type="submit" value="{% trans 'Create group' %}" class="greyishBtn submitForm" />
                </fieldset>
            </form>
        </div>

    </div>
</div>

{% endblock %}

{% block js %}
<script type="text/javascript">
function onAfterStaffCreate(id) {
    bb.redirect("{{ 'staff/manage'|alink }}/" + id + '#tab-permissions');
}

$(function() {
    $('input[name=check_ip]').click(function(){
        if($(this).val() == 1) {
            $('#check_ip').slideDown();
        } else {
            $('#check_ip').slideUp();
        }
    });
});

</script>
{% endblock %}