{include file="_partials/head.tpl"}

<div class="page-head">
  <div>
    <h1>Маркетплейс</h1>
    <p class="muted">Каталог плагинов и тем с сервера <code>{mp_url}</code>. Установка в один клик.</p>
  </div>
  <div class="actions">
    <a class="btn" href="/admin/marketplace/updates">Проверить обновления</a>
  </div>
</div>

[if {mp_error}]
<div class="card" style="border-color:#f3c2c2">
  <p style="margin:0">{mp_error}</p>
  <p class="muted" style="margin:6px 0 0">Проверьте адрес маркетплейса (опция <code>marketplace_url</code> или <code>MARKETPLACE_URL</code> в .env) и доступность сервера.</p>
</div>
[/if]

[if {updates}]
<div class="card">
  <h3>Доступные обновления</h3>
  <table class="table">
    <thead><tr><th>Пакет</th><th>Новая версия</th><th>Изменения</th><th class="actions"></th></tr></thead>
    <tbody>
      [foreach var="updates" as="u"]
      <tr>
        <td><code>{u.slug}</code></td>
        <td><b>{u.version}</b></td>
        <td class="muted">{u.changelog}</td>
        <td class="actions">
          <form method="post" action="/admin/marketplace/install" style="display:inline">
            {csrf}
            <input type="hidden" name="slug" value="{u.slug}">
            <input type="hidden" name="type" value="plugin">
            <input type="hidden" name="version" value="{u.version}">
            <button class="btn btn-primary" type="submit">Обновить</button>
          </form>
        </td>
      </tr>
      [/foreach]
    </tbody>
  </table>
</div>
[/if]

<div class="card">
  <div class="actions" style="justify-content:space-between;align-items:center;display:flex;gap:12px;flex-wrap:wrap">
    <div>
      <a class="btn [if {type}="plugin"]btn-primary[/if]" href="/admin/marketplace?type=plugin">Плагины</a>
      <a class="btn [if {type}="theme"]btn-primary[/if]" href="/admin/marketplace?type=theme">Темы</a>
    </div>
    <form method="get" action="/admin/marketplace" style="display:flex;gap:8px">
      <input type="hidden" name="type" value="{type}">
      <input type="hidden" name="category" value="{category}">
      <input type="text" name="q" value="{q}" placeholder="Поиск…">
      <button class="btn" type="submit">Найти</button>
    </form>
  </div>
  [if {categories}]
  <div style="display:flex;gap:6px;flex-wrap:wrap;margin-top:12px">
    <a class="btn btn--sm [if {category}][else]btn-primary[/if]" href="/admin/marketplace?type={type}">Все</a>
    [foreach var="categories" as="c"]<a class="btn btn--sm [if {c.active}="1"]btn-primary[/if]" href="/admin/marketplace?type={type}&category={c.name}">{c.name}</a>[/foreach]
  </div>
  [/if]
</div>

[if {items}]
<div class="mp-grid" style="display:grid;grid-template-columns:repeat(auto-fill,minmax(280px,1fr));gap:16px;margin-top:16px">
  [foreach var="items" as="p"]
  <div class="card" style="margin:0;display:flex;flex-direction:column;gap:8px">
    <div style="display:flex;align-items:center;gap:10px">
      [if {p.icon_url}]<img src="{p.icon_url}" alt="" style="width:40px;height:40px;border-radius:8px;object-fit:cover">[/if]
      <div>
        <a href="/admin/marketplace/item/{p.slug}"><b>{p.name}</b></a>
        <div class="muted" style="font-size:12px">{p.author} · v{p.latest_version}[if {p.category}] · {p.category}[/if]</div>
      </div>
    </div>
    <p class="muted" style="margin:0;flex:1">{p.summary}</p>
    <div style="display:flex;align-items:center;justify-content:space-between;gap:8px">
      <span class="badge">{p.price_label}</span>
      [if {p.installed}="1"]
        <span class="muted">Установлено</span>
      [else]
        <form method="post" action="/admin/marketplace/install" style="display:inline">
          {csrf}
          <input type="hidden" name="slug" value="{p.slug}">
          <input type="hidden" name="type" value="{p.type}">
          <input type="hidden" name="version" value="latest">
          <button class="btn btn-primary" type="submit">Установить</button>
        </form>
      [/if]
    </div>
  </div>
  [/foreach]
</div>
[else]
[if {mp_error}][else]
<div class="empty-state" style="margin-top:16px"><p>Ничего не найдено.</p></div>
[/if]
[/if]

{include file="_partials/foot.tpl"}
