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

{% set menu = 'admin_app_center' %}

{% set script_controller = 'app/center' %}

{% block main %}
  <div class="mbl btn-group js-upgrades-count" data-url="{{ path('admin_app_upgrades_count') }}" >
    <a href="{{ url('admin_app_center', {postStatus: 'all' }) }}" type="button"
       class="btn btn-default btn-sm {% if type == 'all' %} btn-primary{% endif %}">{{ 'admin.app_center.type.all'|trans }}</a>
    <a href=" {{ url('admin_app_center', {postStatus: 'theme' }) }}" type="button"
       class="btn btn-default btn-sm {% if type == 'theme' %}btn-primary{% endif %}">{{ 'admin.app_center.type.theme'|trans }}</a>
    <a href=" {{ url('admin_app_center', {postStatus: 'app' }) }}" type="button"
       class="btn btn-default btn-sm {% if type == 'app' %}btn-primary{% endif %}">{{ 'admin.app_center.type.app'|trans }}</a>
  </div>

  <div class="checkbox-group appTypeChoices" RepeatDirection="Horizontal" id="appTypeChoices" name="appTypeChoices"
       style="float:right;margin-top:-45px;">
    <div class="sortedCourses">
      <input type="checkbox" name="appTypeChoices" id="installedApps"
             value="installedApps" {% if appTypeChoices|default(null)  == 'installedApps' %} data-url="{{ path('admin_app_center',{postStatus: type,showType:'unhidden'}) }}"  checked {% else %} data-url="{{ path('admin_app_center',{postStatus: type,showType:'hidden'}) }}" {% endif %}>
      {{ 'admin.app_center.buyed_app_hidden'|trans }}
      <input type="hidden" name="type" value='{{ type }}' id="type">
    </div>
  </div>

  {% if status|default(null) == 'error' %}
    <div class="alert alert-danger">{{ 'admin.app_center.error_tips'|trans({'%cloudSettingUrl%': path('admin_setting_cloud')})|raw }}</div>
  {% elseif  status|default(null) == 'unlink' %}
    <p class="alert alert-danger">
      {{'admin.edu_cloud.connect_failed_tips'|trans({'%EduSoho%': 'EduSoho'|copyright_less })}}
    </p>
  {% else %}
    <table class="table table-striped table-hover" id="app-table-container" data-install-package-id="{{ app.request.query.get('installPackageId') | default('') }}">
      <thead>

      </thead>
      <tbody>

      {% if type == 'all' %}
        {% set apps = apps %}
      {% elseif type == 'theme' %}
        {% set apps = theme %}
      {% elseif type == 'app' %}
        {% set apps = allApp %}
      {% endif %}

      {% for app in apps %}
        {% set installedApp = installedApps[app.code]|default(null) %}

        {% if appTypeChoices|default(null)  == 'installedApps' %}

          {% if app.purchased|default(false) == true or installedApp %}

            {% if installedApp %}
            {% else %}
              <tr>
                <td>
                  {% include 'admin/app/app-detail.html.twig' %}
                  <div class="right-info pull-right">
                    <a href="#" data-toggle="modal" data-target="#modal"
                       data-package-id="{{ app.latestPackageId }}" data-url="{{ path('admin_app_package_update_modal', {id:app.latestPackageId, type:'install'}) }}"
                       class="btn btn-primary app-btn" data-keyboard="false" data-backdrop="static">{{ 'admin.app_center.package_update_btn'|trans }}</a>
                  </div>
                </td>
              </tr>
            {% endif %}
          {% else %}
            <tr>
              <td>
                {% include 'admin/app/app-detail.html.twig' %}

                <div class="right-info pull-right">
                  {% if app.userAccess|default(null) == "impossible" %}
                    <a href="http://open.edusoho.com" class="user-access" target=_blank>{{ 'admin.app_center.user_access'|trans }}</a>
                  {% else %}
                    {% if app.price == 0 %}
                      <a href="#" data-toggle="modal" data-target="#modal"
                         data-package-id="{{ app.latestPackageId }}" data-url="{{ path('admin_app_package_update_modal', {id:app.latestPackageId, type:'install'}) }}"
                         class="btn btn-primary app-btn" data-keyboard="false"
                         data-backdrop="static">{{ 'admin.app_center.package_update_btn'|trans }}</a>
                    {% else %}
                      <a href="http://open.edusoho.com/app/{{ app.code }}" class="btn btn-warning app-btn"
                         target="_blank">{{ 'admin.app_center.buy_btn'|trans }}</a>
                    {% endif %}
                    <p class="mtm"><span class="text-muted">{{ 'admin.app_center.price'|trans }}</span><span
                          class="price"> ¥ {% if app.price > 0 %}{{ app.price }}{% else %}{{ 'admin.app_center.free'|trans }}{% endif %}</span>
                    </p>
                  {% endif %}
                </div>
              </td>
            </tr>
          {% endif %}

        {% else %}
          <tr>
            <td>
              {% include 'admin/app/app-detail.html.twig' %}
              <div class="right-info pull-right">

                {% if app.purchased|default(false) == true or installedApp %}

                  {% if installedApp %}
                    {% if (app.ontrial|default(false)) and (app.userAccess|default(null) != 'ok') %}
                      <p><a href="javascript:;" class="btn btn-default disabled app-btn">{{ 'admin.app_center.expired_btn'|trans }}</a></p>
                      <p><a href="http://open.edusoho.com/app/{{ app.code }}" class="btn btn-warning app-btn"
                            target="_blank">{{ 'admin.app_center.buy_btn'|trans }}</a></p>
                    {% elseif (app.ontrial|default(false)) and app.purchased|default(false) == false %}
                      <p><a href="javascript:;" class="btn btn-default disabled app-btn">{{ 'admin.app_center.ontrial_btn'|trans }}</a></p>
                      <p><a href="http://open.edusoho.com/app/{{ app.code }}" class="btn btn-warning app-btn"
                            target="_blank">{{ 'admin.app_center.buy_btn'|trans }}</a></p>
                    {% else %}
                      <a href="javascript:;" class="btn btn-default disabled app-btn">{{ 'admin.app_center.buyed_btn'|trans }}</a>
                    {% endif %}
                  {% else %}
                    <a href="#" data-toggle="modal" data-target="#modal"
                       data-package-id="{{ app.latestPackageId }}" data-url="{{ path('admin_app_package_update_modal', {id:app.latestPackageId, type:'install'}) }}"
                       class="btn btn-primary app-btn" data-keyboard="false" data-backdrop="static">{{ 'admin.app_center.package_update_btn'|trans }}</a>
                  {% endif %}

                  <p class="mtm"><span class="text-muted">{{ 'admin.app_center.price'|trans }}</span><span
                        class="price"> ¥ {% if app.price > 0 %}{{ app.price }}{% else %}{{ 'admin.app_center.free'|trans }}{% endif %}</span>
                  </p>
                {% else %}

                  {% if app.buyable is defined %}
                    {% set triable = app.triable|default(false) %}
                    {% if triable %}
                      <p><a href="#" data-toggle="modal" data-target="#modal"
                            data-package-id="{{ app.latestPackageId }}" data-url="{{ path('admin_app_package_update_modal', {id:app.latestPackageId, type:'install'}) }}"
                            class="btn btn-primary app-btn" data-keyboard="false"
                            data-backdrop="static">{{ 'admin.app_center.tria_btn'|trans }}</a></p>
                    {% endif %}
                    {% if app.buyable %}
                      <p><a href="http://open.edusoho.com/app/{{ app.code }}" class="btn btn-warning app-btn"
                            target="_blank">{{ 'admin.app_center.buy_btn'|trans }}</a></p>
                    {% else %}
                      <a href="http://open.edusoho.com/app/{{ app.code }}" class="btn btn-warning app-btn"
                         target="_blank">{{ 'admin.app_center.show_detail_btn'|trans }}</a>
                    {% endif %}
                    <p class="mtm"><span class="text-muted">{{ 'admin.app_center.price'|trans }}</span><span
                          class="price"> ¥ {% if app.price > 0 %}{{ app.price }}{% else %}{{ 'admin.app_center.free'|trans }}{% endif %}</span>
                    </p>
                  {% else %}
                    {% if app.userAccess|default(null) == "impossible" %}
                      <a href="http://www.edusoho.com/product/system" class="user-access"
                         target=_blank>{{ 'admin.app_center.user_access'|trans }}</a>
                    {% else %}
                      {% if app.price == 0 %}
                        <a href="#" data-toggle="modal" data-target="#modal"
                           data-package-id="{{ app.latestPackageId }}" data-url="{{ path('admin_app_package_update_modal', {id:app.latestPackageId, type:'install'}) }}"
                           class="btn btn-primary app-btn" data-keyboard="false"
                           data-backdrop="static">{{ 'admin.app_center.package_update_btn'|trans }}</a>
                      {% else %}
                        <a href="http://open.edusoho.com/app/{{ app.code }}" class="btn btn-warning app-btn"
                           target="_blank">{{ 'admin.app_center.buy_btn'|trans }}</a>
                      {% endif %}
                      <p class="mtm"><span class="text-muted">{{ 'admin.app_center.price'|trans }}</span><span
                            class="price"> ¥ {% if app.price > 0 %}{{ app.price }}{% else %}{{ 'admin.app_center.free'|trans }}{% endif %}</span>
                      </p>
                    {% endif %}
                  {% endif %}
                {% endif %}

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