<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java" %>
<%@ page import="com.pandoli365.bibimbap.data.RecruitPostData" %>
<%@ page import="org.springframework.web.util.HtmlUtils" %>
<%@ page import="java.util.Collections" %>
<%@ page import="java.util.List" %>
<%
    String ctx = request.getContextPath();
    List<RecruitPostData> recruitPosts = Collections.emptyList();
    Object recruitPostsAttr = request.getAttribute("recruitPosts");
    if (recruitPostsAttr instanceof List<?>) {
        recruitPosts = (List<RecruitPostData>) recruitPostsAttr;
    }
%>
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <jsp:include page="/WEB-INF/views/theme-init.jsp"/>
    <title>팀원 모집 | bibimbap</title>
    <style>
      html {
        color-scheme: light;
        --surface: #faf8f5;
        --card-bg: #fff;
        --text: #1a1a1a;
        --text-muted: #5c5c5c;
        --accent: #e8a54b;
        --accent-soft: rgba(232, 165, 75, 0.16);
        --border: rgba(0, 0, 0, 0.08);
        --shadow: rgba(0, 0, 0, 0.06);
        --field-bg: #fff;
      }
      html[data-theme="dark"] {
        color-scheme: dark;
        --surface: #121212;
        --card-bg: #1e1e1e;
        --text: #ece8e1;
        --text-muted: #a39e96;
        --border: rgba(255, 255, 255, 0.1);
        --shadow: rgba(0, 0, 0, 0.35);
        --field-bg: #181818;
      }
      body {
        margin: 0;
        min-height: 100vh;
        font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans KR", sans-serif;
        background: var(--surface);
        color: var(--text);
      }
      .recruit-page {
        max-width: 72rem;
        margin: 0 auto;
        padding: 1.5rem max(1rem, env(safe-area-inset-left)) 3rem max(1rem, env(safe-area-inset-right));
      }
      .recruit-hero {
        display: flex;
        align-items: flex-end;
        justify-content: space-between;
        gap: 1rem;
        margin-bottom: 1rem;
      }
      .recruit-hero__eyebrow {
        margin: 0 0 0.35rem;
        color: var(--accent);
        font-size: 0.75rem;
        font-weight: 900;
      }
      .recruit-hero h1 {
        margin: 0;
        font-size: 1.9rem;
        line-height: 1.2;
        letter-spacing: 0;
      }
      .recruit-hero p {
        margin: 0.45rem 0 0;
        color: var(--text-muted);
        line-height: 1.6;
      }
      .recruit-write {
        min-height: 2.75rem;
        padding: 0 1rem;
        border-radius: 10px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 0.45rem;
        background: var(--accent);
        color: #1a1a1a;
        font-size: 0.9375rem;
        font-weight: 900;
        text-decoration: none;
        white-space: nowrap;
      }
      .recruit-toolbar {
        margin-bottom: 1.25rem;
        display: grid;
        gap: 0.75rem;
      }
      .recruit-search {
        display: flex;
        gap: 0.5rem;
      }
      .recruit-search input {
        width: 100%;
        height: 3rem;
        box-sizing: border-box;
        border: 1px solid var(--border);
        border-radius: 12px;
        padding: 0 1rem;
        background: var(--field-bg);
        color: var(--text);
        font-size: 1rem;
      }
      .filter-group {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 0.5rem;
      }
      .filter-group__label {
        color: var(--text-muted);
        font-size: 0.8125rem;
        font-weight: 800;
      }
      .filter-chip {
        min-height: 2.25rem;
        border: 1px solid var(--border);
        border-radius: 999px;
        padding: 0 0.85rem;
        background: var(--card-bg);
        color: var(--text);
        font: inherit;
        font-size: 0.875rem;
        font-weight: 800;
        cursor: pointer;
      }
      .filter-chip[aria-pressed="true"] {
        border-color: rgba(232, 165, 75, 0.65);
        background: var(--accent-soft);
        color: var(--accent);
      }
      .recruit-grid {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 1rem;
      }
      .recruit-card {
        min-height: 17rem;
        padding: 1.15rem;
        border: 1px solid var(--border);
        border-radius: 12px;
        display: flex;
        flex-direction: column;
        gap: 0.8rem;
        background: var(--card-bg);
        color: inherit;
        text-decoration: none;
        box-shadow: 0 2px 8px var(--shadow);
      }
      .recruit-card:hover {
        border-color: rgba(232, 165, 75, 0.45);
        box-shadow: 0 8px 20px var(--shadow);
        transform: translateY(-2px);
      }
      .recruit-card__top {
        display: flex;
        justify-content: space-between;
        gap: 0.75rem;
      }
      .recruit-card__role,
      .recruit-card__status {
        min-height: 1.75rem;
        padding: 0 0.55rem;
        border-radius: 8px;
        display: inline-flex;
        align-items: center;
        font-size: 0.75rem;
        font-weight: 900;
      }
      .recruit-card__role {
        background: var(--accent-soft);
        color: var(--accent);
      }
      .recruit-card__status {
        background: var(--surface);
        color: var(--text-muted);
      }
      .recruit-card h2 {
        margin: 0;
        font-size: 1.15rem;
        line-height: 1.35;
        letter-spacing: 0;
      }
      .recruit-card__desc {
        margin: 0;
        color: var(--text-muted);
        font-size: 0.875rem;
        line-height: 1.55;
      }
      .recruit-card__meta {
        margin: auto 0 0;
        padding: 0;
        list-style: none;
        display: grid;
        gap: 0.35rem;
        color: var(--text-muted);
        font-size: 0.8125rem;
      }
      .recruit-card__meta strong {
        color: var(--text);
      }
      .recruit-empty {
        min-height: 10rem;
        border: 1px dashed var(--border);
        border-radius: 12px;
        display: none;
        align-items: center;
        justify-content: center;
        background: var(--card-bg);
        color: var(--text-muted);
        text-align: center;
      }
      .recruit-empty.is-visible {
        display: flex;
      }
      @media (max-width: 900px) {
        .recruit-grid {
          grid-template-columns: repeat(2, minmax(0, 1fr));
        }
      }
      @media (max-width: 640px) {
        .recruit-hero {
          align-items: stretch;
          flex-direction: column;
        }
        .recruit-write {
          width: 100%;
          box-sizing: border-box;
        }
        .recruit-grid {
          grid-template-columns: 1fr;
        }
      }
    </style>
