{% extends "layout_default.phtml" %}

{% import "macro_functions.phtml" as mf %}

{% block meta_title %}{{ 'Staff'|trans }}{% endblock %}
{% set active_menu = 'system' %}

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

{% block content %}
<div class="widget simpleTabs">
    <ul class="tabs">
        <li><a href="#tab-staff">{{ 'Staff'|trans }}</a></li>
        <li><a href="#tab-settings">{{ 'Settings'|trans }}</a></li>
        <li><a href="#tab-new">{{ 'New staff member'|trans }}</a></li>
        <li><a href="#tab-groups">{{ 'Manage groups'|trans }}</a></li>
        <li><a href="#tab-new-group">{{ 'New group'|trans }}</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">{{ 'Name'|trans }}</td>
                        <td>{{ 'Email'|trans }}</td>
                        <td>{{ 'Group'|trans }}</td>
                        <td>{{ 'Status'|trans }}</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">
                            {{ 'The list is empty'|trans }}
                        </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="{{ 'Configuration updated'|trans }}">

                <fieldset>
                    <div class="rowElem noborder">
                        <label>{{ 'Check login IP'|trans }}</label>
                        <div class="formRight">
                            <input type="radio" name="check_ip" value="1" {% if params.check_ip == "1" %}checked="checked"{% endif %}/><label>{{ 'Yes'|trans }}</label>
                            <input type="radio" name="check_ip" value="0" {% if not params.check_ip %}checked="checked"{% endif %}/><label>{{ 'No'|trans }}</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">{{ 'Allowed IPs. One per line'|trans }}</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="{{ 'Update'|trans }}" 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>{{ 'Group'|trans }}</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>{{ 'Name'|trans }}</label>
                        <div class="formRight">
                            <input type="text" name="name" value="" required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>

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

                    <input type="submit" value="{{ 'Create'|trans }}" 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>{{ 'Name'|trans }}</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">
                            {{ 'The list is empty'|trans }}
                        </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>{{ 'Name'|trans }}</label>
                        <div class="formRight">
                            <input type="text" name="name" value="" required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>
                    <input type="submit" value="{{ 'Create group'|trans }}" 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 %}