{% extends '../layout/admin.html' %}

{% block html_title %}{{ customTitle(t('user_management.User management')) }}{% endblock %}

{% block content_header %}
<div class="header-wrap">
  <header id="page-header">
    <h1 id="admin-title" class="title">{{ t('user_management.User management') }}</h1>
  </header>
</div>
{% endblock %}

{% block content_main %}
<div class="content-main">
  {% set smessage = req.flash('successMessage') %}
  {% if smessage.length %}
  <div class="alert alert-success">
    {{ smessage }}
  </div>
  {% endif %}

  {% set emessage = req.flash('errorMessage') %}
  {% if emessage.length %}
  <div class="alert alert-danger">
    {{ emessage }}
  </div>
  {% endif %}

  <div class="row">
    <div class="col-md-3">
      {% include './widget/menu.html' with {current: 'user'} %}
    </div>

    <div class="col-md-9">
      <p>
        <button data-toggle="collapse" class="btn btn-default" href="#inviteUserForm" {% if isUserCountExceedsUpperLimit %}disabled{% endif %}>
          {{ t("user_management.invite_users") }}
        </button>
        <a class="btn btn-default btn-outline" href="/admin/users/external-accounts">
          <i class="icon-user-follow" aria-hidden="true"></i>
          {{ t("user_management.external_account") }}
        </a>
      </p>
      <form role="form" action="/admin/user/invite" method="post">
        <div id="inviteUserForm" class="collapse">
          <div class="form-group">
            <label for="inviteForm[emailList]">メールアドレス (複数行入力で複数人招待可能)</label>
            <textarea class="form-control" name="inviteForm[emailList]" placeholder="例: user@growi.org"></textarea>
          </div>
          <div class="checkbox checkbox-info">
            <input type="checkbox" id="inviteWithEmail" name="inviteForm[sendEmail]" checked>
            <label for="inviteWithEmail">招待をメールで送信</label>
          </div>
          <button type="submit" class="btn btn-primary">招待する</button>
        </div>
        <input type="hidden" name="_csrf" value="{{ csrf() }}">
      </form>

      {% if isUserCountExceedsUpperLimit === true %}
      <label>{{ t('user_management.cannot_invite_maximum_users') }}</label>
      {% endif %}
      {% if userUpperLimit !== 0 %}
      <label>{{ t('user_management.current users') }}{{ activeUsers }}</label>
      {% endif %}

      {% set createdUser = req.flash('createdUser') %}
      {% if createdUser.length %}
      <div class="modal fade in" id="createdUserModal">
        <div class="modal-dialog">
          <div class="modal-content">

            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
              <div class="modal-title">ユーザーを招待しました</div>
            </div>

            <div class="modal-body">
              <p>
                作成したユーザーは仮パスワードが設定されています。<br>
                仮パスワードはこの画面を閉じると二度と表示できませんのでご注意ください。<span class="text-danger">招待メールを送っていない場合、この画面で必ず仮パスワードをコピーし、招待者へ連絡してください。</span>
              </p>

              <pre>{% for cUser in createdUser %}{% if cUser.user %}{{ cUser.email }} {{ cUser.password }}<br>{% else %}{{ cUser.email }} 作成失敗<br>{% endif %}{% endfor %}</pre>
            </div>

          </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
      </div><!-- /.modal -->
      {% endif %}

      {# FIXME とりあえずクソ実装。React化はやくしたいなー(チラッチラッ #}
      <div class="modal fade" id="admin-password-reset-modal">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
              <div class="modal-title">パスワードを新規発行しますか?</div>
            </div>

            <div class="modal-body">
              <p>
              新規発行したパスワードはこの画面を閉じると二度と表示できませんのでご注意ください。<br>
              <span class="text-danger">新規発行したパスワードを、対象ユーザーへ連絡してください。</span>
              </p>
              <p>
              Reset user: <code id="admin-password-reset-user"></code>
              </p>

              <form method="post" id="admin-users-reset-password">
                <input type="hidden" name="user_id" value="">
                <input type="hidden" name="_csrf" value="{{ csrf() }}">
                <button type="submit" value="" class="btn btn-primary">
                  実行
                </button>
              </form>

            </div>

          </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
      </div>
      <div class="modal fade" id="admin-password-reset-modal-done">
        <div class="modal-dialog">
          <div class="modal-content">

            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
              <div class="modal-title">Password reset!</div>
            </div>

            <div class="modal-body">
              <p class="alert alert-danger">Let the user know the new password below and strongly recommend to change another one immediately. </p>
              <p>
              Reset user: <code id="admin-password-reset-done-user"></code>
              </p>
              <p>
              New password: <code id="admin-password-reset-done-password"></code>
              </p>
            </div>
            <div class="modal-footer">
              <button class="btn btn-primary" data-dismiss="modal">OK</button>
            </div>
          </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
      </div>

      <h2>{{ t("user_management.user_list") }}</h2>

      <table class="table table-default table-bordered table-user-list">
        <thead>
          <tr>
            <th width="100px">#</th>
            <th>Status</th>
            <th><code>username</code></th>
            <th>{{ t('Name') }}</th>
            <th>{{ t('Email') }}</th>
            <th width="100px">{{ t('user_management.Date created') }}</th>
            <th width="150px">{{ t('user_management.Last login') }}</th>
            <th width="70px"></th>
          </tr>
        </thead>
        <tbody>
          {% for sUser in users %}
          {% set sUserId = sUser._id.toString() %}
          <tr>
            <td>
              <img src="{{ sUser|picture }}" class="picture img-circle" />
              {% if sUser.admin %}
              <span class="label label-inverse label-admin">
                Admin
              </span>
              {% endif %}
            </td>
            <td>
              <span class="label {{ css.userStatus(sUser) }}">
                {{ consts.userStatus[sUser.status] }}
              </span>
            </td>
            <td>
              <strong>{{ sUser.username }}</strong>
            </td>
            <td>{{ sUser.name }}</td>
            <td>{{ sUser.email }}</td>
            <td>{{ sUser.createdAt|date('Y-m-d', sUser.createdAt.getTimezoneOffset()) }}</td>
            <td>
              {% if sUser.lastLoginAt %}
                {{ sUser.lastLoginAt|date('Y-m-d H:i', sUser.createdAt.getTimezoneOffset()) }}
              {% endif %}
            </td>
            <td>
              <div class="btn-group admin-user-menu">
                <button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
                  <i class="icon-settings"></i> <span class="caret"></span>
                </button>
                <ul class="dropdown-menu" role="menu">
                  <li class="dropdown-header">{{ t('user_management.Edit menu') }}</li>
                  <li>
                    <a href="#"
                        data-user-id="{{ sUserId }}"
                        data-user-email="{{ sUser.email }}"
                        data-target="#admin-password-reset-modal"
                        data-toggle="modal">
                      <i class="icon-fw icon-key"></i>
                      {{ t('user_management.Reissue password') }}
                    </a>
                  </li>
                  <li class="divider"></li>
                  <li class="dropdown-header">{{ t('user_management.Status') }}</li>

                  {% if sUser.status == 1 %}
                  <form id="form_activate_{{ sUserId }}" action="/admin/user/{{ sUserId }}/activate" method="post">
                    <input type="hidden" name="_csrf" value="{{ csrf() }}">
                  </form>
                  <li>
                    <a href="javascript:form_activate_{{ sUserId }}.submit()">
                      <i class="icon-fw icon-user-following"></i> 承認する
                    </a>
                  </li>
                  {% endif  %}

                  {% if sUser.status == 2 %}
                  <form id="form_suspend_{{ sUserId }}" action="/admin/user/{{ sUserId }}/suspend" method="post">
                    <input type="hidden" name="_csrf" value="{{ csrf() }}">
                  </form>
                  <li>
                    {% if sUser.username != user.username %}
                    <a href="javascript:form_suspend_{{ sUserId }}.submit()">
                      <i class="icon-fw icon-ban"></i>
                      {{ t('user_management.Deactivate account') }}
                    </a>
                    {% else %}
                    <a disabled>
                      <i class="icon-fw icon-ban"></i>
                      {{ t('user_management.Deactivate account') }}
                    </a>
                    <p class="alert alert-danger m-l-10 m-r-10 p-10">{{ t("user_management.your_own") }}</p>
                    {% endif %}
                  </li>
                  {% endif %}

                  {% if sUser.status == 3 %}
                  <form id="form_activate_{{ sUserId }}" action="/admin/user/{{ sUserId }}/activate" method="post">
                    <input type="hidden" name="_csrf" value="{{ csrf() }}">
                  </form>
                  <form id="form_remove_{{ sUserId }}" action="/admin/user/{{ sUserId }}/remove" method="post">
                    <input type="hidden" name="_csrf" value="{{ csrf() }}">
                  </form>
                  <li>
                    <a href="javascript:form_activate_{{ sUserId }}.submit()">
                      <i class="icon-fw icon-action-redo"></i> 元に戻す
                    </a>
                  </li>
                  <li>
                    {# label は同じだけど、こっちは論理削除 #}
                    <a href="javascript:form_remove_{{ sUserId }}.submit()">
                      <i class="icon-fw icon-fire text-danger"></i> 削除する
                    </a>
                  </li>
                  {% endif  %}

                  {% if sUser.status == 1 || sUser.status == 5 %}
                  <form id="form_removeCompletely_{{ sUserId }}" action="/admin/user/{{ sUser._id.toString() }}/removeCompletely" method="post">
                    <input type="hidden" name="_csrf" value="{{ csrf() }}">
                  </form>
                  <li class="dropdown-button">
                    {# label は同じだけど、こっちは物理削除 #}
                    <a href="javascript:form_removeCompletely_{{ sUserId }}.submit()">
                      <i class="icon-fw icon-fire text-danger"></i> 削除する
                    </a>
                  </li>
                  {% endif  %}

                  {% if sUser.status == 2 %} {# activated な人だけこのメニューを表示 #}
                  <li class="divider"></li>
                  <li class="dropdown-header">{{ t('user_management.Administrator menu') }}</li>

                  {% if sUser.admin %}
                  <form id="form_removeFromAdmin_{{ sUserId }}" action="/admin/user/{{ sUser._id.toString() }}/removeFromAdmin" method="post">
                    <input type="hidden" name="_csrf" value="{{ csrf() }}">
                  </form>
                  <li>
                    {% if sUser.username != user.username %}
                      <a href="javascript:form_removeFromAdmin_{{ sUserId }}.submit()">
                        <i class="icon-fw icon-user-unfollow"></i> 管理者からはずす
                      </a>
                    {% else %}
                      <a disabled>
                        <i class="icon-fw icon-user-unfollow"></i> 管理者からはずす
                      </a>
                      <p class="alert alert-danger m-l-10 m-r-10 p-10">{{ t("user_management.cannot_remove") }}</p>
                    {% endif %}
                  </li>
                  {% else %}
                  <form id="form_makeAdmin_{{ sUserId }}" action="/admin/user/{{ sUser._id.toString() }}/makeAdmin" method="post">
                    <input type="hidden" name="_csrf" value="{{ csrf() }}">
                  </form>
                  <li>
                    <a href="javascript:form_makeAdmin_{{ sUserId }}.submit()">
                      <i class="icon-fw icon-magic-wand"></i> 管理者にする
                    </a>
                  </li>
                  {% endif %}

                  {% endif %}
                </ul>
              </div>
            </td>
          </tr>
          {% endfor %}
        </tbody>
      </table>

      {% include '../widget/pager.html' with {path: "/admin/users", pager: pager} %}

    </div>
  </div>
</div>
{% endblock content_main %}

{% block content_footer %}
{% endblock content_footer %}