</head>
<body>
    <jsp:include page="/WEB-INF/views/header.jsp"/>
    <main class="recruit-page">
        <section class="recruit-hero" aria-labelledby="recruit-title">
            <div>
                <p class="recruit-hero__eyebrow">INDIE GAME TEAM</p>
                <h1 id="recruit-title">함께 만들 팀원을 찾는 공간</h1>
                <p>기획, 아트, 프로그래머가 작은 프로젝트부터 출시 목표 팀까지 가볍게 만나는 게시판입니다.</p>
            </div>
            <a class="recruit-write" href="<%= ctx %>/recruit/new">모집글 작성</a>
        </section>

        <section class="recruit-toolbar" aria-label="모집글 검색 및 필터">
            <div class="recruit-search">
                <input type="search" id="recruit-search" placeholder="프로젝트명, 장르, 소개 검색" autocomplete="off">
            </div>
            <div class="filter-group" data-filter-group="role">
                <span class="filter-group__label">역할</span>
                <button class="filter-chip" type="button" data-value="all" aria-pressed="true">전체</button>
                <button class="filter-chip" type="button" data-value="기획" aria-pressed="false">기획</button>
                <button class="filter-chip" type="button" data-value="아트" aria-pressed="false">아트</button>
                <button class="filter-chip" type="button" data-value="프로그래머" aria-pressed="false">프로그래머</button>
            </div>
            <div class="filter-group" data-filter-group="type">
                <span class="filter-group__label">참여 형태</span>
                <button class="filter-chip" type="button" data-value="all" aria-pressed="true">전체</button>
                <button class="filter-chip" type="button" data-value="취미" aria-pressed="false">취미</button>
                <button class="filter-chip" type="button" data-value="수익쉐어" aria-pressed="false">수익쉐어</button>
                <button class="filter-chip" type="button" data-value="유급" aria-pressed="false">유급</button>
                <button class="filter-chip" type="button" data-value="게임잼" aria-pressed="false">게임잼</button>
            </div>
        </section>

        <section class="recruit-grid" id="recruit-grid" aria-label="팀원 모집 목록">
            <%
                for (RecruitPostData post : recruitPosts) {
                    if (post == null || post.getId() == null) {
                        continue;
                    }
                    String role = HtmlUtils.htmlEscape(post.getRole() == null || post.getRole().isBlank() ? "역할" : post.getRole());
                    String type = HtmlUtils.htmlEscape(post.getParticipationType() == null || post.getParticipationType().isBlank() ? "참여 방식 미정" : post.getParticipationType());
                    String projectName = HtmlUtils.htmlEscape(post.getProjectName() == null || post.getProjectName().isBlank() ? "프로젝트 이름 없음" : post.getProjectName());
                    String genre = HtmlUtils.htmlEscape(post.getGenre() == null || post.getGenre().isBlank() ? "장르 미정" : post.getGenre());
                    String status = HtmlUtils.htmlEscape(post.getProjectStatus() == null || post.getProjectStatus().isBlank() ? "진행 상태 미정" : post.getProjectStatus());
                    String summary = HtmlUtils.htmlEscape(post.getSummary() == null || post.getSummary().isBlank() ? "소개가 아직 없습니다." : post.getSummary());
                    String searchText = HtmlUtils.htmlEscape(String.join(" ",
                            projectName,
                            genre,
                            role,
                            type,
                            status,
                            summary
                    ));
            %>
            <a class="recruit-card" href="<%= ctx %>/recruit/<%= post.getId() %>" data-role="<%= role %>" data-type="<%= type %>" data-search="<%= searchText %>">
                <div class="recruit-card__top">
                    <span class="recruit-card__role"><%= role %> 모집</span>
                    <span class="recruit-card__status">모집중</span>
                </div>
                <h2><%= projectName %></h2>
                <p class="recruit-card__desc"><%= summary %></p>
                <ul class="recruit-card__meta">
                    <li><strong>장르</strong> <%= genre %></li>
                    <li><strong>상태</strong> <%= status %></li>
                    <li><strong>참여</strong> <%= type %></li>
                </ul>
            </a>
            <%
                }
            %>
        </section>
        <div class="recruit-empty <%= recruitPosts.isEmpty() ? "is-visible" : "" %>" id="recruit-empty">아직 올라온 팀원 모집글이 없습니다.</div>
    </main>
    <jsp:include page="/WEB-INF/views/footer.jsp"/>
    <script>
(function () {
  var state = { role: 'all', type: 'all', query: '' };
  var cards = Array.prototype.slice.call(document.querySelectorAll('.recruit-card'));
  var empty = document.getElementById('recruit-empty');
  var search = document.getElementById('recruit-search');

  function applyFilters() {
    var shown = 0;
    cards.forEach(function (card) {
      var roleOk = state.role === 'all' || card.getAttribute('data-role') === state.role;
      var typeOk = state.type === 'all' || card.getAttribute('data-type') === state.type;
      var haystack = (card.getAttribute('data-search') || '').toLowerCase();
      var queryOk = !state.query || haystack.indexOf(state.query.toLowerCase()) !== -1;
      var visible = roleOk && typeOk && queryOk;
      card.hidden = !visible;
      if (visible) shown += 1;
    });
    empty.classList.toggle('is-visible', shown === 0);
  }

  document.querySelectorAll('.filter-group').forEach(function (group) {
    group.addEventListener('click', function (ev) {
      var btn = ev.target.closest('.filter-chip');
      if (!btn) return;
      var name = group.getAttribute('data-filter-group');
      state[name] = btn.getAttribute('data-value');
      group.querySelectorAll('.filter-chip').forEach(function (chip) {
        chip.setAttribute('aria-pressed', chip === btn ? 'true' : 'false');
      });
      applyFilters();
    });
  });

  if (search) {
    search.addEventListener('input', function () {
      state.query = search.value.trim();
      applyFilters();
    });
  }
})();
    </script>
</body>
</html>
